Posts

Mounting NFS directories using AutoFS

AutoFS allows to automatically mount network shares on demand. Compared with traditional fstab configurations, using AutoFS has the following benefits.

  • With AutoFS, directories are automatically mounted when they are accessed and are unmounted after a period of inactivity — can reduce boot time and improve overall performance.
  • Auto-mounting via fstab will fail if the network connection to the network share is not yet established during boot time (common issue with WiFi netwoks) —  not an issue with AutoFS, as the shares are mounted on demand.

However, mounting a NFS directory using AutoFS requires a little more work, compared to adding a rule to the /etc/fstab file, here is an example.

Install dependencies:

sudo apt-get install autofs5

Create a mount point:

sudo mkdir /nfs

Configure the mount point, add the following line to the end of the file /etc/auto.master:

/nfs /etc/auto.nfs --ghost

Note: That line tells the AutoFS service that the mount points under /nfs are configured in file /etc/auto.nfs.

Add the following line to the file /etc/auto.nfs (create the file if it does not exist and adopt the hostname of the NAS and the name of the NFS directory):

music -fstype=nfs,ro,noacl,noatime,nodiratime,noac,tcp mynas:/volume1/music

Note: In the example above, the NFS share is mounted read-only, with some flags to optimize performance. Adopt the NFS mounting options to your needs, if necessary.

Restart the AutoFS service:

sudo systemctl restart autofs

External Links

  1. Ubuntu AutoFS Documentation
  2. Red Hat Enterprise Linux 7 AutoFS Documentation