From 544a48c61b45f0a9cff025799a2a98ca85c33295 Mon Sep 17 00:00:00 2001 From: nvazquez Date: Thu, 10 Feb 2022 12:55:48 -0300 Subject: [PATCH 1/3] Fix attach volume error for VM on different storage pool --- .../com/cloud/storage/VolumeApiServiceImpl.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index e42ea4349c08..7ada00a81dd8 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -2024,20 +2024,20 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device } UserVmVO vm = _userVmDao.findById(vmId); - VolumeVO exstingVolumeOfVm = null; + VolumeVO existingVolumeOfVm = null; VMTemplateVO template = _templateDao.findById(vm.getTemplateId()); List rootVolumesOfVm = _volsDao.findByInstanceAndType(vmId, Volume.Type.ROOT); if (rootVolumesOfVm.size() > 1 && template != null && !template.isDeployAsIs()) { throw new CloudRuntimeException("The VM " + vm.getHostName() + " has more than one ROOT volume and is in an invalid state."); } else { if (!rootVolumesOfVm.isEmpty()) { - exstingVolumeOfVm = rootVolumesOfVm.get(0); + existingVolumeOfVm = rootVolumesOfVm.get(0); } else { // locate data volume of the vm List diskVolumesOfVm = _volsDao.findByInstanceAndType(vmId, Volume.Type.DATADISK); for (VolumeVO diskVolume : diskVolumesOfVm) { if (diskVolume.getState() != Volume.State.Allocated) { - exstingVolumeOfVm = diskVolume; + existingVolumeOfVm = diskVolume; break; } } @@ -2051,8 +2051,9 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device //don't create volume on primary storage if its being attached to the vm which Root's volume hasn't been created yet StoragePoolVO destPrimaryStorage = null; - if (exstingVolumeOfVm != null && !exstingVolumeOfVm.getState().equals(Volume.State.Allocated)) { - destPrimaryStorage = _storagePoolDao.findById(exstingVolumeOfVm.getPoolId()); + if (existingVolumeOfVm != null && !existingVolumeOfVm.getState().equals(Volume.State.Allocated)) { + Long destPoolId = volumeToAttach.getPoolId().equals(existingVolumeOfVm.getPoolId()) ? existingVolumeOfVm.getPoolId() ? volumeToAttach.getPoolId(); + destPrimaryStorage = _storagePoolDao.findById(destPoolId); } boolean volumeOnSecondary = volumeToAttach.getState() == Volume.State.Uploaded; @@ -2071,7 +2072,7 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device // reload the volume from db newVolumeOnPrimaryStorage = volFactory.getVolume(newVolumeOnPrimaryStorage.getId()); - boolean moveVolumeNeeded = needMoveVolume(exstingVolumeOfVm, newVolumeOnPrimaryStorage); + boolean moveVolumeNeeded = needMoveVolume(existingVolumeOfVm, newVolumeOnPrimaryStorage); if (moveVolumeNeeded) { PrimaryDataStoreInfo primaryStore = (PrimaryDataStoreInfo)newVolumeOnPrimaryStorage.getDataStore(); @@ -2079,7 +2080,7 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device throw new CloudRuntimeException( "Failed to attach local data volume " + volumeToAttach.getName() + " to VM " + vm.getDisplayName() + " as migration of local data volume is not allowed"); } - StoragePoolVO vmRootVolumePool = _storagePoolDao.findById(exstingVolumeOfVm.getPoolId()); + StoragePoolVO vmRootVolumePool = _storagePoolDao.findById(existingVolumeOfVm.getPoolId()); try { newVolumeOnPrimaryStorage = _volumeMgr.moveVolume(newVolumeOnPrimaryStorage, vmRootVolumePool.getDataCenterId(), vmRootVolumePool.getPodId(), vmRootVolumePool.getClusterId(), From 996bf7e9e2232dec62478ec49850e0250b57fbf1 Mon Sep 17 00:00:00 2001 From: nvazquez Date: Thu, 10 Feb 2022 12:59:54 -0300 Subject: [PATCH 2/3] Fix typo --- .../src/main/java/com/cloud/storage/VolumeApiServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index 7ada00a81dd8..4b5768e44327 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -2052,7 +2052,7 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device //don't create volume on primary storage if its being attached to the vm which Root's volume hasn't been created yet StoragePoolVO destPrimaryStorage = null; if (existingVolumeOfVm != null && !existingVolumeOfVm.getState().equals(Volume.State.Allocated)) { - Long destPoolId = volumeToAttach.getPoolId().equals(existingVolumeOfVm.getPoolId()) ? existingVolumeOfVm.getPoolId() ? volumeToAttach.getPoolId(); + Long destPoolId = volumeToAttach.getPoolId().equals(existingVolumeOfVm.getPoolId()) ? existingVolumeOfVm.getPoolId() : volumeToAttach.getPoolId(); destPrimaryStorage = _storagePoolDao.findById(destPoolId); } From 6515aaffd4d300b37e86a63888023c4e41329fd6 Mon Sep 17 00:00:00 2001 From: nvazquez Date: Thu, 10 Feb 2022 13:34:34 -0300 Subject: [PATCH 3/3] Refactor --- .../cloudstack/engine/orchestration/VolumeOrchestrator.java | 3 ++- .../src/main/java/com/cloud/storage/VolumeApiServiceImpl.java | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java b/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java index a311912624b2..43f3e82be83c 100644 --- a/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java +++ b/engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java @@ -1053,7 +1053,8 @@ private VolumeInfo copyVolume(StoragePool rootDiskPool, VolumeInfo volume, Virtu public VolumeInfo createVolumeOnPrimaryStorage(VirtualMachine vm, VolumeInfo volume, HypervisorType rootDiskHyperType, StoragePool storagePool) throws NoTransitionException { VirtualMachineTemplate rootDiskTmplt = _entityMgr.findById(VirtualMachineTemplate.class, vm.getTemplateId()); DataCenter dcVO = _entityMgr.findById(DataCenter.class, vm.getDataCenterId()); - Pod pod = _entityMgr.findById(Pod.class, storagePool.getPodId()); + Long podId = storagePool.getPodId() != null ? storagePool.getPodId() : vm.getPodIdToDeployIn(); + Pod pod = _entityMgr.findById(Pod.class, podId); ServiceOffering svo = _entityMgr.findById(ServiceOffering.class, vm.getServiceOfferingId()); DiskOffering diskVO = _entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId()); diff --git a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java index 4b5768e44327..3bee5df955da 100644 --- a/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java @@ -2052,8 +2052,7 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device //don't create volume on primary storage if its being attached to the vm which Root's volume hasn't been created yet StoragePoolVO destPrimaryStorage = null; if (existingVolumeOfVm != null && !existingVolumeOfVm.getState().equals(Volume.State.Allocated)) { - Long destPoolId = volumeToAttach.getPoolId().equals(existingVolumeOfVm.getPoolId()) ? existingVolumeOfVm.getPoolId() : volumeToAttach.getPoolId(); - destPrimaryStorage = _storagePoolDao.findById(destPoolId); + destPrimaryStorage = _storagePoolDao.findById(existingVolumeOfVm.getPoolId()); } boolean volumeOnSecondary = volumeToAttach.getState() == Volume.State.Uploaded;