Bookmark this page

Automount Network-Attached Storage

Objectives

Describe the benefits of using the automounter, and automount NFS exports by using direct and indirect maps.

Mount NFS Exports with the Automounter

The automounter is a service (autofs) that automatically mounts file systems and NFS exports on demand, and automatically unmounts file systems and NFS exports when the mounted resources are no longer in current use.

The automounter function was created to solve the problem that unprivileged users do not have sufficient permissions to use the mount command. Without use of the mount command, normal users cannot access removable media such as CDs, DVDs, and removable disk drives. Furthermore, if a local or remote file system is not mounted at boot time by using the /etc/fstab configuration, then a normal user cannot mount and access those unmounted file systems.

The automounter configuration files are populated with file-system mount information, in a similar way to /etc/fstab entries. Although /etc/fstab file systems mount during system boot and remain mounted until system shutdown or other intervention, automounter file systems do not necessarily mount during system boot. Instead, automounter-controlled file systems mount on demand, when a user or application attempts to enter the file-system mount point to access files.

Automounter Benefits

Resource use for automounter file systems is equivalent to file systems that are mounted at boot, because a file system uses resources only when a program is reading and writing open files. Mounted but idle file systems and unmounted file systems use almost no resources.

The automounter advantage is that by unmounting the file system each time that it is no longer in use, the file system is protected from unexpected corruption when it is open. When the file system is directed to mount again, the autofs service uses the most current mount configuration, unlike an /etc/fstab mount, which might still use a configuration that was mounted months ago during the last system boot. Additionally, if your NFS server configuration includes redundant servers and paths, then the automounter can select the fastest connection each time that a new file system is requested.

The Automounter autofs Service Method

The autofs service supports the same local and remote file systems as in the /etc/fstab file, including NFS and SMB file sharing protocols, and supports the same protocol-specific mount options, including security parameters. File systems that are mounted through the automounter are available by default to all users, but can be restricted through access permission options.

Because the automounter is a client-side configuration that uses the standard mount and umount commands to manage file systems, automounted file systems in use exhibit the same behavior to file systems that are mounted by using /etc/fstab. The difference is that an automounter file system remains unmounted until the mount point is accessed, which causes the file system to mount immediately, and to remain mounted when the file system is in use. When all files on the file system are closed, and all users and processes leave the mount point directory, the automounter unmounts the file system after a minimal timeout.

Direct and Indirect Map Use Cases

The automounter supports both direct and indirect mount-point mapping, to handle the two types of demand mounting. A direct mount is when a file system mounts to an unchanging, known mount point location. Almost all the file system mounts that you configured, before learning about the automounter, are examples of direct mounts. A direct mount point exists as a permanent directory, the same as other normal directories.

An indirect mount is when the mount point location is not known until the mount demand occurs. An example of an indirect mount is the configuration for remote-mounted home directories, where a user's home directory includes their username in the directory path. The user's remote file system is mounted to their home directory, only after the automounter learns which user specified to mount their home directory, and determines the mount point location to use. Although indirect mount points appear to exist, the autofs service creates them when the mount demand occurs, and deletes them again when the demand ended and the file system is unmounted.

Configure the Automounter Service

The process to configure an automount has many steps.

First, you must install the autofs and nfs-utils packages.

[user@host ~]$ sudo dnf install autofs nfs-utils

These packages contain all requirements to use the automounter for NFS exports.

Create a Master Map

Next, add a master map file to /etc/auto.master.d. This file identifies the base directory for mount points, and identifies the mapping file to create the automounts.

[user@host ~]$ sudo vim /etc/auto.master.d/demo.autofs

The name of the master map file is mostly arbitrary (although typically meaningful), and it must have an extension of .autofs for the subsystem to recognize it. You can place multiple entries in a single master map file; alternatively, you can create multiple master map files, each with its own logically grouped entries.

Include the following content in the master map entry for indirectly mapped mounts:

/shares  /etc/auto.demo

