Skip to content

Commit fe03e30

Browse files
authored
Fix attach volume error for VM - different scope for VM volume and volume disk offering (#5982)
* Fix attach volume error for VM on different storage pool * Fix typo * Refactor
1 parent 9f23fbe commit fe03e30

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,7 +1053,8 @@ private VolumeInfo copyVolume(StoragePool rootDiskPool, VolumeInfo volume, Virtu
10531053
public VolumeInfo createVolumeOnPrimaryStorage(VirtualMachine vm, VolumeInfo volume, HypervisorType rootDiskHyperType, StoragePool storagePool) throws NoTransitionException {
10541054
VirtualMachineTemplate rootDiskTmplt = _entityMgr.findById(VirtualMachineTemplate.class, vm.getTemplateId());
10551055
DataCenter dcVO = _entityMgr.findById(DataCenter.class, vm.getDataCenterId());
1056-
Pod pod = _entityMgr.findById(Pod.class, storagePool.getPodId());
1056+
Long podId = storagePool.getPodId() != null ? storagePool.getPodId() : vm.getPodIdToDeployIn();
1057+
Pod pod = _entityMgr.findById(Pod.class, podId);
10571058

10581059
ServiceOffering svo = _entityMgr.findById(ServiceOffering.class, vm.getServiceOfferingId());
10591060
DiskOffering diskVO = _entityMgr.findById(DiskOffering.class, volume.getDiskOfferingId());

server/src/main/java/com/cloud/storage/VolumeApiServiceImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,20 +2024,20 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device
20242024
}
20252025

20262026
UserVmVO vm = _userVmDao.findById(vmId);
2027-
VolumeVO exstingVolumeOfVm = null;
2027+
VolumeVO existingVolumeOfVm = null;
20282028
VMTemplateVO template = _templateDao.findById(vm.getTemplateId());
20292029
List<VolumeVO> rootVolumesOfVm = _volsDao.findByInstanceAndType(vmId, Volume.Type.ROOT);
20302030
if (rootVolumesOfVm.size() > 1 && template != null && !template.isDeployAsIs()) {
20312031
throw new CloudRuntimeException("The VM " + vm.getHostName() + " has more than one ROOT volume and is in an invalid state.");
20322032
} else {
20332033
if (!rootVolumesOfVm.isEmpty()) {
2034-
exstingVolumeOfVm = rootVolumesOfVm.get(0);
2034+
existingVolumeOfVm = rootVolumesOfVm.get(0);
20352035
} else {
20362036
// locate data volume of the vm
20372037
List<VolumeVO> diskVolumesOfVm = _volsDao.findByInstanceAndType(vmId, Volume.Type.DATADISK);
20382038
for (VolumeVO diskVolume : diskVolumesOfVm) {
20392039
if (diskVolume.getState() != Volume.State.Allocated) {
2040-
exstingVolumeOfVm = diskVolume;
2040+
existingVolumeOfVm = diskVolume;
20412041
break;
20422042
}
20432043
}
@@ -2051,8 +2051,8 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device
20512051

20522052
//don't create volume on primary storage if its being attached to the vm which Root's volume hasn't been created yet
20532053
StoragePoolVO destPrimaryStorage = null;
2054-
if (exstingVolumeOfVm != null && !exstingVolumeOfVm.getState().equals(Volume.State.Allocated)) {
2055-
destPrimaryStorage = _storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
2054+
if (existingVolumeOfVm != null && !existingVolumeOfVm.getState().equals(Volume.State.Allocated)) {
2055+
destPrimaryStorage = _storagePoolDao.findById(existingVolumeOfVm.getPoolId());
20562056
}
20572057

20582058
boolean volumeOnSecondary = volumeToAttach.getState() == Volume.State.Uploaded;
@@ -2071,15 +2071,15 @@ private Volume orchestrateAttachVolumeToVM(Long vmId, Long volumeId, Long device
20712071

20722072
// reload the volume from db
20732073
newVolumeOnPrimaryStorage = volFactory.getVolume(newVolumeOnPrimaryStorage.getId());
2074-
boolean moveVolumeNeeded = needMoveVolume(exstingVolumeOfVm, newVolumeOnPrimaryStorage);
2074+
boolean moveVolumeNeeded = needMoveVolume(existingVolumeOfVm, newVolumeOnPrimaryStorage);
20752075

20762076
if (moveVolumeNeeded) {
20772077
PrimaryDataStoreInfo primaryStore = (PrimaryDataStoreInfo)newVolumeOnPrimaryStorage.getDataStore();
20782078
if (primaryStore.isLocal()) {
20792079
throw new CloudRuntimeException(
20802080
"Failed to attach local data volume " + volumeToAttach.getName() + " to VM " + vm.getDisplayName() + " as migration of local data volume is not allowed");
20812081
}
2082-
StoragePoolVO vmRootVolumePool = _storagePoolDao.findById(exstingVolumeOfVm.getPoolId());
2082+
StoragePoolVO vmRootVolumePool = _storagePoolDao.findById(existingVolumeOfVm.getPoolId());
20832083

20842084
try {
20852085
newVolumeOnPrimaryStorage = _volumeMgr.moveVolume(newVolumeOnPrimaryStorage, vmRootVolumePool.getDataCenterId(), vmRootVolumePool.getPodId(), vmRootVolumePool.getClusterId(),

0 commit comments

Comments
 (0)