Search in sources :

Example 36 with Domain

use of org.libvirt.Domain in project cloudstack by apache.

the class LibvirtComputingResourceTest method testMemoryFreeInKBsDomainReturningOfSomeMemoryStatistics.

@Test
public void testMemoryFreeInKBsDomainReturningOfSomeMemoryStatistics() throws LibvirtException {
    LibvirtComputingResource libvirtComputingResource = new LibvirtComputingResource();
    MemoryStatistic[] mem = createMemoryStatisticFreeMemory100();
    Domain domainMock = getDomainConfiguredToReturnMemoryStatistic(mem);
    long memoryFreeInKBs = libvirtComputingResource.getMemoryFreeInKBs(domainMock);
    Assert.assertEquals(100, memoryFreeInKBs);
}
Also used : Domain(org.libvirt.Domain) MemoryStatistic(org.libvirt.MemoryStatistic) Test(org.junit.Test)

Example 37 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getInterfaces.

protected List<InterfaceDef> getInterfaces(Connect conn, String vmName) {
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        parser.parseDomainXML(dm.getXMLDesc(0));
        return parser.getInterfaces();
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<InterfaceDef>();
    } catch (Exception e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<InterfaceDef>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException e) {
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 38 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

private synchronized Answer execute(MigrateCommand cmd) {
    String vmName = cmd.getVmName();
    State state = null;
    String result = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    Domain dm = null;
    Connect dconn = null;
    Domain destDomain = null;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        dconn = new Connect("qemu+tcp://" + cmd.getDestinationIp() + "/system");
        /*
			 * Hard code lm flags: VIR_MIGRATE_LIVE(1<<0) and
			 * VIR_MIGRATE_PERSIST_DEST(1<<3)
			 */
        destDomain = dm.migrate(dconn, (1 << 0) | (1 << 3), vmName, "tcp:" + cmd.getDestinationIp(), _migrateSpeed);
    } catch (LibvirtException e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } catch (Exception e) {
        s_logger.debug("Can't migrate domain: " + e.getMessage());
        result = e.getMessage();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
            if (dconn != null) {
                dconn.close();
            }
            if (destDomain != null) {
                destDomain.free();
            }
        } catch (final LibvirtException e) {
        }
    }
    if (result != null) {
        synchronized (_vms) {
            _vms.put(vmName, state);
        }
    } else {
        destroy_network_rules_for_vm(conn, vmName);
        cleanupVM(conn, vmName, getVnetId(VirtualMachineName.getVnet(vmName)));
    }
    return new MigrateAnswer(cmd, result == null, result, null);
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Example 39 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class CloudZonesComputingResource method setupDhcpManager.

private void setupDhcpManager(Connect conn, String bridgeName) {
    _dhcpSnooper = new DhcpSnooperImpl(bridgeName, _dhcpTimeout);
    List<Pair<String, String>> macs = new ArrayList<Pair<String, String>>();
    try {
        int[] domainIds = conn.listDomains();
        for (int i = 0; i < domainIds.length; i++) {
            Domain vm = conn.domainLookupByID(domainIds[i]);
            if (vm.getName().startsWith("i-")) {
                List<InterfaceDef> nics = getInterfaces(conn, vm.getName());
                InterfaceDef nic = nics.get(0);
                macs.add(new Pair<String, String>(nic.getMacAddress(), vm.getName()));
            }
        }
    } catch (LibvirtException e) {
        s_logger.debug("Failed to get MACs: " + e.toString());
    }
    _dhcpSnooper.initializeMacTable(macs);
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) DhcpSnooperImpl(com.cloud.agent.dhcp.DhcpSnooperImpl) ArrayList(java.util.ArrayList) Domain(org.libvirt.Domain) Pair(com.cloud.utils.Pair)

Example 40 with Domain

use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method getRealPowerState.

protected State getRealPowerState(String vm) {
    int i = 0;
    s_logger.trace("Checking on the HALTED State");
    Domain dm = null;
    for (; i < 5; i++) {
        try {
            Connect conn = LibvirtConnection.getConnection();
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vm.getBytes()));
            DomainInfo.DomainState vps = dm.getInfo().state;
            if (vps != null && vps != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF && vps != DomainInfo.DomainState.VIR_DOMAIN_NOSTATE) {
                return convertToState(vps);
            }
        } catch (final LibvirtException e) {
            s_logger.trace(e.getMessage());
        } catch (Exception e) {
            s_logger.trace(e.getMessage());
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
            }
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }
    return State.Stopped;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) DomainInfo(org.libvirt.DomainInfo) Domain(org.libvirt.Domain) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException)

Aggregations

Domain (org.libvirt.Domain)67 LibvirtException (org.libvirt.LibvirtException)54 Connect (org.libvirt.Connect)30 InternalErrorException (com.cloud.exception.InternalErrorException)24 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)17 URISyntaxException (java.net.URISyntaxException)17 IOException (java.io.IOException)16 ConfigurationException (javax.naming.ConfigurationException)16 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)15 FileNotFoundException (java.io.FileNotFoundException)14 Test (org.junit.Test)14 DomainInfo (org.libvirt.DomainInfo)14 ExecutionException (java.util.concurrent.ExecutionException)13 Answer (com.cloud.agent.api.Answer)11 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)11 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)11 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)11 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)11 NicTO (com.cloud.agent.api.to.NicTO)10 ArrayList (java.util.ArrayList)10