vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.01k stars 922 forks source link

New Sample:Addition of CPU Cores and Memory to the Existing machine #265

Open Suryadhulipudi opened 8 years ago

Suryadhulipudi commented 8 years ago

Can someone give some idea,how to add cpu cores and memory to the existing machine.

prziborowski commented 8 years ago

You should be able to use vm.Reconfigure api and set the cpu (core) and memory as desired. If the VM is powered on, it will require hot-add support (also requires the guest to support it). So it should look the same as creating a new VM except you call Reconfigure on the VM object rather than CreateVm on the folder object.

cspec = vim.vm.ConfigSpec()
cspec.numCPUs = 4 # if you want 4 cpus
cspec.numCoresPerSocket = 2 # if you want dual-processor with dual-cores
cspec.memoryMB = 1024 # 1GB of memory
WaitForTask(vm.Reconfigure(cspec))

If you want to set the hot-add properties, you can only do that when a VM is off:

cspec = vim.vm.ConfigSpec()
cspec.cpuHotAddEnabled = True
cspec.memoryHotAddEnabled = True
WaitForTask(vm.Reconfigure(cspec))
Suryadhulipudi commented 8 years ago

@prziborowski Thank you very much for the quick help...i have made the changes in the script and i am getting the error like "The attempted operation cannot be performed in the current state("Power on")"...Actually i want to do this while the vm state is powered on...Kindly help on this.

prziborowski commented 8 years ago

For that to work, you'd first have to set the hot-add properties while the VM is off, and be using a guest OS that supports hot-add of CPU/memory. Then you should be able to hot-add them.

You can check if the currently used guest OS supports it with something like:

envBrowser = vm.environmentBrowser
configOption = envBrowser.QueryConfigOption()
osDescription = filter(lambda x: x.id == vm.config.guestId, configOption.guestOSDescriptor)[0]
print "Supports cpuAdd: %s, memoryAdd: %s" % (osDescription.supportsCpuHotAdd, osDescription.supportsMemoryHotAdd)
Suryadhulipudi commented 8 years ago

Hi @prziborowski Hot cpu and memory is enabled for the vm and below command confirms the same and please find the attachment. cpu memory error

ubuntu@localhost:~$ python vmware.py -s x.x.x.x -o 443 -v ubuntu-test -u Administrator@vsphere.local -p xxxxxxxxxx Supports cpuAdd: True, memoryAdd: True

matan12333 commented 8 years ago

Hey I would love if you would send the full code. I am new to python (not to speak of the Pyvmomi librayry) and would love a good example script

Suryadhulipudi commented 8 years ago

Hi @matan12333... Please find the script in this link https://github.com/Suryadhulipudi/Scripts/blob/master/Vmware_CPU_Memory_Addition.py.

matan12333 commented 8 years ago

@sureshsundriyal Thanks a lot!

tianhao64 commented 8 years ago

@Suryadhulipudi Looks like changing numCoresPerSocket is not supported for hot-add. It's grayed out in the UI. Also in doc reconfigure privilege only mentioned CpuCount and not numCoresPerSocket for cpuHotAddEnabled. So I doubt you can modify numCoresPerSocket while vm is powered on. Can you try to only update the number of cpus in your script?

Suryadhulipudi commented 8 years ago

Hi @tianhao64 you are correct,reconfigure privilege not their for numCoresPerSocket for cpuHotAddEnabled.Now i am able to add the CPU and Memory to the existing vm.@tianhao64 @prziborowski @matan12333 Thanks you very much for quick help...appreciating your support.

satwikk commented 8 years ago

@prziborowski Is there a way to set "grant memory" for a VM via pyVmomi?

prziborowski commented 8 years ago

By "grant memory" do you mean in terms of resource allocation's reservation?

If so, you can do something like:

from task import WaitForTask

memReserve = vm.config.hardware.memoryMB * 2
spec = vim.vm.ConfigSpec()
spec.memoryAllocation = vim.ResourceAllocationInfo(reservation=memReserve)
WaitForTask(vm.Reconfigure(spec))

I haven't tried the above, but kind of working off the documentation. In this case, if you have memoryHotAddEnabled set to true, I believe it should let you expand to what your reservation is without risking the request being denied by out of resources (since it would reserve them earlier)

If you had another meaning for "grant memory", can you clarify?

satwikk commented 8 years ago

@prziborowski Thanks for your prompt reply. According to docs, ESXi memory Management provides three memory related setting parameters, reservation, limit and share,

from task import WaitForTask
memReserve = vm.config.hardware.memoryMB * 2
spec = vim.vm.ConfigSpec()
spec.memoryAllocation = vim.ResourceAllocationInfo(reservation=memReserve)
WaitForTask(vm.Reconfigure(spec))

