Friday, October 2, 2009

Wake-on-lan (WOL) & NFS Mounts

In my current setup I have quite a lot of machines but not all are powered on. Some machines also have storage (disks) onboard and sometimes you just need to get a file from a machine and are too lazy to get up and turn it on. Or in my case: the machine in question is a MythTV backend machine and the frontend wants to view a recording.
Wouldn't it be nice if nfs-mounts would wake the target machine if needed? After some digging I came up with the following solution: combine autofs with etherwake and some shellscripting.

The thinking in this solution is as follows: autofs will create stubs where real filesystems appear when a file is requested. For this autofs will mount the NFS share when required. It falls back to /sbin/mount.nfs for this (which is also the backend for NFS mounts performed via mount -t nfs). So if we replace the mount.nfs with a version that first sends a WOL packet and then calls the original mount.nfs to perform the actual mount, we are all set.

So here are the steps:

1) Install etherwake (apt-get install etherwake)
2) Rename /sbin/mount.nfs to /sbin/mount.nfs.orig (cd /sbin && mv mount.nfs mount.nfs.orig)
3) Create the following script in /usr/local/sbin/mount.nfs.wol
#!/bin/sh

ORIG_MOUNT=/sbin/mount.nfs.orig

# arg 4 are the options. Parse them
args=`echo $4 | tr ',' ' '`
target_args=
for f in $args
do
        if [ "wol" = "${f:0:3}" ]
        then
                wol=`echo ${f} | cut -f 2 -d=`
        else
                if [ ! -z "${target_args}" ]
                then
                        target_args="${target_args},"
                fi
                target_args="${target_args}${f}"
        fi
done

etherwake ${wol}

exec ${ORIG_MOUNT} $1 $2 $3 ${target_args}

4) Create a link to this script (cd /sbin && ln -s /usr/local/sbin/mount.nfs.wol mount.nfs). Make sure the script is executable! (chmod +x /usr/local/sbin/mount.nfs.wol)
5) Add a parameter to /etc/fstab for the wakeonlan enabled mounts
(for example: if your current fstab contains a line like:
"server:/media/nfs /mnt nfs hard,tcp 0 0" change it to:
"server:/media/nfs /mnt nfs hard,tcp,wol=aa:bb:cc:dd:ee:ff 0 0" where the mac address is changed to the mac-address of the machine to wake. You can find this using the 'arp' command).
6) Make sure that the target machine can receive wake-on-lan packets (apt-get install ethtool, add 'ethtool-wol g' to the network interface entry in /etc/network/interfaces)

I've got this running now for a non autofs-enabled setup and will test with autofs next.

1 comment:

  1. This has started giving me the following issues when mounting my fstab drives:

    mount.nfs4: mount(2): Invalid argument
    mount.nfs4: an incorrect mount option was specified

    It used to work for several months, an update or something must have ruined it.

    ReplyDelete