RHEL/CentOS 7: KVM – How to Add CPU’s on the Fly

0
54

In order to be able to add CPUs on the fly to a guest, the guest’s configuration must support them.

There are two ways to achieve this:

  • It must be created with the max option, as follows:
    --vcpus 2,maxvcpus=4
  • You can set the maximum using virsh (which will be applied at the next boot) through the following command:
    ~]# virsh setvcpus --domain <guestname> --count <max cpu count> --config --maximum
    
  • You can edit the guests’ XML files, as follows:
    ~]# virsh edit <guestname>
    

The last two options will require you to shut down and boot (not reboot) your guest as these commands cannot change the “live” configuration.

The guest’s XML file must contain the following element with the subsequent attributes:

<domain type='kvm'>
...
<vcpu current='2'>4</vcpu>
...
</domain>

Here, current indicates the number of CPUs in use, and the number within the node indicates the maximum number of vCPUs that can be assigned. This number can be increased but should never exceed the number of cores or threads in your host.

Let’s add some CPUs to the guest.

On the KVM host, perform the following steps:

  1. Get the maximum number vCPUs that you can assign, as follows:
    ~]# virsh dumpxml <guestname> |grep vcpu <vcpu placement='static' current='4'>8</vcpu>
    
  2. Now, set the new number of vCPUs through this command:
    ~]# virsh setvcpus --domain <guestname> --count <# of CPUs> --live
    

On the KVM guest, perform the following:

Tell your guest OS there are more CPUs available by executing the following command:

~]# for i in $(grep -H 0 /sys/devices/system/cpu/cpu*/online | awk -F: '{print $1}'); do echo 1 > $i; done

LEAVE A REPLY

Please enter your comment!
Please enter your name here