Skip to content

Commit ec0c6e2

Browse files
shwstpprdhslove
authored andcommitted
xenserver: do not destroy halted hypervisor vm (apache#9175)
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 068e110 commit ec0c6e2

8 files changed

Lines changed: 41 additions & 20 deletions

File tree

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@
5151
import javax.naming.ConfigurationException;
5252
import javax.xml.parsers.ParserConfigurationException;
5353

54-
import com.trilead.ssh2.SFTPException;
55-
import com.trilead.ssh2.SFTPv3Client;
56-
import com.trilead.ssh2.SFTPv3DirectoryEntry;
57-
import com.trilead.ssh2.SFTPv3FileAttributes;
5854
import org.apache.cloudstack.api.ApiConstants;
5955
import org.apache.cloudstack.diagnostics.CopyToSecondaryStorageAnswer;
6056
import org.apache.cloudstack.diagnostics.CopyToSecondaryStorageCommand;
@@ -71,6 +67,8 @@
7167
import org.apache.commons.io.FileUtils;
7268
import org.apache.commons.lang3.BooleanUtils;
7369
import org.apache.commons.lang3.StringUtils;
70+
import org.apache.log4j.Logger;
71+
import org.apache.maven.artifact.versioning.ComparableVersion;
7472
import org.apache.xmlrpc.XmlRpcException;
7573
import org.joda.time.Duration;
7674
import org.w3c.dom.Document;
@@ -152,6 +150,10 @@
152150
import com.cloud.vm.VirtualMachine.PowerState;
153151
import com.cloud.vm.VmDetailConstants;
154152
import com.trilead.ssh2.SCPClient;
153+
import com.trilead.ssh2.SFTPException;
154+
import com.trilead.ssh2.SFTPv3Client;
155+
import com.trilead.ssh2.SFTPv3DirectoryEntry;
156+
import com.trilead.ssh2.SFTPv3FileAttributes;
155157
import com.xensource.xenapi.Bond;
156158
import com.xensource.xenapi.Connection;
157159
import com.xensource.xenapi.Console;
@@ -625,7 +627,7 @@ public boolean cleanupHaltedVms(final Connection conn) throws XenAPIException, X
625627

626628
if (VmPowerState.HALTED.equals(vmRec.powerState) && vmRec.affinity.equals(host) && !isAlienVm(vm, conn)) {
627629
try {
628-
vm.destroy(conn);
630+
destroyVm(vm, conn);
629631
} catch (final Exception e) {
630632
logger.warn("Catch Exception " + e.getClass().getName() + ": unable to destroy VM " + vmRec.nameLabel + " due to ", e);
631633
success = false;
@@ -1450,7 +1452,7 @@ public VM createVmFromTemplate(final Connection conn, final VirtualMachineTO vmS
14501452
vm.setPVBootloader(conn, "pygrub");
14511453
vm.setPVBootloaderArgs(conn, CitrixHelper.getPVbootloaderArgs(guestOsTypeName));
14521454
} else {
1453-
vm.destroy(conn);
1455+
destroyVm(vm, conn, true);
14541456
throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader());
14551457
}
14561458
}
@@ -2036,7 +2038,7 @@ void forceShutdownVM(final Connection conn, final VM vm) {
20362038
final Long domId = vm.getDomid(conn);
20372039
callHostPlugin(conn, "vmopspremium", "forceShutdownVM", "domId", domId.toString());
20382040
vm.powerStateReset(conn);
2039-
vm.destroy(conn);
2041+
destroyVm(vm, conn);
20402042
} catch (final Exception e) {
20412043
final String msg = "forceShutdown failed due to " + e.toString();
20422044
logger.warn(msg, e);
@@ -3689,7 +3691,7 @@ public String handleVmStartFailure(final Connection conn, final String vmName, f
36893691
}
36903692
if (vm.getPowerState(conn) == VmPowerState.HALTED) {
36913693
try {
3692-
vm.destroy(conn);
3694+
destroyVm(vm, conn, true);
36933695
} catch (final Exception e) {
36943696
logger.warn("VM destroy failed due to ", e);
36953697
}
@@ -5213,7 +5215,7 @@ protected void startvmfailhandle(final Connection conn, final VM vm, final List<
52135215
}
52145216
if (vm.getPowerState(conn) == VmPowerState.HALTED) {
52155217
try {
5216-
vm.destroy(conn);
5218+
destroyVm(vm, conn, true);
52175219
} catch (final Exception e) {
52185220
final String msg = "VM destroy failed due to " + e.toString();
52195221
logger.warn(msg, e);
@@ -5880,4 +5882,23 @@ private void umountNfs(Connection conn, String remoteDir, String localDir) {
58805882
logger.warn(errMsg);
58815883
}
58825884
}
5885+
5886+
public boolean isDestroyHaltedVms() {
5887+
ComparableVersion version = new ComparableVersion(getHost().getProductVersion());
5888+
if (version.compareTo(new ComparableVersion("8.0")) >= 0) {
5889+
return false;
5890+
}
5891+
return true;
5892+
}
5893+
5894+
public void destroyVm(VM vm, Connection connection, boolean forced) throws XenAPIException, XmlRpcException {
5895+
if (!isDestroyHaltedVms() && !forced) {
5896+
return;
5897+
}
5898+
vm.destroy(connection);
5899+
}
5900+
5901+
public void destroyVm(VM vm, Connection connection) throws XenAPIException, XmlRpcException {
5902+
destroyVm(vm, connection, false);
5903+
}
58835904
}

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xen56/XenServer56FenceCommandWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public Answer execute(final FenceCommand command, final XenServer56Resource xenS
5454
for (final VM vm : vms) {
5555
logger.info("Fence command for VM " + command.getVmName());
5656
vm.powerStateReset(conn);
57-
vm.destroy(conn);
57+
xenServer56.destroyVm(vm, conn);
5858
}
5959
return new FenceAnswer(command);
6060
} catch (final XmlRpcException e) {

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xen56p1/XenServer56FP1FenceCommandWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Answer execute(final FenceCommand command, final XenServer56Resource xenS
6666
}
6767
logger.info("Fence command for VM " + command.getVmName());
6868
vm.powerStateReset(conn);
69-
vm.destroy(conn);
69+
xenServer56.destroyVm(vm, conn);
7070
for (final VDI vdi : vdis) {
7171
final Map<String, String> smConfig = vdi.getSmConfig(conn);
7272
for (final String key : smConfig.keySet()) {

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixCreateVMSnapshotCommandWrapper.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public Answer execute(final CreateVMSnapshotCommand command, final CitrixResourc
9898
vm = citrixResourceBase.getVM(conn, vmName);
9999
vmState = vm.getPowerState(conn);
100100
} catch (final Exception e) {
101+
s_logger.debug(String.format("Failed to find VM with name: %s due to:", vmName), e);
101102
if (!snapshotMemory) {
102103
vm = citrixResourceBase.createWorkingVM(conn, vmName, guestOSType, platformEmulator, listVolumeTo);
103104
}
@@ -176,13 +177,11 @@ public Answer execute(final CreateVMSnapshotCommand command, final CitrixResourc
176177
vdi.destroy(conn);
177178
}
178179
}
179-
vmSnapshot.destroy(conn);
180+
citrixResourceBase.destroyVm(vmSnapshot, conn, true);
180181
}
181182
}
182-
if (vmState == VmPowerState.HALTED) {
183-
if (vm != null) {
184-
vm.destroy(conn);
185-
}
183+
if (vmState == VmPowerState.HALTED && vm != null) {
184+
citrixResourceBase.destroyVm(vm, conn);
186185
}
187186
} catch (final Exception e2) {
188187
logger.error("delete snapshot error due to " + e2.getMessage());

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixDeleteVMSnapshotCommandWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Answer execute(final DeleteVMSnapshotCommand command, final CitrixResourc
6666
if (command.getTarget().getType() == VMSnapshot.Type.DiskAndMemory) {
6767
vdiList.add(snapshot.getSuspendVDI(conn));
6868
}
69-
snapshot.destroy(conn);
69+
citrixResourceBase.destroyVm(snapshot, conn, true);
7070
for (final VDI vdi : vdiList) {
7171
vdi.destroy(conn);
7272
}

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixRevertToVMSnapshotCommandWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public Answer execute(final RevertToVMSnapshotCommand command, final CitrixResou
6666
try {
6767
vm = citrixResourceBase.getVM(conn, vmName);
6868
} catch (final Exception e) {
69+
s_logger.debug(String.format("Failed to find VM with name: %s due to:", vmName), e);
6970
vm = citrixResourceBase.createWorkingVM(conn, vmName, command.getGuestOSType(), command.getPlatformEmulator(), listVolumeTo);
7071
}
7172

@@ -88,7 +89,7 @@ public Answer execute(final RevertToVMSnapshotCommand command, final CitrixResou
8889
}
8990

9091
if (!snapshotMemory) {
91-
vm.destroy(conn);
92+
citrixResourceBase.destroyVm(vm, conn);
9293
vmState = PowerState.PowerOff;
9394
} else {
9495
vmState = PowerState.PowerOn;

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixStartCommandWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public Answer execute(final StartCommand command, final CitrixResourceBase citri
7373
for (final VM v : vms) {
7474
final VM.Record vRec = v.getRecord(conn);
7575
if (vRec.powerState == VmPowerState.HALTED) {
76-
v.destroy(conn);
76+
citrixResourceBase.destroyVm(v, conn, true);
7777
} else if (vRec.powerState == VmPowerState.RUNNING) {
7878
final String host = vRec.residentOn.getUuid(conn);
7979
final String msg = "VM " + vmName + " is runing on host " + host;

plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/wrapper/xenbase/CitrixStopCommandWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public Answer execute(final StopCommand command, final CitrixResourceBase citrix
141141
for (final VIF vif : vifs) {
142142
networks.add(vif.getNetwork(conn));
143143
}
144-
vm.destroy(conn);
144+
citrixResourceBase.destroyVm(vm, conn);
145145
final SR sr = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName(), false);
146146
citrixResourceBase.removeSR(conn, sr);
147147
final SR configDriveSR = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName(), true);

0 commit comments

Comments
 (0)