Part 4 of 7 · 4 min read
Special file systems
Think of a notice board that shows the time, the weather and the room temperature. Nobody writes the notices by hand. Each time you look, the board makes them from the instruments. The analogy stops in one place: a board is read only, while some of these file systems also accept writes that change the system.
In plain words: Not every file system sits on a disk. proc shows the state of processes and the kernel, and it makes each file when it is read (Level 0, page 6). sysfs shows the devices and the settings of drivers, such as /sys/block/vda/queue/rotational. tmpfs keeps its files in memory, up to a size limit, and loses them at reboot. devtmpfs holds the device files of /dev. All of them plug into the VFS, so ordinary tools such as cat and ls work on them. Their statfs report shows no disk blocks, and they have their own device numbers that are not real disks (0:21 and so on).
A worked example. This program prints the device number, the file system type and the size figures for each path that is given to it.
#include <stdio.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/vfs.h>
static const char *name(long magic) {
switch (magic) {
case 0xEF53: return "ext2/3/4";
case 0x01021994: return "tmpfs";
case 0x9fa0: return "proc";
case 0x62656572: return "sysfs";
case 0x794c7630: return "overlayfs";
default: return "other";
}
}
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
struct stat st; struct statfs sf;
if (stat(argv[i], &st) || statfs(argv[i], &sf)) { printf("%-14s cannot read\n", argv[i]); continue; }
printf("%-14s device %u:%-3u type %-9s block size %5ld, blocks %9lu, free %9lu\n",
argv[i], major(st.st_dev), minor(st.st_dev), name((long)sf.f_type),
(long)sf.f_bsize, (unsigned long)sf.f_blocks, (unsigned long)sf.f_bfree);
}
return 0;
}
On the test machine it was run with the paths / /proc /sys /dev/shm /tmp/l8/mnt /tmp/l8/mnt/t:
/ device 254:0 type ext2/3/4 block size 4096, blocks 66053021, free 61973667
/proc device 0:21 type proc block size 4096, blocks 0, free 0
/sys device 0:22 type sysfs block size 4096, blocks 0, free 0
/dev/shm device 0:27 type tmpfs block size 4096, blocks 2054162, free 2054160
/tmp/l8/mnt device 7:0 type ext2/3/4 block size 4096, blocks 2811, free 2803
/tmp/l8/mnt/t device 0:41 type tmpfs block size 4096, blocks 256, free 255
The root is on a block device (254:0, the virtual disk vda of Level 8). The mounted image is on loop device 7:0. proc and sysfs report 0 blocks, because they store nothing. The tmpfs that was mounted with a limit of 1 MiB shows 256 blocks of 4 KiB, which is 1 MiB.
The key lines. The device number changes at each mount point. A tool such as find -xdev stays on one file system by stopping where the device number changes. That is how du and backup programs avoid walking into /proc.
Special file system
A special file system is a file system whose files are not stored on a disk but are made by the kernel or kept in memory. proc and sysfs expose kernel data, and tmpfs stores ordinary files in RAM. They use the same VFS interface as disk file systems.
Example: reading /proc/meminfo makes its text at the moment of the read.
Common mistake
Files in tmpfs use memory, not disk. A large file in /dev/shm or /tmp (when /tmp is a tmpfs) can use up RAM and cause the memory pressure of Level 7, page 7, even though df shows a "disk".
Check yourself:
1. Why does proc report zero blocks?
2. What happens to the files of a tmpfs at reboot?
3. How does the device number help a tool stay inside one file system?