Chapter 11. Linux Filesystems and the VFS

Objectives

  • Explain the basic filesystem organization
  • Understand the role of the VFS
  • Know what filesystems are available in Linux and which ones can be used on your actual system
  • Grasp why journalling filesystems represent significant advances
  • Discuss the use of special filesystems in Linux

Filesystem Basics

  • A file is an abstraction from the physical hard disk
  • A file can be in a physical partition or a logical partition controlled by a Logical Volume Manager
  • Filesystems can be also of a network nature (with their true physical part hidden over the network)

Filesystem Tree Organization

  • / is the root of the filesystem
  • the filesystem may be composed of other filesystems
  • /dev - /proc - /sys - /run are virtual pseudo filesystems stored only in memory

Available File Systems

Name Description
ext2     Native Linux filesystem
minix Predecessor to ext2
proc Used for /proc/
msdos MSDOS
umsdos Extensions to MSDOS    
vfat Windows VFAT (includes FAT32, FAT, etc.)
ntfs Windows NT NTFS (read-only)
sysv SystemV/Xenit/Coherent FS
hpfs OS/2 HPFS
ufs Sun
udf CD R/W, DVD
hfs Apple MacIntosh Filesystem (HFS)
hfs+ Apple MacIntosh Extended HFS
befs BeOS filesystem
jffs, jffs2 Journalling Flash Filesystem
iso9660 cdrom etc., including Joliet extensions
cramfs Compressed read-only filesystem
romfs Small read-only filesystem
tmpfs Ram disk that is swappable
ramfs Ram disk, for example of a filesystem
gfs2 Clustering filesystem from Red Hat
nfs Network Filesystem (through version 4)
smb Samba networking
ncp Novell Netware FS using NCP Protocol
coda Experimental distributed filesystem
afs Andrewdistributed filesystem, from Carnegie Mellon
ocfs2 Extent-based, disk cluster filesystem from Oracle

Journalling Filesystems

Journalling filesystems are those that implements a mechanism to recover from crashes because they maintain a journal in the modifications of the filesystem that allows to keep track of the changes, those at the same time are made through transactions.

These are some of the journalling filesystems freely available under Linux

  • ext3
  • ext4
  • reiserfs
  • JSF
  • XFS
  • btrfs

Current Filesystems Types

Execute the following command

$ cat /proc/filesystems

Special Filesystems

There are some special filesystems used to access certain kernel data structures or for tunning kernel behavior or to implement particular functions

Filesystem Mount Point Purpose
 rootfs  None  During kernel load, provides an empty root directory.
 hugetlbfs  Anywhere  Provides extended page access (2 or 4 MB on X86).
 bdev  None  Used for block devices.
 proc  /proc  Pseudo filesystem access to many kernel structures and  subsystems.
 sockfs  None  Used by BSD Sockets.
 tmpfs  Anywhere  RAM disk with swapping, re-sizing.
 shm  None  Used by System V IPC Shared Memory.
 pipefs  None  Used for pipes.
 binfmt_misc  Anywhere  Used by various executable formats.
 devpts  /dev/pts  Used by Unix98 pseudo-terminals.
 usbfs  /proc/bus/usb  Used by USB sub-system for dynamical devices.
 sysfs  /sys (or elsewhere)  Used as a device tree.
 debugfs  /sys/kernel/debug (or elsewhere)  Used for simple debugging file access.

LAB 1.1 The tmpfs Special Filesystem

tmpfs is one of many special filesystems used under Linux. Some of these are not really used as filesystems, but just take advantage of the filesystem abstraction. However, tmpfs is a real filesystem that applications can do I/O on.
Essentially, tmpfs functions as a ramdisk; it resides purely in memory. But it has some nice properties that old-fashioned conventional ramdisk implementations did not have:

1. The filesystem adjusts its size (and thus the memory that is used) dynamically; it starts at zero and expands as necessary up to the maximum size it was mounted with.

2. If your RAM gets exhausted, tmpfs can utilize swap space. (You still can’t try to put more in the filesystem than its maximum capacity allows, however.)

3. tmpfs does not require having a normal filesystem placed in it, such as ext3 or vfat; it has its own methods for dealing with files and I/O that are aware that it is really just space in memory (it is not actually a block device), and as such are optimized for speed.
Thus there is no need to pre-format the filesystem with a mkfs command; you merely just have to mount it and use it.

Mount a new instance of tmpfs anywhere on your directory structure with a command like: 

$ sudo mkdir /mnt/tmpfs
$ sudo mount -t tmpfs none /mnt/tmpfs

See how much space the filesystem has been given and how much it is using:

$ df -h /mnt/tmpfs

You should see it has been alotted a default value of half of your RAM; however, the usage is zero, and will only start to grow as you place files on /mnt/tmpfs.

You could change the allotted size as a mount option as in:

$ sudo mount -t tmpfs -o size=1G none /mnt/tmpfs

You might try filling it up until you reach full capacity and see what happens. Do not forget to unmount when you are done with:

$ sudo umount /mnt/tmpfs

Virutally all modern Linux distributions mount an instance of tmpfs at /dev/shm:

​$ df -h /dev/shm
Filesystem
tmpfs
Type   Size  Used Avail Use% Mounted on
tmpfs  3.9G   24M  3.9G   1% /dev/shm

Many applications use this such as when they are using POSIX shared memory as an inter-process communication mechanism. Any user can create, read and write files in /dev/shm, so it is a good place to create temporary files in memory.
Create some files in /dev/shm and note how the filesystem is filling up with df.
In addition, many distributions mount multiple instances of tmpfs; for example, on a RHEL 7 system:

$ df -h | grep tmpfs

Some distributions (such as Fedora) may (by default) mount /tmp as a tmpfs system; in such cases one has to avoid putting large files in /tmp to avoid running out of memory. Or one can disable this behavior as we discussed earlier when describing /tmp.