## HOWTO use shutdown & resume with Avicap

back to Avicap Index
docu by: Alexander Rawass (alexannika@users.sourceforge.net)
if anyone can write a better documentation - please do it and tell me
this howto describes how you can shutdown,poweroff,resume & reboot your linux with Avicap, to have a video recording tool with a programmable timer, that allows your computer to be powered-off in-between recordings

- Check your BIOS
- Install nvram-wakeup
- root scripts for your linux
- Configuring Avicap for shutdown & resume
- known problems

### Step 1: Check your BIOS

Most modern BIOS have an advanced power management section, which allows you not only to configure sleep/suspend modes, but also on which signals your computer should poweron from 'soft-off'
Goto to the BIOS, to the advanced power management section
Turn on advanced power management on, if you haven't enabled it before, you might want to set most entries to 'disabled'.
For awakening after a soft-off, we need to enable
'RTC Alarm Resume from Soft off'
Below this, you should be able to set the date and the time to wakeup.
If you don't find such entries in your BIOS, well, it won't work for you,sorry.

### Step 2: Install nvram-wakeup

http://sourceforge.net/projects/nvram-wakeup
Download the latest version of nvram-wakeup.
nvram-wakeup is a tool to set the RTC alarm time in your Bios - WARNING, this may be dangerous, be careful what you do!
compile nvram-wakeup.
Read the README file. Read it again.
Read the README.mb file. Read it twice again!
Now you can be lucky, then your motherboard/bios will be supported by nvram-wakeup.
If you're unlucky (like me with my Asus K7M motherboard), you'll have to find out the bytes in your nvram that contain the RTC alarm date.
Read the README.mb file again.
Maybe you can use the latest CVS version of nvram-wakeup, else you'll have to run the 'guess' script that's delivered with nvram-wakeup, it's usage is described in README.mb
If you've now followed the steps in README.mb and you now seem to have a working configuration for nvram-wakeup, test it yourself, and be aware that false bytes written in your nvram may you cause to loose your precious bios settings.
All I can say, the guess script worked for me, with a little modification.
To use nvram-wakeup with avicap, you should edit 'nvram-wakeup.h' and set these defines to zero like this:
#define WAKEUP_BEFORE 0
#define NEED_TO_SHTDWN 0
cause these values can be set in Avicap's GUI directly.

### Step 3: root scripts for your linux

