[ https://issues.apache.org/jira/browse/CLOUDSTACK-8609?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16587913#comment-16587913 ]
ASF GitHub Bot commented on CLOUDSTACK-8609: -------------------------------------------- rhtyd closed pull request #2091: CLOUDSTACK-8609: [VMware] VM is not accessible after migration across clusters URL: https://github.com/apache/cloudstack/pull/2091 This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): 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 d2e8b91d7f0..e35a71566a1 100644 --- a/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java +++ b/plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java @@ -1665,6 +1665,7 @@ protected StartAnswer execute(StartCommand cmd) { String existingVmName = null; VirtualMachineFileInfo existingVmFileInfo = null; VirtualMachineFileLayoutEx existingVmFileLayout = null; + List<DatastoreMO> existingDatastores = new ArrayList<DatastoreMO>(); Pair<String, String> names = composeVmNames(vmSpec); String vmInternalCSName = names.first(); @@ -1787,6 +1788,7 @@ protected StartAnswer execute(StartCommand cmd) { existingVmName = existingVmInDc.getName(); existingVmFileInfo = existingVmInDc.getFileInfo(); existingVmFileLayout = existingVmInDc.getFileLayout(); + existingDatastores = existingVmInDc.getAllDatastores(); existingVmInDc.unregisterVm(); } Pair<ManagedObjectReference, DatastoreMO> rootDiskDataStoreDetails = null; @@ -2253,7 +2255,18 @@ protected StartAnswer execute(StartCommand cmd) { // Since VM was successfully powered-on, if there was an existing VM in a different cluster that was unregistered, delete all the files associated with it. if (existingVmName != null && existingVmFileLayout != null) { - deleteUnregisteredVmFiles(existingVmFileLayout, dcMo, true); + List<String> vmDatastoreNames = new ArrayList<String>(); + for (DatastoreMO vmDatastore : vmMo.getAllDatastores()) { + vmDatastoreNames.add(vmDatastore.getName()); + } + // Don't delete files that are in a datastore that is being used by the new VM as well (zone-wide datastore). + List<String> skipDatastores = new ArrayList<String>(); + for (DatastoreMO existingDatastore : existingDatastores) { + if (vmDatastoreNames.contains(existingDatastore.getName())) { + skipDatastores.add(existingDatastore.getName()); + } + } + deleteUnregisteredVmFiles(existingVmFileLayout, dcMo, true, skipDatastores); } return startAnswer; @@ -2941,7 +2954,14 @@ private void postDiskConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO v } } - private void deleteUnregisteredVmFiles(VirtualMachineFileLayoutEx vmFileLayout, DatacenterMO dcMo, boolean deleteDisks) throws Exception { + private void checkAndDeleteDatastoreFile(String filePath, List<String> skipDatastores, DatastoreMO dsMo, DatacenterMO dcMo) throws Exception { + if (dsMo != null && dcMo != null && (skipDatastores == null || !skipDatastores.contains(dsMo.getName()))) { + s_logger.debug("Deleting file: " + filePath); + dsMo.deleteFile(filePath, dcMo.getMor(), true); + } + } + + private void deleteUnregisteredVmFiles(VirtualMachineFileLayoutEx vmFileLayout, DatacenterMO dcMo, boolean deleteDisks, List<String> skipDatastores) throws Exception { s_logger.debug("Deleting files associated with an existing VM that was unregistered"); DatastoreFile vmFolder = null; try { @@ -2954,8 +2974,7 @@ private void deleteUnregisteredVmFiles(VirtualMachineFileLayoutEx vmFileLayout, else if (file.getType().equals("config")) vmFolder = new DatastoreFile(fileInDatastore.getDatastoreName(), fileInDatastore.getDir()); DatastoreMO dsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(fileInDatastore.getDatastoreName())); - s_logger.debug("Deleting file: " + file.getName()); - dsMo.deleteFile(file.getName(), dcMo.getMor(), true, VmwareManager.s_vmwareSearchExcludeFolder.value()); + checkAndDeleteDatastoreFile(file.getName(), skipDatastores, dsMo, dcMo); } // Delete files that are present in the VM folder - this will take care of the VM disks as well. DatastoreMO vmFolderDsMo = new DatastoreMO(dcMo.getContext(), dcMo.findDatastore(vmFolder.getDatastoreName())); @@ -2963,14 +2982,12 @@ else if (file.getType().equals("config")) if (deleteDisks) { for (String file : files) { String vmDiskFileFullPath = String.format("%s/%s", vmFolder.getPath(), file); - s_logger.debug("Deleting file: " + vmDiskFileFullPath); - vmFolderDsMo.deleteFile(vmDiskFileFullPath, dcMo.getMor(), true, VmwareManager.s_vmwareSearchExcludeFolder.value()); + checkAndDeleteDatastoreFile(vmDiskFileFullPath, skipDatastores, vmFolderDsMo, dcMo); } } // Delete VM folder if (deleteDisks || files.length == 0) { - s_logger.debug("Deleting folder: " + vmFolder.getPath()); - vmFolderDsMo.deleteFolder(vmFolder.getPath(), dcMo.getMor()); + checkAndDeleteDatastoreFile(vmFolder.getPath(), skipDatastores, vmFolderDsMo, dcMo); } } catch (Exception e) { String message = "Failed to delete files associated with an existing VM that was unregistered due to " + VmwareHelper.getExceptionMessage(e); @@ -4905,7 +4922,7 @@ protected Answer execute(UnregisterVMCommand cmd) { VirtualMachineFileLayoutEx vmFileLayout = vmMo.getFileLayout(); context.getService().unregisterVM(vmMo.getMor()); if (cmd.getCleanupVmFiles()) { - deleteUnregisteredVmFiles(vmFileLayout, dataCenterMo, false); + deleteUnregisteredVmFiles(vmFileLayout, dataCenterMo, false, null); } return new Answer(cmd, true, "unregister succeeded"); } catch (Exception e) { 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 0078793df8d..b700b6d8f63 100644 --- a/vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java +++ b/vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java @@ -34,6 +34,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; +import org.apache.commons.collections.CollectionUtils; import org.apache.log4j.Logger; import com.google.gson.Gson; @@ -932,6 +933,38 @@ else if (prop.getName().startsWith("value[")) { return networks; } + public List<DatastoreMO> getAllDatastores() throws Exception { + PropertySpec pSpec = new PropertySpec(); + pSpec.setType("Datastore"); + pSpec.getPathSet().add("name"); + + TraversalSpec vmDatastoreTraversal = new TraversalSpec(); + vmDatastoreTraversal.setType("VirtualMachine"); + vmDatastoreTraversal.setPath("datastore"); + vmDatastoreTraversal.setName("vmDatastoreTraversal"); + + ObjectSpec oSpec = new ObjectSpec(); + oSpec.setObj(_mor); + oSpec.setSkip(Boolean.TRUE); + oSpec.getSelectSet().add(vmDatastoreTraversal); + + PropertyFilterSpec pfSpec = new PropertyFilterSpec(); + pfSpec.getPropSet().add(pSpec); + pfSpec.getObjectSet().add(oSpec); + List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>(); + pfSpecArr.add(pfSpec); + + List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr); + + List<DatastoreMO> datastores = new ArrayList<DatastoreMO>(); + if (CollectionUtils.isNotEmpty(ocs)) { + for (ObjectContent oc : ocs) { + datastores.add(new DatastoreMO(_context, oc.getObj())); + } + } + return datastores; + } + /** * Retrieve path info to access VM files via vSphere web interface * @return [0] vm-name, [1] data-center-name, [2] datastore-name ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > [VMware] VM is not accessible after a migration across clusters. > ---------------------------------------------------------------- > > Key: CLOUDSTACK-8609 > URL: https://issues.apache.org/jira/browse/CLOUDSTACK-8609 > Project: CloudStack > Issue Type: Bug > Security Level: Public(Anyone can view this level - this is the > default.) > Reporter: Likitha Shetty > Assignee: Suresh Kumar Anaparti > Priority: Major > Fix For: Future > > > +Steps to reproduce+ > 1. Deploy a VMware zone with 2 clusters (a host each, H1 and H2) and one > zone-wide primary storage spanning the two clusters. > 2. Deploy a VM (VM1) on one of the hosts (H1). > 3. Stop VM1. > 4. Make the host that contains the VM unsuitable for further VM deployments > - host runs out of capacity (cpu/memory) > - host has maximum VMs deployed on it > 5. Start VM1. > 6. VM will be powered on H2 but will not be accessible because the .vmx and > other VM files associated with the VM have been deleted. -- This message was sent by Atlassian JIRA (v7.6.3#76005)