Durch die bereitgestellte IMG-Datei wird beim Löschen von Dateien kein Speicherplatz freigegeben.

Durch die bereitgestellte IMG-Datei wird beim Löschen von Dateien kein Speicherplatz freigegeben.

Ich muss mehr Inodes verwenden, als normalerweise auf den meisten Dateisystemen verfügbar sind. Daher erstelle und mounte ich ein ext4-Dateisystem mit einer benutzerdefinierten Inode-Einstellung:

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

Dadurch wird eine 5000 MB große Datei auf der Festplatte erstellt (über ls /mnt), aber wenn ich sie überprüfe, dfwird angezeigt, dass die Festplatte nicht belegt ist (der Speicherplatz erhöht sich nicht um 5000 MB). Ich vermute, dass dies daran liegt, dass die Festplatte auf Null gesetzt ist und es so aussieht, als wäre ungenutzter Speicherplatz vorhanden.

Wenn die Festplatte voll ist, habe ich ein Programm, das versucht, die zuletzt verwendeten Dateien zu löschen, bis der Speicherplatzmangel behoben ist. Nun, es stellt sich heraus, dass trotz des Löschens der Dateien in /mntimmer noch Speicherplatz auf meiner Festplatte belegt wird, zumindest laut dfund anderen Systemaufrufen zur Festplattenfreigabe.

Muss ich bei rmden Dateien auf dieser virtuellen Festplatte auf eine besondere Weise registrieren, dass Speicherplatz frei ist, oder muss ich den freien Speicherplatz auf eine nicht standardmäßige Weise abfragen?

Bearbeiten: Beachten Sie beim vollständigen Befehl und der Ausgabe, dass die Festplattennutzung /dev/vda1nicht um 5000 MB ansteigt.

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

Antwort1

Sie können Folgendes verwenden 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

Der ungenutzte (im Hinblick auf das Dateisystem auf dem Image) Speicherplatz wird in "Löcher" umgewandelt (in derSparse-DateiSinn).

Wie Sie vielleicht schon vermutet haben, können Sie -o discardauch mit mounten – beachten Sie nur, dass das Ergebnis möglicherweise etwas verzögert auftritt.

verwandte Informationen