Mounting a filesystem automatically using 'autofs'

February 04, 2005
There was a drawback in Linux when compared to other OSes in that, you cannot read the contents of a filesystem as easily as you do in other OSes like Windows without executing the mount commands (this is especially true for removable media like a Floppy, CDROM or remote filesystems like an NFS share). For example, if you want to read the contents of a floppy, you have to mount the floppy into a directory using the mount command. And after you have read the data , and before you pull out the floppy from the floppy drive, you have to remember to unmount the floppy using umount command. If by any chance, you have forgotten to unmount the floppy, there is a good chance that your data will be corrupted.
But now you have an easy way out of the hassle of mounting and unmounting your filesystem and using it like you use in windows. This is achieved by the autofs daemon.
Autofs is a standard feature in all major linux distributions. It is installed by default. Here I will explain how to configure autofs in RedHat distribution. The configuration is the same in other distributions other than the chkconfig and service scripts which are a RedHat feature.
First check if the autofs daemon is configured to start at boot time by running the command:
# chkconfig autofs --list
autofs 0:off 1:off 2:off 3:off 4:off 5:off 6:off
As you can see from the above listing that autofs is OFF on my system. So I turn it ON using the command:
# chkconfig autofs on
# chkconfig autofs --list
autofs 0:off 1:off 2:on 3:on 4:on 5:on 6:off
Now the autofs daemon is configured to start automatically when you boot up your machine.
The main configuration files for autofs is /etc/auto.master. This file has the syntax as follows:
Each mount point is given on a seperate line. For eg:
# File: /etc/auto.master
/mnt /etc/auto.misc
Where, /mnt is the mount point and it points to another file called autofs.misc (the file can have any name but following precedence, it should start with the name auto), which contains the details of how and what to mount.Suppose I have to make autofs automount my floppy whenever I change into my /mnt/floppy directory, and unmount it automatically when I come out of the /mnt/floppy directory, I just include the following line into my /etc/auto.misc file.
# File: /etc/auto.misc
floppy -fstype=auto,rw,users :/dev/fd0
Each line in the file is divided into 3 sections seperated by one or more spaces.They are :
  • A key - The key can be any single word but preferably the name of the device or the name of the sub-directory below the mount point specified in the auto.master file.
  • The mount options for the filesystem associated with the key. In the listing of my auto.misc file, I have set it to mount with read/write permission and autodetect the type of filesystem - whether ext3 or vfat etc - and who all can mount the filesystem (see man mount for more options).
  • The path of the device file associated with the filesystem. It should be preceeded by a colon (:).
Now I save and exit the /etc/auto.misc file. The last thing to do is to restart the autofs daemon to re-read the /etc/auto.master file.
# service autofs restart
Note: Each time any changes are made to the /etc/auto.master file, you have to restart the autofs daemon to bring the changes to effect. But you need not restart autofs if you change the contents of auto.misc file or the contents of any other files listed in the /etc/auto.master file. Now I test my setup by inserting a floppy into the floppy drive and execute the following command:
# cd /mnt/floppy
Success!! The floppy gets mounted automatically and I am able to list the contents of the floppy.Now I come out of the directory. And after a period of inactivity (default is 60 sec), the floppy is automatically unmounted.
You can likewise set the remote NFS share (at IP address to mount using autofs. For example, I could append a line in /etc/auto.misc file as follows:
# File: /etc/auto.misc
nfsshare -fstype=auto,soft,intr
cdrom -fstype=iso9660,ro :/dev/cdrom
.. and so on.