Search in sources :

Example 6 with VmConfigSpec

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

use of com.vmware.vim25.VmConfigSpec 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 8 with VmConfigSpec

use of com.vmware.vim25.VmConfigSpec 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 9 with VmConfigSpec

use of com.vmware.vim25.VmConfigSpec 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 10 with VmConfigSpec

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

the class VirtualMachineMO method setVncConfigInfo.

public boolean setVncConfigInfo(boolean enableVnc, String vncPassword, int vncPort, String keyboard) throws Exception {
    VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
    OptionValue[] vncOptions = VmwareHelper.composeVncOptions(null, enableVnc, vncPassword, vncPort, keyboard);
    vmConfigSpec.getExtraConfig().addAll(Arrays.asList(vncOptions));
    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 : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) OptionValue(com.vmware.vim25.OptionValue) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)19 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)17 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)11 VirtualDevice (com.vmware.vim25.VirtualDevice)10 IOException (java.io.IOException)8 CloudException (com.cloud.exception.CloudException)7 InternalErrorException (com.cloud.exception.InternalErrorException)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)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 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)5 OptionValue (com.vmware.vim25.OptionValue)4 VirtualDisk (com.vmware.vim25.VirtualDisk)4 DatacenterMO (com.cloud.hypervisor.vmware.mo.DatacenterMO)3 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)3