use of org.libvirt.DomainInfo.DomainState in project cloudstack by apache.
the class LibvirtComputingResourceTest method testManageSnapshotCommandLibvirt.
@Test
public void testManageSnapshotCommandLibvirt() {
final StoragePool storagePool = Mockito.mock(StoragePool.class);
;
final String volumePath = "/123/vol";
final String vmName = "Test";
final long snapshotId = 1l;
final String preSnapshotPath = "/snapshot/path";
final String snapshotName = "snap";
final ManageSnapshotCommand command = new ManageSnapshotCommand(snapshotId, volumePath, storagePool, preSnapshotPath, snapshotName, vmName);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Connect conn = Mockito.mock(Connect.class);
final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
final KVMStoragePool primaryPool = Mockito.mock(KVMStoragePool.class);
final Domain vm = Mockito.mock(Domain.class);
final DomainInfo info = Mockito.mock(DomainInfo.class);
final DomainState state = DomainInfo.DomainState.VIR_DOMAIN_RUNNING;
info.state = state;
final KVMPhysicalDisk disk = Mockito.mock(KVMPhysicalDisk.class);
final StorageFilerTO pool = command.getPool();
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
when(libvirtComputingResource.getDomain(conn, command.getVmName())).thenReturn(vm);
when(vm.getInfo()).thenReturn(info);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
when(storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid())).thenReturn(primaryPool);
when(primaryPool.getPhysicalDisk(command.getVolumePath())).thenReturn(disk);
when(primaryPool.isExternalSnapshot()).thenReturn(false);
try {
when(vm.getUUIDString()).thenReturn("cdb18980-546d-4153-b916-70ee9edf0908");
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertTrue(answer.getResult());
verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of org.libvirt.DomainInfo.DomainState in project cloudstack by apache.
the class LibvirtComputingResourceTest method testStopCommandCheckVmRunning.
@Test
public void testStopCommandCheckVmRunning() {
final Connect conn = Mockito.mock(Connect.class);
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
final Domain vm = Mockito.mock(Domain.class);
final DomainInfo info = Mockito.mock(DomainInfo.class);
final DomainState state = DomainInfo.DomainState.VIR_DOMAIN_RUNNING;
info.state = state;
final String vmName = "Test";
final StopCommand command = new StopCommand(vmName, false, true);
when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
try {
when(libvirtUtilitiesHelper.getConnectionByVmName(vmName)).thenReturn(conn);
when(conn.domainLookupByName(command.getVmName())).thenReturn(vm);
when(vm.getInfo()).thenReturn(info);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
assertNotNull(wrapper);
final Answer answer = wrapper.execute(command, libvirtComputingResource);
assertFalse(answer.getResult());
verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
try {
verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vmName);
} catch (final LibvirtException e) {
fail(e.getMessage());
}
}
use of org.libvirt.DomainInfo.DomainState in project cloudstack by apache.
the class LibvirtComputingResource method stopVM.
public String stopVM(final Connect conn, final String vmName, final boolean forceStop) {
DomainState state = null;
Domain dm = null;
// delete the metadata of vm snapshots before stopping
try {
dm = conn.domainLookupByName(vmName);
cleanVMSnapshotMetadata(dm);
} catch (LibvirtException e) {
s_logger.debug("Failed to get vm :" + e.getMessage());
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
s_logger.debug("Try to stop the vm at first");
if (forceStop) {
return stopVMInternal(conn, vmName, true);
}
String ret = stopVMInternal(conn, vmName, false);
if (ret == Script.ERR_TIMEOUT) {
ret = stopVMInternal(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.domainLookupByName(vmName);
state = dm.getInfo().state;
break;
} catch (final LibvirtException e) {
s_logger.debug("Failed to get vm status:" + e.getMessage());
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
if (state == null) {
s_logger.debug("Can't get vm's status, assume it's dead already");
return null;
}
if (state != DomainState.VIR_DOMAIN_SHUTOFF) {
s_logger.debug("Try to destroy the vm");
ret = stopVMInternal(conn, vmName, true);
if (ret != null) {
return ret;
}
}
}
return null;
}
use of org.libvirt.DomainInfo.DomainState 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);
}
;
}
}
}
use of org.libvirt.DomainInfo.DomainState in project cosmic by MissionCriticalCloud.
the class LibvirtComputingResource method getHostVmStateReport.
private HashMap<String, HostVmStateReportEntry> getHostVmStateReport(final Connect conn) {
final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<>();
String[] vms = null;
int[] ids = null;
try {
ids = conn.listDomains();
} catch (final LibvirtException e) {
logger.warn("Unable to listDomains", e);
return null;
}
try {
vms = conn.listDefinedDomains();
} catch (final LibvirtException e) {
logger.warn("Unable to listDomains", e);
return null;
}
Domain dm = null;
for (final int id : ids) {
try {
dm = conn.domainLookupByID(id);
final DomainState ps = dm.getInfo().state;
final PowerState state = convertToPowerState(ps);
logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
final String vmName = dm.getName();
vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
} catch (final LibvirtException e) {
logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
logger.trace("Ignoring libvirt error.", e);
}
}
}
for (final String vm : vms) {
try {
dm = conn.domainLookupByName(vm);
final DomainState ps = dm.getInfo().state;
final PowerState state = convertToPowerState(ps);
final String vmName = dm.getName();
logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
vmStates.put(vmName, new HostVmStateReportEntry(state, conn.getHostName()));
} catch (final LibvirtException e) {
logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
logger.trace("Ignoring libvirt error.", e);
}
}
}
return vmStates;
}
Aggregations