Skip to content

Commit c69da4c

Browse files
Koushik DasMurali Reddy
authored andcommitted
CLOUDSTACK-595 : Recreate root volume scenarios doesn't work in vmware
Issue seen during system vm template upgrade and restoreVM command scenarios for vmware. In these cases CS tries to recreate root disk with same name as the existing one, in case of vmware this results in creation of vmdk file with same name for both existing and new root volume. This results in undesired behavior when storage cleanup thread tries to cleanup old volume. Made the vmdk file name unique by adding the volume id to it. This will ensure that during volume recreation in the scenarios mentioned vmdk will get created with a new name and there will be no undesired side effects of running the storage cleanup thread.
1 parent 97f6fe1 commit c69da4c

2 files changed

Lines changed: 34 additions & 29 deletions

File tree

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,10 +3708,12 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
37083708
ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, pool.getUuid());
37093709
if (morDatastore == null)
37103710
throw new Exception("Unable to find datastore in vSphere");
3711-
3711+
37123712
DatastoreMO dsMo = new DatastoreMO(context, morDatastore);
37133713

3714-
if (cmd.getDiskCharacteristics().getType() == Volume.Type.ROOT) {
3714+
if (dskch.getType() == Volume.Type.ROOT) {
3715+
// attach volume id to make the name unique
3716+
String vmdkName = dskch.getName() + "-" + dskch.getVolumeId();
37153717
if (cmd.getTemplateUrl() == null) {
37163718
// create a root volume for blank VM
37173719
String dummyVmName = getWorkerName(context, cmd, 0);
@@ -3723,16 +3725,15 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
37233725
throw new Exception("Unable to create a dummy VM for volume creation");
37243726
}
37253727

3726-
String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), cmd.getDiskCharacteristics().getName());
3728+
String volumeDatastorePath = String.format("[%s] %s.vmdk", dsMo.getName(), vmdkName);
37273729
synchronized (this) {
37283730
s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
3729-
VmwareHelper.deleteVolumeVmdkFiles(dsMo, cmd.getDiskCharacteristics().getName(), dcMo);
3730-
vmMo.createDisk(volumeDatastorePath, (int) (cmd.getDiskCharacteristics().getSize() / (1024L * 1024L)), morDatastore, -1);
3731+
VmwareHelper.deleteVolumeVmdkFiles(dsMo, vmdkName, dcMo);
3732+
vmMo.createDisk(volumeDatastorePath, (int) (dskch.getSize() / (1024L * 1024L)), morDatastore, -1);
37313733
vmMo.detachDisk(volumeDatastorePath, false);
37323734
}
37333735

3734-
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), cmd.getDiskCharacteristics().getName(), pool.getPath(), cmd
3735-
.getDiskCharacteristics().getName(), cmd.getDiskCharacteristics().getSize(), null);
3736+
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), dskch.getName(), pool.getPath(), vmdkName, dskch.getSize(), null);
37363737
return new CreateAnswer(cmd, vol);
37373738
} finally {
37383739
vmMo.detachAllDisks();
@@ -3756,41 +3757,40 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
37563757
throw new Exception(msg);
37573758
}
37583759

3759-
String name = cmd.getDiskCharacteristics().getName();
3760-
if(dsMo.folderExists(String.format("[%s]", dsMo.getName()), name))
3761-
dsMo.deleteFile(String.format("[%s] %s/", dsMo.getName(), name), dcMo.getMor(), false);
3760+
if(dsMo.folderExists(String.format("[%s]", dsMo.getName()), vmdkName))
3761+
dsMo.deleteFile(String.format("[%s] %s/", dsMo.getName(), vmdkName), dcMo.getMor(), false);
37623762

