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.
Steps
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
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.
p
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:
d
... and then entering the partition number. Repeat until all partitions are gone.
1
Create the root partition with:
n
Make it a primary partion
p
Accept the defaults if you wish for the root partition to fill the entire disk.
Make the partition bootable:
a
Write changes and quit:
w
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
Now reboot for the changes to take effect.
sudo reboot
Once your guest has rebooted and you have logged in again, resize the filesystem to fill the partition:
sudo resize2fs /dev/vda1
References
- LinuxConfig.org - How to resize ext4 root partition live without umount on Linux
- Unix & Linux - How do I expand a file system to fill a partition?
- The Geek Stuff - 7 Linux fdisk Command Examples to Manage Hard Disk Partition
- Github Gist - Resizing a filesystem using qemu-img and fdisk
- Redhat Docs - How to Resize a Partition using fdisk
First published: 16th August 2018