Linux Filesystem Hierarchy (FHS 3.0), Inode Links & Device Nodes

Assembling an embedded root filesystem from scratch fails immediately when BusyBox symlinks encode host-absolute paths, /run and /proc mounts are confused with persistent flash directories, or character and block device major:minor numbers are misconfigured.

20 minfree previewLinux 6.12C11glibc 2.39→ module 2 lab
What you'll learn
  • Map FHS 3.0 directories (/bin, /sbin, /usr, /etc, /var, /run, /proc, /sys) to read-only flash, tmpfs RAM, and kernel pseudo-filesystems.
  • Contrast hard-link inode sharing (link, st_nlink, EXDEV) against symbolic-link path indirection (symlink, lstat vs stat) inside staged rootfs trees.
  • Decode character and block device nodes via makedev, major, and minor numbers for serial UARTs and MMC storage partitions.

Why Everything in Linux Looks Like a File Yet Lives in Different Worlds

When an ARMv7-A board mounts its root filesystem (/) at the end of Linux kernel boot, user space expects strict Filesystem Hierarchy Standard (FHS 3.0) invariants. Essential binaries (/bin/sh, /sbin/init) and the ELF dynamic loader (/lib/ld-linux-armhf.so.3) must exist inside / before any secondary partition mounts. Meanwhile, /run is a pure tmpfs RAM filesystem holding volatile PID files and Unix sockets that vanish on power-off, and /proc (procfs) plus /sys (sysfs) consume zero flash bytes—reading /sys/class/net/eth0/carrier invokes a live kernel show() callback on the spot!

Understanding how the Virtual Filesystem (VFS) binds names to inodes prevents two classic rootfs bugs. In a BusyBox rootfs, 300+ applets (/bin/ls, /sbin/ifconfig) are symlinks to one binary. If an engineer creates an absolute host symlink ln -s /home/dev/rootfs/bin/busybox ls, ls works on the host and fails with No such file or directory on the board because /home/dev/rootfs does not exist on target silicon! Conversely, /dev/ttymxc0 and /dev/mmcblk1p2 store major:minor driver IDs (st_rdev) rather than data blocks.

fig_m02_l01_a.svg — VFS unifies flash files, RAM tmpfs (/run), kernel…

Figure 2.1a. VFS unifies flash files, RAM tmpfs (/run), kernel pseudo-filesystems (/proc, /sys), and major:minor device nodes under one namespace.

mental model: Think of an inode (st_ino) as a numbered warehouse vault and directory entries (dentries) as catalog cards: a hard link prints a second catalog card stamped with the exact same vault number #4092 (the vault is emptied only after the last card is shredded and the last worker steps out of the vault), whereas a symbolic link is a sticky note on an empty box saying *'walk one aisle left to ../bin/busybox'*—if that note says *'go to the factory in Chicago'*, it breaks the moment you ship the crate to the lab!

Where it stops working: Vault numbers (ino_t) are unique only within one filesystem (st_dev)—both / (ext4) and /proc (procfs) can legitimately have an inode #2—which is why link(2) returns EXDEV (Invalid cross-device link) across mount points.

definition (Inode Reference Count (st_nlink) and Device ID (dev_t st_rdev)): Every POSIX file object is represented by a struct inode identified by (st_dev, st_ino). Regular files reclaim disk blocks only when both on-disk st_nlink == 0 and in-RAM open struct file count reach 0—explaining why deleting a huge log file (unlink) while a daemon still holds it open leaves df -h 100% full! For S_IFCHR and S_IFBLK device inodes, st_size is unused and st_rdev packs major (driver class, e.g., 179 mmcblk) and minor (partition index) via makedev(maj, min).

ast_m02_l01.svg — Keep rootfs symlinks relative, check st_dev+st_ino for…step 1/4
  1. Step 1. Inspect live mount types with findmnt so runtime logs and sockets go to /run (tmpfs) rather than wearing out raw flash.
  2. Step 2. Use ls -li and stat to verify whether two paths share one st_ino (hard link) or traverse a separate S_IFLNK inode.
  3. Step 3. Audit staged rootfs symlinks with readlink and find -type l to ensure zero host-workspace paths leaked into target links.
  4. Step 4. Confirm 'c'/'b' file type indicators and major, minor pairs exposed by devtmpfs under /dev.

Figure 2.1b. Keep rootfs symlinks relative, check st_dev+st_ino for hard links, and read st_rdev on /dev.

Tracing linkat, symlinkat, and newfstatat Flags in strace

Inspect the strace capture closely: glibc translates stat() and lstat() into the same newfstatat() syscall, passing 0 for stat() (follow symlink -> -1 ENOENT on broken) and AT_SYMLINK_NOFOLLOW for lstat() (inspect link inode -> 0!). Meanwhile, linkat() increments st_nlink to 2 with same_ino=1, and makedev(179, 2) decodes back to major=179 minor=2 cleanly.

