Search in sources :

Example 31 with VirtualDisk

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

the class VirtualMachineMO method detachAllDisks.

public void detachAllDisks() throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachAllDisk(). target MOR: " + _mor.get_value());
    VirtualDisk[] disks = getAllDiskDevice();
    if (disks.length > 0) {
        VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[disks.length];
        for (int i = 0; i < disks.length; i++) {
            deviceConfigSpecArray[i] = new VirtualDeviceConfigSpec();
            deviceConfigSpecArray[i].setDevice(disks[i]);
            deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.remove);
        }
        reConfigSpec.setDeviceChange(deviceConfigSpecArray);
        ManagedObjectReference morTask = _context.getService().reconfigVM_Task(_mor, reConfigSpec);
        String result = _context.getServiceUtil().waitForTask(morTask);
        if (!result.equals("sucess")) {
            if (s_logger.isTraceEnabled())
                s_logger.trace("vCenter API trace - detachAllDisk() done(failed)");
            throw new Exception("Failed to detach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
        _context.waitForTaskProgressDone(morTask);
    }
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachAllDisk() done(successfully)");
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDisk(com.vmware.vim25.VirtualDisk) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 32 with VirtualDisk

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

the class VirtualMachineMO method detachDisk.

// vmdkDatastorePath: [datastore name] vmdkFilePath
public List<Pair<String, ManagedObjectReference>> detachDisk(String vmdkDatastorePath, boolean deleteBackingFile) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachDisk(). target MOR: " + _mor.get_value() + ", vmdkDatastorePath: " + vmdkDatastorePath + ", deleteBacking: " + deleteBackingFile);
    // Note: if VM has been taken snapshot, original backing file will be renamed, therefore, when we try to find the matching
    // VirtualDisk, we only perform prefix matching
    Pair<VirtualDisk, String> deviceInfo = getDiskDevice(vmdkDatastorePath, false);
    if (deviceInfo == null) {
        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - detachDisk() done (failed)");
        throw new Exception("No such disk device: " + vmdkDatastorePath);
    }
    List<Pair<String, ManagedObjectReference>> chain = getDiskDatastorePathChain(deviceInfo.first(), true);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(deviceInfo.first());
    if (deleteBackingFile) {
        deviceConfigSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.destroy);
    }
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.remove);
    deviceConfigSpecArray[0] = deviceConfigSpec;
    reConfigSpec.setDeviceChange(deviceConfigSpecArray);
    ManagedObjectReference morTask = _context.getService().reconfigVM_Task(_mor, reConfigSpec);
    String result = _context.getServiceUtil().waitForTask(morTask);
    if (!result.equals("sucess")) {
        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - detachDisk() done (failed)");
        throw new Exception("Failed to detach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    _context.waitForTaskProgressDone(morTask);
    // VMware does not update snapshot references to the detached disk, we have to work around it
    SnapshotDescriptor snapshotDescriptor = null;
    try {
        snapshotDescriptor = getSnapshotDescriptor();
    } catch (Exception e) {
        s_logger.info("Unable to retrieve snapshot descriptor, will skip updating snapshot reference");
    }
    if (snapshotDescriptor != null) {
        for (Pair<String, ManagedObjectReference> pair : chain) {
            DatastoreFile dsFile = new DatastoreFile(pair.first());
            snapshotDescriptor.removeDiskReferenceFromSnapshot(dsFile.getFileName());
        }
        Pair<DatacenterMO, String> dcPair = getOwnerDatacenter();
        String dsPath = getSnapshotDescriptorDatastorePath();
        assert (dsPath != null);
        String url = getContext().composeDatastoreBrowseUrl(dcPair.second(), dsPath);
        getContext().uploadResourceContent(url, snapshotDescriptor.getVmsdContent());
    }
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachDisk() done (successfully)");
    return chain;
}
Also used : VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDisk(com.vmware.vim25.VirtualDisk) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) Pair(com.cloud.utils.Pair) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 33 with VirtualDisk

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

the class VirtualMachineMO method getDiskDevice.

