Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

The mount Command

Use the mount command to mount a block device onto the filesystem. You need to specify what device contains the filesystem, what type it is, and where in the directory hierarchy to mount it.

A mount command looks like this:

mount -t type -o options device mount-point

device should either be a block device or if it contains a colon it can be the name of another machine from which to mount a filesystem (see Chapter 16). The mount-point should be an existing directory (or you get an error); the filesystem will appear at this position. (Anything previously in that directory will be hidden.) The filesystem type and options are optional, and the variety and meaning of options depend on the type of filesystem being mounted. If the filesystem you want to mount is specified in the /etc/fstab file, you need to specify only the mount point or the device name; the other details are read from /etc/fstab by mount.

Here is an example of the mount command being used:

# ls /mnt/floppy
filesystem not mounted
# mount -t vfat /dev/fd1 /mnt/floppy
mount: block device /dev/fd1 is write-protected, mounting read-only
# ls /mnt/floppy
grub-0.4.tar.gz
# umount /mnt/floppy
# ls /mnt/floppy
filesystem not mounted

In this example, I listed the files in my /mnt/floppy mount point. I saw that no diskette was mounted, so I mounted a diskette containing a vfat filesystem at the mount point. I got an informational message telling me that the diskette was write-protected; if the diskette had not been write-protected, there would have been no message. The directory /mnt/floppy already existed. I used ls to see what was on the disk and unmounted it again using the umount command. I then ran ls again, and the response I got was simply the name of a file that I leave in the directory /mnt/floppy on my hard disk to remind me that there currently is nothing mounted there. This hint enables me to distinguish a written diskette from an empty diskette that is mounted. You can also use the df command to see what filesystems are mounted. For example:

# mount -t vfat /dev/fd0 /mnt/floppy
# df /mnt/floppy/
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/fd0                  1423         0      1423   0% /mnt/floppy
# umount /mnt/floppy
# df /mnt/floppy/
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hda6              2028066   1850753     72493  96% /

As you see from the example, when a device is mounted on a mount point, df will output statistics for that device; otherwise it will output statistics about the device upon which the directory resides.

Mounting a vfat diskette as we did earlier causes the Linux kernel to automatically load the vfat driver into the kernel while it is needed. The kernel module handler loads these drivers, and when they become unused after the filesystem is unmounted, they are unloaded to recover the memory that they occupied. See Chapter 27, "Configuring and Building Kernels," for more information about kernel modules.

Potential Problems with mount

Any one of several things can cause the mount command to fail, for example:

When you mount a filesystem, the mount point (the point at which the filesystem is to be mounted) must be a directory. This directory doesn't have to be empty, but after the filesystem is mounted, anything underneath it is inaccessible. Linux provides a singly rooted filesystem, which is in contrast to those operating systems that give each filesystem a separate drive letter. This is more flexible because the size of each block device (hard disk or whatever) is hidden from programs, and things can be moved around. For example, if you have some software that expects to be installed in /opt/umsp, you can install it in /big-disk/stuff/umsp and make /opt/umsp a symbolic link. There is also no need to edit a myriad of configuration files that are using the wrong drive letter after you install a new disk drive.

Many options govern how a mounted filesystem behaves; for example, it can be mounted read-only. There are options for filesystems such as msdos that don't have any concept of users. The filesystems enable you to give each file a particular file mode (for security or to allow access by everyone). When you mount an nfs filesystem, there is so much flexibility available that the options have a separate manual page (man nfs), although the defaults are perfectly reasonable. The nfs filesystem is explained in more detail in Chapter 16.

Table 18.1 contains options useful for mount in alphabetical order. Unless otherwise indicated, these options are valid for all filesystem types, although asking for asynchronous writes to a CD-ROM is no use. Options applicable only to NFS filesystems are not listed here; refer to the nfs command manual page for those.

Table 18.1. mount Options

Option Description
async Write requests for the filesystem normally should wait until the data has reached the hardware; with this option, the program continues immediately instead. This does mean that the system is slightly more prone to data loss in the event of a system crash, but on the other hand, crashes are rare with Linux. This option speeds up NFS filesystems to a startling extent. The opposite of this option is sync.
auto Indicates to mount that it should mount the device when given the -a flag. This flag is used by the startup scripts to make sure that all the required filesystems are mounted at boot time. The opposite of this option is noauto.
defaults Turns on the options rw, suid, dev, exec, auto, nouser, and async.
dev Allows device nodes on the system to be used. Access to devices is completely determined by access rights to the on-disk device node. Hence, if you mount an ext2 filesystem on a diskette and you have previously placed a writable /dev/kmem device file on the disk, then you've just gained read/write access to kernel memory. System administrators generally prevent this from happening by mounting removable filesystems with the nodev mount option.
exec Indicates to the kernel that it should allow the execution of programs on the filesystem. It is more usual to use the negated version of exec—that is, noexec—which indicates to the kernel that execution of programs on this filesystem shouldn't be allowed. This is generally used as a security precaution or for NFS filesystems mounted from another machine that contains executable files of a format unsuitable for this machine (for example, intended for a different CPU).
noauto Opposite of auto.
nodev Opposite of dev.
noexec Opposite of exec.
nosuid Opposite of suid.
nouser Opposite of user.
remount Allows the mount command to change the flags for an already-mounted filesystem without interrupting its use. You can't unmount a filesystem that is currently in use, and this option is basically a workaround. The system startup scripts, for example, use the command mount -n -o remount,ro / to change the root filesystem from read-only (it starts off this way) to read/write (its normal state). The -n option indicates to mount that it shouldn't update /etc/fstab because it can't do this while the root filesystem is still read-only.
ro Mounts the filesystem read-only. This is the opposite of the option rw.
rw Mounts the filesystem read/write. This is the opposite of the option ro.
suid Allows the set user ID and set group ID file mode bits to take effect. The opposite of this option is nosuid. The nosuid option is more usual; it is used for the same sorts of reasons that nodev is used.
sync All write operations cause the calling program to wait until the data has been committed to the hardware. This mode of operation is slower but a little more reliable than its opposite, asynchronous I/O, which is indicated by the option async.
user Allows ordinary users to mount the filesystem. When there is a user option in /etc/fstab, ordinary users indicate which filesystem they want to mount or unmount by giving the device name or mount point; all the other relevant information is taken from the /etc/fstab file. For security reasons, user implies the noexec, nosuid, and nodev options.

Options are processed by the mount command in the order they appear on the command line (or in /etc/fstab). Thus, it is possible to allow users to mount a filesystem and then run set user ID executables by using the options user, suid in that order. Using them in reverse order (suid, user) wouldn't work because the user option would turn off the suid option again.

There are many other options available, but they are all specific to particular filesystems. All the valid options for mount are detailed in its manual page. An example is the umask flag for the vfat and fat filesystems, which allows you to make all the files on your MS-DOS or Windows partitions readable (or even writable if you prefer) for all the users on your Linux system.

Share ThisShare This

Informit Network