Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Expand Qcow2 Root Filesystem

When your KVM guests run out of disk storage, you have two options. Strap on another disk (easy), or resize your root partition which is somewhat harder, but this tutorial will show you how to do.

Initially I tried to resize my guest disk using virt-resize, but I hit a snag and didn't want to have to run virt-resize with sudo privileges. I didn't see why it would need to access my hypervisors kernel.


Shutdown the guest

virsh shutdown $GUEST_ID

Resize the disk to add the amount of storage you desire:

qemu-img resize my-guest-disk.qcow2 +20G

This won't work if the disk image has internal snapshots which virsh does not remove (at least in 16.04). You need to use the relevant qemu-img commands to remove the snapshots.

Start the guest.

virsh start $GUEST_ID

login to the guest (all commands going forward will assume you are inside the guest).

virsh console $GUEST_ID

Enter the interactive fdisk tool for our disk.

sudo fdisk /dev/vda

Print out the partition table if you like. This can be useful.


Now we are going to delete all the existing partitions before recreating the root one (don't worry this works).

For each partition, delete it by pressing:


... and then entering the partition number. Repeat until all partitions are gone.


Create the root partition with:


Make it a primary partion


Accept the defaults if you wish for the root partition to fill the entire disk. If you get asked whether you wish to delete a signature, I did when testing and it still worked.

Make the partition bootable:


Write changes and quit:


If you feel you made a mistake and want to quit out without saving changes, use q.

Tell the system to perform a filesystem check next boot:

sudo touch /forcefsck

If you deleted a swap partition (or any other partitions that auto mount), make sure to remove them from /etc/fstab.

sudo vim /etc/fstab

If you accidentally forget this step, don't panic, you will just have to wait a loooong time for the VM to boot up.

Now reboot for the changes to take effect.

sudo reboot

Don't panic if it takes a little longer for your guest to boot up than normal.

Once your guest has rebooted and you have logged in again, resize the filesystem to fill the partition:

sudo resize2fs /dev/vda1