From 21416cd40347836444411561633a6ce33a96c798 Mon Sep 17 00:00:00 2001 From: Felipe <124818914+FelipeM525@users.noreply.github.com> Date: Wed, 8 Jan 2025 04:49:21 -0300 Subject: [PATCH] Restrict the migration of volumes attached to VMs in Starting state (#9725) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bernardo De Marco Gonçalves --- .../com/cloud/storage/VolumeApiServiceImpl.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index b506858b237..7f867eb01a9 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -3201,6 +3201,7 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic VMInstanceVO vm = null; if (instanceId != null) { vm = _vmInstanceDao.findById(instanceId); + checkVmStateForMigration(vm, vol); } // Check that Vm to which this volume is attached does not have VM Snapshots @@ -3398,6 +3399,22 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic return orchestrateMigrateVolume(vol, destPool, liveMigrateVolume, newDiskOffering); } + private void checkVmStateForMigration(VMInstanceVO vm, VolumeVO vol) { + List suitableVmStatesForMigration = List.of(State.Stopped, State.Running, State.Shutdown); + + if (!suitableVmStatesForMigration.contains(vm.getState())) { + s_logger.debug(String.format( + "Unable to migrate volume: [%s] Id: [%s] because the VM: [%s] Id: [%s] is in state [%s], which is not supported for migration.", + vol.getName(), vol.getId(), vm.getInstanceName(), vm.getUuid(), vm.getState() + )); + + throw new CloudRuntimeException(String.format( + "Volume migration is not allowed when the VM is in the %s state. Supported states are: %s.", + vm.getState(), suitableVmStatesForMigration + )); + } + } + private boolean isSourceOrDestNotOnStorPool(StoragePoolVO storagePoolVO, StoragePoolVO destinationStoragePoolVo) { return storagePoolVO.getPoolType() != Storage.StoragePoolType.StorPool || destinationStoragePoolVo.getPoolType() != Storage.StoragePoolType.StorPool;