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) {
}
}
}
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;
}
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) {
}
}
}
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;
}
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();
}
}
}
Aggregations