Python Example of Configuring the CPU and Memory of a Virtual Machine

These examples are based on the code in the
cpu.py
and
memory.py
sample files.
For a complete and up-to-date version of the sample code, see the
vsphere-automation-sdk-python
VMware repository at GitHub.
The following example shows how you can update the CPU configuration of a virtual machine.
... vm = None vm_name = None stub_config = None cpu_svc = None cleardata = False orig_cpu_info = None ... server, username, password, cleardata, skip_verification, vm_name = \ parse_cli_args_vm(testbed.config['VM_NAME_DEFAULT']) stub_config = vapiconnect.connect(server, username, password, skip_verification) ... def run(): global vm vm = get_vm(stub_config, vm_name) if not vm: exit('Sample requires an existing vm with name ({}). ' 'Please create the vm first.'.format(vm_name)) print("Using VM '{}' ({}) for Cpu Sample".format(vm_name, vm)) # Create CPU stub used for making requests global cpu_svc cpu_svc = Cpu(stub_config) # Get the current CPU configuration cpu_info = cpu_svc.get(vm) print('vm.hardware.Cpu.get({}) -> {}'.format(vm, pp(cpu_info))) # Save current CPU info to verify that we have cleaned up properly global orig_cpu_info orig_cpu_info = cpu_info # Update the number of CPU cores of the virtual machine update_spec = Cpu.UpdateSpec(count=2) print('vm.hardware.Cpu.update({}, {})'.format(vm, update_spec)) cpu_svc.update(vm, update_spec) # Get the new CPU configuration cpu_info = cpu_svc.get(vm) print('vm.hardware.Cpu.get({}) -> {}'.format(vm, pp(cpu_info))) # Update the number of cores per socket and # enable adding CPUs while the virtual machine is running update_spec = Cpu.UpdateSpec(cores_per_socket=2, hot_add_enabled=True) print('vm.hardware.Cpu.update({}, {})'.format(vm, update_spec)) cpu_svc.update(vm, update_spec) ...
The following example demonstrates how you can add memory to a running virtual machine.
... vm = None vm_name = None stub_config = None memory_svc = None cleardata = False orig_memory_info = None ... server, username, password, cleardata, skip_verification, vm_name = \ parse_cli_args_vm(testbed.config['VM_NAME_DEFAULT']) stub_config = vapiconnect.connect(server, username, password, skip_verification) ... global vm vm = get_vm(stub_config, vm_name) if not vm: exit('Sample requires an existing vm with name ({}). ' 'Please create the vm first.'.format(vm_name)) print("Using VM '{}' ({}) for Memory Sample".format(vm_name, vm)) # Create Memory stub used for making requests global memory_svc memory_svc = Memory(stub_config) # Get the current Memory configuration memory_info = memory_svc.get(vm) print('vm.hardware.Memory.get({}) -> {}'.format(vm, pp(memory_info))) # Update the memory size of the virtual machine update_spec = Memory.UpdateSpec(size_mib=8 * 1024) print('vm.hardware.Memory.update({}, {})'.format(vm, update_spec)) memory_svc.update(vm, update_spec) # Get the new Memory configuration memory_info = memory_svc.get(vm) print('vm.hardware.Memory.get({}) -> {}'.format(vm, pp(memory_info))) # Enable adding memory while the virtual machine is running update_spec = Memory.UpdateSpec(hot_add_enabled=True) print('vm.hardware.Memory.update({}, {})'.format(vm, update_spec)) memory_svc.update(vm, update_spec) ...