Difference between revisions of "Shutdown Wakeup"

From MythTV Official Wiki
Jump to: navigation, search
(MythTV Shutdown / Wakeup using nvram-wakeup How to)
(MythTV Shutdown / Wakeup using nvram-wakeup How to)
Line 75: Line 75:
  
 
'''Using a custom GRUB boot option'''
 
'''Using a custom GRUB boot option'''
 +
 +
This option works for most modern mother boards. For older motherboards you may find that the system will halt but not poweroff.
  
 
Edit /boot/grub/grub.conf (for OpenSUSE users use YasT to edit /boot/grub/menu.lst)
 
Edit /boot/grub/grub.conf (for OpenSUSE users use YasT to edit /boot/grub/menu.lst)

Revision as of 08:46, 5 April 2008

System Wakeup

Time.png Outdated: The information on this page may no longer be relevant to the current release of MythTV, 34.0. Please consider helping to update it. This page was last modified on 2008-04-05.

This is a basic tutorial on using the shutdown and wakeup features of MythTV as described here [1].

It is also possible on some motherboards to use the ACPI functionality as detailed in ACPI_Wakeup.

MythTV Shutdown / Wakeup using nvram-wakeup How to

Warning this procedure worked for me, but it may hose your BIOS rendering your motherboard a poor excuse for a frisbee. I hold no responsibility

This assumes you are using a combined frontend/backend machine. Directions are FC2 specific, but should work for many distributions.

# apt-get update
# apt-get install nvram-wakeup
# /usr/sbin/guess-helper.sh
under Fedora Core 3 this might be called just /usr/sbin/guess-helper

Follow the instructions from the script, it will ask you to change the bios settings and reboot several times.

Run through rebooting your computer and creating the config file. The config file nvram-wakeup.conf will be found in the root home directory in the guess-directisa directory. Copy this file somewhere permanent and name it mymb.conf.

To test that you have nvram-wakeup working properly,

 # /usr/sbin/nvram-wakeup -A -C /path/to/mymb.conf -s $((`date +%s` + 10 * 60))
# reboot 

you may not need the -A, but that was the only way it worked for me Check the BIOS upon reboot that the values have been stored correctly. If not, search Google, or as a last resort email the project admin Sergei, he is very helpful.

Here is sample output of a successful nvram-wakeup command

#/usr/sbin/nvram-wakeup  -A -C /etc/mymb.conf -s $((`date +%s` + 10 * 60))
All values are displayed as they are stored in the nvram/rtc.
(and do not correspond necessarily to the system date/time)
WakeUp  : Enabled (0x80)
Day     : 01 (0x01)
Hour    : 21 (0x15)
Minute  : 10 (0x0A)
Second  : 16 (0x10)
rtcDay  : 01 (0x01)
Checksum: 0xB755
Enabling (0x80) WakeUp-on-RTC in nvram.
New Day     : 01 (0x01)
New Hour    : 21 (0x15)
New Minute  : 20 (0x14)
New Second  : 36 (0x24)
New rtcDay  : 01 (0x01)
New Checksum: 0xB773
Now really WRITING into /dev/nvram...


Be VERY careful with nvram-wakeup. guess-helper incorrectly guessed my offsets, in particular my checksum offset. I only found out because I looked through the source code and found previous versions of my BIOS. The short story is that if your BIOS isn't recognised, check the sourcecode in nvram-wakeup-mb.c after using guess-helper - or get yourself a BIOS Saviour or similar first.

Motherboards that require a reboot

Many Motherboards require a reboot for the RTC time to take effect. You can achieve this by one of two methods

1. Have MythTV reboot the computer to a special poweroff-kernel that immediately shuts down the computer.

2. Create a custom boot option in GRUB to power off the system and have MythTV reboot to this poweroff option.

Using a Poweroff-kernel

  • download the poweroff-kernel from the nvram-download page [2]
 $ tar jxvf reboot-2004.10.03.tar.bz2
# cp reboot-2004.10.03/[[bz Image]].2.4.20.poweroff /boot/
  • Now append the following lines to /boot/grub/grub.conf
Title Power Off
     savedefault --default=0
     root (hd0,1)
     kernel /bzImage.2.4.20.poweroff root=/dev/none

Default should be the same as number as your default kernel listed in grub.conf (remember they are numbered 0,1,2...)

Using a custom GRUB boot option

This option works for most modern mother boards. For older motherboards you may find that the system will halt but not poweroff.

Edit /boot/grub/grub.conf (for OpenSUSE users use YasT to edit /boot/grub/menu.lst)

