From e263fe5126b705994963fc62f28f7b1209eb537d Mon Sep 17 00:00:00 2001 From: Felipe Evangelista Date: Mon, 23 Sep 2024 15:39:55 -0300 Subject: [PATCH 1/2] created a private method within VolumeApiServiceImpl to restrict a volume that is attached to a vm in starting mode from being migrated --- .../java/com/cloud/storage/VolumeApiServiceImpl.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index b506858b2377..25979c003d2d 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 Volume migrateVolume(MigrateVolumeCmd cmd) { VMInstanceVO vm = null; if (instanceId != null) { vm = _vmInstanceDao.findById(instanceId); + checkIfVmIsStarting(vm, vol); } // Check that Vm to which this volume is attached does not have VM Snapshots @@ -3398,6 +3399,14 @@ public Volume migrateVolume(MigrateVolumeCmd cmd) { return orchestrateMigrateVolume(vol, destPool, liveMigrateVolume, newDiskOffering); } + private void checkIfVmIsStarting(VMInstanceVO vm, VolumeVO vol) { + if (vm.getState().equals(State.Starting)) { + s_logger.debug(String.format("Unable to migrate volume: [%s] Id: [%s] because the VM: [%s] Id: [%s] has not started yet.", vol.getName(), vol.getId(), vm.getInstanceName(), vm.getUuid())); + + throw new CloudRuntimeException("Volume migration is not allowed while the VM is starting."); + } + } + private boolean isSourceOrDestNotOnStorPool(StoragePoolVO storagePoolVO, StoragePoolVO destinationStoragePoolVo) { return storagePoolVO.getPoolType() != Storage.StoragePoolType.StorPool || destinationStoragePoolVo.getPoolType() != Storage.StoragePoolType.StorPool; From 7903aa591b4a6aadc293bf678b654d6cba35dc68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernardo=20De=20Marco=20Gon=C3=A7alves?= Date: Fri, 3 Jan 2025 15:21:25 -0300 Subject: [PATCH 2/2] refactor vm state validation --- .../cloud/storage/VolumeApiServiceImpl.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index 25979c003d2d..7f867eb01a97 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -3201,7 +3201,7 @@ public Volume migrateVolume(MigrateVolumeCmd cmd) { VMInstanceVO vm = null; if (instanceId != null) { vm = _vmInstanceDao.findById(instanceId); - checkIfVmIsStarting(vm, vol); + checkVmStateForMigration(vm, vol); } // Check that Vm to which this volume is attached does not have VM Snapshots @@ -3399,11 +3399,19 @@ public Volume migrateVolume(MigrateVolumeCmd cmd) { return orchestrateMigrateVolume(vol, destPool, liveMigrateVolume, newDiskOffering); } - private void checkIfVmIsStarting(VMInstanceVO vm, VolumeVO vol) { - if (vm.getState().equals(State.Starting)) { - s_logger.debug(String.format("Unable to migrate volume: [%s] Id: [%s] because the VM: [%s] Id: [%s] has not started yet.", vol.getName(), vol.getId(), vm.getInstanceName(), vm.getUuid())); + private void checkVmStateForMigration(VMInstanceVO vm, VolumeVO vol) { + List suitableVmStatesForMigration = List.of(State.Stopped, State.Running, State.Shutdown); - throw new CloudRuntimeException("Volume migration is not allowed while the VM is starting."); + 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 + )); } }