Hi everyone,
This is a little 'how to' guide on setting up automatic wakeup to record and shutdown on your R5.5 Knoppmyth box by utilising the wake on lan (wol) method with a router acting as your concierge!
A little background: I tried to use ACPI wakeup and nvram wakeup which is probably easier to setup if your computer hardware supports it, but unfortunately my hardware does not so I've decided to use the WOL method which will help to save electricity by only switching on my mythbox when it needs to record!
Hardware Requirements: This (as far as I'm aware) only works on wired ethernet network connections and is unsupported on wireless cards. Before you proceed make sure your computer supports wake on lan and is enabled (so you don't waste your time or money) - check in your computer's BIOS that it is enabled and test a wake on lan by executing the following command on another linux pc when the mythbox you want to wakeup is switched off (of course!
):
Code:
wol -p 7 -h 192.168.1.255 00:11:22:AA:BB:CC
where -p 7 indicates the port on which to send the 'magic' wake on lan packet and -h 192.168.1.255 is the broadcast for your subnet range (on my internal network I have computers in the range 192.168.1.*) and 00:11:22:AA:BB:CC is the MAC address of the mythbox you want to wakeup. For Windows, you can use various freeware WOL utilities, this site seems to have plenty:
http://www.depicus.com/wake-on-lan/For Mac OSX:
http://www.apple.com/downloads/macosx/n ... onlan.htmlOnce you've checked you can boot your mythbox by the WOL method you can begin!
Part 1: Setting up your Router / Access Point / Other Supported DeviceThe general idea is to use a device that you currently leave switched on permanently and which is supported by firmware from DD-WRT (
http://www.dd-wrt.com ) or Open-WRT (
http://www.openwrt.org ). This allows us to upload wake times to the device and create and run a small script as a CRON job to check for and send out the magic WOL packet to boot your mythbox at the correct time!
I personally use the DD-WRT firmware on my router because it was supported for my device and it is very easy to use after flashing with a good web interface. You can of course use Open-WRT or others but I have no experience with them and shall not be describing them in this HOW TO.
WARNING: This procedure requires re-flashing your router / access point / chosen device with new firmware. Doing so will almost certainly invalidate your warantey / guarentee with such a device. If you mis-flash the device or the flash goes wrong or indeed if anything goes amiss then you will end up with nothing more than a paperweight as it is
VERY hard (if not impossible) to re-cover such devices from a bad flash (
http://www.dd-wrt.com/wiki/index.php/Re ... _Bad_Flash ). I have bricked (
http://www.dd-wrt.com/wiki/index.php/Glossary#Brick ) one of my access points (USR5451A) by flashing it with DD-WRT v24.SP1 even though it said on their website that it WAS supported! So be warned - if you cannot risk irreparably losing your router / access point then do NOT continue. You can find more precautions here on the DD-WRT wiki:
http://www.dd-wrt.com/wiki/index.php/In ... recautions - read this page as it tells you about installing.
So warnings aside (!) and CONTINUING AT YOUR OWN RISK, check that your chosen device is supported on DD-WRT here:
http://www.dd-wrt.com/wiki/index.php/Supported_DevicesIf it is - great! But has your device got enough flash memory?
You will need at least 4MB of flash memory on your device at the time or writing (DD-WRT v24). otherwise SSH is not included with the firmware.
Download the firmware (that works with your device) from their website - I'd recommend the
mini version of DD-WRT as this will give room to save a script on a 4MB device - and follow the instructions on their website on how to flash your device. From personal experience I recommend resetting your device back to factory settings before flashing with DD-WRT (I didn't do this with my USR5451A and it bricked, whereas I reset another and flashed it and it was fine).
- After flashing your device with DD-WRT, log onto your device at its ip address of http://192.168.1.1/ and click on the setup tab. Enter the initial username and password for the device which is 'root' and 'admin' respectively. Cange all the settings on this page to how you want it for your network setup. Of special note is the NTP time settings here - chose an NTP time server and select your current time zone to give the device an accurate current time. Select 'Save Settings' and 'Apply Setttings' to configure your device.
- Next change the username and password on the 'Administration' tab which is very important if your device is connected to the internet. On this page enable JFFS2 and also 'Clean JFFS2' - this will create a small writeable filesystem on the device's flash memory for saving your boot script. Again click 'Save Settings' and 'Apply Settings'.
- On the Administration tab now disable 'Clean JFFS2' and then save and apply the setting as this will prevent your script being erased upon the devices reboot.
- On the Services tab enalbe SSHd under 'Secure Shell' section. I personally have SSH TCP Forwarding disabled and Password Login enabled. Again save and apply the settings.
- These are the minimum steps needed to getting the device able to boot the mythbox but you will probably want to go through the settings and configure everything else for your own network, such as any wireless devices etc.
Now all we have to do is upload our script to the device and getting it running as a CRON job. The script I use is one which I found in a very good guide on MythTV wiki:
http://mythtv.org/wiki/index.php/Wake_On_LAN_Router which is well worth a look at and a read as some background on setting up such a system.
Code:
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/sbin/wol -p 7 -h 192.168.1.255 00:11:22:AA:BB:CC
rm /tmp/wakeup.txt
echo "woke up mythtv" >> /tmp/wakeup.log
fi
Basic things to change are under the wol commands -p is the port you which to send the WOL packet on (usually port 7), -h is the broadcast address on your network (for my network with addresses 192.168.1.* it is 192.168.1.255) and then obviously the MAC address of your mythbox.
To add this script to your device SSH into it by using Putty or another SSH client. The device is very easy to SSH into, just point your client to the device's IP address and logon with the username root (even if you have changed the web logon username to something different) and the password which you have chosen for the web interface logon. A text editor exists on the device - vi - you must type /bin/vi to run it though, so to create this script and save it to flash I'd type:
Code:
/bin/vi /jffs/checkwakeup.sh
where the flash partition is /jffs and checkwakeup.sh is the name of the script I'm creating. Press i to begin typing and then ESC and then type ZZ (case sensitive) to save and exit.
Now we must make this script executable, to do this type
Code:
chmod +x /jffs/checkwakeup.sh
. This script basically checks for a file called wakeup.txt which has been sent to the device by your mythbox when it shuts down. It contains the epoch time when your mythbox wants to be woken up again. If this time has elapsed then the file is deleted and a WOL packet sent to wake your mythbox up again. Else the script simply exits. So we want this script to run every minute (or other time interval) to check whether it is time to wakeup the mythbox. This is accomplished with a CRON job. The CRON job is easily created by going to the devices web interface and selecting the 'Administration' tab. Under the 'Cron' section, ensure that it is enabled and then in 'Additional Cron jobs' paste the following:
Code:
*/1 * * * * root /tmp/checkwakeup.sh
NB: root must be included for the script to actually run and the time interval for running is set as */1 which means every one minutes.
Save and apply the settings and the script should now be running fine.
This location has changed - see post on flash wearing below...
The following is new...To reduce flash wearing and hopefully prevent your device from burning up its flash we shall run the checkwakeup script from the /tmp directory which is the RAM on the router. Since this is cleaned every time the router reboots we must ensure a copy exists here. To do this we create a startup script on the router's website administration consol. Goto: Administration -> Commands and paste the following in the command window (you may have to edit the existing startup script if one exists):
Code:
cp /jffs/checkwakeup.sh /tmp/checkwakeup.sh
Click 'Save Startup' and then reboot your device.
You've now set up your device to boot your mythbox!
Part 2: Setting up your MythBox running Knoppmyth R5.5This part of the how to guide is based upon the MythTV wiki article on this subject:
http://mythtv.org/wiki/index.php/Wake_On_LAN_Router and how to by graysky on the Knoppmyth forum:
http://knoppmyth.net/phpBB2/viewtopic.php?t=18605I've used SSH on my mythbox to do most of the editing and I find it very useful compared to crouching in front of the television with a keyboard. If you are wanting to use SSH you must ensure it is enabled (it is already by default on R5.5) and enable mythtv as a user on it temporarily, edit the /etc/ssh/sshd_config file:
Code:
# nano /etc/ssh/sshd_config
and remove the pounding (#) from mythtv. Then save it and restart your mythbox.
All we need to do is disable mythtv booting when we switch on our mythbox and instead ask mythwelcome to start for us which will handle all the wakeup time settings on our booting device. To do this modify the .fluxbox/apps file to boot mythwelcome:
Code:
$ nano /home/mythtv/.fluxbox/apps
As graysky says in their tutorial simply pound out the line [startup] {KnoppMyth-Run} and then type a new line replacing KnoppMyth-Run with mythwelcome, so that it looks like:
Code:
#[startup] {KnoppMyth-run}
[startup] {mythwelcome}
Save it and exit.
Now exit mythfrontend by pressing ESC repeatedly. Halt MythBackend by issueing a command like:
Code:
# pkill mythbackend
to do so press Alt-X on the homescreen and then su to root. Then press Alt-S to bring up the mythbackend setup. Under the general section skip through to the Shutdown/Wakeup Options page and fill it out exactly as graysky suggests:
Code:
Wakeup time format: yyyy-MM-dd:hh:mm
Command to set Wakeup Time: /usr/bin/mythshutdown --setwakeup $time
Server Halt Command: sudo /usr/bin/mythshutdown --shutdown
Pre-shutdown Check Command: /usr/bin/mythshutdown --check
But you are free to configure your own timeouts. Then skip through the setup and exit the mythbackend setup program.
Press Alt-X again and then type:
Code:
mythwelcome --setup
Under 'Command to Set Wakeup Time' type:
Code:
/home/mythtv/wakeup/mythwakeup.sh --settime $time
For wakeup time type:
Code:
time_t
Ensure the nvram command box is left blank and then configure the rest of the settings as follows:
Code:
Command to reboot: /sbin/reboot
Command to shutdown: /sbin/poweroff
Command to run Xterm: xterm
Command to start the frontend: KnoppMyth-run
Select finish and exit this setup.
Now start a terminal as the user mythtv. Type:
Code:
mkdir /home/mythtv/wakeup
to create the directory structure for our wakeup setting script. The actual script itself that I've used is an adaptation from the mythtv wiki page:
http://mythtv.org/wiki/index.php/Wake_On_LAN_Router To create this script type the commands:
Code:
cd /home/mythtv/wakeup
nano mythwakeup.sh
and type in the following script *:
NB: This script is now an old version and has been left here as an archive Code:
#!/bin/bash
echo $2 > /home/mythtv/wakeup/wakeup.txt
echo "shutdown from mythtv will wake up at $@" > /home/mythtv/wakeup/mythwakeup.log
scp /home/mythtv/wakeup/wakeup.txt root@192.168.1.1:/tmp/wakeup.txt > /dev/null
Where 192.168.1.1 is the IP address of your booting device that you are about to scp your wakeup time to.
Here is the latest version of this script:Code:
#! /bin/bash
tw=$2
cur_count=$(tune2fs -l /dev/hda3 | awk '/Mount count:/ { print $3 }')
max_count=$(tune2fs -l /dev/hda3 | awk '/Maximum mount count:/ { print $4 }')
if [ $cur_count -ge $max_count ]; then
tw=$[$w-600]
fi
echo $tw > /home/mythtv/wakeup/wakeup.txt
echo "shutdown from mythtv will wake up at $@" > /home/mythtv/wakeup/mythwakeup.log
scp /home/mythtv/wakeup/wakeup.txt root@192.168.1.1:/tmp/wakeup.txt > /dev/null
See posting * below for an explanation.
Save this script and then chmod it like so to make it executable:
Code:
chmod +x /home/mythtv/wakeup/mythwakeup.sh
The final step we must do to make this work is to allow dropbear scp - that is a secure file transfer without a password. This is straight forward to do (and described at various places on the internet, I took my advice from here:
http://www.linuxjournal.com/article/8600 ), so first start a command terminal as the user mythtv again. Then issue the command:
Code:
ssh-keygen -t rsa
Simply press enter when promtped for a filename and passphrase. This generates a private and public key pair. Note the key location and open the file ending .pub with nano. Copy the entire (long!) line of text in this file and paste it into the web interface of your device under the Services tab -> Secure Shell -> Authorised Keys. Save and apply the settings on your device. I recommend now copying a few (very small!) files over to the device to ensure that copying by scp is occuring without a password or else you will not send the wakeup time to your device! For instance you will probably have to save the devices SSH key to your mythbox the first time you ever copy a file.
Now you're completely done! Shutdown and switch on your mythbox by issueing a command like
Code:
sudo shutdown -h now
and hopefully everthing should work smoothly!
Part 3: Common Troubleshooting Problems
My mythbox outputted the epoch wakeup time in UTC time format which was different to the time zone (including daylight savings etc.) to the time on my device. So check what timezone you are outputting to with a web epoch to 'readable' time web converter: e.g.
www.epochconverter.com and then change your timezone on your device at Setup -> NTP to account for this.
Make sure your mythbox has power on from the socket when it is off or it won't be able to switch on!
The wakeup time is stored in RAM on the booting device to reduce flash wear. This means that rebooting the device or a power outage would erase the wakeup time and so you would miss your recording. You can store this file in flash by changing /tmp/wakeup.txt to /jffs/wakeup.txt in the above scripts but this will eventually permanently wear out the flash memory on these devices!
Fsck will check your ext3 partitions every 20 or so boots on Knoppmyth R5.5, either change your partition file system (see forum or wiki for how to...) or increase your wakeup before boot time in mythbackend setup to give enough time for a file system check (which may be several minutes on your computer). This will avoid missing the start of some programs!