DEVOPS, SRE AND CLOUD · PART 1 · LINUX, FROM THE INSIDE · FILES · 11 OF 187

Linux Inodes, Hard and Soft Links, and Mounts

What a file really is: an inode holding the data and metadata, and names that point to it. Hard links share one inode (in our test the same inode 3515172, link count 2); symbolic links point to a name and break when it goes. Mounts attach filesystems into the tree; we mounted a 200 MB ext4 image with 51,200 inodes. Plus the classic outage: a deleted log still filling the disk because a process holds it open.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Inode = the file
  • hard link = another name
  • symlink = a path
  • mount = attach a filesystem
  • lsof +L1
  • df -i

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.

stat a.txt (our run)
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

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.

Our 200 MB test disk
$ 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.

df -i /mnt/lab
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.

Our test
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.

Free the space
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is an inode?

The on-disk record of a file: owner, group, permissions, size, timestamps and the location of its data blocks. It does not contain the file name; directories map names to inode numbers.

Q2What is the difference between a hard link and a symbolic link?

A hard link is another name for the same inode (same filesystem, not directories, data stays until the last link goes). A symlink is a separate file containing a path; it can cross filesystems and point to directories, and breaks if the target is removed.

Q3Why is the disk still full after deleting a large log?

A process still has the file open, so the inode and its blocks stay allocated. Find it with lsof +L1, then restart/reload the process or truncate via /proc/<pid>/fd/<n>.

Q4What does "No space left on device" with free space mean?

Often the filesystem has run out of inodes (check df -i), typically from huge numbers of small files.

Q5What is a mount?

Attaching a filesystem (a disk, partition, network share or virtual filesystem) at a folder in the single directory tree; findmnt lists them, /etc/fstab makes them permanent.

Q6Why does df show less space than the disk size?

Filesystem metadata (inode tables, journal) uses some space, and ext4 reserves about 5% for root by default.

Q7Why do deployments use a "current" symlink?

Switching the symlink to a new release folder is a quick, single-step change, and rolling back is pointing it at the previous folder.

Q8How do you see a file's inode number?

ls -i shows it next to each name, and stat shows it with the link count, size and timestamps.