Skip to content

Commit c23c135

Browse files
gpordeusdhslove
authored andcommitted
Fix VMWare leftovers when deleting VM without root disk (apache#9735)
1 parent 2a08f58 commit c23c135

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.agent.api;
21+
22+
/**
23+
* This command will destroy a leftover VM during the expunge process if it wasn't destroyed before.
24+
*
25+
*/
26+
public class CleanupVMCommand extends Command {
27+
String vmName;
28+
boolean executeInSequence;
29+
30+
public CleanupVMCommand(String vmName) {
31+
this(vmName, false);
32+
}
33+
public CleanupVMCommand(String vmName, boolean executeInSequence) {
34+
this.vmName = vmName;
35+
this.executeInSequence = executeInSequence;
36+
}
37+
38+
@Override
39+
public boolean executeInSequence() {
40+
return executeInSequence;
41+
}
42+
43+
public String getVmName() {
44+
return vmName;
45+
}
46+
}

plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/guru/VMwareGuru.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828
import java.util.UUID;
2929

30+
import com.cloud.agent.api.CleanupVMCommand;
3031
import javax.inject.Inject;
3132

3233
import com.cloud.agent.api.to.NfsTO;
@@ -370,6 +371,13 @@ private String resolveNameInGuid(String guid) {
370371
return tokens[0] + "@" + vCenterIp;
371372
}
372373

374+
@Override public List<Command> finalizeExpunge(VirtualMachine vm) {
375+
List<Command> commands = new ArrayList<Command>();
376+
final CleanupVMCommand cleanupVMCommand = new CleanupVMCommand(vm.getInstanceName(), true);
377+
commands.add(cleanupVMCommand);
378+
return commands;
379+
}
380+
373381
@Override public List<Command> finalizeExpungeNics(VirtualMachine vm, List<NicProfile> nics) {
374382
List<Command> commands = new ArrayList<Command>();
375383
List<NicVO> nicVOs = nicDao.listByVmId(vm.getId());

plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/resource/VmwareResource.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.UUID;
4646
import java.util.stream.Collectors;
4747

48+
import com.cloud.agent.api.CleanupVMCommand;
4849
import javax.naming.ConfigurationException;
4950
import javax.xml.datatype.XMLGregorianCalendar;
5051

@@ -583,6 +584,8 @@ public Answer executeRequest(Command cmd) {
583584
return execute((ResizeVolumeCommand) cmd);
584585
} else if (clz == UnregisterVMCommand.class) {
585586
return execute((UnregisterVMCommand) cmd);
587+
} else if (clz == CleanupVMCommand.class) {
588+
return execute((CleanupVMCommand) cmd);
586589
} else if (cmd instanceof StorageSubSystemCommand) {
587590
checkStorageProcessorAndHandlerNfsVersionAttribute((StorageSubSystemCommand) cmd);
588591
return storageHandler.handleStorageCommands((StorageSubSystemCommand) cmd);
@@ -5796,6 +5799,26 @@ protected Answer execute(PvlanSetupCommand cmd) {
57965799
return new Answer(cmd, true, "success");
57975800
}
57985801

5802+
protected Answer execute(CleanupVMCommand cmd) {
5803+
VmwareContext context = getServiceContext();
5804+
VmwareHypervisorHost hyperHost = getHyperHost(context);
5805+
5806+
try {
5807+
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
5808+
if (vmMo == null) {
5809+
String msg = String.format("VM [%s] not found on vCenter, cleanup not needed.", cmd.getVmName());
5810+
s_logger.debug(msg);
5811+
return new Answer(cmd, true, msg);
5812+
}
5813+
vmMo.destroy();
5814+
String msg = String.format("VM [%s] remnants on vCenter cleaned up.", cmd.getVmName());
5815+
s_logger.debug(msg);
5816+
return new Answer(cmd, true, msg);
5817+
} catch (Exception e) {
5818+
return new Answer(cmd, false, createLogMessageException(e, cmd));
5819+
}
5820+
}
5821+
57995822
protected Answer execute(UnregisterVMCommand cmd) {
58005823
VmwareContext context = getServiceContext();
58015824
VmwareHypervisorHost hyperHost = getHyperHost(context);

0 commit comments

Comments
 (0)