Topic: Automated Remote FileSystem Mount over NFS/SSHFS
Hi folks, not quite my first post here, but close, so forgive me if I miss something that should be included...
I have a little QNAP TS-110 at home that I use as a backup server/media server/seedbox/print server and I got tired of manually mounting the share, so i figured I'd share the solution I came up with, as it's been really nice to always have access to my larger drive when I'm away from home.
The script is pretty simple, it just does a quick check for the router (gateway) MAC address, and if it's the MAC address of your home router it mounts the drive over NFS (could work for SMB or whatever else, though you'd have to modify /etc/fstab). If my home router's mac is not found as the gateway mac, the script defaults to mounting the drive over sshfs.
first
#!/bin/bash
#make script executable at /etc/NetworkManager/dispatcher.d/50-mount-remote
#customize below for your setup
gateway=`ip route show 0.0.0.0/0 | awk ‘{print $3}’`
mactest=$(arp -n -a $gateway | awk ‘{print $4}’)
targetmac="00:00:00:00:00:00"
homeupcommand="mount /home/nathan/remote"
homedowncommand="umount /home/nathan/remote"
awayupcommand="cd /home/nathan && sudo -u nathan sshfs nathan@your.dyndns.com:/home/nathan /home/nathan/remote"
awaydowncommand="cd /home/nathan && sudo -u nathan fusermount -u /home/nathan/remote"
#
# should not need to modify below here unless you know what you are doing
#
if [ $mactest==$targetmac ]
then
case "$2" in
up)
$homeupcommand
;;
down)
$homedowncommand
;;
esac
else
case "$2" in
up)
$awayupcommand
;;
down)
$awaydowncommand
;;
esac
fi
exit $?and the /etc/fstab entry... (the noauto mount option is the important part)
homenet:/home/nathan /home/nathan/remote nfs noauto,rw,rsize=8192,wsize=8192,timeo=14,intr 0 0*note: this does depend on the use of NetworkManager, though the task could still be reworked for ifup/ifdown use instead
enjoy! within a few seconds of establishing a network connection, you should have remote access to your server/share drive!
Last edited by nathwill (2010-11-07 03:40:42)