Title Power Off
   savedefault 0
   cat /boot/grub/default
   halt

Now you can check that your poweroff options work work in GRUB by selecting it from your GRUB menu when you next boot.


Edited : recent versions of grub have a bug which fails to flush buffers to disk before the halt command is executed. Reading the file grub uses to store the default data forces it to be flushed to disk.


At this point, all the pieces are in place. We just need to tell Myth what to do. $ mythtvsetup

  • Select General
  • Page through to the Shutdown / Wakeup Options
  • Uncheck Block shutdown before client connected
  • Tweak these as you like, but I used Idle Timeout: 180 | Max. wait for recording: 30 | Startup before rec.: 120
  • Wakeup time format: time_t
  • Set wakeuptime command: /usr/sbin/nvram-wakeup -A -C /path/to/mymb.conf -s $(($time + 300)) (again, you may not need the -A)
  • Server halt command: /usr/sbin/mythshutdown or if you do not require reboot shutdown -h now
  • Pre Shutdown check-command: this should return a value
 * 0 : Allows the box to reboot
 * 1 : Sends the backend around the idle timeout period again
 * 2 : Resets the "a client has connected" flag

If you don't use the box for anything else, just stick "exit 0" in here. Since I tend to have other uses for my box (ssh access to various things, mostly), I don't want it shutting down in the middle of things, so I use ;

#!/bin/bash

if last | head | grep -q "pts/.*still logged in" # checks to see if any users are logged in
then
   exit 1
else
   exit 0
fi


Now create the /usr/sbin/mythshutdown file in your favorite text editor.

 ### /usr/sbin/mythshutdown ###
echo "savedefault --default=1 --once quit" | grub
reboot

Default is whatever place your shutdown kernel is in grub.conf. Most likely 1 or 2 (dual-boot). Make it executable: # chmod 755 /usr/sbin/mythshutdown

The docs for grub recommend you now use the grub-set-default program which is far easier. In addition, many people have a setup where /boot is on a seperate partition and is not mounted in normal system operation for safety. If you don't mount it first, grub-set-default will just write the default option to /boot on your root partition instead. Which doesn't work.

Support for savedefault at the grub prompt has also been removed from recent builds (0.95 - 0.97) as the patch to include this support conflicts with the new location for storing the default (text in /boot/grub/default instead of a binary field in stage2).

#!/bin/bash
BOOT_PARTITION=/dev/hda1
POWEROFF_BOOTOPTION=2
if ! grep -q /boot /etc/mtab
  then mount $BOOT_PARTITION /boot
fi
grub-set-default $POWEROFF_BOOTOPTION
reboot

Finally, change /boot/grub/menu.lst to start from the saved menu entry by default, i.e., add

default saved

to the top of /boot/grub/menu.lst.


Now give it a try. Exit the frontend and wait to see what happens. After the idle time, it should reboot, then shutdown after the grub screen, and wakeup just in time for the next recording. Check the mailing list if you have any problems [3].

Next problem. If you have the frontend startup automatically, it will never shutdown without you manually quitting the frontend. I got around this by starting up the frontend by lirc.

  • Remove link to autostart frontend

$ rm ~/.kde/Autostart/mythfrontend

  • Autostart irexec

ln -s /usr/bin/irexec ~/.kde/Autostart/

  • Edit your ~/.lircrc file to include a button to startup the frontend I used the power button on my grey hauppauge
begin
prog = irexec
button = OFF
repeat = 0
config = /usr/bin/mythfrontend
end

Now when you want to use Myth, just power on the machine and hit the power button on your remote once KDE loads to fire up the frontend.

Good luck, please make additions and comments where necessary.

Troubleshooting

If you have problems with your config file that has been generated, Run nvram-wakeup in debug mode, and see which lines are causing the problem. Delete the problem lines and until the debug output seems shows valid output.

