HOWTO: Create a KNOPPIX CD with your own startup script

March 2005


Send errors, corrections, enhancements

KNOPPIX is a Linux distribution which fits on a single boot CD and brings everything you need for system maintenance (and much more, including a complete KDE desktop). It does not need to be installed on the hard disk, but runs from the CD.

You can remaster the CD, which means you have to install it to a hard disk, change things, create a new ISO filesystem - a long process. The worst of this is that you need a hard disk partition with a few GB of space.

This HOWTO explains an easier way to customize KNOPPIX using the knoppix.sh. This script is executed by KNOPPIX at the end of the boot process (think autoexec.bat). The example I use here is a comfortable bootable CD which backups the whole system without any manual input. Useful for a system which does not have Linux on the hard drive at all, for example; and you can do quite a few other nice things too. :-)

What do you get?

After the changes explained here, you get a KNOPPIX CD, which, when booted, displays a text menu with any actions you want it to show. You can add any kind of script here, which can in turn make use of all parts of KNOPPIX (network, NTFS access, CD burning, etc. etc.). This is in no way restricted to backup tasks, of course.

Note: KNOPPIX itself can make use of that knoppix.sh to store settings between boots. Those functions of knoppix.sh are not impeded by this approach, and can be used along with it.

Dialog example

What do you need to do?

You need to do nothing except download the KNOPPIX CD iso image, add a few files to it, and burn it. This in turn does not need any tools except KNOPPIX itself, i.e. if you are able to get KNOPPIX to run, you're set. You do not need any hard disk at all. You should be slightly comfortable with Linux and editing shell scripts. The steps are:

The KNOPPIX boot process and knoppix.sh

KNOPPIX boots like this:

Adding a knoppix.sh on a floppy disk

The first step to play with your own knoppix.sh is to create it in an easily accessible place - in my case, the floppy disk. When booting KNOPPIX, specify the following at the boot prompt:

KNOPPIX boot prompt
knoppix floppyconfig

The floppy should have the knoppix.sh in the top directory.

Using init to run your script

The knoppix.sh can do everything any other script does, but at that moment, the system is still in single-user mode (or rather, runmode S) and not completely booted, i.e., there are no shells listening on the console yet, and X is not running.

One approach is to use init to start anything you want, which effectively defers your script until the system is completely booted. As the file /etc/inittab is living in the KNOPPIX ramdisk, and is not directly accessible on the CD, we can - and must - change it at boot time to override KNOPPIX defaults. So here's the knoppix.sh that does this, in all its beauty:


# Make run-mode 4 default (no X)
echo ':%s/^id.*initdefault.*$/id:4:initdefault:/
'| ex /etc/inittab

# Put shell.sh on tty1
echo ':%s/^1.*tty1.*$/1:12345:respawn:\/bin\/bash -login -c \/mnt\/floppy\/shell.sh >\/dev\/tty1 2>\&1 <\/dev\/tty1/
'| ex /etc/inittab

# Re-read inittab
telinit q

(You can download knoppix.sh.) So, what does it do? It uses ex (which is vim in KNOPPIX, started in ex mode) to edit /etc/inittab. The first part replaces the default runmode of 5 (with X) with the default runmode of 4 (without X):

The new default run mode



Then it replaces the shell which is normally running on tty1 (the first "window", accessed with Alt-F1 or Ctrl-Alt-F1) with my own script /mnt/floppy/shell.sh:

The new process started on tty1
1:12345:respawn:/bin/bash -login >/dev/tty1 2>&1 </dev/tty1


1:12345:respawn:/bin/bash -login -c /mnt/floppy/shell.sh >/dev/tty1 2>&1 </dev/tty1

The final telinit q simply tells init to re-read /etc/inittab.

Luckily, at the time knoppix.sh is run, the system still in runmode S, which means that the bash that would run on tty1 in a normal KNOPPIX is not started yet. So we don't have to bother killing it, it will simply never be started.

A shell.sh with a little menu

knoppix.sh has told init to run shell.sh on the first terminal, and not to start X, so after the KNOPPIX boot process finished, shell.sh is the thing displayed to the user.

Here is an example shell.sh which displays a small dialog:



