vsphere VM hot plug CPU script

I was teaching myself how to code scripts using the vSphere SDK for perl.

I was running all this on an Ubuntu 10.04 system

It’s not the fanciest script in the world – it was just to demonstrate the concept of modifying a VM configuration on the fly and seeing what happened in the VM (in this case the VM is a SLES 11 x86_64 system).

Note: Not all systems support hot plugging memory or cpu and they will need to have the option enabled to allow hot plugging – this of course has to be set when the VM is powered off. Once set you’re ok for the future.

The script is called vmhotplugcpu.pl

–server enter the vCenter server you want to connect to.
-vmname enter the name of the VM you want
–cpu Enter the amount of vCPU’s you want to add or remove from the VM either as a positive or negative number

There are plenty of other options as set by the SDK itself.

The best way to run this is after you’ve created a credstore so you don’t have to constantly re-enter the username / password of the VC account.

As you can see from this screenshot the VM in question a SLES11 system only has one CPU.

and this is confirmed by top on the system

If I run my hotplug script

the VC shows some activity

and if we look at the VM setting once the script has run – note – this VM was powered on when we did this.

What’s this, the system still shows 1 cpu !

If we look at /var/log/messages we can see the cpu being added

But to make it active we need to bring it online

and now we get

So what happens if we try to remove a cpu

If we check the VM it doesn’t support hot removal of cpu’s 😦

The best we can do is to mark the cpu offline in Linux

and we can see in /var/log/message the cpu has gone offline

The script is here in case you wondered and I haven’t cleaned it up – I was just trying to work out the API for myself so the code isn’t pretty. You can find plenty of examples supplied with the SDK, that’s how I got the start for this script.

#!/usr/bin/perl -w

use strict;
use warnings;

use FindBin;
use lib "$FindBin::Bin/../";

use VMware::VIRuntime;
use XML::LibXML;
use AppUtil::VMUtil;
use AppUtil::XMLInputUtil;

$Util::script_version = "1.0";

sub display;
sub customize;
sub validate { my $valid = 1; return $valid; };
sub check_missing_value;

my %opts = (
'vmname' => {
type => "=s",
help => "The name of the virtual machine",
required => 1,
'cpu' => {
type => "=s",
help => "The number opf cpus to add or remove",
required => 1,


my $cpucount = Opts::get_option('cpu');
my $vmname = Opts::get_option('vmname');

# connect to the server

sub customize() {
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine',
filter => {"config.name" => $vmname});
if(defined @$vm_views) {
foreach(@$vm_views) {
if ($_->runtime->powerState->val eq 'poweredOff'){
Util::trace(0, "For hot(un)plugging cpus, VM '$vmname' should be powered on\n");
else {
my $num_cpu = $_->config->hardware->numCPU + $cpucount;
Util::trace(0, "VM '$vmname' CPUs =$num_cpu\n");
my $vmConfig =
VirtualMachineConfigSpec->new (numCPUs => $num_cpu);

eval {
Util::trace(0,"Updating cpu allocation...\n");
$_->ReconfigVM (spec => $vmConfig);

if ($@) {
if (ref($@) eq 'SoapFault') {
if (ref($@->detail) eq 'CustomizationFault') {
Util::trace(0, "\n Cannot Perfrom this operation"
." System Error" . "\n");
elsif (ref($@->detail) eq 'NotSupported') {
Util::trace(0, "\nThe operation is not supported"
." on the object" . "\n");
elsif (ref($@->detail) eq 'HostNotConnected') {
Util::trace(0, "\nUnable to communicate with the remote host, "
."since it is disconnected" . "\n");
elsif (ref($@->detail) eq 'InvalidState') {
Util::trace(0, "\nThe operation is not allowed in the"
." current state" . "\n");
elsif (ref($@->detail) eq 'InvalidPowerState') {
Util::trace(0, "\nThe attempted operation cannot be"
." performed in the current state" . "\n");
elsif (ref($@->detail) eq 'UncustomizableGuest') {
Util::trace(0, "\nCustomization is not supported for"
." the guest operating system" . "\n");
else {
Util::trace(0, "\n". $@ . "\n\n");
else {
Util::trace(0, "\n". $@ . "\n\n");
else {
Util::trace(0, "No Virtual Machine Found With Name '$vmname'\n");



2 thoughts on “vsphere VM hot plug CPU script

  1. nice! hot plug 4tw

  2. Pingback: Informations about CPU- and RAM-HotPlug at VMs on VMware vSphere 5 › Virtual Professional

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s