Extend disk for KVM virtual images¶
You have a KVM virtual machine which boots from a “disk” mapped to a file on the host system, and at some point you realize you need to extend that.
We assume the disk file is called /mnt/vm/05.qcow2 and we want to extend it.
download GParted and copy it to the host system
Power the VM off, take advantage of the downtime to also increase RAM and CPU, if needed. Make sure your VM has a virtual CD/DVD.
On the host system, extend the disk-file:
qemu-img resize /mnt/vm/05.qcow2 +50G # On our host, file extension is qcow2 but really the disk format is RAW root@pa1-r1-s06:~# qemu-img info /mnt/vm/05.qcow2 image: /mnt/vm/05.qcow2 file format: raw virtual size: 100G (107374182400 bytes) disk size: 48G
from virt-manager, double-click on your VM name, which will pop-up the VM management window:
go to the settings tab, from Boot Options select enable boot menu
connect the CD/DVD-Rom to the GParted image
boot the VM, press F12 to enter boot menu and select boot from CD/DVD
GParted will start, extend /dev/vda to maximum size, apply changes, reboot (you will be prompted for disconnecting the GParted image from CD/DVD, and press Enter)
boot the machine normally, once it’s up connect as root and:
extend the LVM volume (we keep couple of GBs free):
lvextend -L+48G /dev/vgroot/lvroot
resize the filesystem (it is ext4 in our case):
resize2fs /dev/vgroot/lvroot
reboot
Verify the filesystem has grown by (nearly) 50G, enjoy!