[HOWTO] Automatically wake NAS/server on playback request
#1
This will only work for linux. I've only used Ubuntu but it should work on other distros.

If like me you use your HTPC for other tasks (watching live tv, games, browsing etc) then you probably don't want it waking your server/NAS on startup or resume. I have managed to get a working solution where the server/NAS is woken only when playback of a file on the server is requested (or if you 'update library'). No source code changes or compiling needed.

What you need to do is mount your remote shares using AutoFS, and point xbmc at the local mounted location on you HTPC.
Eg, i have a share located at \\SERVERIP\DATA1 which i mount to /mnt/DATA1.
I've had much better luck with smb rather than nfs, not sure why as both should work as far as i can tell.

First, installl AutoFS and etherwake:
Code:
sudo apt-get install autofs etherwake

I'm not going to go into AutoFS and its settings (i don't know most of them for a start), you can read up on it yourself.
My /etc/auto.master file has 1 uncommented line in it:
Code:
/mnt    /etc/auto.server --timeout=60 --ghost

Then you need to create a new file /etc/auto.server. Hopefully it is fairly self-explanatory, but just ask if it isn't.
Code:
#!/bin/sh

#ping server ip in case its already up
ping -c 1 -w 1 -q SERVERIP > /dev/null
status=$?
logger "AUTOFS: Attempting to mount $1 filesystem from SERVERIP"
logger "AUTOFS: Result from ping - $status"
if [ $status -ne 0 ]
then
logger "AUTOFS: Host is not up. Sending wake-on-lan"
/usr/sbin/etherwake SERVERMAC
logger "AUTOFS: Sleeping for 5 seconds to allow host to wake"
sleep 5
fi

logger "AUTOFS: Re-pinging host"
ping -c 1 -w 1 -q SERVERIP > /dev/null
status=$?
logger "AUTOFS: Result - $status"

if [ $status -eq 0 ]
then
logger "AUTOFS: Mounting $1 filesystem"
#echo out autofs settings. Mount $1 share. $1 is passed in by autofs
/bin/echo -n "-fstype=smbfs,rw,username=user,password=password ://SERVERIP/$1"
else
logger "AUTOFS: Still no ping result from host"
(replace SERVERIP and SERVERMAC with values for your system. Also, you may have to tinker with the sleep value)

Make the file executable:
Code:
sudo chmod +x /etc/auto.server

So now whenever you (or xbmc) try to access /mnt/<share_name> on your local system autofs will kick in and call that /etc/auto.server file. That will ping the server and WOL it if it isn't on. Then autofs mounts the filesystem once the server is up.

Check /var/log/syslog or /var/log/messages (distro/version dependent i think) to see the logger outputs from the script.

Hopefully this will be useful to someone Big Grin
Reply
#2
Thats a thing I was always looking for.

I set up as written by you but I don´t have an idea how to point XBMC to the /mnt/
I added a new source in xbmc but as long as there is nothing in /mnt/ the wol won´t happen ... or ?

I am stuck, isn´t there a mounting required before this can work?
Then there needs some xbmc database update with the new path... doh I am too stupid for this ;-)
Reply
#3
if your autofs is set up correctly then if you 'cd' into /mnt/<share_name> then it should automount and be there.
Then you will be able to add it as a source in xbmc.
After that, every time xbmc accesses the source it will mount it.
Reply
#4
This is something that I want! Ive been using the AWOL plugin to wake my server and I had begun to mod skin files to have it launched when anything in the videos menu was accessed, but this method looks simpler and doesn't require any changes to xbmc.

One question, have you had any problems with xbmc timing out when trying to play a file?
Reply
#5
If you use Openelec you can use this :
http://openelec.tv/forum/69-network/31703-pm-suspend
Reply
#6
Looks like I have to give it another try.
BTW here is a modification that someone did over at the heise.de forum:
http://pastebin.com/ZDJVuFAQ
it should print the messages.. don´t know if it works since I haven´t got it working Undecided

teeedubb: "...doesn't require any changes to xbmc."
Well at least you have to fix your database I guess...

