Skip to content
Visual explainer 2h 30m to learn 4 steps

The VFS, mounts, /proc & /sys

Explain how the VFS unifies different file systems and read kernel data from /proc.

By Invicly Tech, , in Linux System Programming. About 11 minutes to read.

Topic 12 of 42

In short

The Virtual File System (VFS) gives every file system the same interface, so open and read work identically on ext4, xfs, NFS or tmpfs. Mounting attaches a file system to a directory in the single directory tree. Pseudo file systems like /proc and /sys expose live kernel data as files you can read with cat.

Explained simply

Linux supports dozens of file systems: ext4 and xfs on disks, NFS over the network, tmpfs in memory, and more. Your program does not care which one it is talking to. The Virtual File System (VFS) is a common front desk: every request comes in through the same open, read and write, and the VFS passes it to the right driver.

There is only one directory tree, starting at /. Mounting attaches a whole file system to a directory in that tree. A USB stick might appear at /media/usb, and /tmp might be a RAM disk. If you mount over a directory that already has files, they are hidden until you unmount.

Some file systems have no disk at all. /proc and /sys are made up by the kernel on the spot. Reading /proc/meminfo is really asking the kernel a question, and the answer is written for you at the moment you read.

Many tools work this way. ps, top and free simply read files in /proc. Once you know where to look, you can get the same information from a script or a C program.

Press Play on a diagram to walk through it one step at a time.

LayersA read() through the VFS
  1. 1
    System call

    read(fd, buf, n) from user space.

  2. 2
    VFS

    Generic inodes, dentries and the page cache.

  3. 3
    File system driver

    ext4, xfs, nfs, procfs, tmpfs.

  4. 4
    Block layer

    Request queues and I/O scheduling.

  5. 5
    Device driver and disk

    NVMe, SATA or virtio.

CompareThree kinds of file system
Disk (ext4, xfs)
  • Data stored on a block device
  • Survives reboots
  • Cached in the page cache
  • Mounted from /etc/fstab or systemd
Memory (tmpfs)
  • Data in RAM, can be swapped
  • Lost at reboot
  • Size limit set with size=
  • Used for /dev/shm, /run, often /tmp
Pseudo (proc, sysfs)
  • Nothing is stored at all
  • Content made by the kernel at read time
  • Files often show size 0
  • Some files accept writes to change settings

In depth

The four VFS objects and path lookup

The VFS works with four kinds of objects. A superblock represents one mounted file system. An inode represents one file. A dentry (directory entry) links a name to an inode, and the kernel keeps recently used dentries in a cache so repeated lookups are fast. A file object is an open file, holding the offset and flags. Each file system driver supplies tables of functions, such as how to read a page or look up a name, and the VFS calls them.

To open /srv/app/conf.ini, the kernel walks the path one name at a time. At each directory it checks that you have search (x) permission, looks up the next name in the dentry cache or asks the driver, and notices when a directory is a mount point so it continues in the mounted file system. At the end it creates a file object and returns a descriptor.

FlowPath lookup for /srv/app/conf.ini
  1. 1
    Start at /

    The process's root directory

  2. 2
    Look up srv

    Check x on /, use the dentry cache

  3. 3
    Cross a mount

    If /srv is a mount point, continue in that file system

  4. 4
    Look up app, then conf.ini

    Check x on each directory

  5. 5
    Open the inode

    Check r on the file, create a file object, return an fd

Reading the mount table

/proc/self/mounts lists what the reading process can see, one mount per line: device, mount point, type, options and two zeros, the same format as /etc/fstab. /etc/mtab is normally just a symlink to it. /proc/self/mountinfo adds more detail: a mount ID, the parent mount, the device numbers and how mount events propagate between namespaces.

The list is per process because of mount namespaces. A container has its own namespace and sees its own set of mounts. findmnt shows the table as a tree, and findmnt -T path tells you which mount holds a given path. In C, getmntent from mntent.h parses the format for you.

Watch the options column. ro means read-only. nosuid ignores setuid bits, nodev ignores device files, and noexec refuses to run programs from that file system. relatime, the usual default, updates access times only occasionally to save writes.

  • findmnt -t ext4,xfs lists only real disk file systems
  • findmnt -T . shows the mount holding your current directory
  • mount -o remount,ro /data changes options in place

/proc/PID: a live view of each process

Every process has a directory /proc/PID, and /proc/self always points to the reader's own. status gives human-readable details. cmdline and environ hold NUL-separated strings, and environ is readable only by the owner or root. exe and cwd are symlinks to the program file and the working directory. fd lists open descriptors, and fdinfo gives each one's offset and flags. maps shows the memory layout, limits the resource limits, and io the bytes read and written.

These files show size 0 in ls, yet return text when read, because the content is generated during the read. Read until end of file and never trust st_size. A process can exit while you are reading its directory, so tools must handle ENOENT and ESRCH. Some files, such as maps and environ, need ptrace-level access to another user's process, and a hidepid mount option can hide other users' processes entirely.

MapUseful entries in /proc/PID
/proc/1234
  • Identity
    • status
    • cmdline
    • exe (symlink to the program)
  • Files
    • fd/
    • fdinfo/
    • cwd (symlink)
  • Memory
    • maps
    • smaps_rollup
    • VmRSS in status
  • Limits and accounting
    • limits
    • io
    • stat
  • Threads
    • task/ (one directory per thread)

