KVM/QEMU: Reduce the size of your VM files

By | 11th November 2019

When virt-manager creates new images by default they will take the full size assigned to the virtual storage on the VM.

If your host system is using a filesystem with built-in compression like ZFS or BTRFS it won’t affect the amount of free space left on the host.

But if you aren’t or want to copy the VM to another host, then you would like to reduce its size.

You can remove unneeded files/defragment/zero the empty space of the filesystem depending on the OS the VM is running before to help the file reduction.

For comparison purposes you can check the internal disk size before the optimisation and also after with the following command:

# qemu-img info linux-vm.qcow2
image: linux-vm.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.6G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

Stop the VM and then process the VM file:

# qemu-img convert -O qcow2 linux-vm.qcow2 linux-vm-compressed.qcow2

Depending on the size of the image and the machine running the command it can be time consuming, but the size reduction is worth it.

An alternative way of reducing the VM size is by using virt-sparsify.

Install the libguestfs-tools package.

[CentOS 7]
yum install libguestfs-tools

[CentOS 8]
dnf install libguestfs-tools

[Ubuntu/Debian]
# apt install libguestfs-tools

Stop the VM and then process it with:

# virt-sparsify --in-place linux-vm.qcow2

The –in-place option won’t create a new file and will compress the existing one.

image_printPrint this page

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.