Sunday, July 1, 2007

Image selector

Basic idea

I was thinking, with squashfs-lzma, I can have several rootfs on my card and with my ramdisk, I can eassily switch between them by simply editing configuration file. But it can be even more simple. What about bootup menu which let you decide, what rootfs image you want to boot? Well, it can be implemented in PalmOS so it just modify your configuration file, but why? I like to have as simple bootloader as it can be. I prefer solving this issue in Linux. So we can write C program, compile it and use it. But it's not cool ;-) Everybody can do that and you have to take care about libraries and such. So why don't write it in shell? :-D Yes, this sounds cool. Couse Palm doesn't have keyboard. We've got only few keys - some of them are FX and then we've got Dpad. So I've decided to write rootfs selector, which can be controlled by single key :-D I'll use just enter. Main idea is to wait X seconds (where X is user definable) and then run current image. By using enter key, you can switch between several rootfs.

Implementation

How to implement something like this in shell? We need several processes and we need them to communicate with each other somehow. We need one process to wait for enter key and another process to wait and count seconds. So I divided it into 3 different files. First of them is main. It looks simple:


#!/bin/sh

mkdir /tmp/chooser.$$
sh ./dialog 5 /tmp/chooser.$$ 2> /dev/null
. /tmp/chooser.$$/action
rm -r /tmp/chooser

echo $NUM

It's just a simple example. It just runs dialog with 5 seconds of timeout and after that it read's up, what it should do and do it ;-) Now let's take a look at dialog script.


#!/bin/sh

[ "$1" ] && TIME=$1
[ "$2" ] && WDIR=$2
[ "$NUM" ] || NUM=1

echo NUM=$NUM > $WDIR/action
sh ./timedkill $TIME $WDIR $$ &
read tmp
echo switched
NUM=`expr $NUM + 1`
kill `cat $WDIR/killing`
. ./dialog

Here comes my main idea. We call somebody and tell him, what is our PID. That somebody waits X seconds and then kill us. But if user press enter, we will be faster and kill him first. All the time, we are storing current selection, so when we died, our parent would know, how far we get and can run selected operation. Last script is easy, timedkill just wait and then kill ;-)


#!/bin/sh

echo $$ > $2/killing
sleep $1
kill $3
echo action

Conclusion

You can say, I'm mad. You can say that I totaly lost my mind. But I think that in cases like this, the power of shell shows itself. Isn't it really pretty? We can do nearly everything in shell. That's why I like UNIX. Shell RULEZ!

PS: I'll add some graphics and configuration files to it and I'll include it in my future initramdisk ;-) Currently tested only on real computer, but I'll try it on Palm too ;-)

1 comment:

pfalcon said...

Nope, just saying it's all old. There're for example RamdiskRescue and Altboot written in shell to provide kind of UI for PDA booting and some other tasks. The latter is pretty much like what you're going to write...