Tuesday, August 25, 2009

Automount with ssh and nfs

Standard
Introduction

I have for a long time mounted and unmounted network file systems by hand when ever I had a need for them. It is not a big thing, but you have to open a terminal in order to do this every time. I experimented a little with automount and autofs and found, that you can dynamically do things in the mount specification files.

Autofs dynamically executes mount commands triggered when you access a defined folder in your system. This folder then becomes the mount point for a given network or local file system. Another use for autofs is access of cd's, floppies and other removable block devices.

This is especially usefull from a Laptop computer, that sometimes are connected to network file systems behind a firewall, sometimes connected to ssh/sftp systems when you are on the road.

The goal is to be able to dynamically let the laptop figure out, what protocol to use: NFS or SSH/SFTP depending on the location of your connection.

Preparing the software

You will need to add the autofs system to your distribution of choice, this is for Debian / Ubuntu:

$ sudo apt-get install autofs

This will add the autofs system to the list of installed packages on your laptop PC.

You will now need to add netcat and sshfs to your system - it is used to check if a ssh server is accessible and mount the nfs share via SSH if it is:


$ sudo apt-get install netcat sshfs

Autofs will add a daemon to your system for monitoring your configured mount points. The deamon needs to be told to reload configurations whenever you change your configuration:



$ sudo /etc/init.d/autofs reload
Reloading automounter: checking for changes ...
Reloading automounter map for: /misc
Reloading automounter map for: /mnt/media


The configuration for the autofs system is held in files in /etc/auto.* where you can add you mount points as you need them. There is several good examples in the ones already added by default. However I produced a new named auto.media and added it to /etc/auto.master in order to access my media drive. This is the contents:



#!/bin/sh
# check to see if this host has a working internet connection and a IP address
MYIP=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`
ping -c 1 ${MYIP} >> /dev/null 2>&1
if [ "$?" -ne "0" ]
then
exit 1;
fi

# check to see if HOSTNAME can be pinged directly - if yes echo the mount lines for nfs
ping -c 1 HOSTNAME >> /dev/null 2>&1
if [ "$?" -eq "0" ]
then
echo "root -fstype=nfs,rw,sync,soft,intr,rsize=8012,wsize=8129 HOSTNAME:/foo/bar/&"
echo "data2 -fstype=nfs,rw,sync,soft,intr,rsize=8012,wsize=8129 HOSTNAME:/foo/bar/&"
echo "download -fstype=nfs,rw,sync,soft,intr,rsize=8012,wsize=8129 HOSTNAME:/foo/bar/&"
exit 0;
fi

# check to see if SSHHOST can be contacted - if yes use the ssh/nfs to mount using fuse
nc -z SSHHOSTNAME 22 > /dev/null 2>&1
if [ "$?" -eq "0" ]
then
echo "-fstype=fuse,rw,nodev,nonempty,noatime,allow_other,max_read=65536 :sshfs\#USER\@SSHHOST:/foo/bar/&"
exit 0;
exit 0;
fi


The file /etc/auto.media listed above should be made executable with permissions 755. When executed it will dynamically produce the contents of the auto.media file depending on the script execution. As a consequence the mount points will not be visible in /mnt until execution of the script has completed. So you will have to cd to a non existing folder like /mnt/media before you actually see it created in /mnt :-)


Conclusion and caveat

The above works fine for me. There could be a problem, if you are on a foreign network, where another server called HOSTNAME exists. You may need to add more intelligent identification check of the HOSTNAME server in order to make this work correctly in all networks.