Search in sources :

Example 1 with VirtualDiskManagerMO

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;
}
Also used : VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDiskManagerMO(com.cloud.hypervisor.vmware.mo.VirtualDiskManagerMO) VirtualDisk(com.vmware.vim25.VirtualDisk) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Aggregations

InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)1 VirtualDiskManagerMO (com.cloud.hypervisor.vmware.mo.VirtualDiskManagerMO)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 VirtualDisk (com.vmware.vim25.VirtualDisk)1