This probably applies to normal ext4 filesystems as well as LVM ones.
This post is because I deleted 60Gb of data from a qcow2 disk, and when I did a compress only got the disk image to shrink about 5Gb. Obviously something was not working as I expected.
In the past my qcow images used only ext4 filesystems so my initial thought was “bugger, why did I use LVM”… but then I started thinking. What is available to compress ?.
Silly me… if you delete 60Gb of data of course you will not get space back, deleting a file only removes indexes (vtoc entries, pointer to the file etc) and the actual data is still clogging the disk until it is overwritten; a qemu-img compress won’t be able to get data back while that junk is still left there.
I wrote zeros to the free space on the disk… as it was a live running system I tried to guestimate a safe filesize, I guessed wrong and filled the filesystem, thats life.
[mark@vosprey2 ~]$ dd if=/dev/zero of=junk count=1000000 bs=1024000 dd: error writing ‘junk’: No space left on device 80188+0 records in 80187+0 records out 82112401408 bytes (82 GB) copied, 28142.7 s, 2.9 MB/s [mark@vosprey2 ~]$ df -k ./ Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/fedora-root 99653104 94338024 229896 100% / [mark@vosprey2 ~]$ rm junk [mark@vosprey2 ~]$ df -k ./ Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/fedora-root 99653104 14149764 80418156 15% / [mark@vosprey2 ~]$
Then shutdown the VM and use qemu-img to compress the qcow2 disk image. Well that certainly resolved my problem.
[root@vmhost1 VM_disks]# ls -ltr osp* -rw-r--r--. 1 root root 68162533376 Sep 9 22:17 osprey_bkp_aftercompress.qcow2 < == compress of a 75Gb disk before I wrote zeros -rw-r--r--. 1 root root 93995008000 Sep 11 18:12 osprey_precomp.qcow2 <== filesize after writing zeros filled the disk -rw-r--r--. 1 root root 11430384640 Sep 11 18:54 osprey.qcow2 <== size after compress of zero filled filesystem
Well. I got well over the 60Gb I expected back.
So qemu-img compress works great, if you do a little manual work before hand to make sure it is able to compress :-).