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.
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.
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).
- Step 1. Inspect live mount types with findmnt so runtime logs and sockets go to /run (tmpfs) rather than wearing out raw flash.
- 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.
- Step 3. Audit staged rootfs symlinks with readlink and find -type l to ensure zero host-workspace paths leaked into target links.
- 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.
FHS 3.0 Layout, VFS Link Rules & Character/Block Major:Minor Numbers
Inside an embedded ARMv7-A rootfs, FHS 3.0 gives every top-level directory a strict lifecycle contract: /bin, /sbin, /lib, and /usr can be mounted completely read-only (ro SquashFS or ext4) to survive abrupt power cuts; /etc holds board configuration (fstab, inittab, nfs.conf); /var holds persistent mutable logs or package databases; and /run plus /tmp mount as RAM tmpfs so PID locks never persist across hard resets. Modern desktop distributions apply usrmerge (/bin -> usr/bin, /sbin -> usr/sbin, /lib -> usr/lib), so scripts must remain portable across both split and merged layouts.
At the VFS syscall layer, stat(path, &sb) follows symlinks to the terminal target (returning -1 with ENOENT on a dangling symlink!), whereas lstat(path, &sb) inspects the link inode itself (S_ISLNK(sb.st_mode) is true and sb.st_size equals the byte length of the stored target string!). Hard links (link(old, new)) are prohibited on directories (EPERM) so find and garbage collection never encounter reference cycles in the VFS tree.
Figure 2.1b. lstat() inspects the S_IFLNK inode directly instead of dereferencing host paths, catching broken rootfs links before flashing.
| Property | Hard Link (ln a b) | Symbolic Link (ln -s a b) | Device Node (mknod / devtmpfs) |
|---|---|---|---|
Inode (st_ino) | Identical st_ino & st_dev as a | New independent S_IFLNK inode | Independent S_IFCHR or S_IFBLK inode |
| Crosses filesystems? | No (EXDEV across mounts) | Yes (stores arbitrary path text) | N/A (dispatches via kernel st_rdev) |
| Links directories? | No (EPERM prevents VFS cycles) | Yes (e.g., /bin -> usr/bin) | No (represents stream or block hardware) |
If original a is unlinked | b still accesses all file data (st_nlink--) | b dangles (stat -> ENOENT; lstat succeeds) | Other nodes with same major:minor still reach driver |
learner@ksa:~/gl-e00/m02-l01$ bat -H 8 -H 11 stage_fhs_rootfs.sh File: stage_fhs_rootfs.sh #!/usr/bin/env bash set -euo pipefail STAGE="${PWD}/_stage_rootfs" mkdir -p "${STAGE}"/{bin,sbin,etc,lib,proc,sys,run,tmp,usr/bin,var/log} # 1. Portable relative symlink: sbin/init -> ../bin/busybox (survives host stage & target /) ln -snf ../bin/busybox "${STAGE}/sbin/init" # 2. Audit staged tree: fail build if any symlink points into host $HOME or build directory while IFS= read -r -d '' lnk; do tgt="$(readlink "${lnk}")" case "${tgt}" in "${HOME}"*|"${PWD}"*) echo "FATAL: host path in symlink ${lnk} -> ${tgt}" >&2; exit 1 ;; esac done < <(find "${STAGE}" -type l -print0)- Relative '../bin/busybox' resolves from sbin/ regardless of whether STAGE is mounted at /srv/nfs/rootfs or /.
- find -type l -print0 + readlink catches leaked host workspace paths before tar or NFS export.
lstat(2) GL-E00 Quick Reference lstat(2) NAME stat, lstat, link, symlink, makedev - VFS inode metadata, links, and device IDs SYNOPSIS #include <sys/stat.h> #include <sys/sysmacros.h> int stat(const char *pathname, struct stat *statbuf); int lstat(const char *pathname, struct stat *statbuf); dev_t makedev(unsigned int maj, unsigned int min); DESCRIPTION stat() dereferences symbolic links; if a symlink dangles, stat() fails with ENOENT. lstat() inspects the link inode itself (S_ISLNK(statbuf->st_mode) != 0). major(statbuf->st_rdev) and minor(statbuf->st_rdev) decode S_IFCHR / S_IFBLK drivers. ERRORS EXDEV link() oldpath and newpath reside on different mounted filesystems (st_dev). EPERM link() oldpath is a directory (forbidden to keep VFS dentry graph acyclic). Kernel Systems Academy Lesson 2.1 lstat(2) GL-E00 quick reference · full manual page: man 2 lstat Proving Hard Links, Dangling Symlinks & dev_t Encoding in C11
Let us verify these exact VFS behaviors on the Linux host in ISO C11. In verify.c, we create a private /tmp/ksa_vfs_XXXXXX sandbox, write a regular busybox file, create a hard link (bin_sh) via link(2), and create both a valid relative symlink (../bin/busybox) and a deliberately dangling symlink (missing_target) via symlink(2).
Notice how stat() on the dangling symlink fails with errno == ENOENT while lstat() succeeds and reports S_ISLNK(sb.st_mode) with st_size == 14 (exact length of "missing_target"). Finally, we encode i.MX6ULL eMMC partition 2 (major 179, minor 2) with makedev() and verify lossless round-trip extraction.
learner@ksa:~/gl-e00/m02-l01$ bat -H 29 -H 39 verify.c File: verify.c #define _DEFAULT_SOURCE #include <errno.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <sys/sysmacros.h> #include <unistd.h> int main(void) { char root[] = "/tmp/ksa_vfs_XXXXXX"; if (!mkdtemp(root)) return 1; char bin_dir[128], sbin_dir[128], bb_path[160], hlink_path[160], slink_path[160], dang_path[160]; snprintf(bin_dir, sizeof(bin_dir), "%s/bin", root); snprintf(sbin_dir, sizeof(sbin_dir), "%s/sbin", root); snprintf(bb_path, sizeof(bb_path), "%s/bin/busybox", root); snprintf(hlink_path, sizeof(hlink_path), "%s/bin/sh", root); snprintf(slink_path, sizeof(slink_path), "%s/sbin/init", root); snprintf(dang_path, sizeof(dang_path), "%s/sbin/broken", root); mkdir(bin_dir, 0755); mkdir(sbin_dir, 0755); int fd = open(bb_path, O_WRONLY | O_CREAT | O_TRUNC, 0755); if (fd < 0 || write(fd, "ELF_ARM", 7) != 7) return 1; close(fd); if (link(bb_path, hlink_path) != 0) return 1; if (symlink("../bin/busybox", slink_path) != 0) return 1; if (symlink("missing_target", dang_path) != 0) return 1; struct stat sb_bb, sb_hl, sb_sl_lstat, sb_sl_stat, sb_dang_stat, sb_dang_lstat; stat(bb_path, &sb_bb); stat(hlink_path, &sb_hl); lstat(slink_path, &sb_sl_lstat); stat(slink_path, &sb_sl_stat); errno = 0; int dang_stat_rc = stat(dang_path, &sb_dang_stat); int dang_errno = errno; int dang_lstat_rc = lstat(dang_path, &sb_dang_lstat); dev_t mmc_p2 = makedev(179U, 2U); unsigned int maj = major(mmc_p2); unsigned int min = minor(mmc_p2); printf("hard_link: same_ino=%d nlink=%lu\n", sb_bb.st_ino == sb_hl.st_ino, (unsigned long)sb_bb.st_nlink); printf("rel_symlink: is_lnk=%d target_follows_bb=%d\n", S_ISLNK(sb_sl_lstat.st_mode) != 0, sb_sl_stat.st_ino == sb_bb.st_ino); printf("dangling_link: stat_rc=%d(ENOENT=%d) lstat_rc=%d(len=%ld)\n", dang_stat_rc, dang_errno == ENOENT, dang_lstat_rc, (long)sb_dang_lstat.st_size); printf("dev_t_encode: mmcblk1p2 major=%u minor=%u\n", maj, min); unlink(dang_path); unlink(slink_path); unlink(hlink_path); unlink(bb_path); rmdir(sbin_dir); rmdir(bin_dir); rmdir(root); return (sb_bb.st_ino == sb_hl.st_ino && sb_bb.st_nlink == 2 && S_ISLNK(sb_sl_lstat.st_mode) && sb_sl_stat.st_ino == sb_bb.st_ino && dang_stat_rc == -1 && dang_errno == ENOENT && dang_lstat_rc == 0 && maj == 179U && min == 2U) ? 0 : 1; }- link() shares the exact st_ino with busybox (nlink=2), while symlink('../bin/busybox') stores a portable relative string.
- stat() fails with ENOENT on dangling symlinks, whereas lstat() inspects the S_IFLNK inode directly without following.
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-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-l01learner@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 -hreports0 Bfree on/var, you runrm /var/log/daemon.log(2 GiB),lsshows the file is gone, yetdf -hstill reports0 Bfree! - Root Cause:
rmcallsunlink(2), decrementingst_nlinkfrom1to0, but the running daemon still holds an openstruct filedescriptor (fd 3), soext4cannot free the blocks. - Fix: Identify the holder with
lsof +L1(open files withnlink < 1), then either truncate in place (: > /var/log/daemon.logbefore unlinking) or reload/restart the daemon (SIGHUP/systemctl kill -s HUP) so it closesfd 3.
Copying /dev, /proc, or /sys With Plain cp -r Without Archive Flags:
- Symptom: Copying a rootfs directory with
cp -rturns/dev/nullinto a multi-megabyte regular file and converts300BusyBox symlinks into300full1 MiBbinary copies (300 MiBflash bloat!). - Root Cause: Plain
cp -rdereferences symlinks and reads from character device nodes instead of preservingS_IFLNKandS_IFCHRmetadata. - Fix: Always copy rootfs staging directories with
cp -a(-dR --preserve=all) orrsync -aHAX, and keep/proc,/sys,/dev, and/runas 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
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.