Saturday, February 03, 2007

Automatic wakeup for Grumpy

This had me puzzled for the longest time (see my musings from last summer).

Today I decided not only to finally run some backups of Chef, but also to make Grumpy (my MythTV box) shut down after it's done recording. I stumbled over mythwelcome a very slick new feature in MythTV 0.19. It's a welcome screen for mythfrontend (similar to what I wanted to write), and additionally has a few additional features (like automatically turn on the system during some specified period, e.g. in the evening when I'm most likely to watch TV).

A few months ago I collected all the necessary data for nvram-wakeup to set the NVRAM alaram in the BIOS. I got mythwelcome configured just fine following the instructions in the Wiki. It seems to work, but I still don't like messing with the nvram directly. Finally, I saw some notes in the ACPI_Wakeup section of the Wiki about how "some finicky BIOSes reset the RTC alarm when the time gets written". Guess what, every Linux distribution syncs the system time to the hardware clock on shutdown. I have a finicky BIOS!

As susggested in the Wiki, I modified /etc/init.d/hwclock.sh to on shutdown read out /proc/acpi/alarm, save the system time, then write back the alarm time. Lo and behold it worked on the first try.

Here are my final configuration settings for this:

I'm using mythwelcome and have it configured to call /usr/local/bin/myth-set-acpi-wakeup in the nvram-wakeup command line:


#!/bin/bash

# set wakeup time via ACPI alarm
#

logger "wakeup: got command line $0 $1 $2 $3 $4 $5 $6"

if [ -z "$2" ]; then
echo "usage: $0 --settime "
echo "must be in seconds since epoch"
logger "no wakeup time given"
exit 1
fi

# This is a crazy hack
# The problem is that mythwelcome is passing us the timestamp
# in localtime not adjusted for UTC, but date is using the
# base of UTC, so we need to adjust back to PST.
# TODO(bbeck): Daylight savings time is probably going to bite me on this, I should set
# the BIOS clock to UTC.
seconds=$2
utctime=`date -d "1970-01-01 $seconds sec" +"%Y-%m-%d %T"`
time=`date -d "$utctime -0000" +"%Y-%m-%d %T"`
# end of hack

logger "setting wakeup time to $time."

echo "$time" > /proc/acpi/alarm

result=`cat /proc/acpi/alarm`

if [ "$time" == "$result" ]; then
logger "wakeup time set properly"
else
logger "wakeup time not set (got $result)"
fi



The nvram-wakeup command is set to myth-grub-poweroff:

#!/bin/sh

echo "savedefault --default=4 --once" | grub


which triggers grub config 4 in my menu.lst on the next boot:

title PowerOff
savedefault --default=0
cat /boot/grub/default
halt



reboot and poweroff commands are set to

/sbin/reboot and /sbin/poweroff, respectively

Shutdown/Wakeup options in mythtv-setup are configured as follows:

Block shutdown before client is connected is checked.

Idle timeout: 60s

Max. wait for recording: 30m (I'm recording several shows that are 30 minutes apart. No point to shut down in between)

Startup before rec: 180s

Wakeup time format: yyyy-MM-ddThh:mm:ss

Set wakeuptime command: mythshutdown --setwakeup $time

Server halt command: sudo mythshutdown --shutdown

Pre-shutdown check command: mythshutdown --check

No comments: