Search in sources :

Example 6 with VirtualDeviceConfigSpec

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

the class VmwareResourceTest method testConfigureSpecVideoCardNewVRamSize.

@Test
public void testConfigureSpecVideoCardNewVRamSize() {
    when(vmConfigSpec.getDeviceChange()).thenReturn(new ArrayList<VirtualDeviceConfigSpec>());
    _resource.configureSpecVideoCardNewVRamSize(videoCard, VRAM_MEMORY_SIZE, vmConfigSpec);
    InOrder inOrder = Mockito.inOrder(videoCard, vmConfigSpec);
    inOrder.verify(videoCard).setVideoRamSizeInKB(VRAM_MEMORY_SIZE);
    inOrder.verify(videoCard).setUseAutoDetect(false);
    inOrder.verify(vmConfigSpec).getDeviceChange();
}
Also used : InOrder(org.mockito.InOrder) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with VirtualDeviceConfigSpec

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

the class VmwareResource method execute.

private PlugNicAnswer execute(PlugNicCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource PlugNicCommand " + _gson.toJson(cmd));
    }
    getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
    VmwareContext context = getServiceContext();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        String vmName = cmd.getVmName();
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            if (hyperHost instanceof HostMO) {
                ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
                vmMo = clusterMo.findVmOnHyperHost(vmName);
            }
        }
        if (vmMo == null) {
            String msg = "Router " + vmName + " no longer exists to execute PlugNic command";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        /*
            if(!isVMWareToolsInstalled(vmMo)){
                String errMsg = "vmware tools is not installed or not running, cannot add nic to vm " + vmName;
                s_logger.debug(errMsg);
                return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + errMsg);
            }
             */
        // Fallback to E1000 if no specific nicAdapter is passed
        VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000;
        Map<String, String> details = cmd.getDetails();
        if (details != null) {
            nicDeviceType = VirtualEthernetCardType.valueOf((String) details.get("nicAdapter"));
        }
        // find a usable device number in VMware environment
        VirtualDevice[] nicDevices = vmMo.getNicDevices();
        int deviceNumber = -1;
        for (VirtualDevice device : nicDevices) {
            if (device.getUnitNumber() > deviceNumber)
                deviceNumber = device.getUnitNumber();
        }
        deviceNumber++;
        NicTO nicTo = cmd.getNic();
        VirtualDevice nic;
        Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, cmd.getVMType());
        String dvSwitchUuid = null;
        if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
            ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
            DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
            ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
            dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
            s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
            nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), dvSwitchUuid, nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
        } else {
            s_logger.info("Preparing NIC device on network " + networkInfo.second());
            nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
        }
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(nic);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        setNuageVspVrIpInExtraConfig(vmConfigSpec.getExtraConfig(), nicTo, dvSwitchUuid);
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure devices when running PlugNicCommand");
        }
        return new PlugNicAnswer(cmd, true, "success");
    } catch (Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
    }
}
Also used : HostMO(com.cloud.hypervisor.vmware.mo.HostMO) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualEthernetCardType(com.cloud.hypervisor.vmware.mo.VirtualEthernetCardType) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) NicTO(com.cloud.agent.api.to.NicTO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 8 with VirtualDeviceConfigSpec

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

the class VmwareResource method configureSpecVideoCardNewVRamSize.

/**
     * Add edit spec on {@code vmConfigSpec} to modify svga vram size
     * @param videoCard video card device to edit providing the svga vram size
     * @param svgaVmramSize new svga vram size (in KB)
     * @param vmConfigSpec virtual machine spec
     */
protected void configureSpecVideoCardNewVRamSize(VirtualMachineVideoCard videoCard, long svgaVmramSize, VirtualMachineConfigSpec vmConfigSpec) {
    videoCard.setVideoRamSizeInKB(svgaVmramSize);
    videoCard.setUseAutoDetect(false);
    VirtualDeviceConfigSpec arrayVideoCardConfigSpecs = new VirtualDeviceConfigSpec();
    arrayVideoCardConfigSpecs.setDevice(videoCard);
    arrayVideoCardConfigSpecs.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
    vmConfigSpec.getDeviceChange().add(arrayVideoCardConfigSpecs);
}
Also used : VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec)

Example 9 with VirtualDeviceConfigSpec

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

the class VmwareResource method execute.

