How to expand VM memory, CPU and disks
In this article we’ll cover three practical cases for expanding resources in a VM managed with libvirt/KVM:
- Increase the number of vCPUs (cores)
- Increase RAM memory
- Expand disk and filesystem inside the guest
We’ll use virsh commands, libguestfs tools where applicable, and standard utilities inside the guest (growpart, resize2fs, xfs_growfs, LVM). I’ll include “hot” methods when the system supports them and safe cold alternatives.
Note: To run without sudo, your user must belong to the
libvirtandkvmgroups.
Example of initial situation:
virsh list --all
Example output:
Id Name State
-----------------------------
- debian13 shut off
1 Increase vCPU (cores)
There are two limits: the maximum vCPU defined in the VM and the currently assigned number. You can increase vCPU hot if the guest supports it (CPU hotplug), or cold by adjusting the persistent configuration.
View current and maximum vCPU
virsh vcpucount debian13 --live --maximum
virsh vcpucount debian13 --live
virsh vcpucount debian13 --config --maximum
virsh vcpucount debian13 --config
Increase vCPU hot (if possible)
# Increase to 4 vCPU on the running VM and persist the change
virsh setvcpus debian13 4 --live
virsh setvcpus debian13 4 --config
If an error appears, the guest or the definition may not allow hotplug. Make sure the maximum allows it:
# Increase maximum allowed to 8 vCPU (persistent), with VM powered off
virsh setvcpus debian13 8 --maximum --config
Then adjust the current value and start:
virsh setvcpus debian13 4 --config
virsh start debian13
Verification inside the guest
nproc
lscpu | grep -i '^CPU(s)\|Model name\|Thread\|Core\|Socket'
2 Increase RAM memory
Memory also has a maximum (maxmem) and a current value. Memory hotplug depends on the guest (balloon/virtio-mem). If it fails hot, apply cold.
Check memory
virsh dominfo debian13 | grep -i memory
virsh dommemstat debian13
Increase memory hot (if supported)
# Set maximum to 8 GiB and increase current memory to 6 GiB
virsh setmaxmem debian13 8G --config
virsh setmem debian13 6G --live
virsh setmem debian13 6G --config
If it errors on --live, make the change with the VM powered off:
virsh shutdown debian13
virsh setmaxmem debian13 8G --config
virsh setmem debian13 6G --config
virsh start debian13
Verification inside the guest
free -h
cat /proc/meminfo | grep MemTotal
3 Expand disk and filesystem
The general flow is:
- Expand the virtual disk on the host (qcow2/raw or pool volume)
- Inside the guest, detect the new size and expand partition/LVM as appropriate
- Resize the filesystem (ext4/xfs)
Recommendation: Make a snapshot/backup before modifying partitions or LVM.
3.1 Expand the disk on the host
First identify the VM’s disk path:
virsh domblklist debian13
- If using a libvirt pool volume:
virsh vol-list default
virsh vol-resize --pool default debian13.qcow2 +10G
- If using a direct qcow2/raw file:
qemu-img resize /var/lib/libvirt/images/debian13.qcow2 +10G
Note: Avoid resizing a disk with chained snapshots without checking dependencies.
Restart the VM (if it was running) so the guest detects the new disk size if it doesn’t support size hotplug:
virsh shutdown debian13 && virsh start debian13
3.2 Case A: Single partition (no LVM)
Assuming a vda disk with a vda1 partition using ext4 or xfs.
- Expand the partition inside the guest:
sudo apt install -y cloud-guest-utils # for growpart (Debian/Ubuntu)
sudo growpart /dev/vda 1 # expands partition 1 to occupy the new size
lsblk
- Resize the filesystem:
- ext4:
sudo resize2fs /dev/vda1
- xfs (specify the mount point):
sudo xfs_growfs /
Verify:
df -h
3.3 Case B: LVM (PV + VG + LV)
Assuming the PV is on /dev/vda2, the VG is vg0 and the LV is lvroot mounted at /.
- Detect the new size on the PV:
sudo pvscan
sudo pvdisplay
sudo pvresize /dev/vda2
- Expand the LV and the FS at once (ext4/xfs):
sudo lvdisplay
sudo lvextend -r -l +100%FREE /dev/vg0/lvroot
-r attempts to automatically resize the filesystem. Manual alternative:
sudo lvextend -l +100%FREE /dev/vg0/lvroot
# ext4
sudo resize2fs /dev/vg0/lvroot
# xfs (specify mount point)
sudo xfs_growfs /
Verify:
lsblk
df -h
Tips and troubleshooting
- CPU/Memory hotplug: depends on the guest (kernel, drivers, ACPI, virtio). If it fails on
--live, make the change with--configand restart. - Maximum limit: if
setvcpus/setmemfail due to limit, first adjust the maximum with--maximum(CPU) orsetmaxmem(memory). - growpart not available: use
partedorfdiskcarefully, make sure to align correctly and not overwrite data. - xfs doesn’t shrink: it only grows. To shrink xfs you have to recreate, plan accordingly.
References
- curso_virtualizacion_linux — GitHub
- virsh - Official libvirt documentation - Commands: setvcpus, setmem, setmaxmem, dominfo, vcpucount, dommemstat
- qemu-img - QEMU Documentation - Resize command to resize disk images
- cloud-guest-utils - Launchpad - Includes growpart to expand partitions
- resize2fs - e2fsprogs - Resize ext2/ext3/ext4 filesystems
- xfs_growfs - XFS Documentation - Expand XFS filesystems
- LVM Administration Guide - Red Hat - pvresize, lvextend and logical volume management
