Book: LPI Linux Certification in a Nutshell
Section: Chapter 4.  Devices, Linux Filesystems, and the Filesystem Hierarchy Standard (Topic 2.4)



4.3 Objective 3: Control Filesystem Mounting and Unmounting

As discussed in Objective 1, the Linux directory hierarchy is usually made up of multiple partitions, each joined to the root filesystem. Filesystems on removable media, such as CD-ROMs, Zip disks, and floppy disks, are joined in the same way, but usually on a temporary basis. Each of these separate filesystems is mounted to the parent filesystem as a directory (or mount point) in the unified hierarchy.

Directories intended as mount points usually don't contain files or other directories. Instead, they're just empty directories created solely to mount a filesystem. If a directory that already contains files is used as a mount point, its files are obscured and unavailable until the filesystem is unmounted. Typical mount points include the directories /usr, /home, /var, and others.

4.3.1 Managing the Filesystem Table

Since the Linux filesystem hierarchy is spread across separate partitions and/or multiple drives, it is necessary to automatically mount those filesystems at boot time. In addition, removable media and filesystems on remote NFS servers may be used regularly with recurring mount properties. All of this information is recorded in the /etc/fstab file. Filesystems defined in this file are checked and mounted when the system boots. Entries in this file are consulted for default information when users wish to mount removable media.

The /etc/fstab file (see Example 4-1) is plain text and consists of lines with six fields:

Device

This field specifies the device file of the partition holding the filesystem -- for example, /dev/hda1. This field cannot contain a whole device, such as /dev/hda.

Mount point

This field specifies the directory upon which the filesystem is to be mounted. For example, if /dev/hda1 contains the root filesystem, it is mounted at /. The root filesystem will contain additional directories intended as mount points for other filesystems. For example, /boot may be an empty directory intended to mount the filesystem that contains kernel images and other information required at boot time.

Filesystem type

Next, the type of filesystem is specified. These include some ext2 filesystems, as well as swap, iso9660 (CD-ROM), and others.

Mount options

This field contains a comma-separated list of options. Some options are specific to particular filesystem types. Options are described later in this Objective.

Dump frequency

The dump program, a standard Unix backup utility, may consult /etc/fstab for information on how often to dump each filesystem. This field holds an integer, usually set to 1 for ext2 filesystems and to for others.

Pass number for fsck

This field is used by the fsck utility when the -A option is specified, usually at boot time. It is a flag that may contain only the values 0, 1, or 2.

  • A 0 instructs fsck not to check the filesystem.

  • A 1 should be entered for the root filesystem and instructs fsck to check that filesystem first.

  • A 2 instructs fsck to check corresponding filesystems after those with a 1.

Example 4-1. Sample /etc/fstab File
/dev/sda1      /                ext2    defaults        1 1
/dev/sda5      /boot            ext2    defaults        1 2
/dev/sda9      /home            ext2    defaults        1 2
/dev/sda6      /root            ext2    defaults        1 2
/dev/sda10     /tmp             ext2    defaults        1 2
/dev/sda8      /usr             ext2    defaults        1 2
/dev/sda7      /var             ext2    defaults        1 2
/dev/sda11     swap             swap    defaults        0 0
/dev/fd0       /mnt/floppy      ext2    noauto,users    0 0
/dev/hdc       /mnt/cdrom       iso9660 noauto,ro,users 0 0
/dev/hdd       /mnt/zip         vfat    noauto,users    0 0
fs1:/share     /fs1             nfs     defaults        0 0

The fstab in Example 4-1 depicts a system with a single SCSI disk, /dev/sda. The first partition, /dev/sda1, contains an ext2 root filesystem. Partition /dev/sda11 is swap. Partitions /dev/sda5 through /dev/sda10 contain ext2 partitions for /boot, /home, /root, /tmp, /usr, and /var, respectively. All of the local ext2 partitions are to be checked by fsck and dumped. Entries for the floppy disk ( /dev/fd0), CD-ROM ( /dev/hdc), and IDE Zip drive ( /dev/hdd ) hold appropriate mount properties, making manual mounting of these devices simple. Finally, this example shows a remote NFS mount of directory /share of system fs1. It is mounted locally at /fs1.

The /etc/fstab file is automatically created when Linux is installed and is based on the partitioning and mount point configuration specified. This file can be changed at any time to add devices and options, tailoring the filesystem to meet your specific needs.

On the Exam

You should memorize the functions of each column in /etc/fstab and be prepared to answer questions on each.

4.3.2 Mounting Filesystems

Filesystems are mounted using the mount command. At boot time, those filesystems with a nonzero pass number in /etc/fstab are checked and automatically mounted. Later, you can run mount manually to add other filesystems to the filesystem hierarchy.

mount

Syntax

mount [command_line_options] device 
mount [command_line_options] directory
mount [command_line_options] device directory

Description

Used to mount filesystems onto the filesystem hierarchy. The first and second forms consult /etc/fstab and mount the filesystem located on device or intended to be attached to directory, respectively. In both cases, information necessary to complete the mount operation is taken from /etc/fstab. The third form is independent of /etc/fstab and mounts the filesystem on device at mount point directory.

The mount command accepts two kinds of options: command-line and mount. The command-line options provide general direction for the mount command. The mount options, which are generally alphanumeric words, word fragments, or abbreviations, are used to specify additional information about the device being mounted.

Command-line options

-a

Mounts all of the partitions specified in /etc/fstab, except those with the noauto option.

-h

Displays help on the mount command.

-o mount_options

Specifies mount options on the command line.

-r

Mounts the filesystem as read-only.

-t fstype

Specifies that the filesystem to be mounted is of type fstype. This option is typically used interactively, when no entry for the mount exists in /etc/fstab.