/proc/sys and /sys: looking at and tuning the kernel

/proc/sys holds the kernel settings that sysctl manages. The setting net.ipv4.ip_forward is the file /proc/sys/net/ipv4/ip_forward, so sysctl -w net.ipv4.ip_forward=1 and writing 1 into that file as root do the same thing. Changes are lost at reboot; to keep them, put a line in a file under /etc/sysctl.d/.

/sys is sysfs, a view of devices and drivers. /sys/block/DEV/queue/scheduler shows and sets a disk's I/O scheduler, /sys/class/net/IFACE/statistics/ holds live network counters, and /sys/fs/cgroup is the cgroup v2 tree that systemd and containers use to limit CPU and memory. sysfs follows the rule of one value per file, which makes it easy to read from scripts. Writing usually needs root.

Learn it in 4 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Describe the VFS abstraction layers~40m
  2. 2
    List mounts from /proc/self/mounts~40m
  3. 3
    Read process info from /proc/PID~40m
  4. 4
    Explore kernel settings in /sys~40m

Key points

  • Linux has one directory tree; mount grafts other file systems onto it.
  • The page cache keeps recently used file data in RAM for all file systems.
  • /proc/PID exposes per-process information such as status, fd and maps.
  • /sys exposes devices and kernel parameters, some of them writable.

Key terms

Tap a card to flip it. 0/6 known

Examples

bash
head -5 /proc/self/mounts
findmnt /
ls -l /proc/self/fd
grep -E "State|VmRSS" /proc/$$/status
cat /sys/block/*/queue/scheduler

Explore mounts, descriptors and kernel settings as plain files.

List read-only mounts from C

A backup tool should warn when its target is mounted read-only. Step 1: open the mount table with setmntent, which returns a normal FILE. Step 2: loop with getmntent; each call returns one parsed line with fields like mnt_dir and mnt_type. Step 3: hasmntopt checks for a whole option name, so ro matches ro but not errors=remount-ro. Step 4: close the table with endmntent.

c
#include <mntent.h>
#include <stdio.h>
int main(void) {
  FILE *f = setmntent("/proc/self/mounts", "r");
  if (!f) { perror("setmntent"); return 1; }
  struct mntent *m;
  while ((m = getmntent(f)) != NULL)
    if (hasmntopt(m, "ro"))
      printf("%-30s %-10s read-only\n", m->mnt_dir, m->mnt_type);
  endmntent(f);
  return 0;
}

Inspect a running process through /proc

Someone asks what an sshd process is really doing. Step 1: find the oldest matching PID. Step 2: print its full command line; arguments are separated by NUL bytes, so tr turns them into spaces. Step 3: read the state, parent, thread count and memory from status. Step 4: see which program file and directory it uses. Step 5: count open descriptors and compare with its limit. Step 6: as root, see how many bytes it has read and written.

bash
pid=$(pgrep -o sshd)
tr '\0' ' ' < /proc/$pid/cmdline; echo
grep -E '^(State|PPid|Threads|VmRSS)' /proc/$pid/status
sudo readlink /proc/$pid/exe /proc/$pid/cwd
sudo ls /proc/$pid/fd | wc -l
grep 'open files' /proc/$pid/limits
sudo cat /proc/$pid/io

Quick reference

  • findmnt -T /some/path shows which mount holds that path
  • /proc/self/mountinfo shows mount IDs, parents and propagation flags
  • mount -o remount,ro /mnt/data changes mount options without unmounting
  • sudo mount --bind /src /dst makes a directory visible in a second place
  • tr '\0' ' ' < /proc/PID/cmdline prints a process's full command line
  • sysctl net.ipv4.ip_forward reads /proc/sys/net/ipv4/ip_forward
  • Files in /proc show size 0: read until end of file instead of trusting st_size
  • Persistent kernel settings go in a file under /etc/sysctl.d/

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Which VFS object caches the link between a name and an inode?

Question 2

2Why is the mount list read from /proc/self/mounts rather than one global file?

Question 3

3ls -l /proc/self/status shows a size of 0. What happens when you cat it?

Question 4

4How do you make net.ipv4.ip_forward=1 survive a reboot?

Question 5

5You mount a USB disk on /data, which already contained files. What happens to those files?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Show your shell's state, parent PID and number of open descriptors using only files in /proc.

  2. Exercise 2medium

    Write a C program that prints its own resident memory (the VmRSS line) from /proc/self/status.

  3. Exercise 3hard

    Write a tiny ps: list each numeric directory in /proc and print the PID, state letter and command name from /proc/PID/stat. Skip processes that exit while you read.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is /proc stored on the disk?
No. procfs has no storage at all; every file's content is produced by the kernel when you read it.
What is the difference between /proc and /sys?
/proc started as process information and also holds many kernel details and the sysctl settings in /proc/sys. /sys is the newer, more structured view of devices, drivers and kernel objects, with one value per file.
Why does /etc/mtab point to /proc/self/mounts?
The kernel already knows the exact mount table for each process, so a symlink is always correct. A separately written file could fall out of date or differ between namespaces.
What is a bind mount?
A bind mount makes an existing directory appear at a second path, for example mount --bind /srv/data /var/www/data. Containers use bind mounts to share host directories.

Go deeper

ShareXLinkedInWhatsApp