Search in sources :

Example 16 with VirtualMachineConfigSpec

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

the class VirtualMachineMO method configureVm.

public boolean configureVm(Ternary<VirtualDevice, VirtualDeviceConfigSpecOperation, VirtualDeviceConfigSpecFileOperation>[] devices) throws Exception {
    assert (devices != null);
    VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[devices.length];
    int i = 0;
    for (Ternary<VirtualDevice, VirtualDeviceConfigSpecOperation, VirtualDeviceConfigSpecFileOperation> deviceTernary : devices) {
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(deviceTernary.first());
        deviceConfigSpec.setOperation(deviceTernary.second());
        deviceConfigSpec.setFileOperation(deviceTernary.third());
        deviceConfigSpecArray[i++] = deviceConfigSpec;
    }
    configSpec.getDeviceChange().addAll(Arrays.asList(deviceConfigSpecArray));
    ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, configSpec);
    boolean result = _context.getVimClient().waitForTask(morTask);
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
Also used : VirtualDeviceConfigSpecFileOperation(com.vmware.vim25.VirtualDeviceConfigSpecFileOperation) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDeviceConfigSpecOperation(com.vmware.vim25.VirtualDeviceConfigSpecOperation) VirtualDevice(com.vmware.vim25.VirtualDevice) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 17 with VirtualMachineConfigSpec

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

the class VirtualMachineMO method ensureBusLogicDeviceControllers.

public void ensureBusLogicDeviceControllers(int count, int availableBusNum) throws Exception {
    int scsiControllerKey = getBusLogicDeviceControllerKeyNoException();
    if (scsiControllerKey < 0) {
        VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
        int busNum = availableBusNum;
        while (busNum < count) {
            VirtualBusLogicController scsiController = new VirtualBusLogicController();
            scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
            scsiController.setBusNumber(busNum);
            scsiController.setKey(busNum - VmwareHelper.MAX_SCSI_CONTROLLER_COUNT);
            VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
            scsiControllerSpec.setDevice(scsiController);
            scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
            vmConfig.getDeviceChange().add(scsiControllerSpec);
            busNum++;
        }
        if (configureVm(vmConfig)) {
            throw new Exception("Unable to add Scsi BusLogic controllers to the VM " + getName());
        } else {
            s_logger.info("Successfully added " + count + " SCSI BusLogic controllers.");
        }
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualBusLogicController(com.vmware.vim25.VirtualBusLogicController) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec)

Example 18 with VirtualMachineConfigSpec

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

the class VirtualMachineMO method configureVm.

public boolean configureVm(VirtualMachineConfigSpec vmConfigSpec) throws Exception {
    ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, vmConfigSpec);
    boolean result = _context.getVimClient().waitForTask(morTask);
    if (result) {
        _context.waitForTaskProgressDone(morTask);
        return true;
    } else {
        s_logger.error("VMware reconfigVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    return false;
}
Also used : ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 19 with VirtualMachineConfigSpec

use of com.vmware.vim25.VirtualMachineConfigSpec 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 20 with VirtualMachineConfigSpec

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

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.getValue() + ", 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);
    if (deviceInfo == null) {
        s_logger.warn("vCenter API trace - detachDisk() done (failed)");
        throw new Exception("No such disk device: " + vmdkDatastorePath);
    }
    // IDE virtual disk cannot be detached if VM is running
    if (deviceInfo.second() != null && deviceInfo.second().contains("ide")) {
        if (getPowerState() == VirtualMachinePowerState.POWERED_ON) {
            throw new Exception("Removing a virtual disk over IDE controller is not supported while VM is running in VMware hypervisor. " + "Please re-try when VM is not running.");
        }
    }
    List<Pair<String, ManagedObjectReference>> chain = getDiskDatastorePathChain(deviceInfo.first(), true);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(deviceInfo.first());
    if (deleteBackingFile) {
        deviceConfigSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.DESTROY);
    }
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
    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 - 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)

Aggregations

VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)44 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)42 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)32 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)22 VirtualDevice (com.vmware.vim25.VirtualDevice)20 VirtualDisk (com.vmware.vim25.VirtualDisk)10 CloudException (com.cloud.exception.CloudException)7 InternalErrorException (com.cloud.exception.InternalErrorException)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 Gson (com.google.gson.Gson)7 IOException (java.io.IOException)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 ConnectException (java.net.ConnectException)7 RemoteException (java.rmi.RemoteException)7 ConfigurationException (javax.naming.ConfigurationException)7 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)5 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)5 VmwareContext (com.cloud.hypervisor.vmware.util.VmwareContext)5 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)5 Pair (com.cloud.utils.Pair)4