Anyone who has worked with QEMU on libvirtd even a little while gets to know the qemu-img utility pretty well. What users of KVM may not know about is virt-resize.
Say you want to add 30 GB of space to the disk for a virtual machine. How do you "grow" the virtual disk?
Easy, use qemu-img resize
:
$ sudo qemu-img resize /var/lib/libvirt/images/mydisk.qcow2 +30G
At this point the disk "hardware" is an extra 30 GB in size, but the virtual machine filesystems that sit atop it are unchanged. Traditionally, the way to deal with this is to mount the disk on a different system and use a disk partitioning utility like gparted to manage the sizes of the partitions on the disk. This may still be the best approach when you have multiple partitions to resize, because as a graphical tool gparted makes it easy to visualize the changes being made.
But if you're using libvirt/kvm and you're just looking to grow a specific partition, you can skip that step and
use virt-resize
on your kvm host to resize paritions on the virtual disks of guests.
Here's the procedure:
- Resize the virtual disk with
qemu-img resize
. - Make a copy of the virtual disk file,
cp mydisk.qcow2 mydisk-copy.qcow2
. - Run virt-resize on the virtual disk, specifying the partition you want to grow with the extra space available:
$ virt-resize --expand /dev/sda2 mydisk-copy.qcow2 mydisk.qcow2
Check out the documentation for virt-resize for how to specify the amounts of free space to allocate to a given partition, as well as how to shrink partitions (where the filesystem allows shrinking).