The /etc/fstab File

The /etc/fstab file makes it possible to automate the mounting of certain file systems, especially at system start-up. It contains a series of lines describing the file systems, their mount points and other options. Here is an example of an /etc/fstab file:

/dev/hda1   /           ext2    defaults        1 1
/dev/hda5   /home       ext2    defaults        1 2
/dev/hda6   swap        swap    defaults        0 0
/dev/fd0    /mnt/floppy auto    sync,user,noauto,nosuid,nodev,unhide 0 0
/dev/cdrom  /mnt/cdrom  auto    user,noauto,nosuid,exec,nodev,ro 0 0
none        /proc       proc    defaults        0 0
none        /dev/pts    devpts  mode=0622       0 0

Each line consists of:

There is always an entry for the root file system. The swap partitions are special since they are not visible in the tree structure, and the mount point field for those partitions contains the swap keyword. As for the /proc file system, it will be described in more detail in Chapter 10, The /proc Filesystem. Another special file system is /dev/pts.

At this point, we have moved the entire /usr hierarchy to /dev/hdb1 and we want this partition to be mounted as /usr at boot time. To accomplish this, add the following entry to the /etc/fstab file:

/dev/hdb1        /usr         ext2    defaults  1 2

Now the partition will be mounted at each boot, and will be checked for errors if necessary.

There are two special options: noauto and user. The noauto option specifies that the file system should not be mounted at start-up, and is mounted only when you tell it to. The user option specifies that any user can mount and unmount the file system. These two options are typically used for the CD-ROM and floppy drives. There are other options, and /etc/fstab has a man page (fstab(5)) you can read for more information.

One advantage of using /etc/fstab is that it simplifies the mount command syntax. To mount a file system described in the file, you can either reference the mount point or the device. To mount a floppy disk, you can type:

$ mount /mnt/floppy

or:

$ mount /dev/fd0

To finish our partition moving example, let's review what we have already done. We copied the /usr hierarchy and modified /etc/fstab so that the new partition will be mounted at start-up. But for the moment, the old /usr files are still in their original place on the drive, so we need to delete them to free up space (which was, after all, our initial goal). To do so, you first need to switch to single user mode by issuing the telinit 1 command on the command line.

And that's it. Now, go back to multi-user mode (telinit 3 for standard text mode or telinit 5 for the X Window System), and if there is no further administrative work left, you should now log off from the root account.