Search in sources :

Example 21 with VirtualDeviceConfigSpec

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

the class VirtualMachineMO method createDisk.

// vmdkDatastorePath: [datastore name] vmdkFilePath
public void createDisk(String vmdkDatastorePath, VirtualDiskType diskType, VirtualDiskMode diskMode, String rdmDeviceName, int sizeInMb, ManagedObjectReference morDs, int controllerKey) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - createDisk(). target MOR: " + _mor.get_value() + ", vmdkDatastorePath: " + vmdkDatastorePath + ", sizeInMb: " + sizeInMb + ", diskType: " + diskType + ", diskMode: " + diskMode + ", rdmDeviceName: " + rdmDeviceName + ", datastore: " + morDs.get_value() + ", controllerKey: " + controllerKey);
    assert (vmdkDatastorePath != null);
    assert (morDs != null);
    if (controllerKey < 0) {
        controllerKey = getIDEDeviceControllerKey();
    }
    VirtualDisk newDisk = new VirtualDisk();
    if (diskType == VirtualDiskType.thin || diskType == VirtualDiskType.preallocated || diskType == VirtualDiskType.eagerZeroedThick) {
        VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
        backingInfo.setDiskMode(diskMode.persistent.toString());
        if (diskType == VirtualDiskType.thin) {
            backingInfo.setThinProvisioned(true);
        } else {
            backingInfo.setThinProvisioned(false);
        }
        if (diskType == VirtualDiskType.eagerZeroedThick) {
            backingInfo.setEagerlyScrub(true);
        } else {
            backingInfo.setEagerlyScrub(false);
        }
        backingInfo.setDatastore(morDs);
        backingInfo.setFileName(vmdkDatastorePath);
        newDisk.setBacking(backingInfo);
    } else if (diskType == VirtualDiskType.rdm || diskType == VirtualDiskType.rdmp) {
        VirtualDiskRawDiskMappingVer1BackingInfo backingInfo = new VirtualDiskRawDiskMappingVer1BackingInfo();
        if (diskType == VirtualDiskType.rdm) {
            backingInfo.setCompatibilityMode("virtualMode");
        } else {
            backingInfo.setCompatibilityMode("physicalMode");
        }
        backingInfo.setDeviceName(rdmDeviceName);
        if (diskType == VirtualDiskType.rdm) {
            backingInfo.setDiskMode(diskMode.persistent.toString());
        }
        backingInfo.setDatastore(morDs);
        backingInfo.setFileName(vmdkDatastorePath);
        newDisk.setBacking(backingInfo);
    }
    int deviceNumber = getNextDeviceNumber(controllerKey);
    newDisk.setControllerKey(controllerKey);
    newDisk.setKey(-deviceNumber);
    newDisk.setUnitNumber(deviceNumber);
    newDisk.setCapacityInKB(sizeInMb * 1024);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(newDisk);
    deviceConfigSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.create);
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    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 - createDisk() done(failed)");
        throw new Exception("Unable to create disk " + vmdkDatastorePath + " due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    _context.waitForTaskProgressDone(morTask);
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - createDisk() done(successfully)");
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDiskRawDiskMappingVer1BackingInfo(com.vmware.vim25.VirtualDiskRawDiskMappingVer1BackingInfo) VirtualDisk(com.vmware.vim25.VirtualDisk) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 22 with VirtualDeviceConfigSpec

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

the class HypervisorHostHelper method createBlankVm.

public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB, int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent) throws Exception {
    if (s_logger.isInfoEnabled())
        s_logger.info("Create blank VM. cpuCount: " + cpuCount + ", cpuSpeed(MHz): " + cpuSpeedMHz + ", mem(Mb): " + memoryMB);
    // VM config basics
    VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
    vmConfig.setName(vmName);
    VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
    // Scsi controller
    VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
    scsiController.setSharedBus(VirtualSCSISharing.noSharing);
    scsiController.setBusNumber(0);
    scsiController.setKey(1);
    VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
    scsiControllerSpec.setDevice(scsiController);
    scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
    DatastoreMO dsMo = new DatastoreMO(host.getContext(), morDs);
    fileInfo.setVmPathName(String.format("[%s]", dsMo.getName()));
    vmConfig.setFiles(fileInfo);
    VirtualMachineVideoCard videoCard = new VirtualMachineVideoCard();
    videoCard.setControllerKey(100);
    videoCard.setUseAutoDetect(true);
    VirtualDeviceConfigSpec videoDeviceSpec = new VirtualDeviceConfigSpec();
    videoDeviceSpec.setDevice(videoCard);
    videoDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    vmConfig.setDeviceChange(new VirtualDeviceConfigSpec[] { scsiControllerSpec, videoDeviceSpec });
    if (host.createVm(vmConfig)) {
        VirtualMachineMO vmMo = host.findVmOnHyperHost(vmName);
        assert (vmMo != null);
        int ideControllerKey = -1;
        while (ideControllerKey < 0) {
            ideControllerKey = vmMo.tryGetIDEDeviceControllerKey();
            if (ideControllerKey >= 0)
                break;
            s_logger.info("Waiting for IDE controller be ready in VM: " + vmName);
            Thread.sleep(1000);
        }
        if (snapshotDirToParent) {
            String snapshotDir = String.format("/vmfs/volumes/%s/", dsMo.getName());
            s_logger.info("Switch snapshot working directory to " + snapshotDir + " for " + vmName);
            vmMo.setSnapshotDirectory(snapshotDir);
            // Don't have a good way to test if the VM is really ready for use through normal API after configuration file manipulation,
            // delay 3 seconds
            Thread.sleep(3000);
        }
        s_logger.info("Blank VM: " + vmName + " is ready for use");
        return true;
    }
    return false;
}
Also used : VirtualMachineVideoCard(com.vmware.vim25.VirtualMachineVideoCard) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) VirtualLsiLogicController(com.vmware.vim25.VirtualLsiLogicController)

