use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method getDisks.
protected List<DiskDef> getDisks(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.getDisks();
} catch (LibvirtException e) {
s_logger.debug("Failed to get dom xml: " + e.toString());
return new ArrayList<DiskDef>();
} catch (Exception e) {
s_logger.debug("Failed to get dom xml: " + e.toString());
return new ArrayList<DiskDef>();
} 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 stopVM.
protected String stopVM(Connect conn, String vmName, boolean force) {
Domain dm = null;
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
if (force) {
if (dm.getInfo().state != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) {
dm.destroy();
}
} else {
if (dm.getInfo().state == DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) {
return null;
}
dm.shutdown();
int retry = _stopTimeout / 2000;
/* Wait for the domain gets into shutoff state */
while ((dm.getInfo().state != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF) && (retry >= 0)) {
Thread.sleep(2000);
retry--;
}
if (retry < 0) {
s_logger.warn("Timed out waiting for domain " + vmName + " to shutdown gracefully");
return Script.ERR_TIMEOUT;
}
}
} catch (LibvirtException e) {
s_logger.debug("Failed to stop VM :" + vmName + " :", e);
return e.getMessage();
} catch (InterruptedException ie) {
s_logger.debug("Interrupted sleep");
return ie.getMessage();
} catch (Exception e) {
s_logger.debug("Failed to stop VM :" + vmName + " :", e);
return e.getMessage();
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
return null;
}
use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method attachOrDetachDisk.
protected synchronized String attachOrDetachDisk(Connect conn, boolean attach, String vmName, KVMPhysicalDisk attachingDisk, int devId) throws LibvirtException, InternalErrorException {
List<DiskDef> disks = null;
Domain dm = null;
DiskDef diskdef = null;
try {
if (!attach) {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
String xml = dm.getXMLDesc(0);
parser.parseDomainXML(xml);
disks = parser.getDisks();
for (DiskDef disk : disks) {
String file = disk.getDiskPath();
if (file != null && file.equalsIgnoreCase(attachingDisk.getPath())) {
diskdef = disk;
break;
}
}
if (diskdef == null) {
throw new InternalErrorException("disk: " + attachingDisk.getPath() + " is not attached before");
}
} else {
diskdef = new DiskDef();
if (attachingDisk.getFormat() == PhysicalDiskFormat.QCOW2) {
diskdef.defFileBasedDisk(attachingDisk.getPath(), devId, DiskDef.diskBus.VIRTIO, DiskDef.diskFmtType.QCOW2);
} else if (attachingDisk.getFormat() == PhysicalDiskFormat.RAW) {
diskdef.defBlockBasedDisk(attachingDisk.getPath(), devId, DiskDef.diskBus.VIRTIO);
}
}
String xml = diskdef.toString();
return attachOrDetachDevice(conn, attach, vmName, xml);
} finally {
if (dm != null) {
dm.free();
}
}
}
use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class CloudZonesComputingResource method execute.
protected Answer execute(StopCommand cmd) {
final String vmName = cmd.getVmName();
Long bytesReceived = new Long(0);
Long bytesSent = new Long(0);
State state = null;
synchronized (_vms) {
state = _vms.get(vmName);
_vms.put(vmName, State.Stopping);
}
try {
Connect conn = LibvirtConnection.getConnection();
try {
Domain dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
} catch (LibvirtException e) {
state = State.Stopped;
return new StopAnswer(cmd, null, 0, bytesSent, bytesReceived);
}
String macAddress = null;
if (vmName.startsWith("i-")) {
List<InterfaceDef> nics = getInterfaces(conn, vmName);
if (!nics.isEmpty()) {
macAddress = nics.get(0).getMacAddress();
}
}
destroy_network_rules_for_vm(conn, vmName);
String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
try {
cleanupVnet(conn, cmd.getVnet());
_dhcpSnooper.cleanup(macAddress, vmName);
_vmDataServer.handleVmStopped(cmd.getVmName());
} catch (Exception e) {
}
state = State.Stopped;
return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
} catch (LibvirtException e) {
return new StopAnswer(cmd, e.getMessage());
} finally {
synchronized (_vms) {
if (state != null) {
_vms.put(vmName, state);
} else {
_vms.remove(vmName);
}
}
}
}
use of org.libvirt.Domain in project cloudstack by apache.
the class LibvirtCreateVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final CreateVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
String vmName = cmd.getVmName();
String vmSnapshotName = cmd.getTarget().getSnapshotName();
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new CreateVMSnapshotAnswer(cmd, false, "Create VM Snapshot Failed due to can not find vm: " + vmName);
}
DomainState domainState = dm.getInfo().state;
if (domainState != DomainState.VIR_DOMAIN_RUNNING) {
return new CreateVMSnapshotAnswer(cmd, false, "Create VM Snapshot Failed due to vm is not running: " + vmName + " with domainState = " + domainState);
}
String vmSnapshotXML = "<domainsnapshot>" + " <name>" + vmSnapshotName + "</name>" + " <memory snapshot='internal' />" + "</domainsnapshot>";
dm.snapshotCreateXML(vmSnapshotXML);
return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), cmd.getVolumeTOs());
} catch (LibvirtException e) {
String msg = " Create VM snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new CreateVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
Aggregations