A step-by-step guide to resizing a QEMU virtual machine image when the root partition becomes too small.

The Problem

Your QEMU virtual machine is running out of disk space, and you need to expand the virtual disk image to accommodate more data.

Solution Overview

The process involves three main steps:

  1. Resize the image file on the host
  2. Modify the partition table inside the VM
  3. Resize the filesystem

Step 1: Resize the Image on Host

First, resize the QEMU image file (run this on the host system):

qemu-img resize vm_image.qcow2 +10G

Replace vm_image.qcow2 with your actual image filename and adjust the size as needed.

Step 2: Modify Partition with fdisk

Boot your VM and run the following commands as root inside the guest system:

fdisk /dev/vda

In fdisk, perform these steps:

  1. Print current partition table: Type p to see current partitions
  2. Delete the partition: Type d and select the partition number (usually 1)
  3. Create new partition: Type n for new partition
  4. Select primary: Type p for primary partition
  5. Use same start sector: Press Enter to use the same starting position
  6. Use all space: Press Enter to use all available space
  7. Write changes: Type w to write the partition table

Important: Make sure to use the same starting sector as the original partition!

Step 3: Reboot the System

Reboot your virtual machine to ensure the kernel recognizes the new partition size:

reboot

Step 4: Resize the Filesystem

After rebooting, resize the filesystem to use the additional space:

resize2fs /dev/vda1

Replace /dev/vda1 with your actual device name if different.

Verification

Check that the resize was successful:

df -h

You should now see the increased disk space available.

Important Notes

  • Always backup your VM before performing these operations
  • Replace vm_image.qcow2 and /dev/vda with your actual system values
  • All commands must be run with root permissions
  • The exact device names may vary depending on your system configuration

This procedure works for most Linux distributions running in QEMU/KVM virtual machines with ext2/ext3/ext4 filesystems.