Can losetup be made efficient with sparse files?
7
votes
3
answers
2488
views
So my setup is like this.
$ truncate -s 1T volume
$ losetup -f --show volume
/dev/loop0
$ mkfs.ext4 /dev/loop0
$ ls -sh volume
1.1G volume
$ mount /dev/loop0 /mnt/loop
Now I have a 1.1TB volume, as expected. The overhead of ext4 expanded the sparse file to 1.1G, but that's fine. Now to add a file.
$ dd if=/dev/urandom of=/mnt/loop/file bs=1M count=10240
$ ls -sh volume
12G volume
Cool, now I don't want the file.
$ rm /mnt/loop/file
$ ls -sh volume
12G volume
The free space is still taking up space, as expected, and
$ fallocate -d volume
frees up 1gb.
My question is, how can I zero out the free space here without expanding the volume to the full size? $ dd if=/dev/zero
will expand it to full size, and with conv=sparse
makes it create a useless sparse file inside the volume.
TL;DR: Is there a way to make losetup
ignore writes of null blocks to null sectors, while allowing everything else?
Asked by Daffy
(465 rep)
Aug 23, 2018, 12:43 AM
Last activity: Apr 11, 2025, 11:46 PM
Last activity: Apr 11, 2025, 11:46 PM