EDIT: what I meant before AutoFS knows what to to the share has to be mounted - right ?
like: mount -t cifs -o username=you,password=secret //192.168.1.100/shareddrive /mnt/smb

otherwise I´d wonder where it got the information from WHAT to mount from the server
Reply
#7
hmm not working... nothing written to the logs ...
if I enter /mnt/ it just hangs some seconds and thats all... no output, nothing in the logs
any way to see what is happening ?
Reply
#8
apologies for being very tardy on responses guys.

teedub - no, i've not had any issues with it timing out. My server comes up from sleep pretty rapidly though (3 secs maybe). Are you having issues?

tequila - i'm a bit confused as to what you are talking about tbh. You con't have to mount the drive manually for autofs to know what and where to mount. The magic is in the auto.server script. It uses $1 (the argument passed to the script from autofs) as the name of the smb share to mount.
In my case the smb share is called DATA1. If i go into /mnt and do an ls then the directory is empty. However, i can then do a 'cd DATA1' and that is when the auto.server file will be called and will wake and mount to /mnt/DATA1.
Try editing the auto.server file and putting a logger line as the very first line. Also, ensure that your auto.server file is executable, otherwise it won't work.
Then try and cd into any non-existent directory under /mnt and you should get _something_ in your /var/log/messages.

bibi - i don't think that is quite the same. That just wakes the server when the client wakes as far as i can tell. The difference here is that if you were using your xbmc installation to watch live tv (or anything that didn't require your server) then your server would not be woken. With this the server is only woken when it is actually needed.

HTH, and again sorry for the lateness
Reply
#9
Great, just what I was looking for!
Any chance to create an add-on for this (so I could enter the details -like SERVERMAC and SERVERIP - on the XBMC UI and be done with it)?
I am guessing that entering the IP address or the MAC address should be enough (entering one could trigger an ARP or a reverse ARP request to automatically find the other).

Sticky-ing this or a link to this post from a wiki page would be nice.

My server takes 1.5-2 mins to boot (due to the RAID card taking its sweet time loading its own little Linux heaven).
Reply
#10
Thanks to the topicstarter I can start a samba share on demand.I noted that the first post was not complete to get up and running, therefore I added this tutorial. Hopefull someone will benefit from it.

Situation
I have a NAS to store music/movies and pictures. This NAS is not on 24/7, but only when required. This server can be waken up by sending a WOL magic packet.

I also have a Intel NUC (Next Unit Computing) running 24/7 which plays my music(logitechmediaserver), and movies(xbmc). This machine is running on Ubuntu 14.04.

Because I don't want to start the NAS manually, when the music or movies is acccessed, I use autofs to perform this action automatically.

I have the following shares on the NAS server:
\\NAS\movies
\\NAS\music

Install
Before configuring stuff install the following packages:
autofs (required to get the autofs functionality)
etherwake (required to wake NAS server)
cifs-utils (required to mount samba share)

Code:
sudo apt-get install autofs etherwake cifs-utils

Goal
Goal is to activate the NAS, and mount the samba share on the NAS to a local mount point. These local mount points can be used by programs like XBMC and logitechmediaserver to retrieve movies and music.

Configuration on the client (in my case the Intel NUC)

Mount points
The data on the samba shares (residing on the NAS) will be made available with mount points. You have to create these. I decided to put the mount point under /mnt/samba.
Code:
sudo mkdir -p /mnt/samba/movies
sudo mkdir -p /mnt/samba/music

The client will use the mount point (for instance /mnt/samba/movies) to retrieve the movies, NOT the samba share. This is managed by autofs.

Credential files
Most of the time your samba share are protected with userid/password combinations, so not everybody can access them. These information has to be available for autofs. The most secure way to do this is using credentials files, and store these with maximum security.

create a credential file(using vi) for each share and fill them with username and password using the following filename convention: /etc/credential.<sharename>.txt. Afterward the file has be protected. This is done by the chmod 600 commands.

it should have the following content:
Code:
username=<fill in your username for this share>
password=<fill in the password for this share>

Code:
sudo vi /etc/credentials.movies.txt
sudo vi /etc/credentials.music.txt

