Mounting file system resources with AutoFS

AutoFS allows resources to be mounted on an as-needed basis, simplifying the management of NFS shares. This tip walks you through the process of setting up AutoFS.

In Linux, you need to mount resources that you want to use. This way of working is not the most convenient method,...

and for that reason, we have AutoFS. With AutoFS, resources are mounted automatically when they are needed. This is especially convenient between servers where resources are accessed over the Network File System (NFS). In this article, you'll learn how to set up AutoFS.

AutoFS is typically used in NFS server environments. There are two sides to its configuration. On one side, there is the NFS server, on which NFS shares have to be defined and exported. This is part of the normal setup procedure of an NFS server. At the node where you want to set up AutoFS file access, you need the automount process and some associated configuration files. The automount process knows which mount points to monitor, and if activity occurs in a certain mount point, automount automatically mounts the resource. Because the process accomplishes this procedure, there is no need for the current user to perform the mount with root permissions. Also, if the resource is idle for a certain period of time, automount will automatically unmount it.

The start point of the automount configuration is the /etc/sysconfig/autofs file. In this file, you can define some default parameters, such as the master configuration file and the default timeout that causes an inactive mount to be disconnected. Below you'll see some lines that you'll typically find in this file:


Of these, the line MASTER_MAP_NAME defines the name of the starting point of the AutoFS configuration. The next parameter is TIMEOUT, which identifies the maximum idle time after which the resource is automatically dismounted. The third line in this example specifies how long AutoFS should try if it doesn't succeed in mounting the device.

AutoFS maps
If the automount service is loaded, it gets its starting information from the /etc/auto.master file. By default, you'll find the following three lines in this file:

/misc    /etc/auto.misc
/net      -hosts
/-          /etc/auto.direct

The first of these refers to the file /etc/auto.misc. In this file, AutoFS will find additional information about mounts that have to be created automatically if activity occurs in the /misc directory. Next, there is the specification for the /net directory. This line refers to the special map /hosts which is used when a user tries to access anything under /net. Last, there is a line that tells AutoFS to consider the configuration in the file /etc/auto.direct.

This last line is what you'll probably be working with most. It allows you to specify custom directories on which you can mount AutoFS file systems. If, for example, you want to mount the NFS share to /data, that is exported by server1 automatically on /data on your computer (which is the one that runs the AutoFS service). You would add the following line to auto.direct:

/data    -rw,sync           server1:/data

After creating this configuration, just make sure to restart the AutoFS service to automatically get access to /server1:/data when you access the /data share.

Indirect maps
In the example above, you have created a direct map. In this map, the auto.direct file is used to specify which local directory should be used to mount which NFS share with which options. An alternative to using direct maps is the indirect map. The big difference is that with these, the resources are all mounted under one common parent directory. To define indirect maps, the following line from auto.master is used:

/misc    /etc/auto.misc

In this line, the file auto.misc is called to create mounts that all occur under the /misc directory. In this file, you'll see lines that contain three different parts: a relative path name where the resource has to be mounted, the mounts options and the name of the share that is to be mounted. You can see an example below:

data     -rw,sync           server1:/data

When editing the auto.misc file, you'll see that some other lines are already defined. These lines have to make it easy for you to mount other media. The first of these specifies that the cdrom device is automatically mounted on /misc/cd when this directory is accessed. Use this line as an example to automount any device you'd like to. AutoFS can do much more than just mounting NFS devices.

Listing: /etc/auto.misc default contents
[[email protected] ~]# cat /etc/auto.misc


# This is an automounter map and it has the following format

# key [ -mount-options-separated-by-comma ] location

# Details may be found in the autofs(5) manpage


cd                    -fstype=iso9660,ro,nosuid,nodev       :/dev/cdrom


# the following entries are samples to pique your imagination

#linux              -ro,soft,intr                  ftp.example.org:/pub/linux

#boot               -fstype=ext2                :/dev/hda1

#floppy                        -fstype=auto                :/dev/fd0

#floppy                        -fstype=ext2                :/dev/fd0

#e2floppy        -fstype=ext2                :/dev/fd0

#jaz                  -fstype=ext2                :/dev/sdc1

#removable      -fstype=ext2                :/dev/hdd

Working with AutoFS makes managing NFS shares easy. Instead of using fixed mounts that apply to everyone and are defined in the /etc/fstab file, you can use AutoFS to mount devices only when a particular resource is accessed. This makes working with remote resources more convenient in large server environments.

Dig Deeper on Linux servers