Skip to main content

Convert qcow2 image of libvirt VM to thin provisioning

During testing with Talos Kubernetes deployments on Libvirt, I encountered an issue where created virtual machines used fully provisioned disks, which consumed the complete configured disk size on the host system.

Since most of my testing happens on my notebook with locally provisioned virtual machines, I prefer to use thin-provisioned disks to conserve disk space.

The easiest solution is to convert the qcow2 images using virt-sparsify, which is part of Libguesttools. A good reference is the Red Hat Documentation: 21.14. virt-sparsify: Reclaiming Empty Disk Space.

virt-sparsify <disk-image-to-convert> <name-of-new-image>
Warning

Power down the virtual machine before converting the disk images!

Example
#

# Run as root to convert system disk images
sudo su - 
cd /var/lib/libvirt/images
virt-sparsify cnx8-rb-master.qcow2 cnx8-rb-master_thin.qcow2
mv cnx8-rb-master.qcow2 cnx8-rb-master_full.qcow2
mv cnx8-rb-master_thin.qcow2 cnx8-rb-master.qcow2

Test the virtual machine before deleting the old disk!

Space Differences
#

ls -alh cnx8-rb-master*
-rw-r--r--. 1 root root  41G Dec 17 16:06 cnx8-rb-master_full.qcow2
-rw-r--r--. 1 root root 2.3G Dec 17 16:08 cnx8-rb-master_thin.qcow2

Delete Full Provisioned Disk
#

rm cnx8-rb-master_full.qcow2
Christoph Stoettner
Author
Christoph Stoettner
I work at Vegard IT GmbH as a senior consultant, focusing on collaboration software, Kubernetes, security, and automation. I primarily work with HCL Connections, WebSphere Application Server, Kubernetes, Ansible, Terraform, and Linux. My daily work occasionally leads to technical talks and blog articles, which I share here more or less regularly.

Related

Matrix Channel

·306 words·2 mins
The last months I played around with Matrix, a secure and open collaboration system. The protocol looks very promising, it allows hosting your own server and federate it to other systems. So like SMTP, all these matrix users can communicate to each other.