How to reduce the size of KVM QCOW2 images that grow out of hand
This is a very short and practical article useful to anyone who uses KVM in production and somehow needs to reduce the space used by a QCOW2 disk image. It is not enough to just free up space from the VM itself. Once a QCOW2 grows in size it never goes back down, on its own accord.
To do this quickly and safely I recommend the following steps:
- Free up any space in the disk from within the VM
- Shutdown the VM
- Backup the disk image that you want to reduce(Optional, if have a lot of faith in Unix commands that you never heard about before )
- from the directory where your image is stored call:
virt-sparsify — in-place defenders-disk01.qcow2
- Once the command completes successfully, restart the VM
- Verify that the disk space usage went down using df -kh. Should work.