use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method startDomain.
protected String startDomain(Connect conn, String vmName, String domainXML) throws LibvirtException, InternalErrorException {
/* No duplicated vm, we will success, or failed */
boolean failed = false;
Domain dm = null;
try {
dm = conn.domainDefineXML(domainXML);
} catch (final LibvirtException e) {
/* Duplicated defined vm */
s_logger.warn("Failed to define domain " + vmName + ": " + e.getMessage());
failed = true;
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
}
}
/* If failed, undefine the vm */
Domain dmOld = null;
Domain dmNew = null;
try {
if (failed) {
dmOld = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
dmOld.undefine();
dmNew = conn.domainDefineXML(domainXML);
}
} catch (final LibvirtException e) {
s_logger.warn("Failed to define domain (second time) " + vmName + ": " + e.getMessage());
throw e;
} catch (Exception e) {
s_logger.warn("Failed to define domain (second time) " + vmName + ": " + e.getMessage());
throw new InternalErrorException(e.toString());
} finally {
try {
if (dmOld != null) {
dmOld.free();
}
if (dmNew != null) {
dmNew.free();
}
} catch (final LibvirtException e) {
}
}
/* Start the VM */
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
dm.create();
} catch (LibvirtException e) {
s_logger.warn("Failed to start domain: " + vmName + ": " + e.getMessage());
throw e;
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
}
}
return null;
}
use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method VifHotPlug.
private void VifHotPlug(Connect conn, String vmName, String vlanId, String macAddr) throws InternalErrorException, LibvirtException {
NicTO nicTO = new NicTO();
nicTO.setMac(macAddr);
nicTO.setType(TrafficType.Public);
if (vlanId == null) {
nicTO.setBroadcastType(BroadcastDomainType.Native);
} else {
nicTO.setBroadcastType(BroadcastDomainType.Vlan);
nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
}
InterfaceDef nic = createVif(conn, nicTO, InterfaceDef.nicModel.VIRTIO);
Domain vm = getDomain(conn, vmName);
vm.attachDevice(nic.toString());
}
use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method rebootVM.
protected String rebootVM(Connect conn, String vmName) {
Domain dm = null;
String msg = null;
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
String vmDef = dm.getXMLDesc(0);
s_logger.debug(vmDef);
msg = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
msg = startDomain(conn, vmName, vmDef);
return null;
} catch (LibvirtException e) {
s_logger.warn("Failed to create vm", e);
msg = e.getMessage();
} catch (Exception e) {
s_logger.warn("Failed to create vm", e);
msg = e.getMessage();
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
return msg;
}
use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) {
String snapshotName = cmd.getSnapshotName();
String snapshotPath = cmd.getSnapshotPath();
String vmName = cmd.getVmName();
try {
Connect conn = LibvirtConnection.getConnection();
DomainInfo.DomainState state = null;
Domain vm = null;
if (vmName != null) {
try {
vm = getDomain(conn, cmd.getVmName());
state = vm.getInfo().state;
} catch (LibvirtException e) {
}
}
KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPool().getUuid());
KVMPhysicalDisk disk = primaryPool.getPhysicalDisk(cmd.getVolumePath());
if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryPool.isExternalSnapshot()) {
String vmUuid = vm.getUUIDString();
Object[] args = new Object[] { snapshotName, vmUuid };
String snapshot = SnapshotXML.format(args);
s_logger.debug(snapshot);
if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
vm.snapshotCreateXML(snapshot);
} else {
DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
snap.delete(0);
}
/*
* libvirt on RHEL6 doesn't handle resume event emitted from
* qemu
*/
vm = getDomain(conn, cmd.getVmName());
state = vm.getInfo().state;
if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
vm.resume();
}
} else {
/* VM is not running, create a snapshot by ourself */
final Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) {
command.add("-c", disk.getPath());
} else {
command.add("-d", snapshotPath);
}
command.add("-n", snapshotName);
String result = command.execute();
if (result != null) {
s_logger.debug("Failed to manage snapshot: " + result);
return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + result);
}
}
return new ManageSnapshotAnswer(cmd, cmd.getSnapshotId(), disk.getPath() + File.separator + snapshotName, true, null);
} catch (LibvirtException e) {
s_logger.debug("Failed to manage snapshot: " + e.toString());
return new ManageSnapshotAnswer(cmd, false, "Failed to manage snapshot: " + e.toString());
}
}
use of org.libvirt.Domain in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method getAllVms.
private HashMap<String, State> getAllVms() {
final HashMap<String, State> vmStates = new HashMap<String, State>();
String[] vms = null;
int[] ids = null;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
s_logger.debug("Failed to get connection: " + e.getMessage());
return vmStates;
}
try {
ids = conn.listDomains();
} catch (final LibvirtException e) {
s_logger.warn("Unable to listDomains", e);
return null;
}
try {
vms = conn.listDefinedDomains();
} catch (final LibvirtException e) {
s_logger.warn("Unable to listDomains", e);
return null;
}
Domain dm = null;
for (int i = 0; i < ids.length; i++) {
try {
dm = conn.domainLookupByID(ids[i]);
DomainInfo.DomainState ps = dm.getInfo().state;
final State state = convertToState(ps);
s_logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
String vmName = dm.getName();
vmStates.put(vmName, state);
} catch (final LibvirtException e) {
s_logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
}
for (int i = 0; i < vms.length; i++) {
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vms[i].getBytes()));
DomainInfo.DomainState ps = dm.getInfo().state;
final State state = convertToState(ps);
String vmName = dm.getName();
s_logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
vmStates.put(vmName, state);
} catch (final LibvirtException e) {
s_logger.warn("Unable to get vms", e);
} catch (Exception e) {
s_logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
}
return vmStates;
}
Aggregations