Use autofs to wake fileserver on demand
This is the continuation of "Suspend NAS when idle".
I want to automatically resume my NAS server from suspend to ram when accessing the media directory, e.g. via XBMC. I'm connecting my clients via sshfs, so if you're using SMB/CIFS, you have to modify the fstype parameters. The implementation is based upon http://tech-bodges.blogspot.de/2010/06/linux-autofs-and-wake-on-lan-bodge.html.
Required tools:
- sshfs
- autofs
- wol (or any other "wake on lan" tool like etherwake)
- the MAC address (hwaddr) of the server's ethernet interface
I assume that the sever is called "nasserver" and has a user called "htpc" which is able to access the "/srv/media" directory. First of all, if not already done, ensure that the root user of your client can mount the sshfs server without any prompting:
sshfs -o allow_other 'htpc@nasserver:/srv/media' /mnt
If you are asked for a password instead, generate a SSH key and copy the public key into the authorized_keys file of the htpc user on nasserver (there are many other tutorials on the web that explain how this is done). Maybe fuse won't allow you to use the "allow_other" option. In this case, ensure that there is a line "user_allow_other" that is not commented out in /etc/fuse.conf.
If you successfully mounted the directory under /mnt, umount it ("umount /mnt") and go with me to the next step.
autofs mounts shares automatically when they are accessed. You can either define static mount parameters, or have an executable script that produces these mount parameters. We want to use the second option: the script will try to ping the destination host one time. If ping fails, it tries to send a Wake on LAN package to it's MAC address and waits up to two minutes for resume. The autofs directory in this example is /var/autofs/nasserver (create it, if it does not exists!), the share's name "media", so autofs should mount the share whenever a file or directory in /var/autofs/nasserver/media/ is requested.
Locate your auto.* files. Ubuntu and Debian store them in /etc, Arch Linux in /etc/autofs. I'll use the Debian variant.
Create /etc/auto.nasserver and make it executable. Replace the hostname, the hwaddr, the username, the path and - if you want to - the mount options:
#!/bin/bash # Modify these values hostname="nasserver" hwaddr="00:00:00:00:00:00" username="htpc" sharename="media" sharepath="/srv/media" mountopts="-fstype=fuse,rw,nodev,nonempty,noatime,allow_other" wolcmd="wol" test "$1" == "$sharename" || exit 0 if ! ping "$hostname" -nqc1 > /dev/null 2>&1; then $wolcmd "$hwaddr" > /dev/null for n in `seq 1 120`; do ping "$hostname" -nqc1 > /dev/null 2>&1 && break sleep 1 done fi echo "$mountopts :sshfs\\#$username@$hostname\\:$sharepath"
If you've done everything right, "/etc/auto.nasserver" should print nothing, and "/etc/auto.nasserver media" shoud print something like "-fstype=fuse,rw,nodev,nonempty,noatime,allow_other :sshfs\#htpc@nasserver\:/srv/media".
Next, add the following line to /etc/auto.master:
/var/autofs/nasserver /etc/auto.nasserver --timeout=300 --ghost
Finally, restart the autofs service ("service autofs restart" or "systemctl restart autofs"). Now everytime you try to access /var/autofs/nasserver/media, the remote share should be mounted automatically and unmounted after 300 seconds of inactivity. If you like, you can also you a symlink to use "/srv/media" as a shortcut on the client:
ln -s /var/autofs/nasserver/media /srv/media
When running this on a Raspberry Pi, I suggest to add the following mount options to the "mountopts" variable:
max_readahead=8388608,Ciphers=arcfour
This lets sshfs buffers up to 8MB, which reduces the likelyhood of lags and stutterings, and forces SSH to use the weaker (but faster) RC4 encryption algorithm for the connection.
Comments
Marv (not verified)
12. May 2014 - 0:31
Permalink
Wow, this might really help
Wow, this might really help me. I was thinking of a way to let my raspberrypi act as a ftp server with the data being on a Windows HTPC. The HTPC consumes too much energy, so I wanted to wake it when i need it.
This might actually work. I will try it ASAP and Report back :D
Cybso
12. May 2014 - 1:48
Permalink
Hi Marv, please note that the
Hi Marv, please note that the new XBMC 13 has an integrated WoL feature for fileservers (must be enabled somewhere in settings, haven't tried this yet). So, before fiddling around with autofs let me suggest to have a look at that feature, first :-)
Marv (not verified)
12. May 2014 - 9:15
Permalink
Sounds even better, I will
Sounds even better, I will have a look into that as well, Thank you very much :D
Greg (not verified)
3. December 2015 - 15:58
Permalink
Small bug in your code. There
Small bug in your code. There's an extra quotation mark.
echo "$mountopts" sshfs\\#$username@$hostname\\:$sharepath"
should read
echo "$mountopts :sshfs\\#$username@$hostname\\:$sharepath"
----------------------------------------------------------------------------------
Roland: Thanks, fixed that!