use of org.libvirt.Domain in project cosmic by MissionCriticalCloud.
the class LibvirtCreateVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final CreateVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = cmd.getVmName();
final String vmSnapshotName = cmd.getTarget().getSnapshotName();
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final 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);
}
final 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);
}
final String vmSnapshotXML = "<domainsnapshot><name>" + vmSnapshotName + "</name><memory snapshot='internal' /></domainsnapshot>";
dm.snapshotCreateXML(vmSnapshotXML);
return new CreateVMSnapshotAnswer(cmd, cmd.getTarget(), cmd.getVolumeTOs());
} catch (final LibvirtException e) {
final 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 (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
use of org.libvirt.Domain in project cosmic by MissionCriticalCloud.
the class LibvirtRestoreVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RestoreVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = cmd.getVmName();
final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
final PowerState vmState = PowerState.PowerOn;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RestoreVMSnapshotAnswer(cmd, false, "Restore VM Snapshot Failed due to can not find vm: " + vmName);
}
final String xmlDesc = dm.getXMLDesc(0);
final List<VMSnapshotTO> snapshots = cmd.getSnapshots();
final Map<Long, VMSnapshotTO> snapshotAndParents = cmd.getSnapshotAndParents();
for (final VMSnapshotTO snapshot : snapshots) {
final VMSnapshotTO parent = snapshotAndParents.get(snapshot.getId());
final String vmSnapshotXML = libvirtUtilitiesHelper.generateVMSnapshotXML(snapshot, parent, xmlDesc);
s_logger.debug("Restoring vm snapshot " + snapshot.getSnapshotName() + " on " + vmName + " with XML:\n " + vmSnapshotXML);
try {
// VIR_DOMAIN_SNAPSHOT_CREATE_REDEFINE = 1
int flags = 1;
if (snapshot.getCurrent()) {
// VIR_DOMAIN_SNAPSHOT_CREATE_CURRENT = 2
flags += 2;
}
dm.snapshotCreateXML(vmSnapshotXML, flags);
} catch (final LibvirtException e) {
s_logger.debug("Failed to restore vm snapshot " + snapshot.getSnapshotName() + " on " + vmName);
return new RestoreVMSnapshotAnswer(cmd, false, e.toString());
}
}
return new RestoreVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (final LibvirtException e) {
final String msg = " Restore snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RestoreVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
use of org.libvirt.Domain in project cosmic by MissionCriticalCloud.
the class LibvirtRevertToVMSnapshotCommandWrapper method execute.
@Override
public Answer execute(final RevertToVMSnapshotCommand cmd, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = cmd.getVmName();
final List<VolumeObjectTO> listVolumeTo = cmd.getVolumeTOs();
final VMSnapshot.Type vmSnapshotType = cmd.getTarget().getType();
final Boolean snapshotMemory = vmSnapshotType == VMSnapshot.Type.DiskAndMemory;
PowerState vmState;
Domain dm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnection();
dm = libvirtComputingResource.getDomain(conn, vmName);
if (dm == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Revert to VM Snapshot Failed due to can not find vm: " + vmName);
}
final DomainSnapshot snapshot = dm.snapshotLookupByName(cmd.getTarget().getSnapshotName());
if (snapshot == null) {
return new RevertToVMSnapshotAnswer(cmd, false, "Cannot find vmSnapshot with name: " + cmd.getTarget().getSnapshotName());
}
dm.revertToSnapshot(snapshot);
snapshot.free();
if (!snapshotMemory) {
dm.destroy();
if (dm.isPersistent() == 1) {
dm.undefine();
}
vmState = PowerState.PowerOff;
} else {
vmState = PowerState.PowerOn;
}
return new RevertToVMSnapshotAnswer(cmd, listVolumeTo, vmState);
} catch (final LibvirtException e) {
final String msg = " Revert to VM snapshot failed due to " + e.toString();
s_logger.warn(msg, e);
return new RevertToVMSnapshotAnswer(cmd, false, msg);
} finally {
if (dm != null) {
try {
dm.free();
} catch (LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
;
}
}
}
use of org.libvirt.Domain in project cosmic by MissionCriticalCloud.
the class LibvirtStartCommandWrapper method getFreeMemory.
private Long getFreeMemory(final Connect conn, final LibvirtComputingResource libvirtComputingResource) {
try {
long allocatedMem = 0;
final int[] ids = conn.listDomains();
for (final int id : ids) {
final Domain dm = conn.domainLookupByID(id);
allocatedMem += dm.getMaxMemory() * 1024L;
s_logger.debug("vm: " + dm.getName() + " mem: " + dm.getMaxMemory() * 1024L);
}
final Long remainingMem = libvirtComputingResource.getTotalMemory() - allocatedMem;
s_logger.debug("remaining mem" + remainingMem);
return remainingMem;
} catch (final Exception e) {
s_logger.debug("failed to get free memory", e);
return null;
}
}
use of org.libvirt.Domain in project cosmic by MissionCriticalCloud.
the class LibvirtUnPlugNicCommandWrapper method execute.
@Override
public Answer execute(final UnPlugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
final NicTO nic = command.getNic();
final String vmName = command.getVmName();
Domain vm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
vm = libvirtComputingResource.getDomain(conn, vmName);
final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
for (final InterfaceDef pluggedNic : pluggedNics) {
if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
vm.detachDevice(pluggedNic.toString());
// each interface at this point, so inform all vif drivers
for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
vifDriver.unplug(pluggedNic);
}
return new UnPlugNicAnswer(command, true, "success");
}
}
return new UnPlugNicAnswer(command, true, "success");
} catch (final LibvirtException e) {
final String msg = " Unplug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new UnPlugNicAnswer(command, false, msg);
} finally {
if (vm != null) {
try {
vm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
Aggregations