Search in sources :

Example 36 with VirtualMachineConfigSpec

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

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

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

the class VmwareHelper method setBasicVmConfig.

public static void setBasicVmConfig(VirtualMachineConfigSpec vmConfig, int cpuCount, int cpuSpeedMHz, int cpuReservedMhz, int memoryMB, int memoryReserveMB, String guestOsIdentifier, boolean limitCpuUse) {
    // VM config basics
    vmConfig.setMemoryMB((long) memoryMB);
    vmConfig.setNumCPUs(cpuCount);
    ResourceAllocationInfo cpuInfo = new ResourceAllocationInfo();
    if (limitCpuUse) {
        cpuInfo.setLimit((long) (cpuSpeedMHz * cpuCount));
    } else {
        cpuInfo.setLimit(-1L);
    }
    cpuInfo.setReservation((long) cpuReservedMhz);
    vmConfig.setCpuAllocation(cpuInfo);
    ResourceAllocationInfo memInfo = new ResourceAllocationInfo();
    memInfo.setLimit((long) memoryMB);
    memInfo.setReservation((long) memoryReserveMB);
    vmConfig.setMemoryAllocation(memInfo);
    vmConfig.setGuestId(guestOsIdentifier);
}
Also used : ResourceAllocationInfo(com.vmware.vim25.ResourceAllocationInfo)

Example 39 with VirtualMachineConfigSpec

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

the class HypervisorHostHelper method createBlankVm.

public static boolean createBlankVm(VmwareHypervisorHost host, String vmName, String vmInternalCSName, int cpuCount, int cpuSpeedMHz, int cpuReservedMHz, boolean limitCpuUse, int memoryMB, int memoryReserveMB, String guestOsIdentifier, ManagedObjectReference morDs, boolean snapshotDirToParent, Pair<String, String> controllerInfo, Boolean systemVm) throws Exception {
    if (s_logger.isInfoEnabled())
        s_logger.info("Create blank VM. cpuCount: " + cpuCount + ", cpuSpeed(MHz): " + cpuSpeedMHz + ", mem(Mb): " + memoryMB);
    VirtualDeviceConfigSpec controllerSpec = null;
    // VM config basics
    VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
    vmConfig.setName(vmName);
    if (vmInternalCSName == null)
        vmInternalCSName = vmName;
    VmwareHelper.setBasicVmConfig(vmConfig, cpuCount, cpuSpeedMHz, cpuReservedMHz, memoryMB, memoryReserveMB, guestOsIdentifier, limitCpuUse);
    String recommendedController = host.getRecommendedDiskController(guestOsIdentifier);
    String newRootDiskController = controllerInfo.first();
    String newDataDiskController = controllerInfo.second();
    if (DiskControllerType.getType(controllerInfo.first()) == DiskControllerType.osdefault) {
        newRootDiskController = recommendedController;
    }
    if (DiskControllerType.getType(controllerInfo.second()) == DiskControllerType.osdefault) {
        newDataDiskController = recommendedController;
    }
    Pair<String, String> updatedControllerInfo = new Pair<String, String>(newRootDiskController, newDataDiskController);
    String scsiDiskController = HypervisorHostHelper.getScsiController(updatedControllerInfo, recommendedController);
    // If there is requirement for a SCSI controller, ensure to create those.
    if (scsiDiskController != null) {
        int busNum = 0;
        int maxControllerCount = VmwareHelper.MAX_SCSI_CONTROLLER_COUNT;
        if (systemVm) {
            maxControllerCount = 1;
        }
        while (busNum < maxControllerCount) {
            VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
            scsiControllerSpec = getControllerSpec(DiskControllerType.getType(scsiDiskController).toString(), busNum);
            vmConfig.getDeviceChange().add(scsiControllerSpec);
            busNum++;
        }
    }
    if (guestOsIdentifier.startsWith("darwin")) {
        //Mac OS
        s_logger.debug("Add USB Controller device for blank Mac OS VM " + vmName);
        //For Mac OS X systems, the EHCI+UHCI controller is enabled by default and is required for USB mouse and keyboard access.
        VirtualDevice usbControllerDevice = VmwareHelper.prepareUSBControllerDevice();
        VirtualDeviceConfigSpec usbControllerSpec = new VirtualDeviceConfigSpec();
        usbControllerSpec.setDevice(usbControllerDevice);
        usbControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfig.getDeviceChange().add(usbControllerSpec);
    }
    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.getDeviceChange().add(videoDeviceSpec);
    if (host.createVm(vmConfig)) {
        // Here, when attempting to find the VM, we need to use the name
        // with which we created it. This is the only such place where
        // we need to do this. At all other places, we always use the
        // VM's internal cloudstack generated name. Here, we cannot use
        // the internal name because we can set the internal name into the
        // VM's custom field CLOUD_VM_INTERNAL_NAME only after we create
        // the VM.
        VirtualMachineMO vmMo = host.findVmOnHyperHost(vmName);
        assert (vmMo != null);
        vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME, vmInternalCSName);
        int ideControllerKey = -1;
        while (ideControllerKey < 0) {
            ideControllerKey = vmMo.tryGetIDEDeviceControllerKey();
            if (ideControllerKey >= 0)
                break;
            s_logger.info("Waiting for IDE controller be ready in VM: " + vmInternalCSName);
            Thread.sleep(1000);
        }
        return true;
    }
    return false;
}
Also used : VirtualMachineVideoCard(com.vmware.vim25.VirtualMachineVideoCard) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) Pair(com.cloud.utils.Pair)

