Score:0

Mounted .img file does not clear up disk space when files are deleted

cn flag

I have a requirement to use more inodes than normally available on most file systems. Therefore, I create and mount a ext4 filesystem with a custom inode setting:

dd if=/dev/zero of=loop0.img bs=1MB count=5000
losetup --find --show `pwd`/loop0.img
mkfs -t ext4 -i 1024 /dev/loop0
mount /dev/loop0 /mnt

This will create a 5000MB file on the disk (via ls /mnt), but when I check using df it shows as the disk is not occupied (disk space does not go up by 5000MB ). I suspect because it's zeroed out on the disk and looks like unused space.

As the disk gets full, I have a program that tries to delete least-recently-used files until the disk space pressure is removed. Well it turns out that despite deleting the files in /mnt, it still registers as taking up space on my disk, at least according to df and other disk free system calls.

So is there a special way I need to rm the files on this virtual disk in order to register that the space is free? or do I need to inquire about free space in a non-standard way?

Edit: The full command and output, notice that the disk usage for /dev/vda1 does not increase 5000MB.

root@localhost:~# df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              490064       0    490064   0% /dev
tmpfs             101092    3188     97904   4% /run
/dev/vda1       19343152 2699088  15660656  15% /
tmpfs             505448       0    505448   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             505448       0    505448   0% /sys/fs/cgroup
tmpfs             101088       0    101088   0% /run/user/0
root@localhost:~# dd if=/dev/zero of=loop0.img bs=1MB count=5000
5000+0 records in
5000+0 records out
5000000000 bytes (5.0 GB, 4.7 GiB) copied, 6.20117 s, 806 MB/s
root@localhost:~# losetup --find --show `pwd`/loop0.img
/dev/loop0
root@localhost:~# mkfs -t ext4 -i 1024 /dev/loop0
mke2fs 1.44.5 (15-Dec-2018)
Discarding device blocks: done
Creating filesystem with 1220703 4k blocks and 4884000 inodes
Filesystem UUID: 4f308a54-6ddd-4ef6-b685-c193dfec8b84
Superblock backups stored on blocks:
        8176, 24528, 40880, 57232, 73584, 204400, 220752, 400624, 662256,
        1022000

Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

root@localhost:~# mount /dev/loop0 /mnt
root@localhost:~# df
Filesystem     1K-blocks    Used Available Use% Mounted on
udev              490064       0    490064   0% /dev
tmpfs             101092    3216     97876   4% /run
/dev/vda1       19343152 2769404  15590340  16% /
tmpfs             505448       0    505448   0% /dev/shm
tmpfs               5120       0      5120   0% /run/lock
tmpfs             505448       0    505448   0% /sys/fs/cgroup
tmpfs             101088       0    101088   0% /run/user/0
/dev/loop0       3594900   45080   3289556   2% /mnt
Tom Yan avatar
in flag
Note that when you use `dd if=/dev/zero` without `conv=sparse` to create the image, how much the filesystem usage (of the filesystem that consists of the image) will be increased is probably filesystem-specific. If you would like the image to be a sparse file anyway, you can simply use `truncate` to create it.
Score:0
in flag

You can use fstrim:

$ rm /tmp/test.img
rm: cannot remove '/tmp/test.img': No such file or directory
$ truncate -s 1G /tmp/test.img
$ sudo losetup --show -f /tmp/test.img 
/dev/loop0
$ sudo mkfs.ext4 -E root_owner=1000:1000 /dev/loop0
mke2fs 1.46.2 (28-Feb-2021)
Discarding device blocks: done                            
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 70f9b205-0ada-43b1-8636-36983ad79394
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

$ sudo mount /dev/loop0 /tmp/meh/
$ openssl enc -pbkdf2 -aes-256-ctr -in /dev/zero -pass file:/dev/urandom -nosalt 2>/dev/null | dd of=/tmp/meh/fill iflag=count_bytes count=768M
1572864+0 records in
1572864+0 records out
805306368 bytes (805 MB, 768 MiB) copied, 2.15669 s, 373 MB/s
$ sudo umount /tmp/meh/
$ sudo mount /dev/loop0 /tmp/meh/
$ rm /tmp/meh/fill 
$ sudo umount /tmp/meh/
$ sudo mount /dev/loop0 /tmp/meh/
$ du -h /tmp/test.img 
802M    /tmp/test.img
$ sudo fstrim -v /tmp/meh/
/tmp/meh/: 973.4 MiB (1020678144 bytes) trimmed
$ du -h /tmp/test.img 
33M /tmp/test.img

The unused (in terms of the filesystem on the image) space will be converted into "holes" (in the sparse file sense).

As you may have guessed, you can mount with -o discard as well -- just note that the result might be slightly delayed.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.