CHOICE=`dialog --stdout --no-cancel --menu Pick... 0 0 0 \
"$ROOT/palm2cd.sh" "Palm Backup" \
"$ROOT/re-burn.sh" "Re-Burn CD" \
"telinit 5" "X" \
"halt" "Shutdown" \
"reboot" "Reboot"


This really trivial script displays the following dialog:

shell.sh example
      ¦ Pick...                          ¦
      ¦ +------------------------------+ ¦
      ¦ ¦   /palm2cd.sh  Palm Backup   ¦ ¦
      ¦ ¦   /re-burn.sh  Re-Burn CD    ¦ ¦
      ¦ ¦   telinit 5    X             ¦ ¦
      ¦ ¦   halt         Shutdown      ¦ ¦
      ¦ ¦   reboot       Reboot        ¦ ¦
      ¦ +------------------------------+ ¦
      ¦             <  OK  >             ¦

After the user picks a choice, it is simply executed. In this case, palm2cd.sh transfers all files from my palm pilut, gathers the data of my Windows Palm Desktop installation, as well as a pre-installed Windows Palm Emulator executable, and burns all of it on a CD. re-burn.sh burns the current KNOPPIX CD again (more on this later). telinit 5 fires up X, and halt/reboot stop or reboot the system.

When shell.sh exits, init immediately restarts it. At this point, Alt-F2, F3 and F4 are ready and contain the usual shells, logged in as root.

Transfering the floppy to the KNOPPIX cd and burning a new KNOPPIX cd from inside KNOPPIX

Now we want to get rid of the floppy, and put the shell files on a KNOPPIX boot CD. We use this approach:

To do this exactly as shown here, you need a CD drive and a separate CD writer. It could be done with just a single CD drive by changing a few easy steps (and by unmounting the KNOPPIX CD), but I did not try that yet.

The script re-burn.sh, which was mentioned above, is displayed here. We start with some settings. ROOT is the place where our new CD directory will be created. This does not need to have 700 MB of free space, but only enough free space for the new files you want to add plus the files you want to change. CD is the mount point of your source CD, SCRIPTS contains a few file names in the /KNOPPIX/ directory (which are going to be edited), and WRITER is the device of the writer (you can find out your values with cdrecord -scanbus).

re-burn.sh, part 1...

SCRIPTS="knoppix.sh shell.sh re-burn.sh palm2cd.sh"

mkdir $ROOT
if [ -e $ROOT/boot ] ; then
  echo $ROOT/boot exists, remove it first.
  exit 1

Now we ask the user to insert a new media into the writer, and make sure it's there. The additional code in here is in preparation for supporting multi-session CDs later, but it does not do anything now.

re-burn.sh, continued...
echo Insert a blank CDR or the last KNOPPIX CDR and press Enter.

MSINFO=`cdrecord dev=$WRITER -msinfo 2>/dev/null`

if [ "$MSINFO" == "" ]  ; then
  echo Seems to be a new CD...
  touch $ROOT/`date +%Y%m%d`.created
  echo Multi-session CD detected, this is not supported yet.
  exit 1
  echo Continuing multi-session CD...
  touch $ROOT/`date +%Y%m%d`.updated

Then it's time to create the directory which will be the new CD. We do this by first creating the stuff we want to change, and then linking in everything else from the original KNOPPIX CD.

re-burn.sh, continued...
echo Preparing $ROOT from $CD...

mkdir -p $ROOT/boot/isolinux
mkdir -p $ROOT/KNOPPIX
ln -s $CD/* $ROOT/ 2>/dev/null

cp $CD/boot/isolinux/isolinux.bin $ROOT/boot/isolinux/
chmod 644 $ROOT/boot/isolinux/isolinux.bin
ln -s $CD/boot/isolinux/* $ROOT/boot/isolinux/ 2>/dev/null

It is necessary to put isolinux.bin on the ramdisk instead of linking it in from the original CD, because it will be modified later. The next step is to copy some more files from the original CD instead of linking them; these are the files we probably want to modify (our own knoppix.sh, and any other payload you want to have on the CD). If you are starting with a standard KNOPPIX CD, these files will not be there yet, so you have to copy them from somewhere else (i.e., from the floppy you started off with):

If you had the files on the floppy, then make sure to replace /mnt/floppy by /cdrom in all scripts.

re-burn.sh, continued...
for i in $SCRIPTS ; do
ln -s $CD/KNOPPIX/* $ROOT/KNOPPIX/ 2>/dev/null

echo Then press ENTER to burn or Ctrl-C to stop.

After a keypress by the user, the new ISO image is created and burned (without storing it anywhere). This step would need to be adapted if you have only a single CD drive.

re-burn.sh, continued...
echo Creating ISO file system, burning CD...

cd $ROOT
mkisofs -pad -l -r -J -quiet -V "KNOPPIX" -no-emul-boot -boot-load-size 4 -boot-info-table \
-b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -hide-rr-moved -f $MULTI . | \
cdrecord -tao dev=$WRITER -waiti -data fs=64m -eject driveropts=burnfree -

echo Label the CD: '"Custom KNOPPIX ' `date +%d.%m.%Y` '"'

I got buffer underuns on a very well equiped system with a 40X CD reader and a 24X Plextor writer (on the same ATAPI cable), but fs=64m driveropts=burnfree got rid of them.


So that's it so far. I use this solution for simple backup operations of Windows workstations without network backup (insert my custom KNOPPIX CD, switch on PC, put CDR medium into drive, walk away). Works splendid and much easier/safer than any other backup variant I tried which involved starting anything manually. Also less expensive than commercial programs that create backup HDD images for you. :-)