/usr/sbin/nvram-wakeup  -A -C /etc/mymb.conf --debug
nvram-wakeup: Printing debug messages enbled.
nvram-wakeup: $Id: nvram-wakeup.h,v 1.35 2004/07/20 14:20:36 bistr-o-math Exp $
nvram-wakeup: $Id: nvram-wakeup.c,v 1.74 2004/07/20 14:30:40 bistr-o-math Exp $
nvram-wakeup: Opening /etc/mymb.conf in 'r' mode...
nvram-wakeup: /etc/mymb.conf: 2: comment: ################################################
nvram-wakeup: /etc/mymb.conf: 3: comment: ##  Mainboard autodetection information:
nvram-wakeup: /etc/mymb.conf: 4: comment: ##
nvram-wakeup: /etc/mymb.conf: 5: comment: ##    - Mainboard vendor:   ""
nvram-wakeup: /etc/mymb.conf: 6: comment: ##    - Mainboard type:     "K7S41GX"
nvram-wakeup: /etc/mymb.conf: 7: comment: ##    - Mainboard revision: "1.0"
nvram-wakeup: /etc/mymb.conf: 8: comment: ##    - BIOS vendor:        "American Megatrends Inc."
nvram-wakeup: /etc/mymb.conf: 9: comment: ##    - BIOS version:       "P2.00"
nvram-wakeup: /etc/mymb.conf: 10: comment: ##    - BIOS release:       "10/28/2004"
nvram-wakeup: /etc/mymb.conf: 12: name   : addr_stat
nvram-wakeup: /etc/mymb.conf: 12: value  : 0x54
nvram-wakeup: /etc/mymb.conf: 13: name   : shift_stat
nvram-wakeup: /etc/mymb.conf: 13: value  : 7
nvram-wakeup: /etc/mymb.conf: 14: name   : addr_day
nvram-wakeup: /etc/mymb.conf: 14: value  : 0x56
nvram-wakeup: /etc/mymb.conf: 15: name   : addr_hour
nvram-wakeup: /etc/mymb.conf: 15: value  : 0x57
nvram-wakeup: /etc/mymb.conf: 16: name   : addr_min
nvram-wakeup: /etc/mymb.conf: 16: value  : 0x58
nvram-wakeup: /etc/mymb.conf: 17: name   : addr_sec
nvram-wakeup: /etc/mymb.conf: 17: value  : 0x59
nvram-wakeup: /etc/mymb.conf: 18: comment: # but differs somewhere else
nvram-wakeup: /etc/mymb.conf: 18: name   : rtc_day
nvram-wakeup: /etc/mymb.conf: 18: value  : 0x70
nvram-wakeup: /etc/mymb.conf: 19: name   : reset_day
nvram-wakeup: /etc/mymb.conf: 19: value  : ON
nvram-wakeup: /etc/mymb.conf: 20: comment: # guessed
nvram-wakeup: /etc/mymb.conf: 20: name   : addr_chk_h
nvram-wakeup: /etc/mymb.conf: 20: value  : 0x30
nvram-wakeup: /etc/mymb.conf: 21: name   : addr_chk_l
nvram-wakeup: /etc/mymb.conf: 21: value  : 0x31
nvram-wakeup: Closing /etc/mymb.conf ...
nvram-wakeup: Using following bios info:
nvram-wakeup:    need_reboot      = 0
nvram-wakeup:    addr_chk_h       = 0x30
nvram-wakeup:    addr_chk_l       = 0x31
nvram-wakeup:    addr_chk_h2      = 0x00
nvram-wakeup:    addr_chk_l2      = 0x00
nvram-wakeup:    addr_stat        = 0x54
nvram-wakeup:    addr_mon         = 0x00
nvram-wakeup:    addr_day         = 0x56
nvram-wakeup:    addr_wdays       = 0x00
nvram-wakeup:    addr_hour        = 0x57
nvram-wakeup:    addr_min         = 0x58
nvram-wakeup:    addr_sec         = 0x59
nvram-wakeup:    shift_stat       = 7
nvram-wakeup:    shift_mon        = 0
nvram-wakeup:    shift_day        = 0
nvram-wakeup:    shift_wdays      = 0
nvram-wakeup:    shift_hour       = 0
nvram-wakeup:    shift_min        = 0
nvram-wakeup:    shift_sec        = 0
nvram-wakeup:    rtc_time         = 0
nvram-wakeup:    rtc_day          = 0x70
nvram-wakeup:    rtc_mon          = 0x00
nvram-wakeup:    rtc_day_0_is_c0  = 0
nvram-wakeup:    rtc_mon_0_is_c0  = 0
nvram-wakeup:    reset_day        = 1
nvram-wakeup:    reset_mon        = 0
nvram-wakeup:    nr_stat          = 1
nvram-wakeup:    nr_mon           = 4
nvram-wakeup:    nr_day           = 5
nvram-wakeup:    nr_hour          = 5
nvram-wakeup:    nr_min           = 6
nvram-wakeup:    nr_sec           = 6
nvram-wakeup:    nr_rtc_day       = 6
nvram-wakeup:    nr_rtc_mon       = 5
nvram-wakeup:    nr_wdays         = 7
nvram-wakeup:    bcd              = 0
nvram-wakeup:    day_hack         = 0
nvram-wakeup:    upper_method     = 0
nvram-wakeup:    chk_method       = 0
nvram-wakeup: Opening /dev/rtc in O_RDONLY mode...
nvram-wakeup: Hardware clock: 2008-04-01 21:10:14
nvram-wakeup: rtc.tm_isdst : 1
nvram-wakeup: rtc.tm_gmtoff: 3600
nvram-wakeup: diff         : 3600
nvram-wakeup: RTC is running in UTC/GMT!
nvram-wakeup: Test (this should be the current time of the hardware clock): Tue Apr  1 21:10:14 2008
nvram-wakeup: Requesting permissions for io ports 0x70...0x75
nvram-wakeup: The size of NVRAM is 128 bytes.
nvram-wakeup: 0000000: 0000 009F F0C0 0E80 02FF FF2F 00FF FF10
nvram-wakeup: 0000010: 0000 FF00 0000 0000 0000 0040 6F4C 6D4C
nvram-wakeup: 0000020: 09CD FFFF 20AF 69F0 1FF0 0000 0000 0000
nvram-wakeup: 0000030: B755 0000 0000 0000 0000 0000 0000 0000
nvram-wakeup: 0000040: 0000 0000 0110 A602 4909 4129 0080 D0AA
nvram-wakeup: 0000050: 620A 2882 8000 0115 0A10 0000 0000 8028
nvram-wakeup: 0000060: 7500 0048 C303 0D00 0000 0F00 0000 01F4
nvram-wakeup: 0000070: 01C0 1428 1052 2120 0001 0408 2602 0080
nvram-wakeup: value of the addr_stat    byte is: 0x80.
nvram-wakeup: value of the addr_day     byte is: 0x01.
nvram-wakeup: value of the addr_hour    byte is: 0x15.
nvram-wakeup: value of the addr_min     byte is: 0x0A.
nvram-wakeup: value of the addr_sec     byte is: 0x10.
nvram-wakeup: value of the rtc_day      byte is: 0x01.
nvram-wakeup: value of the addr_chk_h   byte is: 0xB7.
nvram-wakeup: value of the addr_chk_l   byte is: 0x55.
nvram-wakeup: Checksum is: 0xB755.
All values are displayed as they are stored in the nvram/rtc.
(and do not correspond necessarily to the system date/time)
WakeUp  : Enabled (0x80)
Day     : 01 (0x01)
Hour    : 21 (0x15)
Minute  : 10 (0x0A)
Second  : 16 (0x10)
rtcDay  : 01 (0x01)
Checksum: 0xB755