37633763
s_logger.info("create linked clone from template");
3764-
if (!vmTemplate.createLinkedClone(name, morBaseSnapshot, dcMo.getVmFolder(), morPool, morDatastore)) {
3764+
if (!vmTemplate.createLinkedClone(vmdkName, morBaseSnapshot, dcMo.getVmFolder(), morPool, morDatastore)) {
37653765
String msg = "Unable to clone from the template";
37663766
s_logger.error(msg);
37673767
throw new Exception(msg);
37683768
}
37693769

3770-
VirtualMachineMO vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(name);
3770+
VirtualMachineMO vmMo = new ClusterMO(context, morCluster).findVmOnHyperHost(vmdkName);
37713771
assert (vmMo != null);
37723772

37733773
// we can't rely on un-offical API (VirtualMachineMO.moveAllVmDiskFiles() any more, use hard-coded disk names that we know
37743774
// to move files
37753775
s_logger.info("Move volume out of volume-wrapper VM ");
3776-
dsMo.moveDatastoreFile(String.format("[%s] %s/%s.vmdk", dsMo.getName(), name, name),
3777-
dcMo.getMor(), dsMo.getMor(),
3778-
String.format("[%s] %s.vmdk", dsMo.getName(), name), dcMo.getMor(), true);
3776+
dsMo.moveDatastoreFile(String.format("[%s] %s/%s.vmdk", dsMo.getName(), vmdkName, vmdkName),
3777+
dcMo.getMor(), dsMo.getMor(),
3778+
String.format("[%s] %s.vmdk", dsMo.getName(), vmdkName), dcMo.getMor(), true);
37793779

3780-
dsMo.moveDatastoreFile(String.format("[%s] %s/%s-delta.vmdk", dsMo.getName(), name, name),
3780+
dsMo.moveDatastoreFile(String.format("[%s] %s/%s-delta.vmdk", dsMo.getName(), vmdkName, vmdkName),
37813781
dcMo.getMor(), dsMo.getMor(),
37823782
String.format("[%s] %s-delta.vmdk", dsMo.getName(), name), dcMo.getMor(), true);
37833783

3784-
s_logger.info("detach disks from volume-wrapper VM " + name);
3784+
s_logger.info("detach disks from volume-wrapper VM " + vmdkName);
37853785
vmMo.detachAllDisks();
37863786

3787-
s_logger.info("destroy volume-wrapper VM " + name);
3787+
s_logger.info("destroy volume-wrapper VM " + vmdkName);
37883788
vmMo.destroy();
37893789

3790-
String srcFile = String.format("[%s] %s/", dsMo.getName(), name);
3790+
String srcFile = String.format("[%s] %s/", dsMo.getName(), vmdkName);
37913791
dsMo.deleteFile(srcFile, dcMo.getMor(), true);
37923792

3793-
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), name, pool.getPath(), name, cmd.getDiskCharacteristics().getSize(), null);
3793+
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), dskch.getName(), pool.getPath(), name, cmd.getDiskCharacteristics().getSize(), null);
37943794
return new CreateAnswer(cmd, vol);
37953795
}
37963796
} else {
@@ -3809,12 +3809,11 @@ public synchronized CreateAnswer execute(CreateCommand cmd) {
38093809
// s_logger.info("Delete file if exists in datastore to clear the way for creating the volume. file: " + volumeDatastorePath);
38103810
VmwareHelper.deleteVolumeVmdkFiles(dsMo, volumeUuid.toString(), dcMo);
38113811

3812-
vmMo.createDisk(volumeDatastorePath, (int) (cmd.getDiskCharacteristics().getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey());
3812+
vmMo.createDisk(volumeDatastorePath, (int) (dskch.getSize() / (1024L * 1024L)), morDatastore, vmMo.getScsiDeviceControllerKey());
38133813
vmMo.detachDisk(volumeDatastorePath, false);
38143814
}
38153815

3816-
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), cmd.getDiskCharacteristics().getName(), pool.getPath(), volumeUuid, cmd
3817-
.getDiskCharacteristics().getSize(), null);
3816+
VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), dskch.getName(), pool.getPath(), volumeUuid, dskch.getSize(), null);
38183817
return new CreateAnswer(cmd, vol);
38193818
} finally {
38203819
s_logger.info("Destroy dummy VM after volume creation");

‎server/src/com/cloud/storage/StorageManagerImpl.java‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,12 +3382,6 @@ public void prepare(VirtualMachineProfile<? extends VirtualMachine> vm, DeployDe
33823382
@DB
33833383
protected VolumeVO switchVolume(VolumeVO existingVolume, VirtualMachineProfile<? extends VirtualMachine> vm) throws StorageUnavailableException {
33843384
Transaction txn = Transaction.currentTxn();
3385-
txn.start();
3386-
try {
3387-
stateTransitTo(existingVolume, Volume.Event.DestroyRequested);
3388-
} catch (NoTransitionException e) {
3389-
s_logger.debug("Unable to destroy existing volume: " + e.toString());
3390-
}
33913385

33923386
Long templateIdToUse = null;
33933387
Long volTemplateId = existingVolume.getTemplateId();
@@ -3398,7 +3392,19 @@ protected VolumeVO switchVolume(VolumeVO existingVolume, VirtualMachineProfile<?
33983392
}
33993393
templateIdToUse = vmTemplateId;
34003394
}
3395+
3396+
txn.start();
34013397
VolumeVO newVolume = allocateDuplicateVolume(existingVolume, templateIdToUse);
3398+
// In case of Vmware if vm reference is not removed then during root disk cleanup
3399+
// the vm also gets deleted, so remove the reference
3400+
if (vm.getHypervisorType() == HypervisorType.VMware) {
3401+
_volsDao.detachVolume(existingVolume.getId());
3402+
}
3403+
try {
3404+
stateTransitTo(existingVolume, Volume.Event.DestroyRequested);
3405+
} catch (NoTransitionException e) {
3406+
s_logger.debug("Unable to destroy existing volume: " + e.toString());
3407+
}
34023408
txn.commit();
34033409
return newVolume;
34043410

0 commit comments

Comments
 (0)