For the shutdown & resume process to work for Avicap, you'll need to have several shell scripts that will be run via sudo:
avicap-shutdown: let's avicap shutdown your computer
avicap-setnvram: sets the RTC alarm date in your BIOS via nvram-wakeup
avicap-boot: a init script for /etc/init.d that starts your computer either via the normal way, or it starts avicap in timer-mode, and manages the 'twice shutdown' problem.
These scripts can be found in avifile/samples/qtvidcap/scripts
They will not get installed by 'make install', you have to copy them to their locations for yourself, cause they could do damage to your system by shutting down unexpectedly.
avicap-shutdown and avicap-setnvram should be copied to /usr/local/bin
avicap-boot should be copied to /etc/init.d
You also have to create these links:
/etc/init.d/boot.d/S01avicap -> /etc/init.d/avicap-boot
/etc/init.d/rc3.d/S99avicap -> /etc/init.d/avicap-boot
You HAVE to edit all these scripts manually to suit them to your system - especially avicap-boot is highly customized to my system, my way of booting and my preferences.
You also have to edit with 'visudo' your sudoers file to allow avicap to call avicap-shutdown and avicap-setnvram, something like this:
alex ALL= NOPASSWD: /home/alex/sbin/avicap-shutdown
alex ALL= NOPASSWD: /home/alex/sbin/avicap-setnvram
Be aware that using these scripts and letting avicap shutdown & resume your computer interferes severely with the running of a full blown unix system.
The shutdown & resume capabilites should only be used on a 'single-person' workstation, with no running jobs in background.
I've prepared such scripts for my system, they might suit you or not, if you've got better solutions, send them to me.
My system is a SuSE 8.1-based system, running kernel 2.4.18 with apm enabled (this is necessary to poweroff your ATX power...
I am only booting my system to runlevel 3 (no networking, no graphics).
I login in textmode and then do a 'startx' manually.
The avicap-reboot script will check at the end of runlevel 3, if it should do nothing or start X and avicap automatically, you have to edit this script to suit it to your system and preferences.
On my system, there's a special problem:
the setting of the RTC and the resume will only work, if the system is resetted, after the RTC alarm date in the nvram has been set.
So the shutdown & resume will have to shutdown TWICE, once to reboot to make a reset, and the other to actually shutdown.
It goes like this:
1. Avicap calls 'sudo avicap-setnvram <timestamp>' to set the RTC
avicap-setnvram will create a file /etc/avicap/next_recording
2. Avicap calls 'sudo avicap-shutdown' to shutdown & reboot my system
avicap-shutdown will create a file /etc/avicap/first_shutdown
then it will make a shutdown -r (reboot) to reboot the system
3. The system reboots, and shortly after booting, 'avicap-boot' is called again (in runlevel S)
4. If the file /etc/avicap/first_shutdown exists, the file gets removed and the system will go down again with halt -p -f (force power off) to poweroff the computer.
5. The computer is now off, till the time you've set for first recording.
6. The RTC alarm will poweron your computer
7. My linux will boot in runlevel 3, at the end of runlevel 3 'avicap-boot' is called.
8. If the file /etc/avicap/next_recording exists, then it is assumed that the re-boot is a 'timed' reboot done by the RTC alarm.
9. avicap-boot will then call 'startx' to start X and KDE (takes a long time...) or Gnome or whatever and then 'avicap -timer'
10. and then avicap should start recording as you liked it
11. until the recording stops and it is time to go down again

### Step 4: Configuring Avicap for shutdown & resume

There's a new Tab in the 'Configure'-Dialog named 'Shutdown&Resume'
These options tell avicap when to shutdown:

- never shut down system
  avicap will never try to shutdown your system
- shutdown after last tecording
  avicap will shutdown (to poweroff) your system only after the last recording, when there are no pending records
  you don't need nvram-wakeup for this
- shutdown in-between recordings
  avicap will shutdown (first to reboot, then to poweroff) your system with nvram-wakeup between recordings

- minimum downtime in min
  the system will only be shut down in-between recordings, when the system would be in power-off longer than this time
  it is probably not sensible to send the system up and down for just 5 minutes downtime
  sensible values could range from 10 to 30 minutes or so
- boot n mins before recording
  a unix system can take quite a lot of time to boot and to start X/KDE/Gnome on top of it.
  the system will power-on and reboot n mins before the recording should start
  make sure that time is long enough, if you are not using a journaling filesystem, you should be aware that sometimes booting can be quite long
  sensible values could range from 5 to 15 minutes
- gracetime for shutdown in min
  if avicap decides that a shutdown should be done, it pops up a critical requester, which shows a countdown for gracetime mins
  press abort in that dialog to abort the shutdown

To use that feature, you also have to enable the 'shutdown' checkbox in the main timertable window.
That checkbox will always be de-activated when you start avicap normally (so you have to check it again).
If you start avicap with 'avicap -timer', avicap will open the timertable window, sets that checkbox and starts the timer and waits for the first recording.
avicap is started with -timer from avicap-boot, so it will go down if the recording has finished.
I advise you strongly to test this feature thoroughly, so that you can be sure it works for you,too.
It would be a shame, if a precious recording is lost - but, alas, Free Software comes without any warranty, so be warned

### Known Problems

- my system is running it's hardware-clock in localtime, so the RTC alarm is also set in localtime, there's code to check if localtime differs from UTC and sets the RTC alarm correctly.
  I believe. Umpft. Test it yourself.
- I have no idea what happens if your system (hardware) clock runs in UTC.
  test it thoroughly and tell me about it
- I have no idea what happens if the local time switches an hour to the future or back - for example, when we Europeans switch from winter time (CET) to summer time (CEST) or back
  Your recordings might start at the right time, an hour too late or an hour too early
- I have no idea what happens when you're flying in an airplane that travels round the world (with or against time), I also don't know what might happen if you fly in a spaceship at 95% light speed, you've been warned, the timer might do really crazy things there ;-)))
- I am using QDateTime for my calculations, I just hope it's clever enough to cope with leapyears and such

When you've started Avicap from a shell window, it will show it's output there when nvram-wakeup is setting the RTC alarm, you should see for yourself if the RTC is set correctly.