From 5f5909141848649574c471ec2d2e8e295ef80a6c Mon Sep 17 00:00:00 2001 From: Sanjay Tripathi Date: Fri, 27 Mar 2015 14:27:48 +0530 Subject: [PATCH] CLOUDSTACK-8350: Volume attachment failed when user stops the VM during attaching DISK. --- .../src/com/cloud/storage/VolumeApiServiceImpl.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/server/src/com/cloud/storage/VolumeApiServiceImpl.java b/server/src/com/cloud/storage/VolumeApiServiceImpl.java index f04656a3009..cae1e1d1ac1 100644 --- a/server/src/com/cloud/storage/VolumeApiServiceImpl.java +++ b/server/src/com/cloud/storage/VolumeApiServiceImpl.java @@ -1210,7 +1210,9 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic destPrimaryStorage = _storagePoolDao.findById(exstingVolumeOfVm.getPoolId()); } - if (destPrimaryStorage != null && (volumeToAttach.getState() == Volume.State.Allocated || volumeToAttach.getState() == Volume.State.Uploaded)) { + boolean volumeOnSecondary = volumeToAttach.getState() == Volume.State.Uploaded; + + if (destPrimaryStorage != null && (volumeToAttach.getState() == Volume.State.Allocated || volumeOnSecondary)) { try { newVolumeOnPrimaryStorage = _volumeMgr.createVolumeOnPrimaryStorage(vm, volumeToAttach, rootDiskHyperType, destPrimaryStorage); } catch (NoTransitionException e) { @@ -1243,6 +1245,13 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic } } VolumeVO newVol = _volsDao.findById(newVolumeOnPrimaryStorage.getId()); + // Getting the fresh vm object in case of volume migration to check the current state of VM + if (moveVolumeNeeded || volumeOnSecondary) { + vm = _userVmDao.findById(vmId); + if (vm == null) { + throw new InvalidParameterValueException("VM not found."); + } + } newVol = sendAttachVolumeCommand(vm, newVol, deviceId); return newVol; }