Example 40 with VirtualMachineConfigSpec

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

the class HypervisorHostHelper method createWorkerVM.

public static VirtualMachineMO createWorkerVM(VmwareHypervisorHost hyperHost, DatastoreMO dsMo, String vmName) throws Exception {
    // Allow worker VM to float within cluster so that we will have better chance to
    // create it successfully
    ManagedObjectReference morCluster = hyperHost.getHyperHostCluster();
    if (morCluster != null)
        hyperHost = new ClusterMO(hyperHost.getContext(), morCluster);
    VirtualMachineMO workingVM = null;
    VirtualMachineConfigSpec vmConfig = new VirtualMachineConfigSpec();
    vmConfig.setName(vmName);
    vmConfig.setMemoryMB((long) 4);
    vmConfig.setNumCPUs(1);
    vmConfig.setGuestId(VirtualMachineGuestOsIdentifier.OTHER_GUEST.value());
    VirtualMachineFileInfo fileInfo = new VirtualMachineFileInfo();
    fileInfo.setVmPathName(dsMo.getDatastoreRootPath());
    vmConfig.setFiles(fileInfo);
    VirtualLsiLogicController scsiController = new VirtualLsiLogicController();
    scsiController.setSharedBus(VirtualSCSISharing.NO_SHARING);
    scsiController.setBusNumber(0);
    scsiController.setKey(1);
    VirtualDeviceConfigSpec scsiControllerSpec = new VirtualDeviceConfigSpec();
    scsiControllerSpec.setDevice(scsiController);
    scsiControllerSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    vmConfig.getDeviceChange().add(scsiControllerSpec);
    if (hyperHost.createVm(vmConfig)) {
        // Ugly work-around, it takes time for newly created VM to appear
        for (int i = 0; i < 10 && workingVM == null; i++) {
            workingVM = hyperHost.findVmOnHyperHost(vmName);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                s_logger.debug("[ignored] interupted while waiting to config vm.");
            }
        }
    }
    if (workingVM != null) {
        workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER, "true");
        String workerTag = String.format("%d-%s", System.currentTimeMillis(), hyperHost.getContext().getStockObject("noderuninfo"));
        workingVM.setCustomFieldValue(CustomFieldConstants.CLOUD_WORKER_TAG, workerTag);
    }
    return workingVM;
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineFileInfo(com.vmware.vim25.VirtualMachineFileInfo) VirtualLsiLogicController(com.vmware.vim25.VirtualLsiLogicController) 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