Notes
The inode
On a Linux filesystem, a file is really an inode, a record with a number. It stores the owner, group, permissions, size, timestamps, and where the data blocks are on disk. What it does not store is the name. The name lives in the folder, which is just a list mapping names to inode numbers. Our test file a dot txt pointed to inode three five one five one seven two, six bytes of data.
a.txt inode=3515172 links=1 size=6 the inode stores: owner, group, permissions, size, times, where the data blocks are the inode does NOT store the file's name
Hard links
A hard link is a second name for the same inode. After ln, both a dot txt and hard dot txt show the same inode number, and the link count is two. Remove a dot txt, and hard dot txt still prints hello: the data is freed only when the link count drops to zero. Hard links only work within one filesystem, and not for folders.
a.txt inode=3515172 links=2 hard.txt inode=3515172 links=2 $ rm a.txt $ cat hard.txt hello data is freed only when the last link is gone
Symbolic links
A symbolic link, or symlink, is different. It is its own small file whose content is a path. Ours had its own inode and a size of five bytes: the five letters of a dot txt. After we removed a dot txt, the symlink broke: cat said no such file. Symlinks can cross filesystems and point to folders, which is why releases often use one: a current link that points to the newest version folder, switched in one step.
soft.txt inode=3515187 links=1 size=5 (5 = the bytes of "a.txt") after rm a.txt: $ cat soft.txt cat: soft.txt: No such file or directory
Mounts
A mount attaches a filesystem into the tree. We made a two hundred megabyte file, turned it into a disk with losetup, formatted it as ext four, and mounted it at mount lab. d f reported a hundred and seventy two megabytes, with a hundred and fifty eight available: the rest is filesystem bookkeeping plus space reserved for root. findmnt lists every mount, including slash proc and slash sys from the last episode.
$ truncate -s 200M disk.img $ losetup -f --show disk.img /dev/loop0 $ mkfs.ext4 -q /dev/loop0 $ mount /dev/loop0 /mnt/lab $ df -h /mnt/lab 172M size 24K used 158M avail $ findmnt # every mount
Out of inodes
Here is the second puzzle. ext four decides how many inodes a filesystem has when you format it. Our small disk got fifty one thousand two hundred. Every file uses one. Millions of tiny files, like cache entries or session files, can use up every inode while gigabytes of space are still free, and the error says no space left on device. d f minus i shows inode use. Always check it when d f minus h looks fine.
Inodes IUsed IFree IUse% 51200 11 51189 1% ext4 fixes the inode count when you format millions of tiny files (cache, sessions, mail) can use them all: "No space left on device" with GB free
Deleted but still full
And the first puzzle. A process wrote a hundred megabytes to a log and kept it open. We deleted the file. The folder was empty, but d f still showed a hundred and one megabytes used: the name was gone, but the open file held the inode alive. lsof plus L one lists open files with no links left, and it showed our Python process holding the deleted log. When the process exited, usage dropped to twenty four kilobytes.
a process writes 100 MB to /mnt/lab/big.log and keeps it open $ rm /mnt/lab/big.log $ df -h → still 101M used $ lsof +L1 python3 1110 ... 104857600 /mnt/lab/big.log (deleted) process exits → 24K used
Fix it in production
In production, find the holder with lsof plus L one. The clean fix is to restart or reload that process so it closes and reopens its logs. If you cannot restart it, you can empty the file through its descriptor under slash proc. To prevent it, rotate logs properly, either by copying and truncating, or by signalling the program to reopen its log, which episode nineteen shows.
- find: lsof +L1
- best: restart or reload the process so it reopens logs
- or truncate through /proc: : > /proc/<pid>/fd/<n>
- prevent: logrotate with copytruncate, or a reopen signal (episode 19)
Common mistakes
Mistakes. Deleting a big log that a process still has open and expecting the space back. Checking free space but not free inodes. Trying to hard link across filesystems, which cannot work. Relative symlinks that break when the folder moves. And mounting a disk by hand without adding it to fstab, so it is missing after the next reboot.
- rm a big open log and expect space back
- checking df -h but not df -i
- hard links across filesystems (not possible)
- relative symlinks that break when moved
- mounts not in /etc/fstab: gone after reboot
Say it like this
In the interview: a file is an inode, and names in folders are links to it. Its space is freed only when no name and no open file handle remain. Here a process still holds the deleted log open, so I find it with lsof plus L one, then restart or reload it, or truncate the file through slash proc. I would also check d f minus i, because running out of inodes gives the same error with free space left.
- "A file is an inode; names are links to it. The space is freed
- when no name and no open handle
- remains. A process still holds
- the deleted log, so I run
- lsof +L1, then restart it or truncate via /proc/pid/fd. I
- also check df -i for inodes."