Professional Documents
Culture Documents
c om
Official Documentation
Page History
Community
Support
Partners
Contribute
Login to edit
Search
AutomaticallyMountNFSSharesWithoutAutofsHowto
Summary
Setup clients on a local network to automatically mount NFS shares whenever the fileserver is up, without using autofs. Instead, a simple bash
script called by a custom upstart job checks if the server is up, and if the shares need to be mounted or unmounted.
Note: A more modern nfs_automount script has been released on 18 July 2013. It is available on GitHub.
Introduction
For a small office or home network populated with Unix-like computers (e.g., a few
Ubuntu desktops or laptops and a fileserver), NFS (Network File System) is a good
way to share storage space and centralise the backup of important documents.
However, having a fileserver running 24/7 is often overkill for such a setup.
One way to have clients mount NFS shares automatically when the fileserver is turned
on, is to use a package called autofs. Unfortunately, there are a few unresolved
issues with using autofs in combination with NFS. In my case, when autofs tries to
mount NFS shares when the fileserver is turned off, the desktop and filebrowsers like
Nautilus in particular become extremely unresponsive, regardless of the options used.
Attempting to mount the share manually from the command line when the server is
down however, does return a message of failure quite promptly, without hanging the
desktop.
Contents
1.
2.
3.
4.
Summary
Introduction
Prerequisites
The script
1. Adjust the script to your situation
2. Installing a custom upstart job
5. Troubleshooting
6. How the script works
To solve this issue, I wrote a simple bash script that is run through the upstart system. The script simply checks if the fileserver is up, if the shares
need mounting or unmounting, and then sleeps for a while before checking again. This works out quite well, so I decided to share this information in
case someone else runs into these issues.
Prerequisites
open in browser PRO version
pdfcrowd.com
Prerequisites
1. This howto assumes that you have an NFS server set up with shares exported, and one or more clients capable of mounting those shares.
For more information on setting up NFS shares and mounting them on a client from the command line, see: SettingUpNFSHowTo.
2. Clients should be able to ping the server to determine if it is running.
3. Naturally, you need administrator access on the clients to install the script and upstart job outlined below.
4. This script assumes that the directory paths of the shares match the location where they are mounted. In my case, the fileserver has two
shares: /media/Storage and /media/Backup. On the clients these shares are mounted on the same paths. If your setup deviates from this, the
script needs some modification.
The script
Download the latest version of the script from https://github.com/jdhoek/util/blob/master/autonfs.sh, or copy and paste the following in your favourite
text editor:
#!/bin/bash
#
#
#
#
#
#
#
#
#
#
#
#
#
AutoNFS v.1.1
2010-09-02
Jeroen Hoek <mail@jeroenhoek.nl>:
* Update script with helpful contributions from other users.
* Stop using logger, simply echo and let the system log it in /var/log/upstart.
2012-07-23
Martin Seener:
* Use rpcinfo instead of ping to check the status of the NFS server daemon, rather
than just the server being up.
* Add some useful mount options for a stable NFS mount.
2012-03-12
tobcro:
* Allow local and remote mountpoint to be different.
2010-01-24
Jeroen Hoek <mail@jeroenhoek.nl>:
* Initial script.
# Configuration parameters.
# The hostname or IP-address of the fileserver:
FILESERVER="yournfsserverhere"
# Mount Options (see mount man pages for info).
MOUNTOPTS="-o rw,hard,intr,tcp,actimeo=3"
# Check every X seconds (60 is a good default):
pdfcrowd.com
INTERVAL=60
# Delimeter used for separating fileserver/client shares below:
DELIMETER="|"
# The shares that need to be mounted. If the local and remote mount point
# differ, write something like "/media/remoteshare|/media/localshare", where "|" is
# the delimeter configured above. If the mount points are the same, you can also use
# the short-hand "/media/share".
MOUNTS=( "/media/exampleRemote1|/media/exampleLocal1" "/media/exampleMount2" )
# Logging. Set to true for debugging and testing; false when everything works. Logs
# are written to /var/log/upstart/autonfs.log.
LOG=true
# End of configuration
function log {
if $LOG; then
echo $1
fi
}
pdfcrowd.com
pdfcrowd.com
mount the remote share on is not on the same path, you can use the | character to enter the local and remote path:
# This will mount remote share /media/Things at /media/SharedThings locally:
MOUNTS=( "/media/Things|/media/SharedThings" )
Save the script somewhere with an obvious name. In this example we call it autonfs.sh. Open a terminal and cd to where you saved the script. Now
make the script executable by calling:
chmod +x autonfs.sh
Next, move it to a place where it can be called by our upstart job, but also from the console to test. A good place to put such custom executables
is /usr/local/bin.
sudo mv autonfs.sh /usr/local/bin
This script will tell you what it doing when run. To test this script, call it from a terminal:
autonfs.sh
If the script works, your shares should show up on the desktop and the computer:// location in Nautilus. If the fileserver goes down or becomes
unreachable, the shares should disappear, and reappear when the fileserver comes back on-line. If this works, move on to the next step.
"Mount NFS-shares"
start on (filesystem)
respawn
exec /usr/local/bin/autonfs.sh
This upstart job starts our script as soon as the filesystem is up, and restarts it (respawn) if it got killed. Save it as autonfs.conf. From a terminal
(cd to the location of this new file), move it into place (it goes in /etc/init), and adjust the read/write permissions. We will also create a link in the
/etc/init.d directory; this way, the upstart job can be treated like a normal init.d job (the SysVInit standard):
sudo chown root:root autonfs.conf
pdfcrowd.com
Troubleshooting
If the script fails to mount shares or if the server is always reported as "down", check if the rpcinfo and mount commands used in the script work as
intended when run from the command line.
If the script (or commands called from it) complains about mount: wrong fs type, bad option, [...]; then double check if nfs-common
is installed. This should be covered in the basic NFS-tutorial linked to above.
This HowTo was originally posted on the Ubuntu Forums. If you get stuck or have a helpful addition, please have a look at
http://ubuntuforums.org/showthread.php?t=1389291.
The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details
pdfcrowd.com