简体   繁体   English

如何使用Java更改vmware VM的RAM / CPU?

[英]How to change RAM/CPUs of vmware VM using Java?

My team is responsible for dynamically changing settings of vmware VMs using java. 我的团队负责使用Java动态更改vmware VM的设置。 I have been tasked with changing the amount of available RAM and number of CPUs assigned to an already created vmware VM which exists on VCenter/VSphere. 我的任务是更改可用内存的数量和分配给VCenter / VSphere上已创建的vmware VM的CPU数量。

How can I change the amount of RAM or number of CPUs for a vmware VM using java? 如何使用Java更改vmware VM的RAM数量或CPU数量? I have been searching all over the net for clues, and havent found much. 我一直在网上寻找线索,还没有发现太多。

Here is the Sample code to change VM RAM and CPU with specified values using vSphere java API. 这是使用vSphere Java API使用指定值更改VM RAM和CPU的示例代码。

Input:: 输入:

  1. CPU(numCPUs) - Provide num of cpus count,it never exceeds host supported num of cpus CPU(numCPUs)-提供cpus数量,它永远不会超过主机支持的cpus数量
  2. RAM(ramMB) - Provide ram value in MB, it never exceed host max alloted memory. RAM(ramMB)-提供以MB为单位的ram值,它永远不会超过主机最大分配的内存。

Note:: For PowerON VM- Before modify RAM/CPU it is mandatory to enable the VM Memory Hot Add & CPU Hot Plug feature and also VM OS supports. 注意:对于PowerON VM-在修改RAM / CPU之前,必须启用VM Memory Hot Add和CPU Hot Plug功能以及VM OS支持。

Sample Code :: 示例代码::

  public static void changeVMRAMandCPU(ServiceInstance si, String vmName,
        long ramMB, int numCPUs) {
    String waitStr = null;
    try {
        Folder rootFolder = si.getRootFolder();
        VirtualMachine vm = (VirtualMachine) new InventoryNavigator(
                rootFolder).searchManagedEntity("VirtualMachine", vmName);
        HostSystem hs = new HostSystem(si.getServerConnection(), vm
                .getRuntime().getHost());
        System.out.println("Host Name ::" + hs.getName());
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        if (isValidCPUCount(numCPUs, hs)) {
            vmConfigSpec.setNumCPUs(numCPUs);
        } else {
            System.out.println("NotAllowed::Provided cpu count " + numCPUs
                    + " is more then the host --> " + hs.getName()
                    + " cpu count "
                    + hs.getHardware().getCpuInfo().getNumCpuCores());
        }

        if (isValidRam(ramMB, hs)) {
            vmConfigSpec.setMemoryMB(ramMB);
        } else {
            System.out.println("NotAllowed::Provided ram " + ramMB
                    + " is more then the host --> " + hs.getName()
                    + " ram " + hs.getHardware().getMemorySize());
        }
        System.out.println("Before modify VM RAM , CPU ==>"
                + vm.getConfig().getHardware().getMemoryMB() + " MB ,"
                + vm.getConfig().getHardware().getNumCPU() + " CPU,");
        Task task = vm.reconfigVM_Task(vmConfigSpec);
        waitStr = task.waitForTask();
        if (waitStr != null && waitStr.equalsIgnoreCase("success")) {
            vm = (VirtualMachine) new InventoryNavigator(rootFolder)
                    .searchManagedEntity("VirtualMachine", vmName);
            System.out.println("After modify VM RAM , CPU ==>"
                    + vm.getConfig().getHardware().getMemoryMB() + " MB ,"
                    + vm.getConfig().getHardware().getNumCPU() + " CPU,");
        } else {
            System.out.println("Error::VM Reconfig fail...");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static boolean isValidCPUCount(int cpu, HostSystem hs) {
    int hostCpusCount = hs.getHardware().getCpuInfo().getNumCpuCores();
    if (cpu <= hostCpusCount) {
        return true;
    }
    return false;
}

public static boolean isValidRam(long ram, HostSystem hs) {
    long hostMem = hs.getHardware().getMemorySize() / (1024L * 1024L);
    if (ram < hostMem) {
        return true;
    }
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM