Skip to content

Commit b4ad04b

Browse files
Allow config drive deletion of migrated VM, on host maintenance (#10045)
1 parent a2f2e87 commit b4ad04b

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

api/src/main/java/com/cloud/vm/VmDetailConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public interface VmDetailConstants {
7373
String ENCRYPTED_PASSWORD = "Encrypted.Password";
7474

7575
String CONFIG_DRIVE_LOCATION = "configDriveLocation";
76+
String LAST_CONFIG_DRIVE_LOCATION = "lastConfigDriveLocation";
7677

7778
String SKIP_DRS = "skipFromDRS";
7879

engine/orchestration/src/main/java/com/cloud/agent/manager/AgentAttache.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.cloud.agent.api.Command;
4747
import com.cloud.agent.api.CreateStoragePoolCommand;
4848
import com.cloud.agent.api.DeleteStoragePoolCommand;
49+
import com.cloud.agent.api.HandleConfigDriveIsoCommand;
4950
import com.cloud.agent.api.MaintainCommand;
5051
import com.cloud.agent.api.MigrateCommand;
5152
import com.cloud.agent.api.ModifySshKeysCommand;
@@ -119,11 +120,10 @@ public int compare(final Object o1, final Object o2) {
119120

120121
public final static String[] s_commandsAllowedInMaintenanceMode = new String[] { MaintainCommand.class.toString(), MigrateCommand.class.toString(),
121122
StopCommand.class.toString(), CheckVirtualMachineCommand.class.toString(), PingTestCommand.class.toString(), CheckHealthCommand.class.toString(),
122-
ReadyCommand.class.toString(), ShutdownCommand.class.toString(), SetupCommand.class.toString(),
123-
CleanupNetworkRulesCmd.class.toString(), CheckNetworkCommand.class.toString(), PvlanSetupCommand.class.toString(), CheckOnHostCommand.class.toString(),
124-
ModifyTargetsCommand.class.toString(), ModifySshKeysCommand.class.toString(),
125-
CreateStoragePoolCommand.class.toString(), DeleteStoragePoolCommand.class.toString(), ModifyStoragePoolCommand.class.toString(),
126-
SetupMSListCommand.class.toString(), RollingMaintenanceCommand.class.toString(), CleanupPersistentNetworkResourceCommand.class.toString()};
123+
ReadyCommand.class.toString(), ShutdownCommand.class.toString(), SetupCommand.class.toString(), CleanupNetworkRulesCmd.class.toString(),
124+
CheckNetworkCommand.class.toString(), PvlanSetupCommand.class.toString(), CheckOnHostCommand.class.toString(), ModifyTargetsCommand.class.toString(),
125+
ModifySshKeysCommand.class.toString(), CreateStoragePoolCommand.class.toString(), DeleteStoragePoolCommand.class.toString(), ModifyStoragePoolCommand.class.toString(),
126+
SetupMSListCommand.class.toString(), RollingMaintenanceCommand.class.toString(), CleanupPersistentNetworkResourceCommand.class.toString(), HandleConfigDriveIsoCommand.class.toString()};
127127
protected final static String[] s_commandsNotAllowedInConnectingMode = new String[] { StartCommand.class.toString(), CreateCommand.class.toString() };
128128
static {
129129
Arrays.sort(s_commandsAllowedInMaintenanceMode);

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2985,9 +2985,10 @@ public String getVolumePath(final Connect conn, final DiskTO volume, boolean dis
29852985

29862986
public static boolean useBLOCKDiskType(KVMPhysicalDisk physicalDisk) {
29872987
return physicalDisk != null &&
2988-
physicalDisk.getPool().getType() == StoragePoolType.Linstor &&
2988+
physicalDisk.getPool() != null &&
2989+
StoragePoolType.Linstor.equals(physicalDisk.getPool().getType()) &&
29892990
physicalDisk.getFormat() != null &&
2990-
physicalDisk.getFormat()== PhysicalDiskFormat.RAW;
2991+
PhysicalDiskFormat.RAW.equals(physicalDisk.getFormat());
29912992
}
29922993

29932994
public static DiskDef.DiskType getDiskType(KVMPhysicalDisk physicalDisk) {
@@ -3402,13 +3403,15 @@ public void detachAndAttachConfigDriveISO(final Connect conn, final String vmNam
34023403
}
34033404
if (configdrive != null) {
34043405
try {
3406+
s_logger.debug(String.format("Detaching ConfigDrive ISO of the VM %s, at path %s", vmName, configdrive.getDiskPath()));
34053407
String result = attachOrDetachISO(conn, vmName, configdrive.getDiskPath(), false, CONFIG_DRIVE_ISO_DEVICE_ID);
34063408
if (result != null) {
3407-
s_logger.warn("Detach ConfigDrive ISO with result: " + result);
3409+
s_logger.warn(String.format("Detach ConfigDrive ISO of the VM %s, at path %s with %s: ", vmName, configdrive.getDiskPath(), result));
34083410
}
3411+
s_logger.debug(String.format("Attaching ConfigDrive ISO of the VM %s, at path %s", vmName, configdrive.getDiskPath()));
34093412
result = attachOrDetachISO(conn, vmName, configdrive.getDiskPath(), true, CONFIG_DRIVE_ISO_DEVICE_ID);
34103413
if (result != null) {
3411-
s_logger.warn("Attach ConfigDrive ISO with result: " + result);
3414+
s_logger.warn(String.format("Attach ConfigDrive ISO of the VM %s, at path %s with %s: ", vmName, configdrive.getDiskPath(), result));
34123415
}
34133416
} catch (final LibvirtException | InternalErrorException | URISyntaxException e) {
34143417
final String msg = "Detach and attach ConfigDrive ISO failed due to " + e.toString();
@@ -3420,16 +3423,20 @@ public void detachAndAttachConfigDriveISO(final Connect conn, final String vmNam
34203423
public synchronized String attachOrDetachISO(final Connect conn, final String vmName, String isoPath, final boolean isAttach, final Integer diskSeq) throws LibvirtException, URISyntaxException,
34213424
InternalErrorException {
34223425
final DiskDef iso = new DiskDef();
3423-
if (isoPath != null && isAttach) {
3424-
final int index = isoPath.lastIndexOf("/");
3425-
final String path = isoPath.substring(0, index);
3426-
final String name = isoPath.substring(index + 1);
3427-
final KVMStoragePool secondaryPool = storagePoolManager.getStoragePoolByURI(path);
3428-
final KVMPhysicalDisk isoVol = secondaryPool.getPhysicalDisk(name);
3429-
final DiskDef.DiskType diskType = getDiskType(isoVol);
3430-
isoPath = isoVol.getPath();
3431-
3432-
iso.defISODisk(isoPath, diskSeq, diskType);
3426+
if (isAttach && StringUtils.isNotBlank(isoPath) && isoPath.lastIndexOf("/") > 0) {
3427+
if (isoPath.startsWith(getConfigPath() + "/" + ConfigDrive.CONFIGDRIVEDIR) && isoPath.contains(vmName)) {
3428+
iso.defISODisk(isoPath, diskSeq, DiskDef.DiskType.FILE);
3429+
} else {
3430+
final int index = isoPath.lastIndexOf("/");
3431+
final String path = isoPath.substring(0, index);
3432+
final String name = isoPath.substring(index + 1);
3433+
final KVMStoragePool storagePool = storagePoolManager.getStoragePoolByURI(path);
3434+
final KVMPhysicalDisk isoVol = storagePool.getPhysicalDisk(name);
3435+
final DiskDef.DiskType diskType = getDiskType(isoVol);
3436+
isoPath = isoVol.getPath();
3437+
3438+
iso.defISODisk(isoPath, diskSeq, diskType);
3439+
}
34333440
} else {
34343441
iso.defISODisk(null, diskSeq, DiskDef.DiskType.FILE);
34353442
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStoragePoolManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ public KVMStoragePool getStoragePoolByURI(String uri) {
287287
URI storageUri = null;
288288

289289
try {
290+
s_logger.debug("Get storage pool by uri: " + uri);
290291
storageUri = new URI(uri);
291292
} catch (URISyntaxException e) {
292293
throw new CloudRuntimeException(e.toString());
@@ -296,7 +297,7 @@ public KVMStoragePool getStoragePoolByURI(String uri) {
296297
String uuid = null;
297298
String sourceHost = "";
298299
StoragePoolType protocol = null;
299-
final String scheme = storageUri.getScheme().toLowerCase();
300+
final String scheme = (storageUri.getScheme() != null) ? storageUri.getScheme().toLowerCase() : "";
300301
List<String> acceptedSchemes = List.of("nfs", "networkfilesystem", "filesystem");
301302
if (acceptedSchemes.contains(scheme)) {
302303
sourcePath = storageUri.getPath();

server/src/main/java/com/cloud/network/element/ConfigDriveNetworkElement.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ public boolean prepareMigration(NicProfile nic, Network network, VirtualMachineP
341341
try {
342342
if (isConfigDriveIsoOnHostCache(vm.getId())) {
343343
vm.setConfigDriveLocation(Location.HOST);
344-
configureConfigDriveData(vm, nic, dest);
345-
346-
// Create the config drive on dest host cache
347-
createConfigDriveIsoOnHostCache(vm, dest.getHost().getId());
344+
if (configureConfigDriveData(vm, nic, dest)) {
345+
// Create the config drive on dest host cache
346+
createConfigDriveIsoOnHostCache(vm, dest.getHost().getId());
347+
}
348348
} else {
349349
vm.setConfigDriveLocation(getConfigDriveLocation(vm.getId()));
350350
addPasswordAndUserdata(network, nic, vm, dest, context);
@@ -373,7 +373,7 @@ public void rollbackMigration(NicProfile nic, Network network, VirtualMachinePro
373373
@Override
374374
public void commitMigration(NicProfile nic, Network network, VirtualMachineProfile vm, ReservationContext src, ReservationContext dst) {
375375
try {
376-
if (isConfigDriveIsoOnHostCache(vm.getId())) {
376+
if (isLastConfigDriveIsoOnHostCache(vm.getId())) {
377377
vm.setConfigDriveLocation(Location.HOST);
378378
// Delete the config drive on src host cache
379379
deleteConfigDriveIsoOnHostCache(vm.getVirtualMachine(), vm.getHostId());
@@ -530,6 +530,17 @@ private boolean isConfigDriveIsoOnHostCache(long vmId) {
530530
return false;
531531
}
532532

533+
private boolean isLastConfigDriveIsoOnHostCache(long vmId) {
534+
final UserVmDetailVO vmDetailLastConfigDriveLocation = _userVmDetailsDao.findDetail(vmId, VmDetailConstants.LAST_CONFIG_DRIVE_LOCATION);
535+
if (vmDetailLastConfigDriveLocation == null) {
536+
return isConfigDriveIsoOnHostCache(vmId);
537+
}
538+
if (Location.HOST.toString().equalsIgnoreCase(vmDetailLastConfigDriveLocation.getValue())) {
539+
return true;
540+
}
541+
return false;
542+
}
543+
533544
private boolean createConfigDriveIsoOnHostCache(VirtualMachineProfile profile, Long hostId) throws ResourceUnavailableException {
534545
if (hostId == null) {
535546
throw new ResourceUnavailableException("Config drive iso creation failed, dest host not available",
@@ -556,7 +567,7 @@ private boolean createConfigDriveIsoOnHostCache(VirtualMachineProfile profile, L
556567
}
557568

558569
profile.setConfigDriveLocation(answer.getConfigDriveLocation());
559-
_userVmDetailsDao.addDetail(profile.getId(), VmDetailConstants.CONFIG_DRIVE_LOCATION, answer.getConfigDriveLocation().toString(), false);
570+
updateConfigDriveLocationInVMDetails(profile.getId(), answer.getConfigDriveLocation());
560571
addConfigDriveDisk(profile, null);
561572
return true;
562573
}
@@ -618,11 +629,23 @@ private boolean createConfigDriveIso(VirtualMachineProfile profile, DeployDestin
618629
answer.getDetails()), ConfigDriveNetworkElement.class, 0L);
619630
}
620631
profile.setConfigDriveLocation(answer.getConfigDriveLocation());
621-
_userVmDetailsDao.addDetail(profile.getId(), VmDetailConstants.CONFIG_DRIVE_LOCATION, answer.getConfigDriveLocation().toString(), false);
632+
updateConfigDriveLocationInVMDetails(profile.getId(), answer.getConfigDriveLocation());
622633
addConfigDriveDisk(profile, dataStore);
623634
return true;
624635
}
625636

637+
private void updateConfigDriveLocationInVMDetails(long vmId, NetworkElement.Location configDriveLocation) {
638+
final UserVmDetailVO vmDetailConfigDriveLocation = _userVmDetailsDao.findDetail(vmId, VmDetailConstants.CONFIG_DRIVE_LOCATION);
639+
if (vmDetailConfigDriveLocation != null) {
640+
if (!configDriveLocation.toString().equalsIgnoreCase(vmDetailConfigDriveLocation.getValue())) {
641+
_userVmDetailsDao.addDetail(vmId, VmDetailConstants.LAST_CONFIG_DRIVE_LOCATION, vmDetailConfigDriveLocation.getValue(), false);
642+
} else {
643+
_userVmDetailsDao.removeDetail(vmId, VmDetailConstants.LAST_CONFIG_DRIVE_LOCATION);
644+
}
645+
}
646+
_userVmDetailsDao.addDetail(vmId, VmDetailConstants.CONFIG_DRIVE_LOCATION, configDriveLocation.toString(), false);
647+
}
648+
626649
private Map<String, String> getVMCustomUserdataParamMap(long vmId) {
627650
UserVmVO userVm = _userVmDao.findById(vmId);
628651
String userDataDetails = userVm.getUserDataDetails();
@@ -737,7 +760,7 @@ private void addConfigDriveDisk(final VirtualMachineProfile profile, final DataS
737760

738761
private boolean configureConfigDriveData(final VirtualMachineProfile profile, final NicProfile nic, final DeployDestination dest) {
739762
final UserVmVO vm = _userVmDao.findById(profile.getId());
740-
if (vm.getType() != VirtualMachine.Type.User) {
763+
if (vm == null || vm.getType() != VirtualMachine.Type.User) {
741764
return false;
742765
}
743766
final Nic defaultNic = _networkModel.getDefaultNic(vm.getId());

0 commit comments

Comments
 (0)