Code:
sudo chmod 600 /etc/credentials.movies.txt
sudo chmod 600 /etc/credentials.music.txt

Configure autofs
Add the line "/mnt/samba /etc/auto.server --timeout=3600 --ghost" to the /etc/auto.master file. This will inform the autofs system to use the script /etc/auto.server, every time a mount point in /mnt/samba is entered. I dont' know if this is relevant, but this was the only line uncommented(no #).

Code:
echo "/mnt/samba /etc/auto.server --timeout=3600 --ghost" | sudo tee -a "/etc/auto.master"

Add the /etc/auto.server script
Add the following content to a file name /etc/auto.server, and make the script executable.

Code:
sudo vi /etc/auto.server


Code:
#!/bin/sh

# Global parameters
SERVERIP=192.168.100.1
SERVERMAC=11:22:33:44:55:66
INTERFACE=eth0

PING_INBETWEEN_SLEEP=5
PING_MAX_SLEEP=60

# Get parameters
AUTOFSMOUNT=$1
MOUNT=`basename ${AUTOFSMOUNT}`

perform_wol() {
    INTERFACE="$1"
    MAC="$2"

    logger "Sending wake-on-lan, on interface(${INTERFACE}) using MAC-ADDRES(${MAC})"
    etherwake -i "${INTERFACE}" "${MAC}"
    status=$?
    
    logger "result=${status}"
    return ${status}
}

wait_for_server() {
    SERVER="$1"
    WAIT="$2"
    MAX_WAIT="$3"

    TOTAL_SLEEP=0
    FOUND="N"
    while [ ${TOTAL_SLEEP} -lt ${MAX_WAIT} ] ; do
        logger "Trying to reach server(${SERVER}) for a total of ${TOTAL_SLEEP} seconds"
        ping -c 1 -q "${SERVER}" > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
            FOUND="Y"
            break
        fi
        sleep ${WAIT}
        TOTAL_SLEEP=$((TOTAL_SLEEP + $WAIT))
    done

    if [ "${FOUND}" = "Y" ] ; then
        return 0
    else
        return 1
    fi
}

perform_mounts() {
    logger "Mounting SAMBA filesystem ${AUTOFSMOUNT}"
    cred_file="/etc/credentials.${MOUNT}.txt"
    if [ ! -f "${cred_file}" ] ; then
        logger "Missing credential file (${cred_file})"
    else
        echo "-fstype=cifs,rw,noperm,credentials=/etc/credentials.${MOUNT}.txt ://${SERVERIP}/${MOUNT}/"
    fi
}

make_server_active() {
    LSERVERIP="$1"
    LSERVERMAC="$2"

    PING_INBETWEEN_SLEEP="$3"
    PING_MAX_SLEEP="$4"

    # ping server ip to check if it already active
    ping -c 1 -w 1 -q ${LSERVERIP} > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        perform_wol "${INTERFACE}" "${SERVERMAC}"
        if [ $? -eq 0 ] ; then
            wait_for_server "${LSERVERIP}" "${PING_INBETWEEN_SLEEP}" "${PING_MAX_SLEEP}"
            if [ $? -ne 0 ] ; then
                logger "Server (${SERVERIP}) could not be waked using wakeon-lan"
                return 1
            fi
        fi
    fi
    return 0
}

# Main

logger "Automounting local dir (${AUTOFSMOUNT}) to external dir(${SERVERIP}/${MOUNT})"
make_server_active "${SERVERIP}" "${SERVERMAC}" "${PING_INBETWEEN_SLEEP}" "${PING_MAX_SLEEP}"
if [ $? -eq 0 ] ; then
    perform_mounts
    return $?
else
    logger "Error waking up Server (${SERVERIP}). Check if server has power, and MAC address is: ${MAC-ADDRESS}"
    return 1
fi


Make sure you change the SERVERIP and SERVERMAC variable in the scripts. These point to your NAS or server device.

Code:
sudo chmod u+x /etc/auto.server

First Usage steps
Make sure autofs is restarted. This will pickup all the changes made.
Make sure your NAS is shutdown and waiting for WOL magic packet.