Above code snippet will allow me to configure that reservation memory, which is the minimum amount of memory that will be made available to that vm.

Now what I mean by "grant memory". If you can please follow this link, in that table you will find vmware's definition to "granted" memory. Also, if you enter esxtop (followed by m, V for better statistical view ) on your ESXi host, there you will find GRANT memory.

I wanted an pyVmomi API to control/set that GRANT memory.

I hope I was able to clearly explain my query, and If you cloud assist me with this?

prziborowski commented 8 years ago

I see. I'm not very familiar with that data. I'm not sure if there are any settings to let you control the grant memory beyond what is in ResourceAllocationInfo. The guestOS, the vmkernel and how much load from other VMs, processes likely will change how much memory is granted. I would think that the reservation would influence that by not allowing it to be swapped out... Sorry I can't be of more help.

satwikk commented 8 years ago

@prziborowski True That, I also searched a lot for that kind of memory management api. Unfortunately, didn't find any! Anyways, I appreciate your help! Thanks.

chengpeng21186 commented 7 years ago

!/usr/bin/env python

-- coding: utf-8 --

Email: chengpeng21186@163.com

QQ: 279379936

from pyVmomi import vim from pyVmomi import vmodl from pyVim.connect import SmartConnect, Disconnect import atexit import argparse import getpass from pyVim.task import WaitForTask import sys

def get_args(): parser = argparse.ArgumentParser( description='Arguments for talking to vCenter')

parser.add_argument('-s', '--host',
                    required=True,
                    action='store',
                    help='vSpehre service to connect to')

parser.add_argument('-o', '--port',
                    type=int,
                    default=443,
                    action='store',
                    help='Port to connect on')

parser.add_argument('-u', '--user',
                    required=True,
                    action='store',
                    help='User name to use')

parser.add_argument('-p', '--password',
                    required=False,
                    action='store',
                    help='Password to use')

parser.add_argument('-v', '--vm-name',
                    required=True,
                    action='store',
                    help='name of the vm')

parser.add_argument('--uuid',
                    required=False,
                    action='store',
                    help='vmuuid of vm')

parser.add_argument('--cpu',
                   required=False,
                   action='store',
                   help='vcpu of vm')

parser.add_argument('--memory',
                   required=False,
                   action='store',
                   help='memory of vm')

args = parser.parse_args()

if not args.password:
    args.password = getpass.getpass(
        prompt='Enter password')
return args

def get_obj(content, vimtype, name): obj = None container = content.viewManager.CreateContainerView( content.rootFolder, vimtype, True) for c in container.view: if c.name == name: obj = c break return obj

def change_vcpu(vm, si, vcpu_nu): vcpu_nu=int(vcpu_nu) cspec = vim.vm.ConfigSpec() cspec.numCPUs = vcpu_nu # (虚拟插槽数) cspec.numCoresPerSocket = 1 #(每个插槽的内核总数,每个插槽的内核数不能大于虚拟CPU的数量) WaitForTask(vm.Reconfigure(cspec))

def change_memory(vm, si, mem_size): mem_size=long(mem_size) cspec = vim.vm.ConfigSpec() cspec.memoryMB = mem_size # MB of memory WaitForTask(vm.Reconfigure(cspec))

def main(): args = get_args()

connect this thing

si = SmartConnect(
    host=args.host,
    user=args.user,
    pwd=args.password,
    port=args.port)
# disconnect this thing
atexit.register(Disconnect, si)
vm = None
if args.uuid:
    search_index = si.content.searchIndex
    vm = search_index.FindByUuid(None, args.uuid, True)
elif args.vm_name:
    content = si.RetrieveContent()
    vm = get_obj(content, [vim.VirtualMachine], args.vm_name)
if vm:
    if args.cpu and args.memory:
        print args.cpu
        print args.memory
    elif args.memory and not args.cpu:
        #print type(args.memory)
        change_memory(vm, si, args.memory)
    elif args.cpu and not args.memory:
        print args.cpu
        change_vcpu(vm, si, args.cpu)
    else: 
        print '-h, --help  show help messages for you!'
else:
    print "VM not found"

start this thing

if name == "main": main()

zhangjianpinghik commented 6 years ago

@Suryadhulipudi i had enale the configurate of memory and cpu hot add ,but when i add cpu and memory to a vm which power on , the error occured: the vm can not support hot add cpu

sebma commented 2 months ago

Hi @matan12333... Please find the script in this link https://github.com/Suryadhulipudi/Scripts/blob/master/Vmware_CPU_Memory_Addition.py.

@Suryadhulipudi Hi, I get a HTTP 404 error when I clic on your hyperlink.

Can you please give the new link ?

sebma commented 2 months ago

@Suryadhulipudi Found it 😄 , the link for your script is Python_Vmware_CPU_Memory_Addition.py.