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