Wake On LAN Router

From MythTV Official Wiki
Jump to: navigation, search

WAKE-ON-LAN HOWTO with Linksys WRT54G Router

Many MythTV users would prefer to have their Myth Backend shutdown when not recording, and automatically power up in time to record the next program. Attempting to use NVRAM Wakeup and ACPI may have failed. An easy alternative is to use Wake On Lan, in which another computer (or the router) sends a packet over ethernet to the Myth backend machine waking it up. The benefit is almost no extra packages or difficult configs to set up on the Myth box. Another bonus is that you can convert your Linksys router into something more familiar and usefull, a little Linux box.


This HOWTO covers setting up the MythBackend system, setup for MythWelcome, creating the shell scripts on the Myth box and on the router. You will be creating a shell script to be executed when Myth wants to shut down. This will set the next recording time and copy it to the router. On the router you will create a cron job that reads this file and wakes up the Myth box before the next recoding starts.

This HOWTO will not duplicate the other HOWTOs about waking up Myth and using MythWelcome, and will only partially cover installing OpenWRT on a router. You should read about all of these on your own, using the links in this document.

Understanding Mythtv Wakeup Support and MythWelcome

For background on how MythTV supports scheduled shutdown and wakeup, Please read these:

Myth Official Doc on Shutdown/Wakeup

Wiki entry on using Shutdown/Wakeup with NVRAM wakeup

MythWelcome HOWTO . MythWelcome will shutdown your box when not in use. We will hook into Mythwelome in this HOWTO.

ACPI Wakeup An alternative to this method and to to NVRAM Wakeup

Setting up the MythTV Backend

Run mythtvsetup on the master backend. On the Shutdown/Wakeup Options page you need to set the idle timeout in seconds, I use 1200 seconds or 20 minutes. If the Myth backend is not flagging, transcoding or recording, then the Myth box will shutdown after 20 minutes. The next config is Max wait for recording, which means the backend will not shutdown if a recording is coming up in this many minutes. Set it to whatever you like, but 25 minutes would work for this example.

The startup before recording (Seconds) is the important one for this HOWTO. A cron job will be running on the router every ten minutes, although you could do it more often. It will check if the time to wake up the backend has passed and it will wake it up. Therefore, I have to set the backend to wake up more than 10 minutes before a recording starts or else I may miss it. If your system also takes some time to boot you have to add in more time for that. Fifteen minutes or 900 seconds would be a good value. Make sure the idle time (above) is longer.

I do not check "Block shutdown before client connected" since my box is a frontend and backend. The rest of the values are defaults.

Mythtvsetupfixed86.jpg

Setting up MythWelcome

MythWelcome is a nice tool that integrates into the Myth Wakeup/Startup. It has a HOWTO on the wiki. Note that you will need to change your system to automatically run Mythwelcome instead of MythFrontend on startup.

We need to configure Mythwelcome to call our own script before shutting down the MythTV backend. This script will be called with the date/time as a parameter, and will copy a file to the router containing the wakeup time.

Start the Mythwelcome setup program from an X console or X shell:

mythwelcome --setup

MythWelcome is expecting to call the NVRAM Wakeup program, but you can fill in any program name you like. In the NVRAM Wakeup command, fill in /usr/share/my_mythwakeup.sh

For the shutdown command I use /sbin/poweroff. Your command may be different.

Example mythwelcome.jpg

Script for the Myth Backend

my_mythwakeup.sh is a shell script that will be called to set the next time to wakeup. Mythwelcome will call your script like this:

my_mythwakeup.sh --settime 1165842090

The date is expressed as seconds. You could manipulate this by adding or subtracting seconds, but we don't have to since we set the values in Mmythtvsetup. Here is the script:

  #!/bin/bash
  echo $2 > /usr/share/wakeup.txt
  echo "shutdown from mythtv will wake up at $@" >> /var/log/mythwakeup.log
  scp /usr/share/wakeup.txt root@192.168.1.1:/tmp/wakeup.txt

It just writes the 2nd parameter, such s 1165842090 to a file and copies the file using scp to the router. A cron job on the router is watching for the file. If you cannot scp to your router without a password, read the section below on setting up the router for password-less scp with OpenWRT. It contains a link to the HOWTO to set this up. It's very easy.

Test the script by running it like this:

  /usr/share/my_mythwakeup.sh --settime 1165842090

if it does not run you can chmod +x to it. It should copy a file to /tmp on your router. Fix the IP address if different.

Script for the router

On the router you need to set up a cron job and add a script. This is the script I use on my router:

 now11=`date  "+%Y%m%d%H%M"`
 echo "checking for wake up at $now11" > /tmp/wakeup.log
 now11=`date  "+%s"`
 if [ ! -f /tmp/wakeup.txt ]
 then
       exit
 fi
 read wakeupat < /tmp/wakeup.txt
 if [ "$wakeupat" -lt "$now11" ]
 then
   /usr/bin/wol -p 65535 -h 192.168.1.255  00:15:32:2D:C6:46
   rm /tmp/wakeup.txt
   echo "woke up mythtv" >> /tmp/wakeup.log
 fi

The script is checking for the file /tmp/wakeup.txt. If it finds no file, it just exits. Otherwise it reads the time stored in the file. It constructs a date in the same format (seconds) and compares them. if the time in the file is less than the current time on the router, it will send the wake-on-lan packet to the mythbox and delete the file.

I add this to cron with

echo "*/10 * * * * /usr/share/checkwakeup.sh" >> /etc/crontabs/root

You can check your cron jobs with

 crontab -l 

The extra echo to /tmp/wakeup.log is for troubleshooting. The script on the backend also writes to a log in /var/log.

You can test your script by running it on the router. make sure you chmod it, and since you copied the file in the earlier step, it should be there.

