Managing On-Demand Mounts Using autofs

WARNING:

This documentation is a draft and is not meant for production use. Branch: OL10-FSADMIN

On-demand mounts are used to automatically mount file systems when they're accessed, rather than maintaining connections for those mounts all the time. When a file system becomes inactive for a certain period, the file system is unmounted.

Using on-demand mounts frees up system resources and improves system performance. Furthermore, when working with network based file systems, such as NFS, and Samba, using on-demand mounts can reduce the likelihood of stale mounts.

On-demand mounts are handled by the automounter, which consists of two components: the autofs kernel module and the automount user-space daemon. Configuration is handled in a set of configuration files including /etc/autofs.conf and /etc/auto.master.

See also the autofs(5), auto.master(5), and autofs.conf(5) manual pages.

On-demand mounts are commonly used with network based file systems such as NFS. For more information about NFS administration, see Oracle Linux 10: Managing the Network File System.

Installing and Enabling autofs

Install the autofs package to use on-demand mounting of file systems. Enable and start the autofs systemd service after configuration to make it active and persistent across system reboots.

  1. Install the autofs package and any other packages that are required to support remote file systems:
    sudo dnf install autofs
  2. Edit the /etc/auto.master configuration file to define map entries that are appropriate to the file systems.

    See Configuring autofs for more information.

  3. Enable and start the autofs service:
    sudo systemctl enable --now autofs

Configuring autofs

Autofs uses a combination of configuration files to manage on-demand mounting of file systems.

Master Map File (/etc/auto.master)

The master map file, at /etc/auto.master, serves is the primary configuration file for autofs mounts. It contains a list of entries that specify the relationship between mount points and their corresponding map files.

  • Mount point: The directory where the file system can be mounted.
  • Map file: The location of the map file that defines the file system details.
  • Options: Optional parameters that control the autofs behavior, such as the timeout value.
/mnt/shared /etc/auto.shared --timeout=300

Each map entry specifies a mount point and a map file that contains definitions of the remote file systems that can be mounted, for example:

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

The previous example shows the following types of map entries:

  • /-: direct map entry. Direct map entries always specify /- as the mount point.

  • /misc: indirect map entry.

  • /net: host map entry. Host maps always specify the keyword -hosts instead of a map file.

Map Files

Map files contain the actual file system definitions, specifying the type of file system, mount options, and other relevant details. You can define two types of mappings:

  • Direct map files: A direct map contains definitions of directories that are automounted at the specified absolute path. In the example, the auto.direct map file might contain an entry similar to the following:

    /mnt/data   -fstype=nfs,ro,soft     host01:/export/data

    This entry is a directive to do the following:

    • Mount the file system /export/data that's exported by host01 by specifying the ro and soft options.

    • Create the /mnt/data mount point if it doesn't already exist. If the mount point exists , the mounted file system hides any existing files that it contains.

  • Indirect map files: An indirect map contains definitions of directories or keys that are automounted relative to the mount point (/misc) that's specified in the /etc/auto.master file. For example, the /etc/auto.misc map file might contain entries similar to the following:

    xyz       -ro,soft                               host01:/xyz
    cd        -fstype=iso9600,ro,nosuid,nodev        :/dev/cdrom
    abc       -fstype=xfs                            :/dev/hda1
    fenetres  -fstype=smb3,credentials=credfile      ://fenetres/c

    Note that the /misc directory must already exist; however, the automounter creates a mount point for the keys xyz, cd , and so on, if they don't already exist, and then removes them when it unmounts the file system. So if a program accesses /misc/xyz/, the mount point is created and mounted on demand, so that you can access the file system immediately.

Other Configuration Files

Other configuration files are available to customize autofs behavior, including:

  • /etc/autofs.conf: The global configuration file for autofs, used to control functionality such as logging, debugging, and the location of the master map file.

  • /etc/auto.net: A useful script that's used by autofs to automatically mount NFS shares for resolvable hosts on the network that have NFS shares exposed. If a host map entry exists, and a command references an NFS server that's relative to the mount point (/net) by name, the automounter mounts all the directories that the server exports within a subdirectory of the mount point named for the server. For example, the cd /net/host03 command causes the automounter to mount all exports from host03 under the /net/host03 directory. By default, the automounter uses the nosuid,nodev,intr mount options. These options are configurable in /etc/auto.master.

    The name of the NFS server must be resolvable to an IP address in DNS or must be present in the /etc/hosts file. You can equally reference hosts directly by their IP address. For example, ls /net/192.168.0.3/ mounts all NFS exports on the server at the IP address 192.168.0.3.

  • /etc/auto.smb: A useful script that's used by autofs to automatically mount CIFS shares for resolvable Windows or Samba file servers on the network. autofs automatically scans the host for CIFS file shares and exposes these within the /cifs/host/ directory. To use this, you must install the samba-client package and add an entry to /etc/auto.master:

    /cifs  /etc/auto.smb --timeout=300