mythtv & wakeup
ENGLISH VERSION BELOW!
Attenzione: post incredibilmente noioso per chi non interessato.
Argomento: come usare un computer per svegliare la propria macchina mythtv e non perdersi nessuna registrazione
Specifico per mythtv, applicabile ad altri sistemi
TOPIC: how to set up a linux pc in order to wake up a mythtv backend... without missing any recording! :)
The guide is mythtv-specific but also applicable to many other use (mostly, simply wake up a machine at specific time).
[tags: mythtv]
A WakeOnLAN slave and a MythTV-Box
(aka Usare uno schiavetto per svegliare la mythbox via wakeonlan)
(aka Setting up a WakeOnLAN box to act as MythTV Concierge, the easy way)
Questa guida illustrerà i passi che ho fatto per risolvere uno dei
problemi più grossi della mia mythbox (Asus Digimatrix, DM): è un
Pentium 4 e consuma quanto un condominio.
DM, nel mio caso, è una
macchina dedicata esclusivamente a mythtv ed è un Backend / Frontend;
tenerla accesa, anche quando non ero in casa e non c'era niente da
registrare, era pura follia.
Mi sono quindi applicato per trovare un
modo per tenerla sempre accesa e svegliarla quando volevo io o quando
doveva fare qualcosa (ad esempio, registrare Scrubs).
Ci sono
vari modi, e google ve li indicherà sicuramente tutti, per raggiungere
questo obiettivo; i principali, a quanto ho visto, sono:
1. usare le funzionalità già presenti in mythtv (e nel nuovo modulo, mythwelcome) per autoshutdown e autowakeup.
2. fare svegliare la mythbox da un altro computer acceso 24 ore su 24 (che tanto, nevvero, ce l'abbiamo tutti per i torrent :).
Inizialmente, ho provato con il metodo 1.
Sostanzialmente,
mythtv permette di specificare vari parametri per autoshutdown e
autowakeup (li trovate in mythtv-setup, schermata Shutdown/Wakeup
options).

I più importanti sono:
- idle timeout: dopo quanti secondi di inattività la mythbox dovrà lanciare il "server halt command" (vedi sotto)
-
max wait for recording: l'intervallo di tempo, in minuti, prima di una
registrazione che inibisce lo spengimento (ad esempio, sono le 18.52 e
la mythbox è in idle; dovrebbe spengersi ma si accorge che dopo 8
minuti comincia una registrazione. Se impostate questo parametro su un
tempo superiore a 8, la mythbox non si spengerà)
- Startup before
rec: quanto tempo prima (di una registrazione) accendersi. Se per
esempio la vostra macchina impiega 50 secondi per partire, un tempo
superiore al minuto dovrebbe garantire alla mythbox di essere pronta a
registrare prima dell'inizio della trasmissione
- Wakeup time
format: come deve essere impostato il formato ora / data (se non
l'avete MAI modificato, non toccatelo per ora! leggete più avanti e
capirete)
- Set wakeuptime command: il comando utilizzato per
impostare l'ora di sveglia. questo comando viene lanciato subito prima
di quello successivo ed utilizza il formato ora / data specificato
sopra (lo passate con il $time)
- Server halt
command: il comando da utilizzare per spengere la macchina (o per metterla in suspend on ram, susp on disk, ecc)
-
Pre shutdown check-command: il comando da lanciare prima dello shutdown
per verificare che la macchina sia veramente idle (ad esempio, se non
specificate niente in questo form, potreste bloccare un transcode con
mythtranscode perchè mythtv si considera idle quando 1. non c'è nessun
frontend attaccato e 2. non c'è niente da registrare, non considerando
minimamente, quindi, altri processi)
Queste opzioni vi
permettono di personalizzare tutti gli aspetti necessari
all'autoshutdown e autowakeup, ma sono principalmente rivolti a chi
vuole gestire il tutto con un'unica macchina.
Infatti, gli strumenti specifici per questo tipo di operazione sono:
- NVRam Wakeup
- Acpi Wakeup
NVRam
è, se non sbaglio, il primo sviluppato ed è molto legato all'hardware
che state utilizzando (non tutte le mobo sono supportate); inoltre, a
quanto ho letto, è potenzialmente pericoloso (gioca con la memoria non
volatile della scheda): se sbagliate qualcosa, potete buttare la vostra
scheda.
Acpi Wakeup è innocuo e funziona generalmente sulla maggior parte delle schede recenti.
E' molto facile da gestire, ma può essere condizionato da limitazioni del BIOS.
Non
entro nello specifico (ci sono zilioni di pagine che lo fanno meglio di
quanto farei io, comunque principamente cercate su google per
/proc/acpi/alarm , /proc/acpi/wakeup).
A causa di alcune limitazioni nel mio BIOS, non potevo utilizzare al meglio ACPI Wakeup e quindi mi sono rivolto ad altro.
Ecco sostanzialmente il metodo che ho trovato (grazie al link passatomi da ale123):
-
il mio DM gestisce solo la parte relativa allo spengimento automatico,
ma subito prima del poweroff comunica ad un altro computer (quello
sempre acceso per i torrent) a che ora si dovrebbe svegliare
- l'altro computer (toshin) tramite uno script in cron sveglia il DM all'ora decisa
Questo il necessario:
- una mythbox (DM) che supporti il wake on lan (fate prima le prove)
- una macchina sempre accesa (toshin) per svegliare DM
Poi, su toshin:
- il comando "cron" (usato per controllare periodicamente se DM ha comunicato un nuovo orario di sveglia)
- il comando "at" (usato per schedulare la sveglia)
- il comando "wakeonlan" (usato per svegliare)
-
una partizione NFS (o altro) "messa a disposizione" di DM per creare il
file in cui specifica a che ora deve svegliarsi (saranno pochy byte,
non preoccupatevi)
Vediamo un attimo come funziona il comando protagonista di tutto sto popo', "at", con un esempio.
Vogliamo
lanciare ad una data specificata (facciamo il 05 dicembre 2006 alle
11.21) un certo comando, ad esempio 'tv_grab_it > guidatv.xml'
il comando che dobbiamo specificare è:
$ echo 'tv_grab_it > guidatv.xml' | at 11:21 12/05/2006
L'aspetto più importante da tenere d'occhio è come (il proprio) "at" accetta il formato data e ora.
Fate
un tot di tentativi finchè non lo trovate (ad esempio, provate ad
eseguire il comando 'touch file_di_prova' ad una certa data/ora e
controllate se tutto ha funzionato guardando data e ora di creazione
del file).
Il formato che indico in questa guida è quello che
accetta "at" su debian unstable, ma potrebbe cambiare se avete
modificato la variabile di sistema relativa; una volta che avete
trovato il formato giusto, segnatevelo da qualche parte.
Detto
questo, se avete fatto un tot di prove a
manina per controllare come
funziona "at" e come svegliare tramite wakeonlan, abbiamo fatto.
Andate sulla mythbox e lanciate mythtv-setup da un terminale (poi capirete perchè) e andate nelle "shutdown/wakeup options".
Impostate i parametri di tempo secondo preferenze e poi buttiamoci sulle cose "difficili".
Lanciando mythtv-setup da terminale, avrete un output simile a
mythtv@digimatrix:/data/psp/tmp$ 2006-12-04 20:43:39.406 Using runtime
prefix = /usr
2006-12-04 20:43:39.572 New DB connection, total: 1
2006-12-04 20:43:39.615 Connected to database 'mythconverg' at host: localhost
2006-12-04 20:43:39.619 Total desktop dim: 1440x900, with 1 screen[s].
2006-12-04 20:43:39.636 Using screen 0, 1440x900 at 0,0
2006-12-04 20:43:39.646 Current Schema Version: 1158
2006-12-04 20:43:39.681 Newest Schema Version : 1123
2006-12-04 20:43:39.682 New DB connection, total: 2
2006-12-04 20:43:39.684 Connected to database 'mythconverg' at host: localhost
Come
vedete all'inizio di ogni linea c'è l'indicazione della data e ora. Da
quanto si vede sopra sembra proprio che mythtv la indichi come:
anno_completo-mese-giorno ore:minuti:secondi
Prestateci
attenzione e controllate cosa avete nel form "wakeup time format". A
quanto ho visto, avrete esattamente lo stesso formato utilizzando i
caratteri:
yyyy/MM/dd hh:mm:ss
In questo modo, potete capire quali sono le lettere (occhio, è case sensitive) associate da mythtv ai mesi, anni, minuti, ecc.
Ora non dovrete fare altro che utilizzare queste lettere per indicare il formato data/ora che accetta il vostro "at".
Nell'esempio che facevo sopra, il mio accetta questo formato:
hh:mm MM/dd/yyyy
Bene, scrivete il vostro in "wakeup time format".
Nel "set wakeuptime command" inserite:
echo $time > /path/per/la/partizione_nfs/condivisa/orario_di_sveglia
Che cosa vuol dire tutto ciò?
La
mythbox, prima di spengersi, creerà un file con l'indicazione della
data / ora di sveglia su di una partizione dell'altra macchina (toshin).
Poi:
- Server Halt command: quello che preferite per spengere la macchina. per semplicità, 'sudo halt'
-
Pre Shutdown check-command: per semplicità, 'exit 0'. Occhio che in
questo caso, la mythbox non farà _nessun_ controllo prima di spengersi
(oltre a quelli default sul frontend attivo e sulle registrazioni).
A questo punto, segnamoci da qualche parte il MACAddress (si può vedere con ifconfig) e abbiamo finito il lavoro sulla mythbox.
Per
provare, mettiamo a registrare qualcosa che parte tra un'ora (all 17:20
del 5 dicembre 2006, ad esempio) e chiudiamo tutti i frontend
eventualmente attivi.
Tra un tot, la mythbox si spengerà.
Andiamo sull'altra macchina (toshin).
Poco
prima di spengersi la mythbox, dovrebbe aver creato su toshin un file
in cui è indicata data e ora per l'accensione. Nell'esempio, il file è
in /data/orario_di_sveglia
$cat /data/orario_di_sveglia
17:18 12/05/2006
Bene. La mythbox ha fatto il suo dovere.
Ora mettiamo uno script in cron che ogni 30 minuti (ad esempio) imposta con "at" l'orario di sveglia per la mythbox.
*/30 * * * * user echo 'wakeonlan macaddress_della_mythbox' | at `cat /data/orario_di_sveglia`
Prestate attenzione che il comando "cat" è compreso tra "backquotes" (ovvero: esegue il comando
all'interno e lo passa ad "at").
Magicamente, alle 17:18 toshin dovrà lanciare un wakeonlan sulla rete per svegliare la vostra mythbox...
To_Do e Limiti:
-
il file /data/orario_di_sveglia con questa configurazione non viene mai
cancellato. Quindi, ogni 30 minuti toshin andrà a schedulare la sveglia
con "at" anche se l'orario è relativo a 10 giorni prima. Non è un
grosso problema ("at", non essendo "DOC" di Ritorno al Futuro, si
rifiutà di mettere in queue comandi nel passato) ma è anche facilmente
risolvibile.
E' sufficiente infatti prevedere, come comando da far
eseguire per svegliare la mythbox, il lancio di wakeonlan e poi la
cancellazione del file /data/orario_di_sveglia
- Dopo aver
modificato il comando di sveglia, cancellando il vecchio file
utilizzato per l'orario, si potrebbe anche cambiare la riga in crontab
per verificare prima se c'è un file con l'orario nuovo e poi schedulare
la sveglia. Anche questo è molto semplice.
ENGLISH VERSION BELOW!
Hi! I noticed that many non-italian visitors come here from google or linuxpedia or other sources. My blog, unfortunately, is italian only since my english is really poor.
I'm trying to translate the most readed post of my blog, mostly mythtv-freevo related.
So...
TOPIC: how to set up a linux pc in order to wake up a mythtv backend... without missing any recording! :)
The guide is mythtv-specific but also applicable to many other use (mostly, simply wake up a machine at specific time).
A WakeOnLAN Slave and a MythTV Box
(aka using a slave to wakeup your backend via wakeonlan)(aka Setting up a WakeOnLAN box to act as MythTV Concierge, the easy way)
In this guide I'll describe the steps that I used to solve one of biggest problem of my mythbox (an Asus Digimatrix, DM): the cpu is a Pentium 4 and it need a lot of juice...even if idle.
DM, in my case, is a mythtv-dedicated box and work as BE/FE: keep it on, even if no recording is coming, is insane.
So I tried to find a way to keep it off and wake it up when I choose so or when a recording is coming.
There is many way to do it (and google can help you to find these); the often-used are, IMHO:
1. use the already present features of mythtv (and in the new tool, mythwelcome) for autoshutdown and autowakeup.
2. use another system (powered 24/7.. you like torrent, don't you?) to wakeup the mythbox.
Let it start with the 1.
Mainly, mythtv let specify some parameters for autoshutdown and autowakeup (find these in mythtv-setup, section Shutdown/Wakeup options)
[Please see the picture above]
The most important are:
- idle timeout: how many seconds of inactivity (no frontend attached) have to wait the mythbox before launching the "server halt command" (see below)
- max wait for recording: how many minutes before the coming recording ihinibites the shutdown (better: it's 06:52PM and the mythbox is idle; it should launch the server halt command but the mythbackend know that in 8 minutes comes a recording to manage. If you set this parameter to a value bigger than 8 (minutes), the mythbox didnt launch the server halt command
- Startup before rec: how many seconds before the scheduled recording for the wakeup. Better: if your mythbox need 50 seconds to boot, you can set a value of 60 seconds being sure (pay attention to the fsck at boot) that the mythbox will be ready to recording at the scheduled time.
- Wakeup time format: format used to set hour / date (if you've here the default setting, don't touch it for now! continue to read and you'll understand! ;)
- Set
wakeuptime command: command used to set the wakeup time. this command is launched right beforse the "server hal command" (see below) and use the hour / date format specified in the previous form (using $time)
- Server halt command: command used to halt the system (or to call suspend on ram, on disk, etc)
- Pre shutdown check-command: command used to check if the system is "really" idle (e.g., if you didnt specify anything in this form, mythtv can interrupt a transcode process launched from mythtransode because mythtv consider the backend idle if 1. there is no frontend attached and 2. there is nothing to record and it didnt consider other processes)
This options let you customize every setting needed for the autoshutdown and autowakeup, but they are mostly specific for the ones that want to manage only a system.
In fact, the tools used for this kind of operation are:
- NVRam Wakeup
- ACPI Wakeup
NVRam Wakeup is, if I'm not wrong, the first one developed and it strictly depends from the hardware used (not every motherboard is supported); moreover, according to many web pages I've read, is potentially dangerous (it use non-volatile-memory on the MB): if you do somehitng wrong, you can trash your motherboard.
ACPI Wakeup is (mostly) harmless and it works with the not-so-old motherboard. Is deadly simple to use, but it can be limited from BIOS limitations.
I'm not going deep in that topic (there is maaaaaaany pages on the web that explain that better than me; just google for "/proc/acpi/alarm" or "/proc/acpi/wakeup".
Of course the motherboard of my mythbox has some limitations (you cant specify a date for the wakeup but only the hour and minutes).
So I cant use ACPI Wakeup for my needs and I've looked for something else.
This is the way I found (thanks also for the link reported below)
- my mythbox manage only the section related to autoshutdown, but right before the shutdown it let the 24/7-torrent-machine know when it should wake up.
- tje 24/7-torrent-machine (toshin, an old toshiba laptop), using a cron script, wakeup the mythbox at the scheduled time
This is what you need:
- a mythbox (DM) supporting WakeOnLAN (do some tests before)
- a 24/7 system (toshin) to wake up DM
Then, on toshin you need:
- "cron" (used to check if the DM comunicated new time to wakeup...brrr poor english)
- "at" (used to schedule the wakeup)
- "wakeonlan" (used to wake up)
- an NFS partition, available to DM, where the DM create the file in them is stored the new scheduled wakeup time (there are few bytes, don't worry). You can create that file also with an ssh(/scp) command, ftp or what you decide.
Let us going deep in the "at" command, the most important tool in this guide.
An example:
We want to lacunch a command ('tv_grab_it > epg.xml') at a specified date and time (05 december 2006 11.21).
The command we've to use is:
$ echo 'tv_grab_it > epg.xml' | at 11:21 12/05/2006
The most important section here is how (your distibution specific) "at" accept the date / time format.
Here comes the boring part: you've to do some test until you find the right format and write it down somewhere! For example, you can try launching a command like 'touch test_file' and checking if everything worked checking the creation date/time of that file.
The format, used in this guide (and of course, on my system), work with debian unstable, but - if you modified the specific environment variable - it can not suite your needs.
So, when you found that format, write it down (paper, please!).
Once done, if you do test for the 'at' format and
for the wake up using wakeonlan, we're really done!
Go on your mythbox and launch mythtv-setup from a terminal (see below why) and go to "shutdown/wakeup options".
Set the timeout parametes as you wish and then see the "difficult ones".
Launching mythtv-setup from a terminal, give you an output like
mythtv@digimatrix:/data/psp/tmp$ 2006-12-04 20:43:39.406 Using runtime
prefix = /usr
2006-12-04 20:43:39.572 New DB connection, total: 1
2006-12-04 20:43:39.615 Connected to database 'mythconverg' at host: localhost
2006-12-04 20:43:39.619 Total desktop dim: 1440x900, with 1 screen[s].
2006-12-04 20:43:39.636 Using screen 0, 1440x900 at 0,0
2006-12-04 20:43:39.646 Current Schema Version: 1158
2006-12-04 20:43:39.681 Newest Schema Version : 1123
2006-12-04 20:43:39.682 New DB connection, total: 2
2006-12-04 20:43:39.684 Connected to database 'mythconverg' at host: localhost
As you can notice, at the start every row these is date/time specification.
From the output above you can see that the format is used by mythtv is:
YYYYear-MMonth-DDay HHour:MMinutes:SSeconds
Pay attention to that and check what you've in the "wakeup time format" form. From what I noticed, you've to find exactly the same format (using the codes now) of the terminal output:
yyyy/MM/dd hh:mm:ss
Doing so you can understand which letters-codes (case sensitive!) use mythtv to specify the month, years, minutes, ...
Now you've only to use these codes to specify the date/time format that your 'at' accept.
In the example used in this guide the format is:
hh:mm MM/dd/yyyy
So, write it in the "wakeup time format".
In the "set wakeuptime command" insert:
echo $time > /path_for_your_share/WakeupTime
The path can be an NFS share (like me) or a directory available to the 2 systems.
What does it mean?
The mythbox, right before the autoshutdown, will create a file in them is wrote the date and time for the wakeup; that file will be in a partition of the other system, toshin, available via NFS.
Then (on mythtv-setup)
- server halt command: you decide. for me: sudo halt.
- pre shutdown check command: for the initial test, 'exit 0'. that means that the mythbox will not do any addition test in order to check if it is really idle (except the default one, frontend check and recording check)
Now, write somewhere the MACAddress (use ifconfig) of your mythbox and we're done with our mythbox.
For our test, schedule a recording that start in 1 hour (05 december 2006 17:20) and close every frontend attached to your backend.
After the "idle timeout" specified with mythtv-setup, the mythbox will power off.
Go on the other system (tostin)
Right before the shutdown, the mythbox should have created a file(in the NFS dir of toshin) where is stored the desired date & time for the wake up. In our example the is /data/WakeupTime
$cat /data/WakeupTime
17:18 12/05/2006
Gooood. the mythbox works perfectly.
Now we've to create a cron script that every 30 minutes (for example) set for "at" the desired date and time when wakeup the mythbox.
Deadly simple script
*/30 * * * * user echo 'wakeonlan mythbox macaddress' | at `cat /data/WakeupTime`
Pay attention: the "cat" command is within backquotes (= execute the "cat" command and give it to "at").
Automagically toshin at 17:18 should lanch the wakeonlan command to wakeup the
mythbox.
TO_DO and Actual Limits:
- the /data/WakeupTime file is not deleted by toshin in this cron script. So every 30 minutes toshin tries to schedule the same time for the wakeup. There is no problem with that but you can just add a "&& rm /data/WakeupTime" at the end of our cron script and you're done!
- pretty simple but yet to do: the cron script on toshin should also check if a /data/WakeupTime file is present and then, if present, schedule the time.
Link:
- in primis quello, segnalatomi da ale123, che ha ispirato il tutto. Nel suo caso, utilizza un sistema più complesso per comunicare data e ora, ma, sinceramente, non ne vedo il motivo... :) [ this one , the one that began everything :). In this case he use a, IMHO, more complex system to comunicate the date and time for the wakeup and, honestly, I don't understand why I've to do so...]