-v

Sets verbose mode.

-w

Mounts the filesystem read/write mode.

Mount options

A number of parameters are available as options for mounting filesystems. These options may be specified in /etc/fstab or as arguments of the -o command-line mount argument. These options modify the way mount configures the mounted filesystem. Some of the options can provide added security by controlling some operations on the filesystem. Others protect the filesystem from damage. Here is a partial list:

async

Establishes asynchronous I/O to the mounted filesystem. The opposite is sync.

auto

Enables a mount specification in /etc/fstab to be processed with the -a command-line option, as needed at boot time. The opposite is noauto.

defaults

Implies rw, suid, dev, exec, auto, nouser, and async. It is commonly found on /etc/fstab entries for ext2 mount points.

dev

Interprets character or block special devices on the filesystem.

exec

Enables the execution of programs contained on the mounted partition. The opposite is noexec.

noauto

Prohibits automatic mounting with the -a option. This is usually specified for removable media.

noexec

Prohibits the execution of executable programs, a potential security measure.

nosuid

Prohibits the effect of suid or sgid bits on executable files.

nouser

Forbids non-root users from mounting and unmounting the filesystem. See user and users for the opposite effect.

ro

Equivalent to specifying the command-line option -r.

rw

Equivalent to specifying the command-line option -w.

suid

Enables the effect of suid and sgid bits on executable files.

sync

Establishes synchronous I/O to the mounted filesystem. The opposite is async.

user

Allows an ordinary user to mount the filesystem but prohibits other ordinary users from unmounting it. This is useful for removable media that an individual requires control over. See also users.

users

Allows any user to mount and unmount the filesystem.

Note that the user and users options make the mount and umount commands available to non-root users. This may be important for some systems where end users must have the ability to mount removable media.

Filesystem types

Mount must be aware of the type of filesystem it is mounting, which is specified with a single filesystem type. This parameter may be included on the command line using the -t option, or in the third field in /etc/fstab. Linux can mount a variety of filesystems. Here are some of the more popular ones:

ext2

The standard Linux filesystem.

msdos

The MS-DOS FAT filesystem, limited to "8.3" filenames (eight characters, a dot, and a three-character extension).

vfat

Virtual FAT, used instead of msdos when long filenames must be preserved. For example, you may wish to have access to Windows partitions on systems configured to boot both Linux and Windows.

iso9660

The CD-ROM format, also the default type.

nfs

Remote servers.

swap

Swap partitions.

proc

This type represents the proc filesystem, which is not really a filesystem at all. The virtual files found in this virtual filesystem provide a window into the kernel. It is usually mounted on /proc.

Example 1

Display filesystems currently mounted on the system:

# mount
/dev/sda1 on / type ext2 (rw)
none on /proc type proc (rw)
/dev/sda5 on /boot type ext2 (rw)
/dev/sda9 on /home type ext2 (rw)
/dev/sda6 on /root type ext2 (rw)
/dev/sda10 on /tmp type ext2 (rw)
/dev/sda8 on /usr type ext2 (rw)
/dev/sda7 on /var type ext2 (rw)
none on /dev/pts type devpts (rw,mode=0622)
/dev/hdd on /mnt/zip type vfat (rw,noexec,nosuid,nodev)

In this example, you can see that most of the filesystems specified in the /etc/fstab from Example 4-1 are already mounted.

Example 2

Mount the IDE CD-ROM device found on /dev/hdc to the existing directory /cdrom, read-only of course:

# mount -rt iso9660 /dev/hdc /cdrom 

Note that without the -r option, you will receive a warning but still get appropriate results:

# mount -t iso9660 /dev/hdc /mnt/cdrom
mount: block device /dev/hdc is write-protected, 
   mounting read-only

Example 3

Mount an MS-DOS floppy in the first floppy disk drive /dev/fd0 (A: in MS-DOS) to the existing directory /floppy :

# mount -t msdos /dev/fd0 /floppy

Example 4

The filesystems mounted at /home and /opt have been unmounted for some kind of maintenance and are now remounted using the -a option:

# mount -av
mount: /dev/hda5 already mounted on /root
mount: /dev/hda9 already mounted on /usr
mount: /dev/hda7 already mounted on /var
mount: none already mounted on /proc
mount: none already mounted on /dev/pts
mount: 192.168.0.2:/ already mounted on /smp
/dev/hda10 on /home type ext2 (rw)
/dev/hda8 on /opt type ext2 (rw)

Note that mount should work silently without the -v option. It also safely skips filesystems that have been previously mounted.

4.3.3 Unmounting Filesystems

Filesystems can be unmounted using the umount command. When a filesystem is unmounted, the buffers of the filesystem are synchronized with the actual contents on disk and the filesystem is made unavailable, freeing the mount point. If the filesystem is busy, umount yields an error. This will happen, for example, when the filesystem contains open files or when a process has a working directory within the filesystem. Other less obvious errors can occur when removable media are exchanged without being unmounted first.

umount

Syntax

umount [options] device 
umount [options] directory

Description

Unmount the filesystem on device or mounted on directory.

-a

Unmounts all of the filesystems described in /etc/mtab. This file is maintained by the mount and umount commands and contains an up-to-date list of mounted filesystems. This option is typically used at shutdown time.

-t fstype

Unmounts only filesystems of type fstype.

Example 1

Unmount the CD-ROM mounted on /dev/hdc at /cdrom:

# umount /cdrom

or:

# umount /dev/hdc

Example 2

Unmount all NFS filesystems:

# umount -at nfs

On the Exam

Be sure that you understand how to use mount and mount points and how /etc/fstab is used when mounting files.