Search in sources :

Example 1 with Domain

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

the class LibvirtComputingResource method getGuestType.

private String getGuestType(Connect conn, String vmName) {
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        String xmlDesc = dm.getXMLDesc(0);
        parser.parseDomainXML(xmlDesc);
        return parser.getDescription();
    } catch (LibvirtException e) {
        return null;
    } catch (Exception e) {
        return null;
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException l) {
        }
    }
}
Also used : 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 2 with Domain

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

the class LibvirtComputingResource method attachOrDetachDevice.

protected synchronized String attachOrDetachDevice(Connect conn, boolean attach, String vmName, String xml) throws LibvirtException, InternalErrorException {
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes((vmName.getBytes())));
        if (attach) {
            s_logger.debug("Attaching device: " + xml);
            dm.attachDevice(xml);
        } else {
            s_logger.debug("Detaching device: " + xml);
            dm.detachDevice(xml);
        }
    } catch (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;
    } catch (Exception e) {
        throw new InternalErrorException(e.toString());
    } finally {
        if (dm != null) {
            try {
                dm.free();
            } catch (LibvirtException l) {
            }
        }
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) InternalErrorException(com.cloud.exception.InternalErrorException) 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 3 with Domain

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

the class LibvirtComputingResource method getVncPort.

protected Integer getVncPort(Connect conn, String vmName) throws LibvirtException {
    LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
    Domain dm = null;
    try {
        dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        String xmlDesc = dm.getXMLDesc(0);
        parser.parseDomainXML(xmlDesc);
        return parser.getVncPort();
    } finally {
        try {
            if (dm != null) {
                dm.free();
            }
        } catch (LibvirtException l) {
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Domain(org.libvirt.Domain)

Example 4 with Domain

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

the class LibvirtComputingResource method stopVM.

protected String stopVM(Connect conn, String vmName, defineOps df) {
    DomainInfo.DomainState state = null;
    Domain dm = null;
    s_logger.debug("Try to stop the vm at first");
    String ret = stopVM(conn, vmName, false);
    if (ret == Script.ERR_TIMEOUT) {
        ret = stopVM(conn, vmName, true);
    } else if (ret != null) {
        /* Retry 3 times, to make sure we can get the vm's status */
        for (int i = 0; i < 3; i++) {
            try {
                dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
                state = dm.getInfo().state;
                break;
            } catch (LibvirtException e) {
                s_logger.debug("Failed to get vm status:" + e.getMessage());
            } catch (Exception e) {
                s_logger.debug("Failed to get vm status:" + e.getMessage());
            } finally {
                try {
                    if (dm != null) {
                        dm.free();
                    }
                } catch (LibvirtException l) {
                }
            }
        }
        if (state == null) {
            s_logger.debug("Can't get vm's status, assume it's dead already");
            return null;
        }
        if (state != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) {
            s_logger.debug("Try to destroy the vm");
            ret = stopVM(conn, vmName, true);
            if (ret != null) {
                return ret;
            }
        }
    }
    if (df == defineOps.UNDEFINE_VM) {
        try {
            dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
            dm.undefine();
        } catch (LibvirtException e) {
        } finally {
            try {
                if (dm != null) {
                    dm.free();
                }
            } catch (LibvirtException l) {
            }
        }
    }
    return null;
}
Also used : LibvirtException(org.libvirt.LibvirtException) 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)

Example 5 with Domain

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

the class LibvirtComputingResource method getVmStat.

private VmStatsEntry getVmStat(Connect conn, String vmName) throws LibvirtException {
    Domain dm = null;
    try {
        dm = getDomain(conn, vmName);
        DomainInfo info = dm.getInfo();
        VmStatsEntry stats = new VmStatsEntry();
        stats.setNumCPUs(info.nrVirtCpu);
        stats.setEntityType("vm");
        /* get cpu utilization */
        vmStats oldStats = null;
        Calendar now = Calendar.getInstance();
        oldStats = _vmStats.get(vmName);
        long elapsedTime = 0;
        if (oldStats != null) {
            elapsedTime = now.getTimeInMillis() - oldStats._timestamp.getTimeInMillis();
            double utilization = (info.cpuTime - oldStats._usedTime) / ((double) elapsedTime * 1000000);
            NodeInfo node = conn.nodeInfo();
            utilization = utilization / node.cpus;
            stats.setCPUUtilization(utilization * 100);
        }
        /* get network stats */
        List<InterfaceDef> vifs = getInterfaces(conn, vmName);
        long rx = 0;
        long tx = 0;
        for (InterfaceDef vif : vifs) {
            DomainInterfaceStats ifStats = dm.interfaceStats(vif.getDevName());
            rx += ifStats.rx_bytes;
            tx += ifStats.tx_bytes;
        }
        if (oldStats != null) {
            long deltarx = rx - oldStats._rx;
            if (deltarx > 0)
                stats.setNetworkReadKBs(deltarx / 1000);
            long deltatx = tx - oldStats._tx;
            if (deltatx > 0)
                stats.setNetworkWriteKBs(deltatx / 1000);
        }
        vmStats newStat = new vmStats();
        newStat._usedTime = info.cpuTime;
        newStat._rx = rx;
        newStat._tx = tx;
        newStat._timestamp = now;
        _vmStats.put(vmName, newStat);
        return stats;
    } finally {
        if (dm != null) {
            dm.free();
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) NodeInfo(org.libvirt.NodeInfo) DomainInterfaceStats(org.libvirt.DomainInterfaceStats) Calendar(java.util.Calendar) DomainInfo(org.libvirt.DomainInfo) VmStatsEntry(com.cloud.agent.api.VmStatsEntry) Domain(org.libvirt.Domain)

Aggregations

Domain (org.libvirt.Domain)112 LibvirtException (org.libvirt.LibvirtException)92 Connect (org.libvirt.Connect)58 InternalErrorException (com.cloud.exception.InternalErrorException)35 Test (org.junit.Test)26 URISyntaxException (java.net.URISyntaxException)23 DomainState (org.libvirt.DomainInfo.DomainState)23 Answer (com.cloud.agent.api.Answer)22 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)22 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)22 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)22 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)22 IOException (java.io.IOException)21 DomainInfo (org.libvirt.DomainInfo)21 NicTO (com.cloud.agent.api.to.NicTO)20 ArrayList (java.util.ArrayList)19 ConfigurationException (javax.naming.ConfigurationException)19 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)16 FileNotFoundException (java.io.FileNotFoundException)16 ExecutionException (java.util.concurrent.ExecutionException)14