Code:
sudo service autofs restart

After that cd into /mnt/samba/movies or /mnt/samba/music. Autofs will use the configuration in /etc/auto.master to determine that a script has to be actived (/etc/auto.server). This script will detect that the NAS has to be started, and generate a WOL magic packet. Then it will wait for the NAS to become active (pingable). The mount string is generated, which is picked up by autofs. After that the mount is created and will be active until no activity has been detected for an hour (3600 seconds).

Notes
- In first script example (by topicstarter) the file system smbfs was used. In Ubuntu 14.04, what i'm using, that has changed to cifs.
- Information about waking the server, and defining the mount points is logged in /var/log/syslog
- To debug autofs perform the following steps:
1) disable autofs: sudo service autofs stop
2) run automount in the foreground using verbose information: sudo automount -f -v

Usefull links
http://tech-bodges.blogspot.de/2010/06/l...bodge.html
http://www.howtoforge.com/accessing_wind...ing_autofs
http://forum.xbmc.org/showthread.php?tid=99854
Reply
#11
Thanks for the update.
Won't work on newer motherboards however, due to this.
Two different boards, two different network controllers, same issue.
Reply
#12
Next to the samba share I also would like to autofs my NFS shares on the same NAS

Install
Before configuring stuff install the following packages:
autofs (required to get the autofs functionality)
etherwake (required to wake NAS server)
nfs-common (required to mount nfs share)

Code:
sudo apt-get install autofs etherwake nfs-common

Goal
Goal is to activate the NAS, and mount the nfs share on the NAS to a local mount point. These local mount points can be used by programs like XBMC and logitechmediaserver to retrieve movies and music.

Configuration on the client (in my case the Intel NUC)

Mount points
The data on the nfs shares (residing on the NAS) will be made available with mount points. You have to create these. I decided to put the mount point under /mnt/nfs.
Code:
sudo mkdir -p /mnt/nfs/movies
sudo mkdir -p /mnt/nfs/music

The client will use the mount point (for instance /mnt/nfs/movies) to retrieve the movies, NOT the nfs share. This is managed by autofs.


Configure autofs
Add the line "/mnt/nfs /etc/auto.server.nfs --timeout=3600 --ghost" to the /etc/auto.master file. This will inform the autofs system to use the script /etc/auto.server, every time a mount point in /mnt/nfs is entered. I dont' know if this is relevant, but this was the only line uncommented(no #).

Code:
echo "/mnt/nfs /etc/auto.server --timeout=3600 --ghost" | sudo tee -a "/etc/auto.master"

Add the /etc/auto.server.nfs script
Add the following content to a file name /etc/auto.server.nfs, and make the script executable.

Code:
sudo vi /etc/auto.server.nfs


Code:
#!/bin/sh

# Global parameters
SERVERIP=192.168.100.1
SERVERMAC=11:22:33:44:55:66
INTERFACE=eth0

PING_INBETWEEN_SLEEP=5
PING_MAX_SLEEP=60

# Get parameters
AUTOFSMOUNT=$1
MOUNT=`basename ${AUTOFSMOUNT}`

perform_wol() {
    INTERFACE="$1"
    MAC="$2"

    logger "Sending wake-on-lan, on interface(${INTERFACE}) using MAC-ADDRES(${MAC})"
    etherwake -i "${INTERFACE}" "${MAC}"
    status=$?
    
    logger "result=${status}"
    return ${status}
}

wait_for_server() {
    SERVER="$1"
    WAIT="$2"
    MAX_WAIT="$3"

    TOTAL_SLEEP=0
    FOUND="N"
    while [ ${TOTAL_SLEEP} -lt ${MAX_WAIT} ] ; do
        logger "Trying to reach server(${SERVER}) for a total of ${TOTAL_SLEEP} seconds"
        ping -c 1 -q "${SERVER}" > /dev/null 2>&1
        if [ $? -eq 0 ] ; then
            FOUND="Y"
            break
        fi
        sleep ${WAIT}
        TOTAL_SLEEP=$((TOTAL_SLEEP + $WAIT))
    done

    if [ "${FOUND}" = "Y" ] ; then
        return 0
    else
        return 1
    fi
}

