This is another way to decrease disk size of kvm virtual machines. we have another post which shows same thing through qemu-img command. But in this post we will see how can we reduce disk size of kvm virtual machine through virt-sparsify command which comes with libguestfs-tools in Linux (RedHat and centOS) Machine or Ubuntu machine as well.

I am using Ubuntu 18.04 Machine latest as of now in Sep 2018.

SetUP

Below are details of setup used within Lab

root@jarvis:~# lsb_release -d
Description:	Ubuntu 18.04.1 LTS

root@jarvis:~# uname  -r
4.15.0-33-generic

root@jarvis:~# kvm --version
QEMU emulator version 2.11.1(Debian 1:2.11+dfsg-1ubuntu7.4)
Copyright (c) 2003-2017 Fabrice Bellard and the QEMU Project developers

root@jarvis:~# virt-sparsify -V
virt-sparsify 1.36.13

virt-sparsify comes with libguestfs-tools package, in my machine i installed it with below command

# apt-get install libguestfs-tools

On RedHat and CentOS Machines , it would install with below command

# yum install libguestfs-tools

Now we have one template Virtual machine used to clone for new machine, that need to be update but after update, disk size would increase that should decrease after process, so we are using virt-sparsify for same like below.

NOTE

Please shutdown virtual Machine before using virt-sparsify on its disk

root@jarvis:~# qemu-img info /data/vm_images/templates/centos6_201802.img
image: centos6_201802.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.5G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

So above disk size is 1.5 GB. Now its always good to take a backup of disk size before using processing on it.

root@jarvis:~# virt-sparsify --in-place /data/vm_images/templates/centos6_201802.img
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[   6.4] Trimming /dev/VolGroup/lv_root
[   7.3] Clearing Linux swap on /dev/VolGroup/lv_swap
[   7.5] Trimming /dev/sda1
[   7.6] Sparsify in-place operation completed with no errors

Now virt-sparsify worked on disk and we hope it decrease some space on disk.

root@jarvis:/data/vm_images/templates# qemu-img info centos6_201802.img
image: centos6_201802.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.0G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

It’s not always possible to decrease disk size through virt-sparsify, it only when you have zeros space utilise on disk which was earlier used through some data in virtual Machine