This entry uses the /shares directory as the base for indirect automounts. The /etc/auto.demo file contains the mount details. Use an absolute file name. The auto.demo file must be created before starting the autofs service.

Create an Indirect Map

Now, create the mapping files. Each mapping file identifies the mount point, mount options, and source location to mount for a set of automounts.

[user@host ~]$ sudo vim /etc/auto.demo

The mapping file-naming convention is /etc/auto.name, where name reflects the content of the map.

work  -rw,sync  serverb:/shares/work

The format of an entry is mount point, mount options, and source location. This example shows an indirect mapping entry. Direct maps and indirect maps that use wildcards are covered later in this section.

Known as the key in the man pages, the autofs service automatically creates and removes the mount point. In this case, the fully qualified mount point is /shares/work (see the master map file). The autofs service creates and removes the /shares and /shares/work directories as needed.

In this example, the local mount point mirrors the server's directory structure. However, this mirroring is not required; the local mount point can have an arbitrary name. The autofs service does not enforce a specific naming structure on the client.

Mount options start with a dash character (-) and are comma-separated with no white space. The file-system mount options for manual mounting are also available when automounting. In this example, the automounter mounts the export with read/write access (rw option), and the server is synchronized immediately during write operations (sync option).

Useful automounter-specific options include -fstype= and -strict. Use fstype to specify the file-system type, for example nfs4 or xfs, and use strict to treat errors when mounting file systems as fatal.

The source location for NFS exports follows the host:/pathname pattern, in this example serverb:/shares/work. For this automount to succeed, the NFS server, serverb, must export the directory with read/write access, and the user that requests access must have standard Linux file permissions on the directory. If serverb exports the directory with read/only access, then the client gets read/only access even if it requested read/write access.

Wildcards in an Indirect Map

When an NFS server exports multiple subdirectories within a directory, then the automounter can be configured to access any of those subdirectories with a single mapping entry.

Continuing the previous example, if serverb:/shares exports two or more subdirectories, and they are accessible with the same mount options, then the content for the /etc/auto.demo file might appear as follows:

*  -rw,sync  serverb:/shares/&

The mount point (or key) is an asterisk character (*), and the subdirectory on the source location is an ampersand character (&). Everything else in the entry is the same.

When a user attempts to access /shares/work, the * key (which is work in this example) replaces the ampersand in the source location and serverb:/exports/work is mounted. As with the indirect example, the autofs service creates and removes the work directory automatically.

Create a Direct Map

A direct map is used to map an NFS export to an absolute path mount point. Only one direct map file is necessary, and can contain any number of direct maps.

To use directly mapped mount points, the master map file might appear as follows:

/-  /etc/auto.direct

All direct map entries use /- as the base directory. In this case, the mapping file that contains the mount details is /etc/auto.direct.

The content for the /etc/auto.direct file might appear as follows:

/mnt/docs  -rw,sync  serverb:/shares/docs

The mount point (or key) is always an absolute path. The rest of the mapping file uses the same structure.

In this example, the /mnt directory exists, and the autofs service does not manage it. The autofs service creates and removes the full /mnt/docs directory automatically.

Start the Automounter Service

Lastly, use the systemctl command to start and enable the autofs service.

[user@host ~]$ sudo systemctl enable --now autofs
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.

The Alternative systemd.automount Method

The systemd daemon can automatically create unit files for entries in the /etc/fstab file that include the x-systemd.automount option. Use the systemctl daemon-reload command after modifying an entry's mount options, to generate a new unit file, and then use the systemctl start unit.automount command to enable that automount configuration.

The naming of the unit is based on its mount location. For example, if the mount point is /remote/finance, then the unit file is named remote-finance.automount. The systemd daemon mounts the file system when the /remote/finance directory is initially accessed.

This method can be simpler than installing and configuring the autofs service. However, a systemd.automount unit can support only absolute path mount points, similar to autofs direct maps.

References

autofs(5), automount(8), auto.master(5), mount.nfs(8), and systemd.automount(5) man pages

Revision: rh199-9.0-4fecb06