Search in sources :

Example 1 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project CloudStack-archive by CloudStack-extras.

the class VirtualMachineMO method copyAllVmDiskFiles.

public void copyAllVmDiskFiles(DatastoreMO destDsMo, String destDsDir, boolean followDiskChain) throws Exception {
    VirtualDevice[] disks = getAllDiskDevice();
    DatacenterMO dcMo = getOwnerDatacenter().first();
    if (disks != null) {
        for (VirtualDevice disk : disks) {
            List<Pair<String, ManagedObjectReference>> vmdkFiles = this.getDiskDatastorePathChain((VirtualDisk) disk, followDiskChain);
            for (Pair<String, ManagedObjectReference> fileItem : vmdkFiles) {
                DatastoreMO srcDsMo = new DatastoreMO(_context, fileItem.second());
                DatastoreFile srcFile = new DatastoreFile(fileItem.first());
                DatastoreFile destFile = new DatastoreFile(destDsMo.getName(), destDsDir, srcFile.getFileName());
                Pair<VmdkFileDescriptor, byte[]> vmdkDescriptor = null;
                vmdkDescriptor = getVmdkFileInfo(fileItem.first());
                s_logger.info("Copy VM disk file " + srcFile.getPath() + " to " + destFile.getPath());
                srcDsMo.copyDatastoreFile(fileItem.first(), dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
                if (vmdkDescriptor != null) {
                    String vmdkBaseFileName = vmdkDescriptor.first().getBaseFileName();
                    String baseFilePath = srcFile.getCompanionPath(vmdkBaseFileName);
                    destFile = new DatastoreFile(destDsMo.getName(), destDsDir, vmdkBaseFileName);
                    s_logger.info("Copy VM disk file " + baseFilePath + " to " + destFile.getPath());
                    srcDsMo.copyDatastoreFile(baseFilePath, dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
                }
            }
        }
    }
}
Also used : VirtualDevice(com.vmware.vim25.VirtualDevice) Pair(com.cloud.utils.Pair) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 2 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project CloudStack-archive by CloudStack-extras.

the class VirtualMachineMO method getDiskDatastorePathChain.

@Deprecated
public List<Pair<String, ManagedObjectReference>> getDiskDatastorePathChain(VirtualDisk disk, boolean followChain) throws Exception {
    VirtualDeviceBackingInfo backingInfo = disk.getBacking();
    if (!(backingInfo instanceof VirtualDiskFlatVer2BackingInfo)) {
        throw new Exception("Unsupported VirtualDeviceBackingInfo");
    }
    List<Pair<String, ManagedObjectReference>> pathList = new ArrayList<Pair<String, ManagedObjectReference>>();
    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
    if (!followChain) {
        pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
        return pathList;
    }
    Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
    VirtualMachineFileInfo vmFilesInfo = getFileInfo();
    DatastoreFile snapshotDirFile = new DatastoreFile(vmFilesInfo.getSnapshotDirectory());
    DatastoreFile vmxDirFile = new DatastoreFile(vmFilesInfo.getVmPathName());
    do {
        if (diskBackingInfo.getParent() != null) {
            pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
            diskBackingInfo = diskBackingInfo.getParent();
        } else {
            // try getting parent info from VMDK file itself 
            byte[] content = null;
            try {
                String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), diskBackingInfo.getFileName());
                content = getContext().getResourceContent(url);
                if (content == null || content.length == 0) {
                    break;
                }
                pathList.add(new Pair<String, ManagedObjectReference>(diskBackingInfo.getFileName(), diskBackingInfo.getDatastore()));
            } catch (Exception e) {
                // if snapshot directory has been changed to place other than default. VMware has a bug
                // that its corresponding disk backing info is not updated correctly. therefore, we will try search
                // in snapshot directory one more time
                DatastoreFile currentFile = new DatastoreFile(diskBackingInfo.getFileName());
                String vmdkFullDsPath = snapshotDirFile.getCompanionPath(currentFile.getFileName());
                String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), vmdkFullDsPath);
                content = getContext().getResourceContent(url);
                if (content == null || content.length == 0) {
                    break;
                }
                pathList.add(new Pair<String, ManagedObjectReference>(vmdkFullDsPath, diskBackingInfo.getDatastore()));
            }
            VmdkFileDescriptor descriptor = new VmdkFileDescriptor();
            descriptor.parse(content);
            if (descriptor.getParentFileName() != null && !descriptor.getParentFileName().isEmpty()) {
                // create a fake one
                VirtualDiskFlatVer2BackingInfo parentDiskBackingInfo = new VirtualDiskFlatVer2BackingInfo();
                parentDiskBackingInfo.setDatastore(diskBackingInfo.getDatastore());
                String parentFileName = descriptor.getParentFileName();
                if (parentFileName.startsWith("/")) {
                    int fileNameStartPos = parentFileName.lastIndexOf("/");
                    parentFileName = parentFileName.substring(fileNameStartPos + 1);
                    parentDiskBackingInfo.setFileName(vmxDirFile.getCompanionPath(parentFileName));
                } else {
                    parentDiskBackingInfo.setFileName(snapshotDirFile.getCompanionPath(parentFileName));
                }
                diskBackingInfo = parentDiskBackingInfo;
            } else {
                break;
            }
        }
    } while (diskBackingInfo != null);
    return pathList;
}
Also used : ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 3 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project CloudStack-archive by CloudStack-extras.

the class VirtualMachineMO method moveAllVmDiskFiles.

