Resizing qcow2 partition with Libguestfs

One way to overcome issues on running bioinformatics tools is to have virtual machines with different distros and different versions of distros. In order to do that, keep a minimal installation of each distro/version of interest and then build something on top of that.

For me, one issue is the space occupied by the images. To deal with that, I have tried to generate “small” images that can serve as serve as template been replicated and resized for production.

Using virt-manager I have installed ubuntu-19.10-desktop-amd64 in a 15G qcow2 storage disk.

Quick search shows Libguestfs as one possibility to deal with that.

Libguestfs is:

a set of tools for accessing and modifying virtual machine (VM) disk images.

For my need, I have used libguestfs to check the qcow2 file and resize it.

Checking qcow2:

virt-filesystems --long \
-h --all \
-a ubuntu-19.10-desktop-amd64.qcow2

Name       Type        VFS   Label  MBR  Size  Parent
/dev/sda1  filesystem  ext4  -      -    15G   -
/dev/sda1  partition   -     -      83   15G   /dev/sda
/dev/sda   device      -     -      -    15G   -

Now, lets create a new disk:

qemu-img create \
-f qcow2 \
-o preallocation=metadata \
ubuntu-19.10-desktop-amd64__production.qcow2 50G

Formatting 'ubuntu-19.10-desktop-amd64__production.qcow2', fmt=qcow2 size=53687091200 cluster_size=65536 preallocation=metadata lazy_refcounts=off refcount_bits=16

and resize:

virt-resize \
--expand /dev/sda1 \
ubuntu-19.10-desktop-amd64.qcow2 \

[   0.0] Examining ubuntu-19.10-desktop-amd64.qcow2

Summary of changes:

/dev/sda1: This partition will be resized from 15.0G to 50.0G.  The 
filesystem ext4 on /dev/sda1 will be expanded using the ‘resize2fs’ 

[   5.1] Setting up initial partition table on ubuntu-19.10-desktop-amd64__production.qcow2
[   6.2] Copying /dev/sda1
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  42.6] Expanding /dev/sda1 using the ‘resize2fs’ method

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

Now, lets check the new qcow2 file:

virt-filesystems --long \
-h --all \
-a ubuntu-19.10-desktop-amd64__production.qcow2

Name       Type        VFS   Label  MBR  Size  Parent
/dev/sda1  filesystem  ext4  -      -    50G   -
/dev/sda1  partition   -     -      83   50G   /dev/sda
/dev/sda   device      -     -      -    50G   -


1- Libguestfs –

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s