use of com.cloud.hypervisor.vmware.mo.VirtualDiskManagerMO in project cloudstack by apache.
the class VMwareGuru method attachRestoredVolumeToVirtualMachine.
@Override
public boolean attachRestoredVolumeToVirtualMachine(long zoneId, String location, Backup.VolumeInfo volumeInfo, VirtualMachine vm, long poolId, Backup backup) throws Exception {
DatacenterMO dcMo = getDatacenterMO(zoneId);
VirtualMachineMO vmRestored = findVM(dcMo, location);
VirtualMachineMO vmMo = findVM(dcMo, vm.getInstanceName());
VirtualDisk restoredDisk = findRestoredVolume(volumeInfo, vmRestored);
String diskPath = vmRestored.getVmdkFileBaseName(restoredDisk);
s_logger.debug("Restored disk size=" + toHumanReadableSize(restoredDisk.getCapacityInKB()) + " path=" + diskPath);
// Detach restored VM disks
vmRestored.detachAllDisks();
String srcPath = getVolumeFullPath(restoredDisk);
String destPath = getDestVolumeFullPath(restoredDisk, vmRestored, vmMo);
VirtualDiskManagerMO virtualDiskManagerMO = new VirtualDiskManagerMO(dcMo.getContext());
// Copy volume to the VM folder
virtualDiskManagerMO.moveVirtualDisk(srcPath, dcMo.getMor(), destPath, dcMo.getMor(), true);
try {
// Attach volume to VM
vmMo.attachDisk(new String[] { destPath }, getDestStoreMor(vmMo));
} catch (Exception e) {
s_logger.error("Failed to attach the restored volume: " + diskPath, e);
return false;
} finally {
// Destroy restored VM
vmRestored.destroy();
}
VirtualDisk attachedDisk = getAttachedDisk(vmMo, diskPath);
if (attachedDisk == null) {
s_logger.error("Failed to get the attached the (restored) volume " + diskPath);
return false;
}
createVolume(attachedDisk, vmMo, vm.getDomainId(), vm.getDataCenterId(), vm.getAccountId(), vm.getId(), poolId, vm.getTemplateId(), backup, false);
return true;
}
Aggregations