Alternative Method

Not all motherboards are compatible with nvram-wakeup. There is another method that can be used whereby the BIOS wake-up date remains fixed and the current time changes as necessary to give the correct delay until the machine wakes up.

First, set the BIOS wake-up time to 31 January 2006 23:59:59 using the BIOS menu. If you can't enter the full date, enter as much as possible (the day of the month, hour, and minutes are the important bits).

This system uses two scripts:

/usr/sbin/settime

#!/bin/bash
BiosWakeup="2006-01-31 23:59:59"
Wakeup=`date -d "$1" +%s`
Now=`date +%s`
Bios=`date -u -d "${BiosWakeup}" +%s`
Diff=$[${Wakeup}-${Now}]
echo "$[${Wakeup}-${Bios}]" > /etc/timediff
date -u -s "${BiosWakeup} ${Diff} seconds ago"
hwclock -w --noadjfile --utc

/usr/sbin/correcttime

#!/bin/bash
if [ -r /etc/timediff ]; then
        Timediff=`date -r /etc/timediff +%s`
        Now=`date +%s`
        if [ "${Timediff}" -gt "${Now}" ]; then
                Diff=`cat /etc/timediff | head -n 1`
                date -s "+${Diff} seconds"
                hwclock -w --noadjfile --utc
                exit 0
        fi
fi

The correcttime scripts needs to be run before mythbackend starts. I call the following script from my /etc/init.d/mythbackend script, before the mythbackend daemon is started:

/usr/sbin/mythstartup

#!/bin/bash
if [ ! -e /dev/dvb/adapter1 ] # I have two adapters - sometimes the numbering
                              # gets messed up so I test and correct
then
        mv /dev/dvb/adapter2 /dev/dvb/adapter1
fi
/usr/local/sbin/correcttime   # Correct the time, according to the difference
                              # stored by the settime script
ntpdate pool.ntp.org          # Check that the time is really correct. RECOMMENDED

Now setup the wakeup and shutdown commands in mythtvsetup, as for the first method above, but use the following settings instead:

  • Set wakeuptime command: /usr/sbin/settime "$time"
  • Server halt command: /sbin/halt -p