How to Hot Add/Remove VCPUs from a Xen Domain

Tags: , , , , ,

Overview

Xen supports vcpu hot add and remove, which allows you to add and remove CPUs from a running system without downtime.

How it works

Inside your domU config, you’ll need to set the “maxvcpus” setting to the maximum number of VCPUs that this domU will be allowed to have. If you don’t define this, it defaults to the value of “vcpus”, so you’ll always be able to hot remove, but wouldn’t be able to hot-add anything more than what it was booted with.

#vcpus - number of VCPUs to boot the system with.
vcpus = 2;
 
#maxvcpus - maximum number of VCPUs (total) that can be hot added later.
maxvcpus = 8;

VCPU Hot Add Example

Lets say I have a virtual machine named foo which I’ve given 1 VCPU. One day I notice that the system is struggling to keep up with a CPU heavy load. So, I want to add another VCPU to the VM, but I can’t afford a downtime. No problem (as long as I configured the maxvcpus value above).

Here’s the system with one 1 VCPU:

# xm list
Name                                      ID Mem(MiB) VCPUs State   Time(s)
foo                                      11  4096     1     -b----     23.7

To resize we use the ‘xm vcpu-set’ command. For example, to re-size our 1 VCPU domain to 2 VCPUs, execute the following.

# xm vcpu-set foo 2
 
# xm list
Name                                      ID Mem(MiB) VCPUs State   Time(s)
foo                                      11  4096     2     -b----     31.6

VCPU Hot Remove Example

Similarly, we can hot remove VCPUs from a domain using the ‘xm vcpu-set’ command.

Here’s the system with one 2 VCPUs:

# xm list
Name                                      ID Mem(MiB) VCPUs State   Time(s)
foo                                      11  4096     2     -b----     52.5

To hot remove VPCUs simply execute ‘xm vcpu-set’, specifying a lower number than what is currently assigned.

# xm vcpu-set foo 1
 
# xm list
Name                                      ID Mem(MiB) VCPUs State   Time(s)
comlag                                      11  4096     1     -b----     56.7

And that’s it. As you can see it’s very straightforward, and as long as you’ve taken the time to set your ‘maxvcpus’ setting in the domU config before booting the machine you’ll be able to adjust your VCPU assignments as load varies.