06 The joys of /etc/fstab


In the previous lesson, we've covered the basics of mounting and unmounting volumes, using a variety of different filesystem implementations. We have also encountered the concept of a virtual filesystem. Now it's time to learn about the various options that one can apply to a mounted volume, and how to mount filesystems automatically.


Before explaining this, there's one more thing you should know. The file /etc/mtab is special - it is used by mount and umount to remember which filesystems were mounted, with which options, and by whom - in other words, the mount table plus some extra information. This can be useful, as is seen later. However, the important thing to realise about /etc/mtab is that it is only maintained by the mount and umount programs themselves - it is not a way of accessing the kernel's internal mount table (to do that on Linux systems, look at /proc/mounts, which is the same format as /etc/mtab but lacks some of the extra information, as it is not remembered by the kernel). This means that if, for some reason, I wrote my own program which mounted a filesystem with the mount() system call, it would not show up in /etc/mtab, because didn't use the normal mount command. This wouldn't really hurt anything, except for the fact that some of the features which rely on /etc/mtab's extra information (such as non-superuser umounts) wouldn't work. More on that later.

Mount options

When you mount a filesystem, you can also give the kernel extra options, by giving mount the -o option, followed by a comma-separated list of options. For example, to mount a volume read-only (ro):

root@rhodium:~# mount -t vfat /dev/fd0 /floppy -o ro

Likewise, -o rw specifies that a volume should be mounted read-write (the default). There are a bunch of options - see man mount for a full explanation.


We have now nearly covered the basic mount system. Only one more thing remains - /etc/fstab. This file is used for automatic mounting and unmounting of volumes, as well as to specify that non-root users are allowed to mount certain filesystems and for filesystem integrity checking (fsck).

The format of /etc/fstab is probably best understood by looking at one, so here's mine:

#             Meredydd's /etc/fstab, as of 26/8/2003
#                                                    Meredydd Luff
/dev/hda2        swap             swap        defaults         0   0
/dev/hda1        /                reiserfs    noatime          1   1
/dev/cdrom       /cdrom           iso9660     noauto,users,ro  0   0
/dev/fd0         /floppy          auto        noauto,users     0   0
/dev/sda1        /memstick        vfat        noauto,users     0   0
proc             /proc            proc        defaults         0   0
devpts           /dev/pts         devpts      gid=5,mode=620   0   0

I've divided it up into columns to make things prettier. Like many files, however, it doesn't really care about "whitespace" such as spaces and tabs between options. As long as there's something separating those options, and each volume is specified on one line, it'll work, so for "the second column", read "the second option on each line". As a little challenge, if anyone on this course produces a decent piece of art which parses as a valid and useful /etc/fstab, I'll send them a small prize...

