Fix VMWare leftovers when deleting VM without root disk (#9735)

This commit is contained in:
Gabriel Pordeus Santos 2025-02-09 05:31:39 -03:00 committed by GitHub
parent a1117acbdf
commit 58a63f64fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,46 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.agent.api;
/**
* This command will destroy a leftover VM during the expunge process if it wasn't destroyed before.
*
*/
public class CleanupVMCommand extends Command {
String vmName;
boolean executeInSequence;
public CleanupVMCommand(String vmName) {
this(vmName, false);
}
public CleanupVMCommand(String vmName, boolean executeInSequence) {
this.vmName = vmName;
this.executeInSequence = executeInSequence;
}
@Override
public boolean executeInSequence() {
return executeInSequence;
}
public String getVmName() {
return vmName;
}
}

View File

@ -27,6 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.cloud.agent.api.CleanupVMCommand;
import javax.inject.Inject;
import com.cloud.agent.api.to.NfsTO;
@ -371,6 +372,13 @@ public class VMwareGuru extends HypervisorGuruBase implements HypervisorGuru, Co
return tokens[0] + "@" + vCenterIp;
}
@Override public List<Command> finalizeExpunge(VirtualMachine vm) {
List<Command> commands = new ArrayList<Command>();
final CleanupVMCommand cleanupVMCommand = new CleanupVMCommand(vm.getInstanceName(), true);
commands.add(cleanupVMCommand);
return commands;
}
@Override public List<Command> finalizeExpungeNics(VirtualMachine vm, List<NicProfile> nics) {
List<Command> commands = new ArrayList<Command>();
List<NicVO> nicVOs = nicDao.listByVmId(vm.getId());

View File

@ -45,6 +45,7 @@ import java.util.TimeZone;
import java.util.UUID;
import java.util.stream.Collectors;
import com.cloud.agent.api.CleanupVMCommand;
import javax.naming.ConfigurationException;
import javax.xml.datatype.XMLGregorianCalendar;
@ -585,6 +586,8 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes
return execute((ResizeVolumeCommand) cmd);
} else if (clz == UnregisterVMCommand.class) {
return execute((UnregisterVMCommand) cmd);
} else if (clz == CleanupVMCommand.class) {
return execute((CleanupVMCommand) cmd);
} else if (cmd instanceof StorageSubSystemCommand) {
checkStorageProcessorAndHandlerNfsVersionAttribute((StorageSubSystemCommand) cmd);
return storageHandler.handleStorageCommands((StorageSubSystemCommand) cmd);
@ -5796,6 +5799,26 @@ public class VmwareResource extends ServerResourceBase implements StoragePoolRes
return new Answer(cmd, true, "success");
}
protected Answer execute(CleanupVMCommand cmd) {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);
try {
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
if (vmMo == null) {
String msg = String.format("VM [%s] not found on vCenter, cleanup not needed.", cmd.getVmName());
s_logger.debug(msg);
return new Answer(cmd, true, msg);
}
vmMo.destroy();
String msg = String.format("VM [%s] remnants on vCenter cleaned up.", cmd.getVmName());
s_logger.debug(msg);
return new Answer(cmd, true, msg);
} catch (Exception e) {
return new Answer(cmd, false, createLogMessageException(e, cmd));
}
}
protected Answer execute(UnregisterVMCommand cmd) {
VmwareContext context = getServiceContext();
VmwareHypervisorHost hyperHost = getHyperHost(context);