private Answer execute(ResizeVolumeCommand cmd) {
    String path = cmd.getPath();
    String vmName = cmd.getInstanceName();
    long newSize = cmd.getNewSize() / 1024;
    long oldSize = cmd.getCurrentSize() / 1024;
    boolean useWorkerVm = false;
    VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
    String poolId = cmd.getPoolUuid();
    VirtualMachineMO vmMo = null;
    DatastoreMO dsMo = null;
    ManagedObjectReference morDS = null;
    String vmdkDataStorePath = null;
    try {
        if (newSize < oldSize) {
            throw new Exception("VMware doesn't support shrinking volume from larger size: " + oldSize / (1024 * 1024) + " GB to a smaller size: " + newSize / (1024 * 1024) + " GB");
        } else if (newSize == oldSize) {
            return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
        }
        if (vmName.equalsIgnoreCase("none")) {
            // we need to spawn a worker VM to attach the volume to and
            // resize the volume.
            useWorkerVm = true;
            vmName = getWorkerName(getServiceContext(), cmd, 0);
            morDS = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, poolId);
            dsMo = new DatastoreMO(hyperHost.getContext(), morDS);
            s_logger.info("Create worker VM " + vmName);
            vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, vmName);
            if (vmMo == null) {
                throw new Exception("Unable to create a worker VM for volume resize");
            }
            synchronized (this) {
                vmdkDataStorePath = VmwareStorageLayoutHelper.getLegacyDatastorePathFromVmdkFileName(dsMo, path + ".vmdk");
                vmMo.attachDisk(new String[] { vmdkDataStorePath }, morDS);
            }
        }
        // find VM through datacenter (VM is not at the target host yet)
        vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
        if (vmMo == null) {
            String msg = "VM " + vmName + " does not exist in VMware datacenter";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        Pair<VirtualDisk, String> vdisk = vmMo.getDiskDevice(path);
        if (vdisk == null) {
            if (s_logger.isTraceEnabled())
                s_logger.trace("resize volume done (failed)");
            throw new Exception("No such disk device: " + path);
        }
        // IDE virtual disk cannot be re-sized if VM is running
        if (vdisk.second() != null && vdisk.second().contains("ide")) {
            throw new Exception("Re-sizing a virtual disk over IDE controller is not supported in VMware hypervisor. " + "Please re-try when virtual disk is attached to a VM using SCSI controller.");
        }
        if (vdisk.second() != null && !vdisk.second().toLowerCase().startsWith("scsi")) {
            s_logger.error("Unsupported disk device bus " + vdisk.second());
            throw new Exception("Unsupported disk device bus " + vdisk.second());
        }
        VirtualDisk disk = vdisk.first();
        if ((VirtualDiskFlatVer2BackingInfo) disk.getBacking() != null && ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent() != null) {
            s_logger.error("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
            throw new Exception("Resize is not supported because Disk device has Parent " + ((VirtualDiskFlatVer2BackingInfo) disk.getBacking()).getParent().getUuid());
        }
        String vmdkAbsFile = getAbsoluteVmdkFile(disk);
        if (vmdkAbsFile != null && !vmdkAbsFile.isEmpty()) {
            vmMo.updateAdapterTypeIfRequired(vmdkAbsFile);
        }
        disk.setCapacityInKB(newSize);
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(disk);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure VM to resize disk. vmName: " + vmName);
        }
        return new ResizeVolumeAnswer(cmd, true, "success", newSize * 1024);
    } catch (Exception e) {
        s_logger.error("Unable to resize volume", e);
        String error = "Failed to resize volume: " + e.getMessage();
        return new ResizeVolumeAnswer(cmd, false, error);
    } finally {
        try {
            if (useWorkerVm == true) {
                s_logger.info("Destroy worker VM after volume resize");
                vmMo.detachDisk(vmdkDataStorePath, false);
                vmMo.destroy();
            }
        } catch (Throwable e) {
            s_logger.info("Failed to destroy worker VM: " + vmName);
        }
    }
}
Also used : VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) ResizeVolumeAnswer(com.cloud.agent.api.storage.ResizeVolumeAnswer) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) VirtualDisk(com.vmware.vim25.VirtualDisk) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualDiskFlatVer2BackingInfo(com.vmware.vim25.VirtualDiskFlatVer2BackingInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 10 with VirtualDeviceConfigSpec

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

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