Before you can use the files in a directory, you need to mount that directory on a partition formatted to some readable filesystem. Linux normally automates this process using the /etc/fstab configuration file. When you boot Linux, specified directories are mounted on configured partitions. The mount options require some explanation, especially for removable media.
You may encounter problems if connections are lost or media is removed. When you configure a server, you could be mounting directories from a number of remote computers. You could also want temporary access to removable media such as USB keys or Zip drives. The automount daemon, also known as the automounter or autofs, can help. It can automatically mount specific directories as needed. It can unmount a directory automatically after a fixed period of time.
Linux stores information about your local and remotely mounted filesystems in /etc/fstab. Open this file in the text editor of your choice. As you can see, different filesystems are configured on each line. A sample /etc/fstab might look like the following:
LABEL=/ / ext3 defaults 1 1 LABEL=/boot /boot ext3 defaults 1 2 none /dev/pts devpts gid=5,mode=620 0 0 none /proc proc defaults 0 0 none /dev/shm proc tmpfs 0 0 /dev/hda3 swap swap defaults 0 0 LABEL= /usr /usr ext3 defaults 1 2 LABEL= /tmp /tmp ext3 defaults 1 2 LABEL= /var /var ext3 defaults 1 2 LABEL=/home /home ext3 defaults 1 2
As you can see, there are six fields associated with each filesystem, which are described from left to right in Table 4-4. Remember, this is a configuration file. You can verify partitions that are actually mounted in /etc/mtab.
Field Name | Description |
---|---|
Label | Lists the device to be mounted. If mounted, you can find the associated partition in /etc/mtab. For example, if you see /dev/hda2 in this file, you can verify its LABEL with the e2label /dev/hda2 command. |
Mount Point | Notes the directory where the filesystem will be mounted. |
Filesystem Format | Describes the filesystem type. Valid filesystem types include ext, ext2, ext3, msdos, vfat, devpts, proc, tmpfs, udf, iso9660, nfs, smb, and swap. |
Mount Options | Covered in the following section. |
Dump Value | Either 0 or 1. A value of 1 means that data is automatically saved to disk by the dump(8) command when you exit Linux. |
Filesystem Check Order | Determines the order that filesystems are checked by fsck(8) during the boot process. The root directory (/) filesystem should be set to 1, and other local filesystems should be set to 2. Removable filesystems such as /mnt/cdrom should be set to 0, which means that they are not checked during the Linux boot process. |
Although defaults is the appropriate mount option for most /etc/fstab filesystems, there are other options, such as those listed in Table 4-5. If you want to use multiple options, separate them by commas. Don't use spaces between options. The list in Table 4-5 is not comprehensive. You can find out more from the mount manual, which you can read by running the following command:
# man mount
Mount Option | Description |
---|---|
async | Data is read and written asynchronously. |
atime | The inode associated with each file is updated each time the file is accessed. |
auto | Searches through /etc/filesystems for the appropriate format for the partition; normally associated with a floppy or removable drive. |
defaults | Uses default mount options rw, suid, dev, exec, auto, nouser, and async. |
dev | Permits access to character devices such as terminals or consoles and block devices such as drives. |
exec | Allows binaries (compiled programs) to be run on this filesystem. |
noatime | The inode associated with each file is not updated when accessed. |
noauto | Requires explicit mounting. Common option for CD and floppy drives. |
nodev | Devices on this filesystem are not read or interpreted. |
noexec | Binaries (compiled programs) cannot be run on this filesystem. |
nosuid | Disallows setuid or setgid permissions on this filesystem. |
nouser | Only root users are allowed to mount the specified filesystem. |
remount | Remounts a currently mounted filesystem. Also an option for the mount command. |
ro | Mounts the filesystem as read-only. |
rw | Mounts the filesystem as read/write. |
suid | Allows setuid or setgid permissions on programs on this filesystem. |
sync | Reads and writes are done at the same speed (synchronously) on this filesystem. |
user | Allows nonroot users to mount this filesystem. By default, this also sets the noexec, nosuid, and nodev options. |
There are more options available, including noatime, noauto, nodev, noexec, nosuid, and nouser, which are the opposites of atime, auto, dev, exec, suid, and user, respectively.
To read USB keys and other removable media, RHEL now automatically mounts most devices (if you're running the GNOME or KDE Desktop Environments). While the details of this process are not part of the Red Hat Exam Prep guide, the process is based on configuration files in the /etc/udev/rules.d directory. If RHEL detects your hardware, it mounts the drive in the /media directory.
Here's relevant output from the mount command after I inserted a DVD and USB key on my system:
/dev/sda on /media/disk type vfat (rw,noexec,nosuid,nodev,shortname=winnt,uid=500) /dev/hdc on /media/RHEL-5 i386 Disc 1 type iso9660 (ro,noexec,nosuid,nodev,uid=500)
If it doesn't work for some reason, you can use the mount command directly. For example, the following command mounts a floppy in a drive:
# mount -t vfat /dev/fd0 /mnt
The -t switch specifies the type of filesystem (vfat). The device file /dev/fd0 represents the first floppy drive; /mnt/ is the directory through which you can access the files on the floppy after mounting. Just remember that it is important to unmount floppy disks before removing them. Otherwise, the data that you thought you wrote to the disk might still be in the cache. In that case, you would lose that data.
On the Job | Here's another example that I suspect you're unlikely to see on the Red Hat exams. Assume you're working with a Zip drive of 100MB. The device is set as /dev/hdd and formatted as a single partition (/dev/hdd1) with the Linux Native (ext3) filesystem. If properly detected, it'll be automounted as a subdirectory of /media. But remember that you have to eject a Zip drive with a command such as # eject /dev/hdd1. |
Once you run the mount command on a partition, it stays mounted until you unmount it or shut down or reboot your computer. This can cause problems. For example, if you've mounted a floppy and then physically removed the disk, Linux may not have had a chance to write the file to the disk. This situation also applies to Zip or other hotswappable removable drives.
Another example, with mounted NFS directories, occurs if the remote computer fails or the connection is lost; your system may become slow or even hang as it looks for the mounted directory.
This is where the automounter can help. It relies on the autofs daemon to mount configured directories as needed, on a temporary basis. In RHEL, the relevant configuration files are /etc/auto.master, /etc/auto.misc, and /etc/auto.net. If you use the automounter, keep the /misc and /net directories free. Red Hat configures automounts on these directories by default, and they won't work if local files or directories are stored there.
Onn the Job | You won't even see the /misc or /net directories unless you properly configure /etc/auto.master and activate the autofs daemon. The /etc/auto.smb configuration file does not work for CIFS shares, such as from Windows XP/ Vista. One option for CIFS shares is an /etc/auto.cifs configuration, described at www.howtoforge.com/accessing_windows_or_samba_shares_using_autofs. |
Default automounter settings are configured in /etc/sysconfig/autofs. The default settings include a timeout of 300 seconds; in other words, if nothing happens on an automount within that time, the share is automatically unmounted:
DEFAULT_TIMEOUT=300
The DEFAULT_BROWSE_MODE can allow you to search from available mounts. The following directive disables it by default:
DEFAULT_BROWSE_MODE="no"
There are a wide variety of additional settings available, as commented in the /etc/sysconfig/autofs file, which also assumes that the autofs daemon is active. To check, run the following command:
# /etc/init.d/autofs status
Make sure to run this command with administrative privileges; otherwise, you'll get the misleading message "automount is stopped".
The standard /etc/auto.master file includes a series of comments, with three default commands. The first refers to the /etc/auto.misc file as the configuration file for this directory. The /net -hosts command allows you to specify the host to automount a network directory, as specified in /etc/auto.net.
/misc /etc/auto.misc /net -hosts +auto.master
In any case, these commands point to configuration files for each service. Shared directories from each service are automatically mounted, on demand, on the given directory (/misc and /net).
You can set up the automounter on other directories. One popular option is to set up the automounter on the /home directory. In this way, you can configure user home directories on remote servers, mounted on demand. Users are given access to their home directories upon login, and based on the DEFAULT_TIMEOUT directive in the /etc/sysconfig/autofs file, all mounted directories are automatically unmounted 300 seconds after that user logs off the system.
# /home /etc/auto.home
This works only if you don't already have a /home directory on your computer.
Red Hat conveniently provides standard automount commands in comments in the /etc/auto.misc file. It's helpful to analyze this file in detail. I use the default RHEL version of this file. The first four lines are comments, which I skip. The first command is:
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
In RHEL, this command is active by default-assuming you've activated the autofs service. In other words, if you have a CD in the /dev/cdrom drive, you can access its files through the automounter with the ls /misc/cd command. The automounter accesses it using the ISO9660 filesystem. It's mounted read-only (ro); set user ID permissions are not allowed (nosuid), and devices on this filesystem are not used (nodev).
With the command from /etc/auto.master, the CD is unmounted 300 seconds after the last time it's accessed. There are a number of other sample commands, commented out, ready for use. Of course, you would have to delete the comment character (#) before using any of these commands. The first of these commented commands allows you to set up a /misc/linux mount point from a shared NFS directory, /pub/linux, on the ftp.example.org computer:
#linux -ro,soft,intr ftp.example.org:/pub/linux
The next command assumes that the /boot directory is stored on the /dev/hda1 partition. With this command, you don't need to mount /boot when you start Linux. Instead, this command allows you to automount it with the mount /misc/boot command.
#boot -fstype=ext2 :/dev/hda1
The following three commands apply to a floppy disk drive on your computer. The first command, set to an "auto" filesystem type, searches through /etc/filesystems to try to match what's on your floppy. The next two commands assume that the floppy is formatted to the ext2 filesystem.
#floppy -fstype=auto :/dev/fd0 #floppy -fstype=ext2 :/dev/fd0 #e2floppy -fstype=ext2 :/dev/fd0
The next command points to the first partition on the third SCSI drive. The jaz at the beginning suggests this is suitable for an Iomega-type Jaz drive.
#jaz -fstype=ext2 :/dev/sdc1
Finally, the last command assumes that you want to apply the automounter to the IDE drive connected as the slave on the secondary controller. The removable at the beginning suggests this is suitable for removable hard drives.
#removable -fstype=ext2 :/dev/hdd
With the possible exception of the floppy commands, you'll need to modify these lines for your own hardware.
With the /etc/auto.net configuration script, you can review and read shared NFS directories. As IP addresses don't work with this script, you'll need either a DNS server or at least an appropriate database entry in /etc/hosts that associates the host name with the IP address of the NFS server.
To make this work, make sure execute permissions are enabled on this file. If necessary, run the following command:
# chmod 755 /etc/auto.net
Activate the automounter as described in the next section. Then to review available shares on my enterprise5fc6d system, I run the following command:
# /etc/auto.net enterprise5fc6d -fstype=nfs,hard,intr,nodev,nosuid \ /inst enterprise5fc6d:/inst
This tells me that the /inst directory on the enterprise5fc6d system is shared via NFS. Based on the directives in /etc/auto.master, I can access this share (assuming appropriate firewall and SELinux settings) with the following command:
# ls /net/enterprise5fc6d/inst
Once you've configured the desired configuration files, you can activate the automounter. As it is governed by the autofs daemon, you can activate it (and make it reread your configuration files) with the following command:
# service autofs restart
With the default command in the /etc/auto.misc file, you should now be able to mount a CD on the /misc/cd directory, automatically, just by accessing the configured directory. Once you have a CD in the drive, the following command should work:
# ls /misc/cd
If you were to make /misc/cd your current directory, the automounter would ignore any timeouts. Otherwise, /misc/cd is automatically unmounted according to the timeout, which according to the command in /etc/auto.master is 60 seconds.
Exercise 4-1: Configuring the Automounter
In this exercise, you'll test the automounter. You'll need at least a CD. Ideally, you should also have a USB key, or possibly a floppy disk. First, however, you need to make sure that the autofs daemon is in operation, modify the appropriate configuration files, and then restart autofs. You can then test the automounter for yourself.
From the command line interface, run the following command to make sure the autofs daemon is running:
# service autofs start
Review the /etc/auto.master configuration file in a text editor. The defaults are sufficient if you want to activate the settings in /etc/auto.misc and /etc/ auto.net.
Check the /etc/auto.misc configuration file in a text editor. Make sure it includes the following line (which should already be there by default). Save and exit from /etc/auto.misc.
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
Now restart the autofs daemon. (I know, this isn't the most efficient method, but it's a good habit to check the status of a service.)
# service autofs restart
The automounter service is now active. Insert a CD into the drive on your computer and when you run the following command, you should see the contents of your CD:
# ls /misc/cd
Run the ls /misc command immediately. You should see the CD directory in the output.
Wait at least five minutes, and repeat the previous command. What do you see?
Optional Exercise 4-2: A Floppy Drive and the Automounter
Now that you're more familiar with the automounter, try using it on a USB key. If you don't have one, try it with a floppy drive, or experiment with some of the other commented commands in /etc/auto.misc.