Example 23 with VirtualDeviceConfigSpec

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

the class VirtualMachineMO method attachDisk.

public void attachDisk(Pair<String, ManagedObjectReference>[] vmdkDatastorePathChain, int controllerKey) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.get_value() + ", vmdkDatastorePath: " + new Gson().toJson(vmdkDatastorePathChain));
    VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, controllerKey, vmdkDatastorePathChain, -1, 1);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(newDisk);
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    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 - 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) 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 VirtualDeviceConfigSpec

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

the class VirtualMachineMO method cloneFromDiskChain.

public void cloneFromDiskChain(String clonedVmName, int cpuSpeedMHz, int memoryMb, String[] disks, ManagedObjectReference morDs) throws Exception {
    assert (disks != null);
    assert (disks.length >= 1);
    HostMO hostMo = getRunningHost();
    VirtualMachineConfigInfo vmConfigInfo = getConfigInfo();
    if (!hostMo.createBlankVm(clonedVmName, 1, cpuSpeedMHz, 0, false, memoryMb, 0, vmConfigInfo.getGuestId(), morDs, false))
        throw new Exception("Unable to create a blank VM");
    VirtualMachineMO clonedVmMo = hostMo.findVmOnHyperHost(clonedVmName);
    if (clonedVmMo == null)
        throw new Exception("Unable to find just-created blank VM");
    boolean bSuccess = false;
    try {
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
        deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec();
        VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, -1, disks, morDs, -1, 1);
        deviceConfigSpecArray[0].setDevice(device);
        deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.add);
        vmConfigSpec.setDeviceChange(deviceConfigSpecArray);
        clonedVmMo.configureVm(vmConfigSpec);
        bSuccess = true;
    } finally {
        if (!bSuccess) {
            clonedVmMo.detachAllDisks();
            clonedVmMo.destroy();
        }
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualMachineConfigInfo(com.vmware.vim25.VirtualMachineConfigInfo)

Example 25 with VirtualDeviceConfigSpec

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

the class VirtualMachineMO method plugDevice.

public void plugDevice(VirtualDevice device) throws Exception {
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
    deviceConfigSpecArray[0] = new VirtualDeviceConfigSpec();
    deviceConfigSpecArray[0].setDevice(device);
    deviceConfigSpecArray[0].setOperation(VirtualDeviceConfigSpecOperation.add);
    vmConfigSpec.setDeviceChange(deviceConfigSpecArray);
    if (!configureVm(vmConfigSpec)) {
        throw new Exception("Failed to add devices");
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec)

Aggregations

VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)44 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)41 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)23 VirtualDevice (com.vmware.vim25.VirtualDevice)19 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)18 VirtualDisk (com.vmware.vim25.VirtualDisk)10 CloudException (com.cloud.exception.CloudException)6 InternalErrorException (com.cloud.exception.InternalErrorException)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 VirtualLsiLogicController (com.vmware.vim25.VirtualLsiLogicController)6 IOException (java.io.IOException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 ConnectException (java.net.ConnectException)6 RemoteException (java.rmi.RemoteException)6 ConfigurationException (javax.naming.ConfigurationException)6 Gson (com.google.gson.Gson)5 VirtualDiskFlatVer2BackingInfo (com.vmware.vim25.VirtualDiskFlatVer2BackingInfo)5 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)4 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)4 Pair (com.cloud.utils.Pair)4