Search in sources :

Example 21 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.

the class VirtualMachineMO method getVmdkFileBaseNames.

public List<String> getVmdkFileBaseNames() throws Exception {
    List<String> vmdkFileBaseNames = new ArrayList<String>();
    VirtualDevice[] devices = getAllDiskDevice();
    for (VirtualDevice device : devices) {
        if (device instanceof VirtualDisk) {
            vmdkFileBaseNames.add(getVmdkFileBaseName((VirtualDisk) device));
        }
    }
    return vmdkFileBaseNames;
}
Also used : ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 22 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.

the class VirtualMachineMO method getAllDiskDatastores.

public List<Pair<Integer, ManagedObjectReference>> getAllDiskDatastores() throws Exception {
    List<Pair<Integer, ManagedObjectReference>> disks = new ArrayList<Pair<Integer, ManagedObjectReference>>();
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk) device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
                    disks.add(new Pair<Integer, ManagedObjectReference>(new Integer(device.getKey()), diskBackingInfo.getDatastore()));
                }
            }
        }
    }
    return disks;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 23 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.

the class VirtualMachineMO method getAllIndependentDiskDevice.

public VirtualDisk[] getAllIndependentDiskDevice() throws Exception {
    List<VirtualDisk> independentDisks = new ArrayList<VirtualDisk>();
    VirtualDisk[] allDisks = getAllDiskDevice();
    if (allDisks.length > 0) {
        for (VirtualDisk disk : allDisks) {
            String diskMode = "";
            if (disk.getBacking() instanceof VirtualDiskFlatVer1BackingInfo) {
                diskMode = ((VirtualDiskFlatVer1BackingInfo) disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskFlatVer2BackingInfo) {
                diskMode = ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskRawDiskMappingVer1BackingInfo) {
                diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo) disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskSparseVer1BackingInfo) {
                diskMode = ((VirtualDiskSparseVer1BackingInfo) disk.getBacking()).getDiskMode();
            } else if (disk.getBacking() instanceof VirtualDiskSparseVer2BackingInfo) {
                diskMode = ((VirtualDiskSparseVer2BackingInfo) disk.getBacking()).getDiskMode();
            }
            if (diskMode.indexOf("independent") != -1) {
                independentDisks.add(disk);
            }
        }
    }
    return independentDisks.toArray(new VirtualDisk[0]);
}
Also used : VirtualDiskSparseVer2BackingInfo(com.vmware.vim25.VirtualDiskSparseVer2BackingInfo) VirtualDiskFlatVer1BackingInfo(com.vmware.vim25.VirtualDiskFlatVer1BackingInfo) VirtualDiskSparseVer1BackingInfo(com.vmware.vim25.VirtualDiskSparseVer1BackingInfo) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ArrayList(java.util.ArrayList) VirtualDiskRawDiskMappingVer1BackingInfo(com.vmware.vim25.VirtualDiskRawDiskMappingVer1BackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 24 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.

the class VirtualMachineMO method detachAllDisksExcept.

public List<String> detachAllDisksExcept(String vmdkBaseName, String deviceBusName) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    List<String> detachedDiskFiles = new ArrayList<String>();
    for (VirtualDevice device : devices) {
        if (device instanceof VirtualDisk) {
            VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
            VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) device.getBacking();
            DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName());
            String backingBaseName = dsBackingFile.getFileBaseName();
            String deviceNumbering = getDeviceBusName(devices, device);
            if (backingBaseName.equalsIgnoreCase(vmdkBaseName) || (deviceBusName != null && deviceBusName.equals(deviceNumbering))) {
                continue;
            } else {
                s_logger.info("Detach " + diskBackingInfo.getFileName() + " from " + getName());
                detachedDiskFiles.add(diskBackingInfo.getFileName());
                deviceConfigSpec.setDevice(device);
                deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
                reConfigSpec.getDeviceChange().add(deviceConfigSpec);
            }
        }
    }
    if (detachedDiskFiles.size() > 0) {
        ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (result) {
            _context.waitForTaskProgressDone(morTask);
        } else {
            s_logger.warn("Unable to reconfigure the VM to detach disks");
            throw new Exception("Unable to reconfigure the VM to detach disks");
        }
    }
    return detachedDiskFiles;
}
Also used : VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice) ArrayList(java.util.ArrayList) VirtualDisk(com.vmware.vim25.VirtualDisk) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 25 with VirtualDisk

use of com.vmware.vim25.VirtualDisk in project cloudstack by apache.

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 = 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)

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