// this method relies on un-offical VMware API
@Deprecated
public void moveAllVmDiskFiles(DatastoreMO destDsMo, String destDsDir, boolean followDiskChain) throws Exception {
    VirtualDevice[] disks = getAllDiskDevice();
    DatacenterMO dcMo = getOwnerDatacenter().first();
    if (disks != null) {
        for (VirtualDevice disk : disks) {
            List<Pair<String, ManagedObjectReference>> vmdkFiles = this.getDiskDatastorePathChain((VirtualDisk) disk, followDiskChain);
            for (Pair<String, ManagedObjectReference> fileItem : vmdkFiles) {
                DatastoreMO srcDsMo = new DatastoreMO(_context, fileItem.second());
                DatastoreFile srcFile = new DatastoreFile(fileItem.first());
                DatastoreFile destFile = new DatastoreFile(destDsMo.getName(), destDsDir, srcFile.getFileName());
                Pair<VmdkFileDescriptor, byte[]> vmdkDescriptor = null;
                vmdkDescriptor = getVmdkFileInfo(fileItem.first());
                s_logger.info("Move VM disk file " + srcFile.getPath() + " to " + destFile.getPath());
                srcDsMo.moveDatastoreFile(fileItem.first(), dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
                if (vmdkDescriptor != null) {
                    String vmdkBaseFileName = vmdkDescriptor.first().getBaseFileName();
                    String baseFilePath = srcFile.getCompanionPath(vmdkBaseFileName);
                    destFile = new DatastoreFile(destDsMo.getName(), destDsDir, vmdkBaseFileName);
                    s_logger.info("Move VM disk file " + baseFilePath + " to " + destFile.getPath());
                    srcDsMo.moveDatastoreFile(baseFilePath, dcMo.getMor(), destDsMo.getMor(), destFile.getPath(), dcMo.getMor(), true);
                }
            }
        }
    }
}
Also used : VirtualDevice(com.vmware.vim25.VirtualDevice) Pair(com.cloud.utils.Pair) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 4 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project CloudStack-archive by CloudStack-extras.

the class VmwareHelper method prepareDiskDevice.

// vmdkDatastorePath: [datastore name] vmdkFilePath
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String vmdkDatastorePath, int sizeInMb, ManagedObjectReference morDs, int deviceNumber, int contextNumber) throws Exception {
    VirtualDisk disk = new VirtualDisk();
    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setDiskMode(VirtualDiskMode.persistent.toString());
    backingInfo.setThinProvisioned(true);
    backingInfo.setEagerlyScrub(false);
    backingInfo.setDatastore(morDs);
    backingInfo.setFileName(vmdkDatastorePath);
    disk.setBacking(backingInfo);
    if (controllerKey < 0)
        controllerKey = vmMo.getIDEDeviceControllerKey();
    if (deviceNumber < 0)
        deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
    disk.setControllerKey(controllerKey);
    disk.setKey(-contextNumber);
    disk.setUnitNumber(deviceNumber);
    disk.setCapacityInKB(sizeInMb * 1024);
    VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    connectInfo.setConnected(true);
    connectInfo.setStartConnected(true);
    disk.setConnectable(connectInfo);
    return disk;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 5 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project CloudStack-archive by CloudStack-extras.

the class VmwareHelper method prepareDiskDevice.

public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, Pair<String, ManagedObjectReference>[] vmdkDatastorePathChain, int deviceNumber, int contextNumber) throws Exception {
    assert (vmdkDatastorePathChain != null);
    assert (vmdkDatastorePathChain.length >= 1);
    VirtualDisk disk = new VirtualDisk();
    VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
    backingInfo.setDatastore(vmdkDatastorePathChain[0].second());
    backingInfo.setFileName(vmdkDatastorePathChain[0].first());
    backingInfo.setDiskMode(VirtualDiskMode.persistent.toString());
    if (vmdkDatastorePathChain.length > 1) {
        Pair<String, ManagedObjectReference>[] parentDisks = new Pair[vmdkDatastorePathChain.length - 1];
        for (int i = 0; i < vmdkDatastorePathChain.length - 1; i++) parentDisks[i] = vmdkDatastorePathChain[i + 1];
        setParentBackingInfo(backingInfo, parentDisks);
    }
    disk.setBacking(backingInfo);
    if (controllerKey < 0)
        controllerKey = vmMo.getIDEDeviceControllerKey();
    if (deviceNumber < 0)
        deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
    disk.setControllerKey(controllerKey);
    disk.setKey(-contextNumber);
    disk.setUnitNumber(deviceNumber);
    VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
    connectInfo.setConnected(true);
    connectInfo.setStartConnected(true);
    disk.setConnectable(connectInfo);
    return disk;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConnectInfo(com.vmware.vim25.VirtualDeviceConnectInfo) VirtualDisk(com.vmware.vim25.VirtualDisk) Pair(com.cloud.utils.Pair)

Aggregations

VirtualDisk (com.vmware.vim25.VirtualDisk)41 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)24 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)20 VirtualDevice (com.vmware.vim25.VirtualDevice)17 ArrayList (java.util.ArrayList)17 Pair (com.cloud.utils.Pair)16 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)16 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)9 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)9 RemoteException (java.rmi.RemoteException)9 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)8 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 VirtualDiskRawDiskMappingVer1BackingInfo (com.vmware.vim25.VirtualDiskRawDiskMappingVer1BackingInfo)6 IOException (java.io.IOException)6 CloudException (com.cloud.exception.CloudException)4 InternalErrorException (com.cloud.exception.InternalErrorException)4 DatastoreMO (com.cloud.hypervisor.vmware.mo.DatastoreMO)4