Search in sources :

Example 21 with VirtualDevice

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

the class VirtualMachineMO method getAllDiskDevice.

public VirtualDisk[] getAllDiskDevice() throws Exception {
    List<VirtualDisk> deviceList = new ArrayList<VirtualDisk>();
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk) {
                deviceList.add((VirtualDisk) device);
            }
        }
    }
    return deviceList.toArray(new VirtualDisk[0]);
}
Also used : ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 22 with VirtualDevice

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

the class VirtualMachineMO method getFreeUnitNumberOnIDEController.

public int getFreeUnitNumberOnIDEController(int controllerKey) throws Exception {
    int freeUnitNumber = 0;
    List<VirtualDevice> devices = (List<VirtualDevice>) _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    int deviceCount = 0;
    int ideDeviceUnitNumber = -1;
    if (devices != null && devices.size() > 0) {
        for (VirtualDevice device : devices) {
            if (device instanceof VirtualDisk && (controllerKey == device.getControllerKey())) {
                deviceCount++;
                ideDeviceUnitNumber = device.getUnitNumber();
            }
        }
    }
    if (deviceCount == 1) {
        if (ideDeviceUnitNumber == 0) {
            freeUnitNumber = 1;
        }
    // else freeUnitNumber is already initialized to 0
    } else if (deviceCount == 2) {
        throw new Exception("IDE controller with key [" + controllerKey + "] already has 2 device attached. Cannot attach more than the limit of 2.");
    }
    return freeUnitNumber;
}
Also used : VirtualDevice(com.vmware.vim25.VirtualDevice) List(java.util.List) ArrayList(java.util.ArrayList) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 23 with VirtualDevice

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

the class VirtualMachineMO method attachDisk.

public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference morDs, String diskController) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain) + ", datastore: " + morDs.getValue());
    int controllerKey = 0;
    int unitNumber = 0;
    if (DiskControllerType.getType(diskController) == DiskControllerType.ide) {
        // IDE virtual disk cannot be added if VM is running
        if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
            throw new Exception("Adding a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. Please re-try when VM is not running.");
        }
        // Get next available unit number and controller key
        int ideDeviceCount = getNumberOfIDEDevices();
        if (ideDeviceCount >= VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER) {
            throw new Exception("Maximum limit of  devices supported on IDE controllers [" + VmwareHelper.MAX_IDE_CONTROLLER_COUNT * VmwareHelper.MAX_ALLOWED_DEVICES_IDE_CONTROLLER + "] is reached.");
        }
        controllerKey = getIDEControllerKey(ideDeviceCount);
        unitNumber = getFreeUnitNumberOnIDEController(controllerKey);
    } else {
        controllerKey = getScsiDiskControllerKey(diskController);
        unitNumber = -1;
    }
    synchronized (_mor.getValue().intern()) {
        VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, controllerKey, vmdkDatastorePathChain, morDs, unitNumber, 1);
        controllerKey = newDisk.getControllerKey();
        unitNumber = newDisk.getUnitNumber();
        VirtualDiskFlatVer2BackingInfo backingInfo = (VirtualDiskFlatVer2BackingInfo) newDisk.getBacking();
        String vmdkFileName = backingInfo.getFileName();
        DiskControllerType diskControllerType = DiskControllerType.getType(diskController);
        VmdkAdapterType vmdkAdapterType = VmdkAdapterType.getAdapterType(diskControllerType);
        if (vmdkAdapterType == VmdkAdapterType.none) {
            String message = "Failed to attach disk due to invalid vmdk adapter type for vmdk file [" + vmdkFileName + "] with controller : " + diskControllerType;
            s_logger.debug(message);
            throw new Exception(message);
        }
        updateVmdkAdapter(vmdkFileName, vmdkAdapterType.toString());
        VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(newDisk);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        reConfigSpec.getDeviceChange().add(deviceConfigSpec);
        ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (!result) {
            if (s_logger.isTraceEnabled())
                s_logger.trace("vCenter API trace - attachDisk() done(failed)");
            throw new Exception("Failed to attach disk due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
        _context.waitForTaskProgressDone(morTask);
    }
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - attachDisk() done(successfully)");
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice) Gson(com.google.gson.Gson) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 24 with VirtualDevice

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

the class VirtualMachineMO method getDiskCurrentTopBackingFileInChain.

public String getDiskCurrentTopBackingFileInChain(String deviceBusName) throws Exception {
    List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
    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;
                    String deviceNumbering = getDeviceBusName(devices, device);
                    if (deviceNumbering.equals(deviceBusName))
                        return diskBackingInfo.getFileName();
                }
            }
        }
    }
    return null;
}
Also used : VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk)

Example 25 with VirtualDevice

use of com.vmware.vim25.VirtualDevice 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)

Aggregations

VirtualDevice (com.vmware.vim25.VirtualDevice)49 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)23 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)23 VirtualDisk (com.vmware.vim25.VirtualDisk)22 ArrayList (java.util.ArrayList)20 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)19 Pair (com.cloud.utils.Pair)15 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)15 VirtualDeviceConnectInfo (com.vmware.vim25.VirtualDeviceConnectInfo)15 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)15 VirtualEthernetCard (com.vmware.vim25.VirtualEthernetCard)11 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)8 IOException (java.io.IOException)8 CloudException (com.cloud.exception.CloudException)6 InternalErrorException (com.cloud.exception.InternalErrorException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 ConnectException (java.net.ConnectException)6 RemoteException (java.rmi.RemoteException)6 ConfigurationException (javax.naming.ConfigurationException)6