| |

Preventing AutoFS from slowing down the system

I am using autofs (or automout) to handle shares presented on an NFS server I am using. When I am not home, or when the server is inaccessible, I do not want to be delayed by autofs failure to mount. This failure could take a long while, causing Autofs delay mount. During that time – every reference to the target, even by a symlink, results at a major delay and slowdown.

I have constructed a framework to allow managing autofs configuration, based on ping check to the remote host. This concept can be extended in the future to other methods as well. The script is very simple, and is based on how autofs works, which I will explain in some details below. Using this method will reduce Autofs delay mount to nearly zero.

The file /etc/auto.master holds a key list of maps to query in order to get a mount directive. For the common ‘indirect’ maps, it means that there is a directory path (for example – /misc), and then – a file. The indirect map file contains a list of the directories (keys) under the indirect path (/misc) to query. Here’s an example of the contents of the file /etc/auto.misc

music   -fstype=nfs,rw,soft,vers=3   nasserver:/data/music

In this example we can see that when we try to access /misc/music, this call itself would trigger autofs to try to mount nasserver:/data/music with the relevant parameters specified on the 2nd field.

In most common cases, the output of any attempt to query the path /misc/music should result in a string like this:

"-fstype=nfs,rw,soft,vers=3   nasserver:/data/music"

We can produce smarter results by using code to manipulate the output in a manner that suites us. For example, we could check first that nasserver responds to ping, or else – return an error. Any non-zero response of this map would prevent a mount and then fail, with a message saying this target doesn’t exist (‘no such file or directory’).

When we want to use a script to produce the output map, we should specify in /etc/auto.master that a program is to run. We do it by setting a line like this in /etc/auto.master:

/misc   program:/usr/local/sbin/misc-mapper.sh

In this case, a key lookup in /misc/ (meaning – attempting an access to any directory under /misc), this script will trigger this script. If the key is correct, it will result in a return value of zero and a stdout of a string such as shown above.

I have decided to create a script that can be flexible enough to handle multiple servers and varying conditions. The script will use an external optional configuration file. Here is the script /usr/local/sbin/misc-mapper.sh, followed by an example of configuration file and a little more in-depth explanation:

#!/bin/bash
CONFDIR=/etc/auto.master.conf
# Defaults
SRV=nassrv
OPTS="-rw,soft,vers=3"
BASE=/data


# Create in /etc/auto.master.conf a file called ${KEY}.conf with the following settings to override:
# SRV=other_server # or can be a FQDN, or even empty
# OPTS=mount options, like "-fstype=davfs,rw,uid=1000,gid=1000" or "-rw,soft,intr,rsize=1024000,wsize=1024000" or "-fstype=cifs,uid=1000,gid=1000,username=cifsuser,password=password"
# BASE=URL Base, like (when NFS server presents) /share/temp ; or https\://mycloud.nextcloud.com/remote.php/webdav/ or //cifsserver/config
# KEY= If you want to override the provided local key, use it, or set it to nothing. Otherwise, do not set it. and use the original key by autofs
# SKIP_PING= -> if set, will not ping before

KEY=${1}
function obtain_info() {
  if [ -f ${CONFDIR}/${KEY}.conf ] ; then
    . ${CONFDIR}/${KEY}.conf
  fi
}

obtain_info
if [ -z "${SKIP_PING}" ] ; then
  if ! ping -W 1 -c 1 ${SRV} > /dev/null 2>&1 ; then
    exit 1
  fi
fi
echo "$OPTS $SRV:${BASE}/${KEY}"

This script will set default values for SRV, OPTS and BASE. In general, unless specified otherwise, presenting the script with a single argument $1 (‘$KEY’), it will attempt to produce an output like this:

"-rw,soft,vers=3    nassrv:/data/${KEY}"

Using a configuration file, we can override it to present different paths. For example, if we want to compare to a result such as presented in this line:

"-rw,hard,nointr,vers=4.1  myserver:/data/common/${KEY}"

we would set the following variables in a file called /etc/auto.master.conf/${KEY}.conf # Replace the variable ${KEY} with your actual mapping)

SRV=myserver
OPTS="-rw,hard,nointr,vers=4.1"
BASE=/data/common

The script will attempt to ping the destination ${SRV} – one packet, with a 1 second timeout, before continuing. It ensures that in case the server is not accessible, the mount will fail almost immediately, and will not wait for a TCP connection timeout, which could last up to a minute, holding our session waiting. We can override this behaviour by setting the variable SKIP_PING in the ${KEY}.conf to any non-empty value.

There are cases where we use different syntax which will produce different results. For example, CIFS configuration file might look like this:

SRV=
OPTS="-fstype=cifs,uid=1000,gid=1000,username=myuser,password=mypassword
BASE="//cifssrv"
KEY="config"
SKIP_PING="yes"

The output of this configuration would look like this:

"-fstype=cifs,uid=1000,gid=1000,username=myuser,password=mypassword ://cifssrv/config"

and the script will not use ping to check access to the CIFS server.

Another non-NFS example would be using webdav. An example configuration would look like this:

SRV=
OPTS="-fstype=davfs,rw,uid=1000,gid=1000"
BASE="https\://mysystem.nextcloud.come/remote.php/webdav"
KEY=
SKIP_PING="yes"

The output of this script would be the string

"-fstype=davfs,rw,uid=1000,gid=1000 :https\://mysystem.nextcloud.come/remote.php/webdav"

One last example would be for the original entry presented in /etc/auto.misc – using a CD. In this example – the file /etc/auto.master.conf/cd.conf contents are:

SRV=
OPTS="-fstype=iso9660,ro,nosuid,nodev"
BASE="/dev/cdrom"
KEY=
SKIP_PING="yes"

The result is the string for mounting a CD using autofs:

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

To sum things up – this script can reduce Autofs delay mount when accessing remote servers. This script can be modified and improved. It both demonstrates the use of ‘program’ directive in autofs, and solves an annoying problem with how NFS mounts for inaccessible servers can cause a delay.

I added this script and a short info to my Github repo. Check it out.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.