I’ve created a sparse file
filesystem.img, formatted in with
cryptsetup luksFormat, created a btrfs filesystem on it. The image file disk usage expanding fine while adding files to the btrfs filesystem. However deleting a file on it of course do not reduce sparse file disk usage, so I need a solution to do it manually.
fstrim does not work, saying
the discard operation is not supported.
I can’t just write zeros by ‘dd’ or ‘freezero’ to a filesystem’s file since encrypted zeros are not zeros and this will result into enlarging, not reducing image size.
I probably could resize the filesystem to it’s minimal size and then truncate the image file to the filesystem size + luks offset size, but I found that btrfs is very shrink-unfriendly, currently
btrfs filesystem usagereports ~23G free and ~81G used but I can’t reduce it further, so I have ~28% overusage.
‘btrfs balance’ probably would help, but looks like it could runs even longer than recopying of all data to new image.
The last of course a solution but not a good one. And it is not always possible to create a new disk image of required space.
I tried to find how ‘decoded zeros’ looks like by creating same passphrase-encrypted zero-empty image, but each of 512 byte block (the size reported by cryptosetup status) is different. Looks like luks do not crypt each block with the same key.
Is there any other ideas?
The problem here is that you’ve used a disk image file to store your filesystem. This severely limits the means by which you could make this file sparse again.
virt-sparsify can make the file sparse, but requires that it is not in use, so you would have to shut down the VM that uses the image before running
virt-sparsify on it.
In future, if you have a need to discard blocks online, use a backing store which supports this, such as ZFS, btrfs, or LVM thin provisioning.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.