Red Hat System Administration II
Describe the benefits of using the automounter, and automount NFS exports by using direct and indirect maps.
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.
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 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.
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.
To use the autofs service, you must install the autofs and nfs-utils packages.
[root@host ~]# dnf install autofs nfs-utilsThe master map file (/etc/auto.master) is the default configuration file for the autofs service. You can use the /etc/autofs.conf file to change the master map file for the autofs service.
Use the /etc/auto.master.d directory to configure the master map file. This file identifies the base directory for mount points, and identifies the mapping file to create the automounts.
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.
The master map uses the following format for automounting:
[root@host ~]#cat /etc/auto.master.d/demo.autofsmount-pointmap-file
Replace the variable with the directory to be used as the base mount point for automounts.mount-point
Replace the variable with the file to be used as the map file. The map file must be created before starting the map-fileautofs service.
Map files configure the properties of individual on-demand mount points.
The automounter creates the directories if they do not exist. If the directories already exist before the automounter starts, then the automounter will not remove them when it exits. If a timeout is specified, then the directory is automatically unmounted if the directory is not accessed for the timeout period.
If you use a single directory name for the mount point, then the directory is mounted as an indirect mount. If you use the full path for the mount point, then the directory is mounted as a direct mount.
The map file uses the following format for automounting:
[root@host ~]#cat /etc/auto.demomount-pointmount-optionssource-location
Replace the variable with the mount-pointautofs mount point.
Replace the variable with the options to be used for mounting the mount-optionsautofs mount point.
Replace the variable with the source location of the mount.source-location
Each mapping file identifies the mount point, mount options, and the source location to mount for a set of automounts.
For indirect automounting, include the following content in the master map file:
[root@host ~]# cat /etc/auto.master.d/indirect.autofs
/shares /etc/auto.indirectThis entry uses the /shares directory as the base mount point for the indirect automount. The /etc/auto.indirect map file contains the mount details.
The following example shows a sample entry in the map file:
[root@host ~]# cat /etc/auto.indirect
work -rw,sync hosta:/shares/workThe naming convention for a map file is /etc/auto., where name reflects the content of the map.name
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 hosta:/shares/work. For this automount to succeed, the NFS server, hosta, must export the directory with read/write access, and the user that requests access must have standard Linux file permissions on the directory. If hosta exports the directory with read/only access, then the client gets read/only access even if it requested read/write access.
A direct map file maps 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, include the following content in the master map file:
[root@host ~]# cat /etc/direct.autofs
/- /etc/auto.directThe naming convention for a map file is /etc/auto., where name reflects the content of the map.name
All direct map entries use the /- path as the base directory. In this case, the /etc/auto.direct mapping file contains the mount details.
The following example shows a sample entry in the map file:
[root@host ~]# cat /etc/auto.direct
/mnt/docs -rw,sync hosta:/shares/docsThe 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.
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 the hosta:/shares export contains two or more subdirectories, and they are accessible with the same mount options, then the entry for the /etc/auto.indirect file might appear as follows:
[root@host ~]# cat /etc/auto.indirect
* -rw,sync hosta:/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 hosta:/shares/work is mounted. As with the indirect map file example, the autofs service creates and removes the work directory automatically.
After you have configured the master map and map files, use the systemctl command to start and enable the autofs service.
[root@host ~]# systemctl enable --now autofs
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.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. Then use the systemctl start command to enable the automount configuration.unit.automount
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 the autofs direct maps.
References
autofs(5), automount(8), auto.master(5), mount.nfs(8), and systemd.automount(5) man pages
For further information, refer to the Mounting File Systems on Demand chapter in the Managing File Systems guide at https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html-single/managing_file_systems/index#the-autofs-service_mounting-file-systems-on-demand