Search in sources :

Example 31 with VirtualMachineConfigSpec

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

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

the class ClusterMO method createVm.

@Override
public boolean createVm(VirtualMachineConfigSpec vmSpec) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - createVM_Task(). target MOR: " + _mor.get_value() + ", VirtualMachineConfigSpec: " + new Gson().toJson(vmSpec));
    assert (vmSpec != null);
    DatacenterMO dcMo = new DatacenterMO(_context, getHyperHostDatacenter());
    ManagedObjectReference morPool = getHyperHostOwnerResourcePool();
    ManagedObjectReference morTask = _context.getService().createVM_Task(dcMo.getVmFolder(), vmSpec, morPool, null);
    String result = _context.getServiceUtil().waitForTask(morTask);
    if (result.equals("sucess")) {
        _context.waitForTaskProgressDone(morTask);
        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - createVM_Task() done(successfully)");
        return true;
    } else {
        s_logger.error("VMware createVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
    }
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - createVM_Task() done(failed)");
    return false;
}
Also used : Gson(com.google.gson.Gson) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 33 with VirtualMachineConfigSpec

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

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

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

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