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 ea5999b
Show file tree
Hide file tree
Showing 2 changed files with 31 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 @@ -762,6 +762,7 @@ public boolean createFullCloneWithSpecificDisk(String cloneName, ManagedObjectRe
VirtualDisk[] vmDisks = getAllDiskDevice();
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
for (VirtualDisk disk : vmDisks) {
s_logger.info("HARI: original disks key: " + disk.getKey());
if (requiredDisk.getKey() != disk.getKey()) {
VirtualDeviceConfigSpec virtualDeviceConfigSpec = new VirtualDeviceConfigSpec();
virtualDeviceConfigSpec.setDevice(disk);
Expand All @@ -784,12 +785,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 cloneName, 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(cloneName);
VirtualDisk[] vmDisks = clonedVm.getAllDiskDevice();
s_logger.debug(String.format("Checking if Cloned VM %s is created only with required Disk, if not detach the remaining disks", cloneName));
if (vmDisks.length != 1) {
VirtualDisk requiredCloneDisk = null;
for (VirtualDisk clonedDisk: vmDisks) {
if (clonedDisk.getKey() == requiredDisk.getKey()) {
requiredCloneDisk = clonedDisk;
break;
}
}
if (requiredCloneDisk != null) {
String baseName = VmwareHelper.getDiskDeviceFileName(requiredCloneDisk);
s_logger.debug(String.format("Detaching all disks for the cloned VM: %s except disk with base name: %s, key=%d", cloneName, baseName, requiredCloneDisk.getKey()));
clonedVm.detachAllDisksExcept(baseName, null);
} else {
s_logger.error(String.format("Failed to identify required disk in cloned VM %s after clone VM operation", cloneName));
}
} else {
s_logger.debug(String.format("Cloned VM %s is created only with required Disk", cloneName));
}
}

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

0 comments on commit ea5999b

Please sign in to comment.