Skip to content

Instantly share code, notes, and snippets.

@ekawahyu
Last active March 9, 2025 10:37
Show Gist options
  • Save ekawahyu/105b0bf96dfee5745e3a17746ff19af5 to your computer and use it in GitHub Desktop.
Save ekawahyu/105b0bf96dfee5745e3a17746ff19af5 to your computer and use it in GitHub Desktop.
Resize QEMU filesystem using qemu-img and fdisk

Debian 12 Bookworm running on QEMU

Initially, it had qcow2 image size of 16GB, and this is how to double its size to 32GB:

host $  qemu-img resize diskimage.qcow2 +16G

Boot into Debian 12 and run fdisk with root privilege:

$ su
$ /usr/sbin/fdisk /dev/vda

Example of partition list:

Device        Start      End  Sectors  Size Type
/dev/vda1      2048  1050623  1048576  512M EFI System
/dev/vda2   1050624 31606783 30556160 14.6G Linux filesystem
/dev/vda3  31606784 33552383  1945600  950M Linux swap

The unallocated space is after /dev/vda3. So, we need to move /dev/vda3 to the very end by deleting it (d) and recreate it (n). Keeping the sector size the same, calculate sector start from the given sector end. Modify partition type (t) to swap. The result should look like the following:

Device        Start      End  Sectors  Size Type
/dev/vda1      2048  1050623  1048576  512M EFI System
/dev/vda2   1050624 31606783 30556160 14.6G Linux filesystem
/dev/vda3  65163231 67106830  1943600  950M Linux swap

Now delete (d) /dev/vda2 and recreate (n) with the same sector start and expand it all the way to the beginning of swap. DO NOT reset/alter/delete when prompted, answer NO to keep it as is! The result should like like follows:

Device        Start      End  Sectors  Size Type
/dev/vda1      2048  1050623  1048576  512M EFI System
/dev/vda2   1050624 65163230 64112607 30.6G Linux filesystem
/dev/vda3  65163231 67106815  1943585  949M Linux swap

Write the new partition table to disk (w) and exit fdisk (q). Reboot the machine. This time it boots up slow because /dev/vda2 and /dev/vda3 may have new UUID. We will fix it later. Now resize /dev/vda2:

$ /usr/sbin/resize2fs /dev/vda2

Let's properly reformat the swap:

$ mkswap /dev/vda3

Use blkid to get new partition UUIDs:

$ /usr/sbin/blkid

Fix UUIDs in two places:

  • /etc/fstab, fstab should have correct /dev/vda2 and /dev/vda3 UUIDs.
  • /etc/initramfs-tools/conf.d/resume, this needs to have the correct swap UUID.

Fixing startup:

$ grub-install /dev/vda

Fix initramfs for all kernel version:

$ update-initramfs -u -k all

Copy the new vmlinuz and initrd.img to user home directory:

$ cp /boot/vmlinuz /home/<user>
$ cp /boot/initrd.img /home/<user>

Use secure copy to get both vmlinuz and initrd.img to local directory. They can then be use start QEMU:

host $  scp <user>@<ipaddr>:/home/debian/vmlinuz ./
host $  scp <user>@<ipaddr>:/home/debian/initrd.img ./

Power off machine, and now it should boot as normal with expanded filesystem.

Troubleshooting: For any error, you may need to export the proper PATH:

$ export PATH=/usr/sbin:$PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment