From e211b92324686a56c88b4cd3e02c7f50b502558b Mon Sep 17 00:00:00 2001 From: Pearl Dsilva Date: Tue, 4 Aug 2020 17:45:09 +0530 Subject: [PATCH] Changed workflow for storing volumes in sec storage --- .../admin/offering/CreateDiskOfferingCmd.java | 2 +- .../offering/CreateServiceOfferingCmd.java | 2 +- .../resource/VmwareStorageProcessor.java | 52 ++++++++----------- .../vmware/mo/VirtualMachineMO.java | 10 ++-- 4 files changed, 30 insertions(+), 36 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateDiskOfferingCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateDiskOfferingCmd.java index 6b9ddce75c2..a830777031f 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateDiskOfferingCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateDiskOfferingCmd.java @@ -152,7 +152,7 @@ public class CreateDiskOfferingCmd extends BaseCmd { since = "4.14") private String cacheMode; - @Parameter(name = ApiConstants.STORAGE_POLICY, type = CommandType.UUID, entityType = VsphereStoragePoliciesResponse.class,required = false, description = "Name of the storage policy defined at vCenter, this is applicable only for VMware") + @Parameter(name = ApiConstants.STORAGE_POLICY, type = CommandType.UUID, entityType = VsphereStoragePoliciesResponse.class,required = false, description = "Name of the storage policy defined at vCenter, this is applicable only for VMware", since = "4.15") private Long storagePolicy; ///////////////////////////////////////////////////// diff --git a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java index 82a0fcbd5b9..745e6a0f3c6 100644 --- a/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java +++ b/api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateServiceOfferingCmd.java @@ -217,7 +217,7 @@ public class CreateServiceOfferingCmd extends BaseCmd { since = "4.13") private Integer minMemory; - @Parameter(name = ApiConstants.STORAGE_POLICY, type = CommandType.UUID, entityType = VsphereStoragePoliciesResponse.class,required = false, description = "Name of the storage policy defined at vCenter, this is applicable only for VMware") + @Parameter(name = ApiConstants.STORAGE_POLICY, type = CommandType.UUID, entityType = VsphereStoragePoliciesResponse.class,required = false, description = "Name of the storage policy defined at vCenter, this is applicable only for VMware", since = "4.15") private Long storagePolicy; ///////////////////////////////////////////////////// diff --git a/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java b/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java index 96e5207615a..cd6edbe964b 100644 --- a/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java +++ b/plugins/hypervisors/vmware/src/main/java/com/cloud/storage/resource/VmwareStorageProcessor.java @@ -1015,11 +1015,9 @@ public class VmwareStorageProcessor implements StorageProcessor { workerVm.attachDisk(new String[] {datastoreVolumePath}, morDs); vmMo = workerVm; clonedWorkerVMNeeded = false; - } else { - vmMo.createSnapshot(exportName, "Temporary snapshot for copy-volume command", false, false); } - exportVolumeToSecondaryStorage(vmMo, volumePath, secStorageUrl, destVolumePath, exportName, hostService.getWorkerName(hyperHost.getContext(), cmd, 1), _nfsVersion, clonedWorkerVMNeeded); + exportVolumeToSecondaryStorage(hyperHost.getContext(), vmMo, hyperHost, volumePath, secStorageUrl, destVolumePath, exportName, hostService.getWorkerName(hyperHost.getContext(), cmd, 1), _nfsVersion, clonedWorkerVMNeeded); return new Pair<>(destVolumePath, exportName); } finally { @@ -1104,7 +1102,7 @@ public class VmwareStorageProcessor implements StorageProcessor { } } - private Ternary createTemplateFromVolume(VirtualMachineMO vmMo, String installPath, long templateId, String templateUniqueName, + private Ternary createTemplateFromVolume(VmwareContext context, VirtualMachineMO vmMo, VmwareHypervisorHost hyperHost, String installPath, long templateId, String templateUniqueName, String secStorageUrl, String volumePath, String workerVmName, String nfsVersion) throws Exception { String secondaryMountPoint = mountService.getMountPoint(secStorageUrl, nfsVersion); @@ -1131,8 +1129,6 @@ public class VmwareStorageProcessor implements StorageProcessor { throw new Exception(msg); } - VmwareContext context = hostService.getServiceContext(null); - VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, null); DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter()); ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool(); vmMo.createFullCloneWithSpecificDisk(templateUniqueName, dcMo.getVmFolder(), morPool, VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), volumeDeviceInfo); @@ -1184,8 +1180,6 @@ public class VmwareStorageProcessor implements StorageProcessor { clonedVm.detachAllDisks(); clonedVm.destroy(); } - - vmMo.removeSnapshot(templateUniqueName, false); } } @@ -1211,7 +1205,7 @@ public class VmwareStorageProcessor implements StorageProcessor { if (volume.getVmName() == null) { ManagedObjectReference secMorDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, volume.getDataStore().getUuid()); DatastoreMO dsMo = new DatastoreMO(hyperHost.getContext(), secMorDs); - workerVmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, "workervm"+volume.getUuid(), null); + workerVmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, "workervm"+volume.getUuid()); if (workerVmMo == null) { throw new Exception("Unable to find created worker VM"); } @@ -1244,9 +1238,8 @@ public class VmwareStorageProcessor implements StorageProcessor { } Ternary result = - createTemplateFromVolume(vmMo, template.getPath(), template.getId(), template.getName(), secondaryStoragePoolURL, volumePath, + createTemplateFromVolume(context, vmMo, hyperHost, template.getPath(), template.getId(), template.getName(), secondaryStoragePoolURL, volumePath, hostService.getWorkerName(context, cmd, 0), _nfsVersion); - TemplateObjectTO newTemplate = new TemplateObjectTO(); newTemplate.setPath(result.first()); newTemplate.setFormat(ImageFormat.OVA); @@ -1568,7 +1561,7 @@ public class VmwareStorageProcessor implements StorageProcessor { ManagedObjectReference dsMor = hyperHost.findDatastoreByName(dsFile.getDatastoreName()); DatastoreMO dsMo = new DatastoreMO(context, dsMor); - VirtualMachineMO workerVM = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName, null); + VirtualMachineMO workerVM = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName); if (workerVM == null) { throw new CloudRuntimeException("Failed to find the newly created worker VM: " + workerVMName); @@ -1706,7 +1699,7 @@ public class VmwareStorageProcessor implements StorageProcessor { } // return Pair - private Pair exportVolumeToSecondaryStorage(VirtualMachineMO vmMo, String volumePath, String secStorageUrl, String secStorageDir, + private Pair exportVolumeToSecondaryStorage(VmwareContext context, VirtualMachineMO vmMo, VmwareHypervisorHost hyperHost, String volumePath, String secStorageUrl, String secStorageDir, String exportName, String workerVmName, String nfsVersion, boolean clonedWorkerVMNeeded) throws Exception { String secondaryMountPoint = mountService.getMountPoint(secStorageUrl, nfsVersion); @@ -1739,13 +1732,18 @@ public class VmwareStorageProcessor implements StorageProcessor { String disks[] = vmMo.getCurrentSnapshotDiskChainDatastorePaths(diskDevice); if (clonedWorkerVMNeeded) { // 4 MB is the minimum requirement for VM memory in VMware - Pair cloneResult = - vmMo.cloneFromCurrentSnapshot(workerVmName, 0, 4, diskDevice, VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), virtualHardwareVersion); - clonedVm = cloneResult.first(); - clonedVm.exportVm(exportPath, exportName, false, false); - } else { - vmMo.exportVm(exportPath, exportName, false, false); + DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter()); + ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool(); + vmMo.createFullCloneWithSpecificDisk(exportName, dcMo.getVmFolder(), morPool, VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), volumeDeviceInfo); + clonedVm = dcMo.findVm(exportName); + if (clonedVm == null) { + String msg = "Failed to clone VM. volume path: " + volumePath; + s_logger.error(msg); + throw new Exception(msg); + } } + vmMo.exportVm(exportPath, exportName, false, false); + return new Pair<>(diskDevice, disks); } finally { if (clonedVm != null) { @@ -1756,12 +1754,12 @@ public class VmwareStorageProcessor implements StorageProcessor { } // Ternary - private Ternary backupSnapshotToSecondaryStorage(VirtualMachineMO vmMo, String installPath, String volumePath, String snapshotUuid, + private Ternary backupSnapshotToSecondaryStorage(VmwareContext context, VirtualMachineMO vmMo, VmwareHypervisorHost hypervisorHost, String installPath, String volumePath, String snapshotUuid, String secStorageUrl, String prevSnapshotUuid, String prevBackupUuid, String workerVmName, String nfsVersion) throws Exception { String backupUuid = UUID.randomUUID().toString(); - Pair snapshotInfo = exportVolumeToSecondaryStorage(vmMo, volumePath, secStorageUrl, installPath, backupUuid, workerVmName, nfsVersion, true); + Pair snapshotInfo = exportVolumeToSecondaryStorage(context, vmMo, hypervisorHost, volumePath, secStorageUrl, installPath, backupUuid, workerVmName, nfsVersion, true); return new Ternary<>(backupUuid, snapshotInfo.first(), snapshotInfo.second()); } @@ -1830,12 +1828,8 @@ public class VmwareStorageProcessor implements StorageProcessor { hasOwnerVm = true; } - if (!vmMo.createSnapshot(snapshotUuid, "Snapshot taken for " + srcSnapshot.getName(), false, false)) { - throw new Exception("Failed to take snapshot " + srcSnapshot.getName() + " on vm: " + vmName); - } - backupResult = - backupSnapshotToSecondaryStorage(vmMo, destSnapshot.getPath(), srcSnapshot.getVolume().getPath(), snapshotUuid, secondaryStorageUrl, + backupSnapshotToSecondaryStorage(context, vmMo, hyperHost, destSnapshot.getPath(), srcSnapshot.getVolume().getPath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1), _nfsVersion); snapshotBackupUuid = backupResult.first(); @@ -1916,7 +1910,7 @@ public class VmwareStorageProcessor implements StorageProcessor { } } } else { - s_logger.error("Can not find the snapshot we just used ?!"); + s_logger.info("No snapshots created to be deleted!"); } } @@ -2281,7 +2275,7 @@ public class VmwareStorageProcessor implements StorageProcessor { * String dummyVmName = hostService.getWorkerName(context, cmd, 0); try { s_logger.info("Create worker VM " + dummyVmName); - vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName, null); + vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName); if (vmMo == null) { throw new Exception("Unable to create a dummy VM for volume creation"); } @@ -2966,7 +2960,7 @@ public class VmwareStorageProcessor implements StorageProcessor { String dummyVmName = hostService.getWorkerName(context, cmd, 0); - VirtualMachineMO vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName, null); + VirtualMachineMO vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, dummyVmName); if (vmMo == null) { throw new Exception("Unable to create a dummy VM for volume creation"); diff --git a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java index fc2621921dc..c4e1493a5f4 100644 --- a/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java +++ b/vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java @@ -303,7 +303,7 @@ public class VirtualMachineMO extends BaseMO { try { Thread.sleep(1000); } catch (InterruptedException e) { - s_logger.debug("[ignored] interupted while powering of vm."); + s_logger.debug("[ignored] interrupted while powering of vm."); } } @@ -336,7 +336,7 @@ public class VirtualMachineMO extends BaseMO { try { Thread.sleep(1000); } catch (InterruptedException e) { - s_logger.debug("[ignored] interupted while powering of vm unconditionaly."); + s_logger.debug("[ignored] interrupted while powering of vm unconditionally."); } } return true; @@ -370,7 +370,7 @@ public class VirtualMachineMO extends BaseMO { try { Thread.sleep(1000); } catch (InterruptedException e) { - s_logger.debug("[ignored] interupted while pausing after power off."); + s_logger.debug("[ignored] interrupted while pausing after power off."); } } else { break; @@ -1780,7 +1780,7 @@ public class VirtualMachineMO extends BaseMO { command.add((new File(name).getName())); } - s_logger.info("Package OVA with commmand: " + command.toString()); + s_logger.info("Package OVA with command: " + command.toString()); command.execute(); // to be safe, physically test existence of the target OVA file @@ -1793,7 +1793,7 @@ public class VirtualMachineMO extends BaseMO { success = true; } } - s_logger.info("volss: copy vmdk and ovf file finishes " + System.currentTimeMillis()); + s_logger.info("volss: copy vmdk and ovf file finished " + System.currentTimeMillis()); } catch (Throwable e) { s_logger.error("Unexpected exception ", e); } finally {