NFS Automount, The Fourth Iteration (the complete rewrite)

** Note: This post has been significantly altered on 18 July 2013 from the original, posted a few days earlier.

A few days ago I released the fourth iteration of the NFS Automount script, with some minor changes to the previous version from December 2011. The earlier versions were released May 2011 (first CentOS Linux version), and July 2010 (originally written for FreeBSD).

Upon releasing the fourth version I realized the script was becoming brittle, the logic was, well, somewhat illogical, and minor refactoring would not help. Hence this complete rewrite of the script, now called “nfs_automount”, was born. It is conceptually based on the older versions, and I also borrowed some ideas from AutoNFS script on Ubuntu’s Community Wiki.

Like the earlier version, the goal of this script is to provide static (i.e. /etc/fstab-like) NFS mounts, while at the same time supporting cross-mounts between servers.

The other non-fstab alternative is to lazy-mount NFS shares with autofs (where available), but with it NFS shares are not continually maintained. When a remote share is accessed, it takes a few moments for it to become accessible as autofs mounts the share on-demand. While autofs times out a mounted share after some time of inactivity, it does not unmount the share before the timeout has lapsed in the event the remote server becomes inaccessible. While on-demand mounting may save some bandwidth, it is not suitable for all applications. Furthermore, when a system has one or more active mounted shares off of a server that goes offline, unexpected behavior is often observed on the client server until the now-defunct NFS shares are unmounted, or the remote server becomes available once again.

nfs_automount offers a solution:

  • The NFS shares are not statically defined in /etc/fstab so that the system startup is not delayed even when the remote server is not available. As soon as the shares become available they’re automatically mounted. If multiple servers cross-mount NFS shares from each other, and the servers are turned on at the same time, nfs_automount ensures that all mounts are established as soon as the shares become available.
  • The shares are monitored at a frequency you define, for example, every 60 seconds. If a share has become dismounted, stale, or their exporting server has become inaccessible, nfs_automount takes action to correct the situation: dismounted and stale shares are attempted to be remounted (stale shares are first immediately unmounted), and shares whose remote NFS service has disappeared are unmounted to prevent impact on the client system stability. Once a remote NFS service returns online, or definition of a previously stale share is reinstated, any shares that were unmounted as a result of those conditions are remounted.
  • The script is intended to run as a daemon (an upstart job script is provided for Ubuntu), and it reads its configuration from /etc/nfs-automount.conf where you can conveniently define the shares to be mounted and monitored along with some other options. You can also set ‘RUNTYPE’ option to ‘cron’, and run the script from crontab if you so choose.
  • You can define the shares to be mounted either as Read/Write, or Read Only. Of course, a share will be Read Only regardless of this setting if it has been exported as Read Only on the remote server.
  • An option to define a remote check file is provided. If provided in the configuration for a share, its unreachability can alert of a problem on the exporting server, such as a failed filesystem mount, even when the NFS share is otherwise working correctly. You can easily expand this feature to add additional functionality.
  • Provides clear logging which provides alerts by default, and more informative detail if you turn ‘DEBUGLOG’ setting to ‘true’.
  • Written in bash script with modular and clear syntax.
  • Tested on Ubuntu 12.x (should also work on Debian) and CentOS 6.x (should also work on RedHat). The service installation instructions (available on GitHub) have been written for Ubuntu, so if you’re installing the script for CentOS/RedHat, you will need to alter the installation steps somewhat. FreeBSD is no longer explicitly supported, but I believe it should work with minor modifications. I have not tested with Solaris or other *NIX environments. If you try, please post comments here!
  • Can be easily run as a service (upstart script is provided), or from crontab; the script works with crontab with just a single configuration switch change.
  • Distributed under MIT license.

Rather than posting the code (now 400+ lines) here, I have created a repository on GitHub from where it is easy to download or clone.

Enjoy! 🙂

7 thoughts on “NFS Automount, The Fourth Iteration (the complete rewrite)”

  1. First of all I’d like to thank you for this great script! Its exactly what I was looking for for quite some time. I got it all up and running but unfortunatly the script runs in circles because it states that the mountpoint is not writeable. So it keeps remounting my shares every 60 seconds. But the weired thing is that my shares work quite fine when I am logged in under my user-account.

    I assume thats because root has no write-access on my freenas-shares. My file-permissions on my fileserver are like:

    drwxrwx— 3 elfi office

    elfi beeing my user on the file-server with the same name and UID as on my client-machine. Other users beeing in the office-group for example are allowed to read and write as well.

    My question now is: How can I tell the script to test for read-write (create a testfile) with certain user-credentials? Or would it be easier to just disable the test-write part of your script?

    Thanks for your help in advance!

  2. Hi Christian! Thanks for your feedback. Your question exposes an area I hadn’t considered when I wrote the script for my own server environment. I’ll take a look at it over the next week and will post an update here.

  3. Hi Christian! Just today another project I was working on reminded me of it. There’s something else, too, that I’ve been thinking of adding to it, so I’m going to look at it and see if I can figure out the issue you mention at the same time. I’ll post an update here once a new version will have been released.

  4. Hey Ville,
    Your automount script works perfectly now under systemd. Here is the script I use:

    <<>>> End of File

    You create that file, leave upstart aside and then:
    < systemctl enable nfs_auto.service

    That was it. Now you reboot and will have the script working away nicely.

    All the best,
    Christian

    P.S. After all these years … *g. Besides, I still suggest to make a bool flag option for the creation of a remote file, cause using that feature does a remount every 60s

    P.P.S. Maybe you could work my systemd script into your github repo? Thanks!

    P.P.P.S: I dunno why, but I checked all sorts of autofs'es, they still don't give me the same comfort as your script

  5. # nfs_automount service file

    [Unit]
    Description=NFS autostart script
    # local-fs.target is implied by network-online.target
    Wants=network-online.target

    [Service]
    WorkingDirectory=/opt/nfs_automount
    Type=forking
    #ExecStartPre=/bin/sh -ec ‘echo Starting nfs_automount process…’
    #ExecStart=/bin/bash nfs_auto_service_script.sh start
    ExecStart=/bin/bash nfs_automount
    #ExecStartPost=/bin/sh -ec ‘echo Stopping nfs_automount process…’
    # on-failure not allowed for type=oneshot
    #Restart=on-failure
    Restart=no
    KillMode=process
    Type=oneshot

    [Install]
    WantedBy=multi-user.target

Leave a Reply to Christian Cancel 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.