use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.
the class LibvirtStopCommandWrapper method execute.
@Override
public Answer execute(final StopCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = command.getVmName();
final Map<String, Boolean> vlanToPersistenceMap = command.getVlanToPersistenceMap();
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
if (command.checkBeforeCleanup()) {
try {
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
final Domain vm = conn.domainLookupByName(command.getVmName());
if (vm != null && vm.getInfo().state == DomainState.VIR_DOMAIN_RUNNING) {
return new StopAnswer(command, "vm is still running on host", false);
}
} catch (final Exception e) {
s_logger.debug("Failed to get vm status in case of checkboforecleanup is true", e);
}
}
File pemFile = new File(LibvirtComputingResource.SSHPRVKEYPATH);
try {
if (vmName.startsWith("s-") || vmName.startsWith("v-")) {
// move the command line file to backup.
s_logger.debug("backing up the cmdline");
try {
Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null, "cp -f " + CMDLINE_PATH + " " + CMDLINE_BACKUP_PATH);
if (!ret.first()) {
s_logger.debug("Failed to backup cmdline file due to " + ret.second());
}
} catch (Exception e) {
s_logger.debug("Failed to backup cmdline file due to " + e.getMessage());
}
}
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
final List<DiskDef> disks = libvirtComputingResource.getDisks(conn, vmName);
final List<InterfaceDef> ifaces = libvirtComputingResource.getInterfaces(conn, vmName);
libvirtComputingResource.destroyNetworkRulesForVM(conn, vmName);
final String result = libvirtComputingResource.stopVM(conn, vmName, command.isForceStop());
performAgentStopHook(vmName, libvirtComputingResource);
if (result == null) {
if (disks != null && disks.size() > 0) {
for (final DiskDef disk : disks) {
libvirtComputingResource.cleanupDisk(disk);
}
} else {
// When using iSCSI-based managed storage, if the user shuts a VM down from the guest OS (as opposed to doing so from CloudStack),
// info needs to be passed to the KVM agent to have it disconnect KVM from the applicable iSCSI volumes.
List<Map<String, String>> volumesToDisconnect = command.getVolumesToDisconnect();
if (volumesToDisconnect != null) {
for (Map<String, String> volumeToDisconnect : volumesToDisconnect) {
libvirtComputingResource.cleanupDisk(volumeToDisconnect);
}
}
}
if (CollectionUtils.isEmpty(ifaces)) {
Map<String, DpdkTO> dpdkInterfaceMapping = command.getDpdkInterfaceMapping();
if (MapUtils.isNotEmpty(dpdkInterfaceMapping)) {
for (DpdkTO to : dpdkInterfaceMapping.values()) {
String portToRemove = to.getPort();
String cmd = String.format("ovs-vsctl del-port %s", portToRemove);
s_logger.debug("Removing DPDK port: " + portToRemove);
Script.runSimpleBashScript(cmd);
}
}
} else {
for (final InterfaceDef iface : ifaces) {
String vlanId = libvirtComputingResource.getVlanIdFromBridgeName(iface.getBrName());
// each interface at this point, so inform all vif drivers
for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
vifDriver.unplug(iface, libvirtComputingResource.shouldDeleteBridge(vlanToPersistenceMap, vlanId));
}
}
}
}
return new StopAnswer(command, result, true);
} catch (final LibvirtException e) {
s_logger.debug("unable to stop VM:" + vmName + " due to" + e.getMessage());
try {
if (vmName.startsWith("s-") || vmName.startsWith("v-"))
s_logger.debug("restoring cmdline file from backup");
Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null, "mv " + CMDLINE_BACKUP_PATH + " " + CMDLINE_PATH);
if (!ret.first()) {
s_logger.debug("unable to restore cmdline due to " + ret.second());
}
} catch (final Exception ex) {
s_logger.debug("unable to restore cmdline due to:" + ex.getMessage());
}
return new StopAnswer(command, e.getMessage(), false);
}
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.
the class LibvirtMigrateCommandWrapperTest method configureAndVerifyTestSearchDiskDefOnMigrateDiskInfoList.
private void configureAndVerifyTestSearchDiskDefOnMigrateDiskInfoList(String serialNumber, String diskPath, boolean isExpectedDiskInfoNull) {
MigrateDiskInfo migrateDiskInfo = new MigrateDiskInfo(serialNumber, DiskType.FILE, DriverType.QCOW2, Source.FILE, "sourceText");
List<MigrateDiskInfo> migrateDiskInfoList = new ArrayList<>();
migrateDiskInfoList.add(migrateDiskInfo);
DiskDef disk = new DiskDef();
disk.setDiskPath(diskPath);
MigrateDiskInfo returnedMigrateDiskInfo = libvirtMigrateCmdWrapper.searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, disk);
if (isExpectedDiskInfoNull)
Assert.assertEquals(null, returnedMigrateDiskInfo);
else
Assert.assertEquals(migrateDiskInfo, returnedMigrateDiskInfo);
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.
the class LibvirtMigrateCommandWrapperTest method deleteOrDisconnectDisksOnSourcePoolTest.
@Test
public void deleteOrDisconnectDisksOnSourcePoolTest() {
LibvirtMigrateCommandWrapper spyLibvirtMigrateCmdWrapper = PowerMockito.spy(libvirtMigrateCmdWrapper);
Mockito.doNothing().when(spyLibvirtMigrateCmdWrapper).deleteLocalVolume("volPath");
List<MigrateDiskInfo> migrateDiskInfoList = new ArrayList<>();
MigrateDiskInfo migrateDiskInfo0 = createMigrateDiskInfo(true);
MigrateDiskInfo migrateDiskInfo2 = createMigrateDiskInfo(false);
List<DiskDef> disks = new ArrayList<>();
DiskDef diskDef0 = new DiskDef();
DiskDef diskDef1 = new DiskDef();
DiskDef diskDef2 = new DiskDef();
diskDef0.setDiskPath("volPath");
disks.add(diskDef0);
disks.add(diskDef1);
disks.add(diskDef2);
LibvirtComputingResource libvirtComputingResource = Mockito.spy(new LibvirtComputingResource());
Mockito.doReturn(true).when(libvirtComputingResource).cleanupDisk(diskDef1);
Mockito.doReturn(migrateDiskInfo0).when(spyLibvirtMigrateCmdWrapper).searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, diskDef0);
Mockito.doReturn(null).when(spyLibvirtMigrateCmdWrapper).searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, diskDef1);
Mockito.doReturn(migrateDiskInfo2).when(spyLibvirtMigrateCmdWrapper).searchDiskDefOnMigrateDiskInfoList(migrateDiskInfoList, diskDef2);
spyLibvirtMigrateCmdWrapper.deleteOrDisconnectDisksOnSourcePool(libvirtComputingResource, migrateDiskInfoList, disks);
InOrder inOrder = Mockito.inOrder(spyLibvirtMigrateCmdWrapper, libvirtComputingResource);
inOrderVerifyDeleteOrDisconnect(inOrder, spyLibvirtMigrateCmdWrapper, libvirtComputingResource, migrateDiskInfoList, diskDef0, 1, 0);
inOrderVerifyDeleteOrDisconnect(inOrder, spyLibvirtMigrateCmdWrapper, libvirtComputingResource, migrateDiskInfoList, diskDef1, 0, 1);
inOrderVerifyDeleteOrDisconnect(inOrder, spyLibvirtMigrateCmdWrapper, libvirtComputingResource, migrateDiskInfoList, diskDef2, 0, 1);
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.
the class LibvirtVMDefTest method testDiskDef.
@Test
public void testDiskDef() {
String filePath = "/var/lib/libvirt/images/disk.qcow2";
String diskLabel = "vda";
DiskDef disk = new DiskDef();
DiskDef.DiskBus bus = DiskDef.DiskBus.VIRTIO;
DiskDef.DiskFmtType type = DiskDef.DiskFmtType.QCOW2;
DiskDef.DiskCacheMode cacheMode = DiskDef.DiskCacheMode.WRITEBACK;
disk.defFileBasedDisk(filePath, diskLabel, bus, type);
disk.setCacheMode(cacheMode);
assertEquals(filePath, disk.getDiskPath());
assertEquals(diskLabel, disk.getDiskLabel());
assertEquals(bus, disk.getBusType());
assertEquals(DiskDef.DeviceType.DISK, disk.getDeviceType());
String xmlDef = disk.toString();
String expectedXml = "<disk device='disk' type='file'>\n<driver name='qemu' type='" + type.toString() + "' cache='" + cacheMode.toString() + "' />\n" + "<source file='" + filePath + "'/>\n<target dev='" + diskLabel + "' bus='" + bus.toString() + "'/>\n</disk>\n";
assertEquals(xmlDef, expectedXml);
}
use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef in project cloudstack by apache.
the class LibvirtComputingResource method getDisks.
public List<DiskDef> getDisks(final Connect conn, final String vmName) {
final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
Domain dm = null;
try {
dm = conn.domainLookupByName(vmName);
parser.parseDomainXML(dm.getXMLDesc(0));
return parser.getDisks();
} catch (final LibvirtException e) {
s_logger.debug("Failed to get dom xml: " + e.toString());
return new ArrayList<DiskDef>();
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
s_logger.trace("Ignoring libvirt error.", e);
}
}
}
Aggregations