简体   繁体   中英

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

My team is responsible for dynamically changing settings of vmware VMs using java. 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.

How can I change the amount of RAM or number of CPUs for a vmware VM using java? 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.

Input::

  1. CPU(numCPUs) - Provide num of cpus count,it never exceeds host supported num of cpus
  2. RAM(ramMB) - Provide ram value in MB, it never exceed host max alloted memory.

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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