Anyway: The first column, on the left-hand side, is the device file representing the volume (unless it's a "nodev" filesystem implementation, in which case it can be anything). The second is the mount point, the third is the filesystem implementation to use, and the fourth contains any options, or "defaults" if none are to be given. The fifth column of numbers is used by an obscure program called dump, about which I have been unable to find out more. Suffice to say that neither I nor anyone I've yet asked has ever used this, so it's safe to leave these numbers as 0, or whatever is already there.

The sixth column, however, is important - it is read by fsck, the filesystem checker. Remember when I said that the block-cache system, although it speeds things up a lot, can lead to data loss if a machine is turned off unexpectedly? fsck is the program which walks across a volume, spotting where there are inconsistencies (because some data got written out to disk and some didn't), and trying to repair the filesystem. fsck also tries to do a bit of damage-control on bad blocks of a disk - the analogous Windows program is Scandisk. This sixth column in /etc/fstab specifies the order in which filesystems are to be checked on bootup, or whether they are to be checked at all. The reason this is important is that fsck makes use of parallelisation - because the slowest bit of checking a filesystem is reading the bally thing off the hardware (the actual checking is relatively quick), fsck can check volumes on different devices, for example two separate hard drives, at the same time. However, sometimes you don't want this - for example, it's probably a good idea to check your root filesystem before doing anything else - so you can specify a number in the sixth column of /etc/fstab, and fsck will just work its way up through the numbers. For example, a multi-disk machine I administrate has its root filesystem with a 1 in that column, and 2 for everything else. This means that, if the machine wasn't properly shut down and needs to be checked, fsck will check the root filesystem first, then do all the other disks at once (because they have the same number). On most systems, don't worry too much about this - put a 1 there if you want the volume to be checked on boot, and 0 if not.

A little more detail

All right, now let's go through those lines one by one, and explain a bit more.

/dev/hda2        swap             swap        defaults         0   0

This is my swap partition. You'll see that it's my second partition on my primary master IDE drive (the only drive in this laptop). It's not really mounted like any other volume - it's just in here to tell the system to start using this partition as virtual memory when it boots up. The filesystem implementation field has "swap" in it, the block device is where to chuck the data, and none of the other options matter. A swap device is initialised with mkswap /dev/something, and can be manually activated with swapon /dev/something, although you'll probably never have to do this unless you install Slackware Linux (*cough*).

/dev/hda1        /                reiserfs    noatime          1   1

This is my root partition. It's mounted on /, it does get checked on bootup, and it's mounted with the noatime option, which is just a little performance tweak - it stops the operating system from writing to the disk each time a file is read from, just to update that file's last-accessed timestamp.

/dev/cdrom       /cdrom           iso9660     noauto,users,ro  0   0
/dev/fd0         /floppy          auto        noauto,users     0   0

These two should be familiar to you by now - they're my CD-ROM and floppy disk drives. Note that the floppy's filesystem implementation is set to auto, which tells the operating system to try anything that works. The option noauto is explained under the next heading.

/dev/sda1        /memstick        vfat        noauto,users     0   0

Nothing particularly interesting here - this line mounts my brother's USB digital camera. It shows up as a generic "storage device", and so gets treated like a disk, and pretends to be a SCSI device. Because I have no real SCSI devices, it becomes /dev/sda1.

proc             /proc            proc        defaults         0   0

This line mounts my proc filesystem, which we have discussed earlier.

devpts           /dev/pts         devpts      gid=5,mode=620   0   0

The directory /dev/pts is a little harder to explain without knowledge of terminals. There are several programs which rely on talking to a character device, say /dev/tty1 (the first virtual terminal under Linux - you can change virtual terminals with Ctrl+Alt+Fx), or /dev/ttyS0 (for a serial console). In fact, most console programs rely on this, which creates a bit of a problem for a terminal emulator such as xterm. The solution is a PTY (short for pseudo-tty) - a "fake" terminal. To create a PTY, the terminal emulator opens /dev/ptmx. What it then sees is one side of a double-ended tunnel, the other side of which suddenly appears in a file called /dev/pts/1 or similar. This file looks like a terminal device, quacks like a terminal device, and generally keeps whatever process you're running happy. Making a file appear out of nowhere...sound familiar? That's right, devpts is another virtual filesystem like proc. Unless you're in the business of writing terminal clients, or want to talk to particular programs, such as su, which refuse to play ball unless they're running on a terminal, you'll probably never run into this.

You may also notice that I have not mentioned the users option. Put briefly, it allows mortal users (people other than root) to mount these filesystems - but only with the options provided here. If there is demand, I will go on to explain exactly how this happens, in another of my little diversions - talk to the list!

Wholesale mounting

It's been a long lesson, but there's just one more thing to mention: mount -a. The -a option tells mount to mount everything in /etc/fstab at once - everything, that is, except for entries with noauto in their options.

mount also allows you to specify in a bit more detail exactly which volumes you wish to mount. For example, the -t option still specifies the type of filesystem - mount -a -t ext2 will mount all entries with "ext2" in the filesystem implementation column. There is also the -O option, for narrowing things down further. For example, mount -a -O XYZ will mount all filesystems with the option XYZ in the options column, and mount -a -O noXYZ will mount all filesystems without the option XYZ.

This is sometimes useful for sequential mounts - for example, if you have made sure that all your network volumes have the bogus option _netdev set, then mount -a -O no_netdev can be used, early in the boot sequence, to mount everything except networked filesystems (useful, as networking isn't usually configured this early on - indeed, on some systems, the root volume doesn't contain the network configuration tools). Later, when the system has networking, a mount -a -O _netdev will mount only the networked filesystems.

mount -a is usually only issued once or twice, during the boot sequence, and rarely if ever at any other time.

Next Lesson

Our next lesson will deal with networked filesystems, such as NFS and SMB (also known as CIFS). If there is sufficient demand, I will post a little diversion on mortal-user mounts, including an explanation of SUID executables and how the su command works.