From b8fdda0a34f5338db284ee616c5e04206c97b82e Mon Sep 17 00:00:00 2001 From: Likitha Shetty Date: Tue, 28 Oct 2014 16:38:18 +0530 Subject: [PATCH] BUG-ID: CLOUDSTACK-7102. Volume migration fails with 'VM i-2-3-VM does not exist in VMware datacenter' expection. Look for a VM in vCenter based on both the vCenter name and CS internal name (required in case 'vm.instancename.flag' is enabled). During Attach Volume and Volume Migration, for lookup and other operations use VM's name as obtained from vCenter instead of using the name set in the agent command. --- .../vmware/resource/VmwareResource.java | 6 ++++-- .../resource/VmwareStorageProcessor.java | 1 + .../hypervisor/vmware/mo/DatacenterMO.java | 18 ++++++------------ 3 files changed, 11 insertions(+), 14 deletions(-) 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 7406ca09ade..65960573c6f 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 @@ -2904,7 +2904,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa } VirtualMachineTO vmTo = cmd.getVirtualMachine(); - final String vmName = vmTo.getName(); + String vmName = vmTo.getName(); VmwareHypervisorHost srcHyperHost = null; VmwareHypervisorHost tgtHyperHost = null; @@ -2954,6 +2954,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa s_logger.error(msg); throw new Exception(msg); } + vmName = vmMo.getName(); // Get details of each target datastore & attach to source host. for (Entry entry : volToFiler.entrySet()) { @@ -3083,7 +3084,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa s_logger.info("Executing resource MigrateVolumeCommand: " + _gson.toJson(cmd)); } - final String vmName = cmd.getAttachedVmName(); + String vmName = cmd.getAttachedVmName(); VirtualMachineMO vmMo = null; VmwareHypervisorHost srcHyperHost = null; @@ -3110,6 +3111,7 @@ public class VmwareResource implements StoragePoolResource, ServerResource, Vmwa s_logger.error(msg); throw new Exception(msg); } + vmName = vmMo.getName(); morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(srcHyperHost, tgtDsName); if (morDs == null) { String msg = "Unable to find the mounted datastore with name " + tgtDsName + " to execute MigrateVolumeCommand"; diff --git a/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java b/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java index 1a394102edd..b256ae3f5a1 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java +++ b/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java @@ -1299,6 +1299,7 @@ public class VmwareStorageProcessor implements StorageProcessor { s_logger.error(msg); throw new Exception(msg); } + vmName = vmMo.getName(); ManagedObjectReference morDs = null; diff --git a/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java b/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java index ef137836dd2..39a30beadfb 100755 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/DatacenterMO.java @@ -82,19 +82,13 @@ public class DatacenterMO extends BaseMO { } public VirtualMachineMO findVm(String vmName) throws Exception { - List ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name"}); - if (ocs != null && ocs.size() > 0) { - for (ObjectContent oc : ocs) { - List props = oc.getPropSet(); - if (props != null) { - for (DynamicProperty prop : props) { - if (prop.getVal().toString().equals(vmName)) - return new VirtualMachineMO(_context, oc.getObj()); - } - } - } + int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME); + if (key == 0) { + s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!"); } - return null; + String instanceNameCustomField = "value[" + key + "]"; + List ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", instanceNameCustomField}); + return HypervisorHostHelper.findVmFromObjectContent(_context, ocs.toArray(new ObjectContent[0]), vmName, instanceNameCustomField); } public List findVmByNameAndLabel(String vmLabel) throws Exception {