Does KVM support vCPU hot-plug ? Will Linux KVM guest can recognize the newly added vCPU’s ? The answer is “YES” off-course. Like KVM memory management, you can add/remove vCPU’s to active VM using “virsh” command. But this works if you have configured the KVM guest with Maximum vCPUs parameter. So while deploying the new virtual machines , you should always consider this parameter as prerequisite.There is no harm specifying the maximum vCPU’s to KVM guest since is going to use only the allocated vCPU (vcpu placement=’static’ current=’N’) .
Understand the VM’s vCPU configuration:
1.List the configured VM’s on KVM host.
[root@UA-HA ~]# virsh list --all Id Name State ---------------------------------------------------- - UAKVM2 shut off [root@UA-HA ~]#
2.Check the current VM configuration.
As per the above screenshot, this VM is privileged to use 1 vCPU and you can add one more vCPU using “virsh vsetcpus” command on fly. But you can’t increase the vCPU more than 2 on running VM.
3. Let’s power on the VM.
[root@UA-HA ~]# virsh start UAKVM2 Domain UAKVM2 started [root@UA-HA ~]#
4. Login to the KVM guest and check the allocated vCPU count.
[root@UA-KVM1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 1 On-line CPU(s) list: 0 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 44 Model name: Westmere E56xx/L56xx/X56xx (Nehalem-C) Stepping: 1 CPU MHz: 2594.058 BogoMIPS: 5188.11 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0 [root@UA-KVM1 ~]#
Increase the vCPU on Running KVM Guest:
Let’s increase the vCPU count from 1 to 2.
1. Switch to the KVM host and increase the vCPU from 1 to 2 using virsh command.
[root@UA-HA ~]# virsh setvcpus UAKVM2 2 [root@UA-HA ~]# virsh dominfo UAKVM2 Id: 38 Name: UAKVM2 UUID: 6013be3b-08f9-4827-83fb-390bd5a86de6 OS Type: hvm State: running CPU(s): 2 CPU time: 51.5s Max memory: 1048576 KiB Used memory: 1048576 KiB Persistent: yes Autostart: disable Managed save: no Security model: selinux Security DOI: 0 Security label: system_u:system_r:svirt_t:s0:c709,c868 (permissive) [root@UA-HA ~]#
2. Go back to KVM guest and check the newly added vCPU. VM will switch to “SMP code (symmetric multiprocessor)” from “UP code(uniprocessor)” .
[root@UA-KVM1 ~]# tail -f /var/log/messages Dec 16 12:48:28 UA-KVM1 kernel: CPU1 has been hot-added Dec 16 12:48:28 UA-KVM1 kernel: SMP alternatives: switching to SMP code Dec 16 12:48:57 UA-KVM1 kernel: smpboot: Booting Node 0 Processor 1 APIC 0x1 Dec 16 12:48:57 UA-KVM1 kernel: kvm-clock: cpu 1, msr 0:3ff87041, secondary cpu clock Dec 16 12:48:57 UA-KVM1 kernel: TSC synchronization [CPU#0 -> CPU#1]: Dec 16 12:48:57 UA-KVM1 kernel: Measured 906183720569 cycles TSC warp between CPUs, turning off TSC clock. Dec 16 12:48:57 UA-KVM1 kernel: tsc: Marking TSC unstable due to check_tsc_sync_source failed Dec 16 12:48:57 UA-KVM1 kernel: KVM setup async PF for cpu 1 Dec 16 12:48:57 UA-KVM1 kernel: kvm-stealtime: cpu 1, msr 3fd0d240 Dec 16 12:48:57 UA-KVM1 kernel: microcode: CPU1 sig=0x206c1, pf=0x1, revision=0x1 Dec 16 12:48:57 UA-KVM1 kernel: Will online and init hotplugged CPU: 1 [root@UA-KVM1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0,1 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 2 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 44 Model name: Westmere E56xx/L56xx/X56xx (Nehalem-C) Stepping: 1 CPU MHz: 2594.058 BogoMIPS: 5188.11 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0,1 [root@UA-KVM1 ~]#
3. Save the VM configuration if you want to make this changes persistent.
[root@UA-HA ~]# virsh setvcpus UAKVM2 2 --config [root@UA-HA ~]#
We have successfully added one vCPU to the KVM guest on fly and made it persistent.
Remove the vCPU from Live KVM Guest:
There is no direct method to remove the vCPU’s from the KVM guest. But you can bring back the CPU core power to the KVM hosts by disabling the CPU core.
1. Login to the KVM host as root user.
2. Assuming that there are two active vCPU’s on KVM guest UAKVM2.
3. Do not use following command to remove the vCPU. You might get error like ” error: internal error: cannot change vcpu count of this domain ”
[root@UA-HA ~]# virsh setvcpus UAKVM2 1 error: internal error: cannot change vcpu count of this domain [root@UA-HA ~]#
Use the following command to reduce the vCPU count on UAKVM2. (Reducing the vCPU to 1 from 2)
[root@UA-HA ~]# virsh setvcpus --live --guest UAKVM2 1 [root@UA-HA ~]#
4. Save the VM config.
[root@UA-HA ~]# virsh setvcpus --config UAKVM2 1 [root@UA-HA ~]#
5. Login to the KVM guest “UAKVM2” and verify.
[root@UA-KVM1 ~]# tail -f /var/log/messages Dec 16 13:01:01 UA-KVM1 systemd: Starting Session 2 of user root. Dec 16 13:19:08 UA-KVM1 kernel: Unregister pv shared memory for cpu 1 Dec 16 13:19:08 UA-KVM1 kernel: smpboot: CPU 1 is now offline [root@UA-KVM1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 2 On-line CPU(s) list: 0 Off-line CPU(s) list: 1 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 44 Model name: Westmere E56xx/L56xx/X56xx (Nehalem-C) Stepping: 1 CPU MHz: 2594.058 BogoMIPS: 5188.11 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0 [root@UA-KVM1 ~]#
Note:
In KVM host, You might still see the two allocated vCPU’s to guest UAKVM2. But this will change when you power cycle the VM.
[root@UA-HA ~]# virsh dominfo UAKVM2 Id: 38 Name: UAKVM2 UUID: 6013be3b-08f9-4827-83fb-390bd5a86de6 OS Type: hvm State: running CPU(s): 2 CPU time: 90.4s Max memory: 1048576 KiB Used memory: 1048576 KiB Persistent: yes Autostart: disable Managed save: no Security model: selinux Security DOI: 0 Security label: system_u:system_r:svirt_t:s0:c709,c868 (permissive) [root@UA-HA ~]#
Just shutdown “UAKVM2” KVM guest to verify it.
[root@UA-HA ~]# virsh destroy UAKVM2 Domain UAKVM2 destroyed [root@UA-HA ~]# virsh dominfo UAKVM2 Id: - Name: UAKVM2 UUID: 6013be3b-08f9-4827-83fb-390bd5a86de6 OS Type: hvm State: shut off CPU(s): 1 Max memory: 1048576 KiB Used memory: 0 KiB Persistent: yes Autostart: disable Managed save: no Security model: selinux Security DOI: 0 [root@UA-HA ~]#
We can see that vCPU has been changed to “One” .
Most of the people will try to add more vCPU’s than configured limits and that will result the following error.
(error: invalid argument: requested vcpus is greater than max allowable vcpus for the domain )
[root@UA-HA ~]# virsh setvcpus UAKVM2 3 error: invalid argument: requested vcpus is greater than max allowable vcpus for the domain: 3 > 2 [root@UA-HA ~]#
Please go back to section – “Understand the VM’s vCPU configuration” step number 2 again. You just can’t exceed the Maximum vCPUs using virsh command. At the same time, Maximum vCPUs can’t be changed at runtime.
How to modify the Maximum vCPU’s per VM ? (Only Offline Method)
1.Login to the KVM host as root user.
2. Halt the virtual machine gracefully.
[root@UA-HA ~]# virsh list Id Name State ---------------------------------------------------- 39 UAKVM2 running [root@UA-HA ~]# virsh shutdown UAKVM2 Domain UAKVM2 is being shutdown [root@UA-HA ~]#
3. Edit the VM configuration like following. Here I am changing the Maximum vCPU count from 2 to 4 .
vCPU XML format:
4. Start the KVM guest “UAKVM2” .
[root@UA-HA ~]# virsh start UAKVM2 Domain UAKVM2 started [root@UA-HA ~]#
5.Verify the vCPU information for UAKVM2 guest.
[root@UA-HA ~]# virsh vcpuinfo UAKVM2 VCPU: 0 CPU: 1 State: running CPU time: 24.0s CPU Affinity: yy [root@UA-HA ~]#
At present , only one vCPU has been allocated to UAKVM2 guest. As we have configured the maximum number of vCPU’s to 4, we can increase the allocated vCPU’s up to 4 on fly.
6. Increase the vCPU to 4 .
[root@UA-HA ~]# virsh setvcpus UAKVM2 4 [root@UA-HA ~]# virsh vcpuinfo UAKVM2 VCPU: 0 CPU: 0 State: running CPU time: 27.6s CPU Affinity: yy VCPU: 1 CPU: 0 State: running CPU time: 0.4s CPU Affinity: yy VCPU: 2 CPU: 0 State: running CPU time: 0.1s CPU Affinity: yy VCPU: 3 CPU: 0 State: running CPU time: 0.1s CPU Affinity: yy [root@UA-HA ~]# [root@UA-HA ~]# virsh setvcpus UAKVM2 4 --config [root@UA-HA ~]#
7. Login to the KVM guest “UAKVM2” and list the vCPUs
[root@UA-KVM1 ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 1 Core(s) per socket: 1 Socket(s): 4 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 44 Model name: Westmere E56xx/L56xx/X56xx (Nehalem-C) Stepping: 1 CPU MHz: 2594.058 BogoMIPS: 5188.11 Hypervisor vendor: KVM Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 4096K NUMA node0 CPU(s): 0-3 [root@UA-KVM1 ~]#
We have successfully increased the maximum vCPU’s limit in offline and increased the allocated vCPU on running VM.
Hope this article is informative to you. Share it ! Comment it !! Be Sociable !!!
Leave a Reply