free up not used space on a qcow2-image-file on kvm/qemu
The image will not shrink automatically, since when you delete files, you don't actually delete data (this is why undelete works). Qemu has a facility to shrink qcow2 images back, but what the utility does is really deduplicate the zeroes from the disk, leaving all other information intact. So the idea would be to:
- Zero-fill the drive (
dd if=/dev/zero of=/some/fileuntil you run out of space)
- delete /some/file
- shut down the VM
- cd to where the images for the VM are kept and run
qemu-img convert -O qcow2 original_image.qcow2 deduplicated_image.qcow2
- change the VM settings to use the new deduplicated_image.qcow2, test the VM is working, and remove the old image
This, afaik, will only work with qcow2 images, I haven't tested other formats.
virt-sparsify can do all this with less hassle on your part: http://libguestfs.org/virt-sparsify.1.html
In addition to the accepted answer (which describe the more common, general method to shrink a qcow2 file), modern version (ie: RHEL7+) of the libvirt/qemu/qcow2 stack supports the
virtio-scsi driver which supports the
If the virtual machine uses
virtio-scsi and the libvirt definition include
discard='unmap', a simple
fstrim on the guest filesystem will release assigned-but-unused free space on the host. This can be checked with
du -hs <guestdisk> on host side (note:
ls -al <guestdisk> will not show the real allocated size, just the logical one).
For even newer libvirt/qemu instances (ie: RHEL8+), guest file size can be reduced even if the guest OS does not support trim: by enabling both
discard_zeroes='unmap' (and using the
virtio-scsi driver), writing a sequential stream of zero on guest side (ie: via
dd if=/dev/zero of=/zero.img bs=1M count=1024) will trigger host-size trimming of the affected LBA ranges. However, please note that this can be CPU-intensive for the host (which had to "parse" any guest write searching for repeating zeroes), so it should be only enabled in specific cases.
Both methods will reduce the physical allocated size (what
du -hs shows). For reducing the logical size (what a simple
ls -al gives) you need to use
qemu-img (as described in the other answers).
You can use virt-sparsify for this. It automatically zeros and sparsifies free space on most filesystems it finds within the source disk image (supports ext2/3/4, btrfs, NTFS, etc) .
Step-by-step instructions for in-place conversion
Shut down the VM
Keep a backup
virt-sparsify --in-place disk.img
Step-by-step instructions for conversion to another file (this is safer but requires more free space):
Shut down the VM
virt-sparsify disk.img new-file.img
Swap old with new image file:
mv disk.img disk.img.bak && mv new-file.img disk.img
If VM starts normally you can remove the backup:
P.S.: You can also convert between formats when you use virt-sparsify:
virt-sparsify disk.raw --convert qcow2 disk.qcow2