Monday, 28 June 2010

Linux autofs and Wake on Lan bodge.

Hi,


Like many people, I have a media server which stores all of my films, music and pictures in all one central place. It's a Centos Linux box with a SATA card and software Raid5, it serves to devices like my Acer Revo running Xbmc and Mythtv frontend. With this configuration it's a little noisy, creates a bit of heat and probably uses a bit of electricity, but turning it on can be a bit of a hassle. Particularly when it's at the other end of the house. My solution autofs and wake on lan.

autofs is a suite of tools that will auto mount devices and NFS shares when you first use them. Wake on LAN on my setup was a small cable that goes between my (dlink) PCI network card and my motherboard and I had to edit the BIOS to allow the machine to be woken. There seems to be a number of different ways to setup WOL on linux. I had to use ethtool to put the network card in a fit state to be woken up when the server was shutdown:


ethtool -s eth1 wol g

I actually put this in a Bash script and then called it from a script called "ifdown-post" located in:

/etc/sysconfig/network-scripts

This gets called on shutdown.

To wake the machine up I used the etherwake command. My xbmc machine uses Ubuntu as it's base OS. The package is simply called "etherwake"

So to wake up the remote machine you would use:


etherwake mac address

In my case:

etherwake 00:26:5A:F0:33:0C


The command is located in /usr/sbin on my install. Execute and bingo the machine powers up.

Now, a little used feature on autofs is the executable map. Maps normally are just plain text showing where to and what to mount and with which options. An executable one, is a shell script, in my case Bash, which the mount bits echo'd out at the end.

To use with autofs, you need to have NFS installed running on the machine you want to mount and a filesystem or directory exported in:

/etc/exports

which looks something like this on mine:

/usr1/media-server/video ws022.mydomainname.com(rw,sync)

This allows ws022 on my network to mount that directory as read/write.


On the mounting machine you need autofs running and the maps in /etc looking something like this:


In:
/etc/auto.master:

/mnt/mnts/media-server /etc/auto.media-server --timeout=60

and in:

/etc/auto.media-server:

#!/bin/sh
hostsrv=media-server
opts="-fstype=nfs,ro,intr,soft"

ping -c 1 -w 1 -q $hostsrv >/dev/null
status=$?
if [ $status -ne 0 ]
then
/usr/sbin/etherwake 00:26:5A:F0:33:0C
sleep 60
fi
/bin/echo -n "video"
/bin/echo -n -e "\t"
/bin/echo -n "$opts"
/bin/echo -n -e "\t"
/bin/echo "$hostsrv:/usr1/media-server/video"

Edit the files and make the auto.media-server file executable:

chmod 0750 /etc/auto.media-server

Then create your mount points:


mkdir /mnt/mnts /mnt/mnts/media-server

then give it a:

service autofs restart


What the above script does, is test if the server responds to a ping. If it doesn't, send a wake up call and wait 60 seconds, this is how long my machine takes to bootup. Once booted or if it already is, echo out the mount info.


So with the server shutdown, I can on the client machine:


cd /mnt/mnts/media-server/video

and the server will power up, the client will wait a while and then mount the directory. Magic!

If anyone spots any errors, improvements or indeed a way to get it to auto shutdown when autofs unmounts, let me know.


;-)


max

3 comments:

  1. Hello,
    thank you for this article! I searched long the net for an answer how to combine wake on lan and autofs. The wake on lan is already working for my nasbox so i tried now to use your script but autofs will not work when I use that code. It executes the wakeup (and this works) but automount says it can't mount.
    The same problem is when I add the execute flag to a default autofs file. So I think the script is not the problem but the execute flag.
    I use Ubuntu 10.10 with autofs 5.05. What version did you use? Maybe you have an idea to this.

    Cheers,
    David

    ReplyDelete
  2. Hi, thanks very much for your post, exactly what I needed to do myself.
    However, it did not work like that for me from the start, and after some research I found that there seems to be an error in the output of your script; in my autofs version at least, the result of the executable map _must not_ contain the name of the directory that one tries to mount; i.e. instead of
    video -fstype=nfs,ro,intr,soft media-server:/usr1/media-server/video
    the script should produce
    -fstype=nfs,ro,intr,soft media-server:/usr1/media-server/video
    and the script needs to check the first argument ($1), because there autofs passes in the name of the directory to be mounted (video, in your case); and only for $1 being video, the script should produce any output.

    ReplyDelete
  3. Thanks for posting your bodging exploration. Using the above as a starting pointing, I created the following for my XBMC setup:

    #!/bin/bash
    ping -c 1 -w 1 -q xbmc > /dev/null
    if [ $? -ne 0 ]
    then
    /usr/sbin/etherwake xbmc
    for (( i=1; i <= 60; i++ ))
    do
    nc -zw 1 xbmc 2049
    [ $? -ne 0 ] || sleep 1; break
    [ $i -ne 60 ] || exit 1
    done
    fi
    echo -e "-fstype=nfs4,ro,soft\txbmc:/$1"

    See http://pastebin.com/gbFtRWcm for a formatted version ;)

    So, with "/mnt/xbmc /etc/auto.xbmc --timeout=30" in auto.master, the XBMC box will wake up and autofs will mount /mnt/xbmc/music or /mnt/xbmc/video or whatever, as soon as nfsd is ready to receive connections.

    ReplyDelete