Skip to content

Commit

Permalink
Move clone operation into virtual machine mo
Browse files Browse the repository at this point in the history
  • Loading branch information
harikrishna-patnala committed Sep 27, 2021
1 parent e4727f7 commit 874b5a3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1244,9 +1244,6 @@ private Ternary<String, Long, Long> createTemplateFromVolume(VmwareContext conte
VirtualDisk requiredDisk = volumeDeviceInfo.first();
vmMo.createFullCloneWithSpecificDisk(templateUniqueName, dcMo.getVmFolder(), morPool, requiredDisk);
clonedVm = dcMo.findVm(templateUniqueName);

checkIfVMHasOnlyRequiredDisk(clonedVm, requiredDisk);

clonedVm.tagAsWorkerVM();
clonedVm.exportVm(secondaryMountPoint + "/" + installPath, templateUniqueName, false, false);

Expand Down Expand Up @@ -1839,7 +1836,6 @@ private Pair<String, String[]> exportVolumeToSecondaryStorage(VmwareContext cont
s_logger.error(msg);
throw new Exception(msg);
}
checkIfVMHasOnlyRequiredDisk(clonedVm, requiredDisk);
clonedVm.tagAsWorkerVM();
vmMo = clonedVm;
}
Expand All @@ -1854,19 +1850,6 @@ private Pair<String, String[]> exportVolumeToSecondaryStorage(VmwareContext cont
}
}

private void checkIfVMHasOnlyRequiredDisk(VirtualMachineMO clonedVm, VirtualDisk requiredDisk) throws Exception {

s_logger.info(String.format("Checking if Cloned VM %s is created only with required Disk, if not detach the remaining disks", clonedVm.getName()));
VirtualDisk[] vmDisks = clonedVm.getAllDiskDevice();
if (vmDisks.length != 1) {
String baseName = VmwareHelper.getDiskDeviceFileName(requiredDisk);
s_logger.info(String.format("Detaching all disks for the cloned VM: %s except disk with base name: %s, key=%d", clonedVm.getName(), baseName, requiredDisk.getKey()));
clonedVm.detachAllDisksExcept(VmwareHelper.getDiskDeviceFileName(requiredDisk), null);
} else {
s_logger.info(String.format("Cloned VM %s is created only with required Disk", clonedVm.getName()));
}
}

// Ternary<String(backup uuid in secondary storage), String(device bus name), String[](original disk chain in the snapshot)>
private Ternary<String, String, String[]> backupSnapshotToSecondaryStorage(VmwareContext context, VirtualMachineMO vmMo, VmwareHypervisorHost hypervisorHost, String installPath, String volumePath, String snapshotUuid,
String secStorageUrl, String prevSnapshotUuid, String prevBackupUuid, String workerVmName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,12 +784,41 @@ public boolean createFullCloneWithSpecificDisk(String cloneName, ManagedObjectRe
if (result) {
_context.waitForTaskProgressDone(morTask);
s_logger.debug(String.format("Cloned VM: %s as %s", getName(), cloneName));
makeSureVMHasOnlyRequiredDisk(cloneName, requiredDisk);
return true;
} else {
s_logger.error("VMware cloneVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
makeSureVMHasOnlyRequiredDisk(cloneName, requiredDisk);
return false;
}
}

return false;
private void makeSureVMHasOnlyRequiredDisk(String vmName, VirtualDisk requiredDisk) throws Exception {
VirtualMachineRuntimeInfo runtimeInfo = getRuntimeInfo();
HostMO hostMo = new HostMO(_context, runtimeInfo.getHost());
DatacenterMO dcMo = new DatacenterMO(_context, hostMo.getHyperHostDatacenter());

VirtualMachineMO clonedVm = dcMo.findVm(vmName);
VirtualDisk[] vmDisks = clonedVm.getAllDiskDevice();
s_logger.debug(String.format("Checking if VM %s is created only with required Disk, if not detach the remaining disks", vmName));
if (vmDisks.length != 1) {
VirtualDisk requiredCloneDisk = null;
for (VirtualDisk vmDisk: vmDisks) {
if (vmDisk.getKey() == requiredDisk.getKey()) {
requiredCloneDisk = vmDisk;
break;
}
}
if (requiredCloneDisk != null) {
String baseName = VmwareHelper.getDiskDeviceFileName(requiredCloneDisk);
s_logger.debug(String.format("Detaching all disks for the VM: %s except disk with base name: %s, key=%d", vmName, baseName, requiredCloneDisk.getKey()));
clonedVm.detachAllDisksExcept(baseName, null);
} else {
s_logger.error(String.format("Failed to identify required disk in VM %s", vmName));
}
} else {
s_logger.debug(String.format("VM %s is created only with required Disk", vmName));
}
}

public boolean createFullClone(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, ManagedObjectReference morDs, Storage.ProvisioningType diskProvisioningType)
Expand Down

0 comments on commit 874b5a3

Please sign in to comment.