perform_mounts() {
    logger "Mounting NFS filesystem ${AUTOFSMOUNT}"
    echo "-fstype=nfs4,rw,async ${SERVERIP}:/volume1/${MOUNT}/"
}

make_server_active() {
    LSERVERIP="$1"
    LSERVERMAC="$2"

    PING_INBETWEEN_SLEEP="$3"
    PING_MAX_SLEEP="$4"

    # ping server ip to check if it already active
    ping -c 1 -q ${LSERVERIP} > /dev/null 2>&1
    if [ $? -ne 0 ] ; then
        perform_wol "${INTERFACE}" "${SERVERMAC}"
        if [ $? -eq 0 ] ; then
            wait_for_server "${LSERVERIP}" "${PING_INBETWEEN_SLEEP}" "${PING_MAX_SLEEP}"
            if [ $? -ne 0 ] ; then
                logger "Server (${SERVERIP}) could not be waked using wakeon-lan"
                return 1
            fi
        fi
    fi
    return 0
}

# Main

logger "Automounting local dir (${AUTOFSMOUNT}) to external dir(${SERVERIP}/${MOUNT})"
make_server_active "${SERVERIP}" "${SERVERMAC}" "${PING_INBETWEEN_SLEEP}" "${PING_MAX_SLEEP}"
if [ $? -eq 0 ] ; then
    perform_mounts
    return $?
else
    logger "Error waking up Server (${SERVERIP}). Check if server has power, and MAC address is: ${MAC-ADDRESS}"
    return 1
fi


Make sure you change the SERVERIP and SERVERMAC variable in the scripts. These point to your NAS or server device.

Code:
sudo chmod u+x /etc/auto.server

First Usage steps
Make sure autofs is restarted. This will pickup all the changes made.
Make sure your NAS is shutdown and waiting for WOL magic packet.

Code:
sudo service autofs restart

After that cd into /mnt/nfs/movies or /mnt/nfs/music. Autofs will use the configuration in /etc/auto.master to determine that a script has to be actived (/etc/auto.server.nfs). This script will detect that the NAS has to be started, and generate a WOL magic packet. Then it will wait for the NAS to become active (pingable). The mount string is generated, which is picked up by autofs. After that the mount is created and will be active until no activity has been detected for an hour (3600 seconds).

Notes
you can check which nfs shares are available on the server, by issueing the following command on the client:
Code:
sudo showmount -e  192.168.100.1

In my case showmount gave the following (slightly adjusted output)
Code:
Export list for 192.168.100.1:
/volume1/movies   x.lan
/volume1/music     x.lan
- Information about waking the server, and defining the mount points is logged in /var/log/syslog
- To debug autofs perform the following steps:
1) disable autofs: sudo service autofs stop
2) run automount in the foreground using verbose information: sudo automount -f -v

Usefull links
http://tech-bodges.blogspot.de/2010/06/l...bodge.html
http://www.howtoforge.com/accessing_wind...ing_autofs
http://forum.xbmc.org/showthread.php?tid=99854
Reply
#13
Thanks kaayman. A few notes, the auto.master script refers to auto.server, not auto.server.nfs. The auto.server script refers to ${SERVERIP}:/volume1/${MOUNT}/ which needs to be changed a bit if your NFS share doesn't have volume1 as base.

EDIT: When recording with TVHeadend as my RPi wakes up my NAS randomly. I never knew about autofs, but that seems like a good solution for my problem as it unmounts after inactivity for a while.
Reply
#14
To complete this, if the kodi client shuts down, a shutdown command on the NAS would be desired. Did you incoorporate such a  thing Kaayman?
Reply
#15
Hi folks,

I am also trying to wake up my NAS with autofs. In general, it is working. My problem is: It works only one time after my RasPi was booting. When my NAS goes sleep and the Pi does not reboot after that, further cd /mnt/samba/music do not work - the NAS does not get woken up.

Any hints to solve this?
Reply

Logout Mark Read Team Forum Stats Members Help
[HOWTO] Automatically wake NAS/server on playback request0