run it like this:

 sh /usr/share/checkwakeup.sh

That should be it. Continue reading if you need help setting up the router. There are links to other howtos. If your machine is not waking up, read on. There is another HowTO in this wiki for seting up your NIC to wake on Lan. Later I also reference the OpenWRT HOWTOs, including setting the correct time on your router.

Scripts for tomato firmware v0.18.x

The above tutorial won't work directly for the tomato router. The /tmp folder is erased and recreated every time to reboot the router..So if you have a power failure, you will miss your next scheduled recording of BSG as I did. So to get around this:

  • in the Tomato admin section, activate your JFFS2 folder, and optionally format it. This will enable the falsh filesystem that will survive after a reboot.
  • Add this to your Scripts Init section in Tomato. I like this method since it recreates the file in ram and sets the cron job at every boot up. Notice the differences in bold.
# Monitor MythTV wakeup.txt to wake computer via WOL
if [ ! -x /tmp/home/root/checkwakeup.sh ] ; then
cat > /tmp/home/root/checkwakeup.sh <<EOF
now11=\`date  "+%Y%m%d%H%M"\`
MAC=YOUR_ROUTERS_MAC_ADDRESS
 echo "checking wakeup \$now11" > /tmp/wakeup.log
 now11=\`date  "+%s"\`
 if [ ! -f /jjfs/wakeup.txt ]
 then
       exit
 fi
 read wakeupat < /jffs/wakeup.txt
 if [ "\$wakeupat" -lt "\$now11" ]
 then
   
   /usr/bin/ether-wake  "\$MAC" 
   rm /jffs/wakeup.txt
   echo "woke up mythtv" >> /tmp/wakeup.log
 fi
EOF
chmod +x /tmp/home/root/checkwakeup.sh
fi

# Execute via cron every minute
cru a wolMythTVWatch "* * * * * /tmp/home/root/checkwakeup.sh"
  • Now slightly modify the script given in the above section.
  #!/bin/bash
 echo $2 > /usr/share/wakeup.txt
 echo "shutdown from mythtv will wake up at $@" >> /var/log/mythwakeup.log
 scp /usr/share/wakeup.txt root@192.168.1.1:/jffs/wakeup.txt

OpenWRT or DD-WRT

OpenWRT is a Linux distribution for embedded devices. You can flash it to overwrite the firmware on several different routers by Buffalo, Linksys, Asus and others. See the OpenWRT page for details:

OpenWRT main page

I currently use DD-Wrt which is a well maintained and documented, and has many features built right into the web user interface so it should be easier to set up WOL and Cron for your Mythbox right in the web user interface. I recommend installing the "mini" version, so you have more room to create a file system on the router.

DD-WRT home page

I already had a Linksys WRT54G Version 1.1, which can be flashed to OpenWRT. You can still buy supported routers from on line retailers such as Egghead, or try your local Craigslist.

If you decide to install OpenWRT, it is quite easy to set up, but please spend some time reading a few different sources before you do it to avoid turing your router into a $50 paperweight.

Once OpenWRT is installed you first telnet to it to set up a password, after the initial telnet, you will use ssh to connect to it like a normal linux box. In the shell you will use commands to set up cron jobs, or install new packages. OpenWRT comes with ipkg, similar to apt or yum, for managing installs.

Make sure you install the correct package for your router, and I recommend squash fs since it may later help you if mess up your installation and need to recover.

Install instructions for OpenWRT and the main docs are here:

White Russion 5 by router

OpenWrt docs link title

Additional Packages for OpenWRT

In addition to the standard OpenWRT (White Russian 5) you need to install the wol package and the ntp client package. This is simple to do using the package manager ipkg. You will need to configure a cron job, ntp, and copy a public key from your Myth backend to the router for password-less scp copying. These are very easy using the following HOWTOs from the OpenWRT wiki.


Additional package from OpenWRT are found here:

http://downloads.openwrt.org/whiterussian/packages/

from this page download:

wol_0.7.1-1_mipsel.ipk

ntpclient_2003_194-2_mipsel.ipk

You can use ipkg to install from the url or copy the ipk files to your router and install locally.

Please read this for setting up NTP client and set your timezone before installing the ntp client.

Please read this for setting up NTP client and set your timezone

Set up cron and verify that it works:

Cron Instructions on the OpenWRT wiki


and finally, setting up the key for scp/open ssh. You generate the keys on the Myth box and then you copy the key to the router and add them to dropbear:

How to generate and add keys


Optional, but good to have, a very nice web interface for the router.

WebIf2 for openWRT

Install wol on Router and and Test it

wol is an official additonal/optional package. You can install WOL on the router from the shell. do:

 ipkg install http://downloads.openwrt.org/whiterussian/packages/wol_0.7.1-1_mipsel.ipk

The download page for other packages is http://downloads.openwrt.org/whiterussian/packages/

Once wol is installed on your router you might as well test it. On your Myth Backend, use ifconfig eth0 to see the MAC id of you ethernet card.

use the poweroff command to power off the mythbackend.

shell to the router using scp. At the prompt do this:

 wol -p 65535 -h 192.168.1.255  00:15:33:4D:C6:28

the last part is your MythBackend ethernet MAC id or MAC address. You can get this from a shell prompt on your Mythbackend like this:

ifconfig eth0

The first line should end with the mac id:

eth0      Link encap:Ethernet  HWaddr 00:15:33:4D:C6:28

The -h 192.168.1.225 assumes that your network is set up in the default linksys way, with the router having the address 192.168.1.1 and the rest of the machines in the network having addresses such as 192.168.1.100 etc. Try it and see if your machine wakes up. If not, you may have to use ethtool to set your nic to wakeup. See this doc on the Myth wiki for setting up ethtool and Wake On Lan