From c8a437b782296e2b4a9727b1204d7f1693f2c806 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. (cherry picked from commit b8fdda0a34f5338db284ee616c5e04206c97b82e) Signed-off-by: Rohit Yadav --- .../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 f55ca67cb40..3b339ab6af6 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 @@ -2921,7 +2921,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; @@ -2971,6 +2971,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()) { @@ -3100,7 +3101,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; @@ -3127,6 +3128,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 757826154d1..621f005bd57 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java +++ b/plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java @@ -1290,6 +1290,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 {