// return pair of VirtualDisk and disk device bus name(ide0:0, etc)
public Pair<VirtualDisk, String> getDiskDevice(String vmdkDatastorePath, boolean matchExactly) throws Exception {
    VirtualDevice[] devices = (VirtualDevice[]) _context.getServiceUtil().getDynamicProperty(_mor, "config.hardware.device");
    s_logger.info("Look for disk device info from volume : " + vmdkDatastorePath);
    DatastoreFile dsSrcFile = new DatastoreFile(vmdkDatastorePath);
    String srcBaseName = dsSrcFile.getFileBaseName();
    if (devices != null && devices.length > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                s_logger.info("Test against disk device, controller key: " + device.getControllerKey() + ", unit number: " + device.getUnitNumber());
                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk) device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
                    do {
                        s_logger.info("Test against disk backing : " + diskBackingInfo.getFileName());
                        DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName());
                        String backingBaseName = dsBackingFile.getFileBaseName();
                        if (matchExactly) {
                            if (backingBaseName.equalsIgnoreCase(srcBaseName)) {
                                String deviceNumbering = getDeviceBusName(devices, device);
                                s_logger.info("Disk backing : " + diskBackingInfo.getFileName() + " matches ==> " + deviceNumbering);
                                return new Pair<VirtualDisk, String>((VirtualDisk) device, deviceNumbering);
                            }
                        } else {
                            if (backingBaseName.contains(srcBaseName)) {
                                String deviceNumbering = getDeviceBusName(devices, device);
                                s_logger.info("Disk backing : " + diskBackingInfo.getFileName() + " matches ==> " + deviceNumbering);
                                return new Pair<VirtualDisk, String>((VirtualDisk) device, deviceNumbering);
                            }
                        }
                        diskBackingInfo = diskBackingInfo.getParent();
                    } while (diskBackingInfo != null);
                }
            }
        }
    }
    return null;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk) Pair(com.cloud.utils.Pair)

Example 34 with VirtualDisk

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

the class VirtualMachineMO method getDiskInfoBuilder.

public VirtualMachineDiskInfoBuilder getDiskInfoBuilder() throws Exception {
    VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder();
    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;
                    while (diskBackingInfo != null) {
                        String deviceBusName = getDeviceBusName(devices, device);
                        builder.addDisk(deviceBusName, diskBackingInfo.getFileName());
                        diskBackingInfo = diskBackingInfo.getParent();
                    }
                }
            }
        }
    }
    return builder;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 35 with VirtualDisk

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

the class VirtualMachineMO method getDiskDevice.

// return pair of VirtualDisk and disk device bus name(ide0:0, etc)
public Pair<VirtualDisk, String> getDiskDevice(String vmdkDatastorePath) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    ArrayList<Pair<VirtualDisk, String>> partialMatchingDiskDevices = new ArrayList<Pair<VirtualDisk, String>>();
    DatastoreFile dsSrcFile = new DatastoreFile(vmdkDatastorePath);
    String srcBaseName = dsSrcFile.getFileBaseName();
    String trimmedSrcBaseName = VmwareHelper.trimSnapshotDeltaPostfix(srcBaseName);
    s_logger.info("Look for disk device info for volume : " + vmdkDatastorePath + " with base name: " + srcBaseName);
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                s_logger.info("Test against disk device, controller key: " + device.getControllerKey() + ", unit number: " + device.getUnitNumber());
                VirtualDeviceBackingInfo backingInfo = ((VirtualDisk) device).getBacking();
                if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) {
                    VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo) backingInfo;
                    do {
                        s_logger.info("Test against disk backing : " + diskBackingInfo.getFileName());
                        DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName());
                        String backingBaseName = dsBackingFile.getFileBaseName();
                        if (backingBaseName.equalsIgnoreCase(srcBaseName)) {
                            String deviceNumbering = getDeviceBusName(devices, device);
                            s_logger.info("Disk backing : " + diskBackingInfo.getFileName() + " matches ==> " + deviceNumbering);
                            return new Pair<VirtualDisk, String>((VirtualDisk) device, deviceNumbering);
                        }
                        if (backingBaseName.contains(trimmedSrcBaseName)) {
                            String deviceNumbering = getDeviceBusName(devices, device);
                            partialMatchingDiskDevices.add(new Pair<VirtualDisk, String>((VirtualDisk) device, deviceNumbering));
                        }
                        diskBackingInfo = diskBackingInfo.getParent();
                    } while (diskBackingInfo != null);
                }
            }
        }
    }
    // No disk device was found with an exact match for the volume path, hence look for disk device that matches the trimmed name.
    s_logger.info("No disk device with an exact match found for volume : " + vmdkDatastorePath + ". Look for disk device info against trimmed base name: " + srcBaseName);
    if (partialMatchingDiskDevices != null) {
        if (partialMatchingDiskDevices.size() == 1) {
            VirtualDiskFlatVer2BackingInfo matchingDiskBackingInfo = (VirtualDiskFlatVer2BackingInfo) partialMatchingDiskDevices.get(0).first().getBacking();
            s_logger.info("Disk backing : " + matchingDiskBackingInfo.getFileName() + " matches ==> " + partialMatchingDiskDevices.get(0).second());
            return partialMatchingDiskDevices.get(0);
        } else if (partialMatchingDiskDevices.size() > 1) {
            s_logger.warn("Disk device info lookup for volume: " + vmdkDatastorePath + " failed as multiple disk devices were found to match" + " volume's trimmed base name: " + trimmedSrcBaseName);
            return null;
        }
    }
    s_logger.warn("Disk device info lookup for volume: " + vmdkDatastorePath + " failed as no matching disk device found");
    return null;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDevice(com.vmware.vim25.VirtualDevice) ArrayList(java.util.ArrayList) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) 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