diff --git a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java index 9ad3a5f3067..e98b82964db 100755 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -3057,6 +3057,18 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa s_logger.debug("Successfully migrated storage of VM " + vmName + " to target datastore(s)"); } + // Consolidate VM disks. + // In case of a linked clone VM, if VM's disks are not consolidated, + // further VM operations such as volume snapshot, VM snapshot etc. will result in DB inconsistencies. + String apiVersion = HypervisorHostHelper.getVcenterApiVersion(vmMo.getContext()); + if (apiVersion.compareTo("5.0") >= 0) { + if (!vmMo.consolidateVmDisks()) { + s_logger.warn("VM disk consolidation failed after storage migration. Yet proceeding with VM migration."); + } else { + s_logger.debug("Successfully consolidated disks of VM " + vmName + "."); + } + } + // Update and return volume path for every disk because that could have changed after migration for (Entry entry : volToFiler.entrySet()) { volume = entry.getKey(); @@ -3166,6 +3178,18 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa s_logger.debug("Successfully migrated volume " + volumePath + " to target datastore " + tgtDsName); } + // Consolidate VM disks. + // In case of a linked clone VM, if VM's disks are not consolidated, + // further volume operations on the ROOT volume such as volume snapshot etc. will result in DB inconsistencies. + String apiVersion = HypervisorHostHelper.getVcenterApiVersion(vmMo.getContext()); + if (apiVersion.compareTo("5.0") >= 0) { + if (!vmMo.consolidateVmDisks()) { + s_logger.warn("VM disk consolidation failed after storage migration."); + } else { + s_logger.debug("Successfully consolidated disks of VM " + vmName + "."); + } + } + // Update and return volume path because that could have changed after migration if (!targetDsMo.fileExists(fullVolumePath)) { VirtualDisk[] disks = vmMo.getAllDiskDevice(); diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java index c2e9d7f8ee7..471b4a8ded5 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java @@ -2638,4 +2638,16 @@ public class VirtualMachineMO extends BaseMO { } return guestOsSupportsMemoryHotAdd && virtualHardwareSupportsMemoryHotAdd; } + + public boolean consolidateVmDisks() throws Exception { + ManagedObjectReference morTask = _context.getService().consolidateVMDisksTask(_mor); + boolean result = _context.getVimClient().waitForTask(morTask); + if (result) { + _context.waitForTaskProgressDone(morTask); + return true; + } else { + s_logger.error("VMware ConsolidateVMDisks_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask)); + } + return false; + } }