Search in sources :

Example 11 with Domain

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

the class LibvirtComputingResource method getVmState.

public PowerState getVmState(final Connect conn, final String vmName) {
    int retry = 3;
    Domain vms = null;
    while (retry-- > 0) {
        try {
            vms = conn.domainLookupByName(vmName);
            final PowerState s = convertToPowerState(vms.getInfo().state);
            return s;
        } catch (final LibvirtException e) {
            s_logger.warn("Can't get vm state " + vmName + e.getMessage() + "retry:" + retry);
        } finally {
            try {
                if (vms != null) {
                    vms.free();
                }
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
    return PowerState.PowerOff;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 12 with Domain

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

the class LibvirtComputingResource method attachOrDetachDevice.

protected synchronized String attachOrDetachDevice(final Connect conn, final boolean attach, final String vmName, final String xml) throws LibvirtException, InternalErrorException {
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        if (attach) {
            s_logger.debug("Attaching device: " + xml);
            dm.attachDevice(xml);
        } else {
            s_logger.debug("Detaching device: " + xml);
            dm.detachDevice(xml);
        }
    } catch (final LibvirtException e) {
        if (attach) {
            s_logger.warn("Failed to attach device to " + vmName + ": " + e.getMessage());
        } else {
            s_logger.warn("Failed to detach device from " + vmName + ": " + e.getMessage());
        }
        throw e;
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

Example 13 with Domain

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

the class LibvirtComputingResource method getAllVmNames.

protected List<String> getAllVmNames(final Connect conn) {
    final ArrayList<String> la = new ArrayList<String>();
    try {
        final String[] names = conn.listDefinedDomains();
        for (int i = 0; i < names.length; i++) {
            la.add(names[i]);
        }
    } catch (final LibvirtException e) {
        s_logger.warn("Failed to list Defined domains", e);
    }
    int[] ids = null;
    try {
        ids = conn.listDomains();
    } catch (final LibvirtException e) {
        s_logger.warn("Failed to list domains", e);
        return la;
    }
    Domain dm = null;
    for (int i = 0; i < ids.length; i++) {
        try {
            dm = conn.domainLookupByID(ids[i]);
            la.add(dm.getName());
        } catch (final LibvirtException e) {
            s_logger.warn("Unable to get vms", e);
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (final LibvirtException e) {
                s_logger.trace("Ignoring libvirt error.", e);
            }
        }
    }
    return la;
}
Also used : LibvirtException(org.libvirt.LibvirtException) ArrayList(java.util.ArrayList) Domain(org.libvirt.Domain)

Example 14 with Domain

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

the class LibvirtComputingResource method getInterfaces.

public List<InterfaceDef> getInterfaces(final Connect conn, final String vmName) {
    final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByName(vmName);
        parser.parseDomainXML(dm.getXMLDesc(0));
        return parser.getInterfaces();
    } catch (final LibvirtException e) {
        s_logger.debug("Failed to get dom xml: " + e.toString());
        return new ArrayList<InterfaceDef>();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (final LibvirtException e) {
            s_logger.trace("Ignoring libvirt error.", e);
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

Example 15 with Domain

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

the class LibvirtComputingResource method startVM.

public String startVM(final Connect conn, final String vmName, final String domainXML) throws LibvirtException, InternalErrorException {
    try {
        /*
                We create a transient domain here. When this method gets
                called we receive a full XML specification of the guest,
                so no need to define it persistent.

                This also makes sure we never have any old "garbage" defined
                in libvirt which might haunt us.
             */
        // check for existing inactive vm definition and remove it
        // this can sometimes happen during crashes, etc
        Domain dm = null;
        try {
            dm = conn.domainLookupByName(vmName);
            if (dm != null && dm.isPersistent() == 1) {
                // this is safe because it doesn't stop running VMs
                dm.undefine();
            }
        } catch (final LibvirtException e) {
        // this is what we want, no domain found
        } finally {
            if (dm != null) {
                dm.free();
            }
        }
        conn.domainCreateXML(domainXML, 0);
    } catch (final LibvirtException e) {
        throw e;
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

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