Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix export snapshot and template to secondary storage to export only required disk #5510

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1241,9 +1241,9 @@ private Ternary<String, Long, Long> createTemplateFromVolume(VmwareContext conte

DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
vmMo.createFullCloneWithSpecificDisk(templateUniqueName, dcMo.getVmFolder(), morPool, VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), volumeDeviceInfo);
VirtualDisk requiredDisk = volumeDeviceInfo.first();
vmMo.createFullCloneWithSpecificDisk(templateUniqueName, dcMo.getVmFolder(), morPool, requiredDisk);
clonedVm = dcMo.findVm(templateUniqueName);

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

Expand Down Expand Up @@ -1828,7 +1828,8 @@ private Pair<String, String[]> exportVolumeToSecondaryStorage(VmwareContext cont
// 4 MB is the minimum requirement for VM memory in VMware
DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
ManagedObjectReference morPool = hyperHost.getHyperHostOwnerResourcePool();
vmMo.createFullCloneWithSpecificDisk(exportName, dcMo.getVmFolder(), morPool, VmwareHelper.getDiskDeviceDatastore(volumeDeviceInfo.first()), volumeDeviceInfo);
VirtualDisk requiredDisk = volumeDeviceInfo.first();
vmMo.createFullCloneWithSpecificDisk(exportName, dcMo.getVmFolder(), morPool, requiredDisk);
clonedVm = dcMo.findVm(exportName);
if (clonedVm == null) {
String msg = "Failed to clone VM. volume path: " + volumePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,43 +751,74 @@ public boolean hasSnapshot() throws Exception {
return false;
}

public boolean createFullCloneWithSpecificDisk(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, ManagedObjectReference morDs, Pair<VirtualDisk, String> volumeDeviceInfo)
public boolean createFullCloneWithSpecificDisk(String cloneName, ManagedObjectReference morFolder, ManagedObjectReference morResourcePool, VirtualDisk requiredDisk)
throws Exception {

assert (morFolder != null);
assert (morResourcePool != null);
assert (morDs != null);
VirtualDisk requiredDisk = volumeDeviceInfo.first();

VirtualMachineRelocateSpec rSpec = new VirtualMachineRelocateSpec();
List<VirtualMachineRelocateSpecDiskLocator> diskLocator = new ArrayList<VirtualMachineRelocateSpecDiskLocator>(1);
VirtualMachineRelocateSpecDiskLocator loc = new VirtualMachineRelocateSpecDiskLocator();
loc.setDatastore(morDs);
loc.setDiskId(requiredDisk.getKey());
loc.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING.value());
diskLocator.add(loc);

rSpec.setDiskMoveType(VirtualMachineRelocateDiskMoveOptions.MOVE_ALL_DISK_BACKINGS_AND_DISALLOW_SHARING.value());
rSpec.getDisk().addAll(diskLocator);

VirtualDisk[] vmDisks = getAllDiskDevice();
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
for (VirtualDisk disk : vmDisks) {
if (requiredDisk.getKey() != disk.getKey()) {
VirtualDeviceConfigSpec virtualDeviceConfigSpec = new VirtualDeviceConfigSpec();
virtualDeviceConfigSpec.setDevice(disk);
virtualDeviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
vmConfigSpec.getDeviceChange().add(virtualDeviceConfigSpec);
}
}
rSpec.setPool(morResourcePool);

VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setPowerOn(false);
cloneSpec.setTemplate(false);
cloneSpec.setLocation(rSpec);
cloneSpec.setMemory(false);
cloneSpec.setConfig(vmConfigSpec);

ManagedObjectReference morTask = _context.getService().cloneVMTask(_mor, morFolder, cloneName, cloneSpec);

boolean result = _context.getVimClient().waitForTask(morTask);
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);
harikrishna-patnala marked this conversation as resolved.
Show resolved Hide resolved
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) {
DaanHoogland marked this conversation as resolved.
Show resolved Hide resolved
VirtualDisk requiredCloneDisk = null;
for (VirtualDisk vmDisk: vmDisks) {
if (vmDisk.getKey() == requiredDisk.getKey()) {
DaanHoogland marked this conversation as resolved.
Show resolved Hide resolved
requiredCloneDisk = vmDisk;
break;
}
}
if (requiredCloneDisk != null) {
DaanHoogland marked this conversation as resolved.
Show resolved Hide resolved
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