Resizing a qemu image root disk partition

May 14, 2021 0 By addshore

Recently I found myself altering some virtual images for loading onto a qemu machine. I wanted to increase the disk space on the root partition, but couldn’t find any straightforward guides. So here is a little guide for future me, and anyone else.

Install libguestfs-tools

libguestfs is a set of tools for accessing and modifying virtual machine (VM) disk images. You can find details in the docs.

apt-get install libguestfs-toolsCode language: JavaScript (javascript)

Create a new resized image

First you need to create a new empty image of the size that you want.

For me that is 20GB

truncate -s 20G ./out.img

Then use virt-resize to expand the existing disk to fill all of the space in the new image that we created. (virt-resize docs)

virt-resize --expand /dev/sda1 ./vm.img ./out.img

Verify it worked

libguestfs-tools also provides a way to view file system information by only using the image file. (virt-filesystems docs)

virt-filesystems --long --parts --blkdevs -h -a ./out.img

You’ll see something like this:

Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   20G   /dev/sda
/dev/sda   device     -    20G   -Code language: PHP (php)