Autofs an fstab alternative

I have been using the /etc/fstab to auto mount the NFS exports from my storage server. However, there were times that when I rebooted my NAS and NFS clients rebooted as well, the clients got stuck because it could not find the NFS server. The solution that I found to address this issue is to use autofs. Here is a blurb describing autofs.

Autofs is similar to fstab, but the mount point will only get mounted if a service or the user accessed the shared directory. If the shared directory has been idle or not being accessed for 5 minutes (by default), autofs will unmount the shared directory until the system or the user uses access the shared directory again.

The flow goes something like this:

  1. Autofs reads the /etc/auto.master or /etc/auto.master.d/*.autofs to find mount points
  2. Then autofs will check the “key” located in /etc/auto.something file you will be creating
  3. If the target “key” is accessed, the key is mounted

To make sense of this, I like to think the mount point in /etc/auto.master or /etc/auto.master.d/*.autofs is the based directory. The “key” is the relative path. E.g. /srv is the base mount point and the nfs_frigate is the relative path, so the absolute path is /srv/nfs_frigate. This is called indirect mapping. The direct mapping uses the /- for mount point.

Figure 1

Another thing about indirect mapping, if you cd to the based mount point and run the command ls, you will see nothing. You will need to specify the “key” directory to access it. E.g. ls -l /srv/nfs_frigate.

To install autofs.

apt install autofs nfs-common

To configure the mount point, we need to edit the /etc/auto.master file. There are two locations where we can specify the mount point. You can add a mount point below the line 7 or /etc/auto.master.d/. I added line 8 as an mount point example. The 3rd column is optional basically, changing the default 5 minutes timeout to 30 minutes.

#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
#/misc	/etc/auto.misc
#/srv   /etc/auto.nfs_surveillance  --timeout=1800
#

Here is another way to create a mount point under /etc/auto.master.d/ directory. Create a file with an extension of .autofs.

# Create the mount point. The file name can be anything that make sense to you.
echo -e "/srv\t/etc/auto.nfs_surveillance\t--timeout=1800" > /etc/auto.master.d/srv.autofs

# Verify the content of the mount point.
cat /etc/auto.master.d/srv.autofs
/srv	/etc/auto.nfs_surveillance	--timeout=1800

Now, that the mount point has been created, we need to create the key.

# Create the key file. The file name can be anything that make sense to you.
echo -e "Frigate storage" >> /etc/auto.nfs_surveillance
echo -e "nfs_frigate\t-fstype=auto\t10.11.7.9:/mnt/disk20/frigate" >> /etc/auto.nfs_surveillance

# Verify the content of the key.
cat /etc/auto.nfs_surveillance 
# Frigate storage
nfs_frigate	-fstype=auto	10.11.7.9:/mnt/disk20/frigate

That is it. It is a bit complicated than /etc/fstab, but it does the job done and save some bandwidth. Just remember when you run df -h you will not see the share mounted until you access it. The base mount point will show empty because you have to specify the directory to access it.

Cheers!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x