learner@ksa: ~/gl-e00/m02-l01
learner@ksa:~/gl-e00/m02-l01gcc -std=c11 -Wall -Wextra -Werror verify.c -o verifylearner@ksa:~/gl-e00/m02-l01./verifyhard_link: same_ino=1 nlink=2rel_symlink: is_lnk=1 target_follows_bb=1dangling_link: stat_rc=-1(ENOENT=1) lstat_rc=0(len=14)dev_t_encode: mmcblk1p2 major=179 minor=2learner@ksa:~/gl-e00/m02-l01
strace — learner@ksa: ~/gl-e00/m02-l01
learner@ksa:~/gl-e00/m02-l01strace -e \trace=linkat,symlinkat,newfstatat,unlinkat ./verifynewfstatat(AT_FDCWD, "/tmp/ksa_vfs_OjbTMI/bin/busybox", {st_mode=S_IFREG|0755, st_size=7, ...}, 0) = 0newfstatat(AT_FDCWD, "/tmp/ksa_vfs_OjbTMI/bin/sh", {st_mode=S_IFREG|0755, st_size=7, ...}, 0) = 0newfstatat(AT_FDCWD, "/tmp/ksa_vfs_OjbTMI/sbin/init", {st_mode=S_IFLNK|0777, st_size=14, ...}, AT_SYMLINK_NOFOLLOW) = 0newfstatat(AT_FDCWD, "/tmp/ksa_vfs_OjbTMI/sbin/init", {st_mode=S_IFREG|0755, st_size=7, ...}, 0) = 0newfstatat(AT_FDCWD, "/tmp/ksa_vfs_OjbTMI/sbin/broken", 0x7ffc811ebe90, 0) = -1 ENOENT (No such file or directory)newfstatat(AT_FDCWD, "/tmp/ksa_vfs_OjbTMI/sbin/broken", {st_mode=S_IFLNK|0777, st_size=14, ...}, AT_SYMLINK_NOFOLLOW) = 0+++ exited with 0 +++

Rootfs Staging, Open-Unlinked Disk Leaks & Pseudo-FS Pitfalls

Mastering FHS lifecycles, relative symlinks, and dev_t semantics prepares you to build clean minimal root filesystems (BusyBox, Buildroot, Debian debootstrap) without boot-time init panics or mysterious flash exhaustion.

Disk Stays 100% Full After rm /var/log/daemon.log:

  • Symptom: df -h reports 0 B free on /var, you run rm /var/log/daemon.log (2 GiB), ls shows the file is gone, yet df -h still reports 0 B free!
  • Root Cause: rm calls unlink(2), decrementing st_nlink from 1 to 0, but the running daemon still holds an open struct file descriptor (fd 3), so ext4 cannot free the blocks.
  • Fix: Identify the holder with lsof +L1 (open files with nlink < 1), then either truncate in place (: > /var/log/daemon.log before unlinking) or reload/restart the daemon (SIGHUP / systemctl kill -s HUP) so it closes fd 3.

Copying /dev, /proc, or /sys With Plain cp -r Without Archive Flags:

  • Symptom: Copying a rootfs directory with cp -r turns /dev/null into a multi-megabyte regular file and converts 300 BusyBox symlinks into 300 full 1 MiB binary copies (300 MiB flash bloat!).
  • Root Cause: Plain cp -r dereferences symlinks and reads from character device nodes instead of preserving S_IFLNK and S_IFCHR metadata.
  • Fix: Always copy rootfs staging directories with cp -a (-dR --preserve=all) or rsync -aHAX, and keep /proc, /sys, /dev, and /run as empty mount-point directories in the image.

Guided Practice

Extend verify.c to open bb_path read-only (fd_hold = open(bb_path, O_RDONLY)), unlink both hlink_path and bb_path (st_nlink drops to 0), and call fstat(fd_hold, &sb) plus read(fd_hold, ...) to prove in C11 that data blocks stay allocated (sb.st_nlink == 0, sb.st_size == 7) until close(fd_hold) runs!

Independent Practice

Write a C11 directory scanner using opendir(), readdir(), lstat(), and readlink() that walks a staged rootfs path and exits non-zero if any symlink points outside the staging root via excessive .. parent traversals.

Verification Checks

quiz · 1 of 3

While staging an embedded BusyBox rootfs at /home/eng/rootfs, why does ln -s /home/eng/rootfs/bin/busybox /home/eng/rootfs/sbin/init cause a kernel panic on the ARMv7-A target?

quiz · 2 of 3

Why does ln /var/log/messages /home/backup/messages fail with Invalid cross-device link (EXDEV) when /var and /home are separate partitions?

quiz · 3 of 3

What do the numbers 179, 2 represent in brw-rw---- 1 root disk 179, 2 Nov 24 01:00 /dev/mmcblk0p2?

Key takeaways

  • FHS 3.0 separates essential boot binaries (/bin, /sbin, /lib) and static configuration (/etc) from volatile tmpfs runtime state (/run) and RAM-backed kernel pseudo-filesystems (/proc, /sys).
  • A hard link creates an additional directory entry pointing to the exact same inode number (st_ino) within a single mounted filesystem (st_dev) and increments st_nlink without duplicating data blocks.
  • A symbolic link allocates a distinct S_IFLNK inode storing a target path string, allowing cross-filesystem and directory links that must remain relative inside staged target root filesystems.
  • Character (S_IFCHR) and block (S_IFBLK) device inodes store no file payload blocks; their st_rdev field encodes major(dev) to select a Linux kernel driver and minor(dev) to select the instance or partition.
Progress is saved on this device.

search — fzf

0/0 ───────────────────────────────────────────

keyboard shortcuts — less

/
search
f
focus mode
[
previous
]
next
?
help
Esc
close

figure