Remaster Knoppix without Remastering

Kyle Rankin

Issue #170, June 2008

Why go through complicated steps to remaster Knoppix, when there are a number of methods to get the most of the functionality without the fuss?

If you have used Knoppix for any extended period of time, chances are you eventually thought about customizing it. You might have simply wanted to make cosmetic changes with a new background and a different theme, or you might have wanted to add programs, change what happens at startup, or make any number of more-intensive changes. Well, you aren't alone. There are more than a hundred different Knoppix-based live CDs in existence. Some have changed only a few tools, and others have performed a complete overhaul of the programs, the themes and even the file structure.

Traditional Remastering

If you had decided to change Knoppix, you quickly would become acquainted with some of the guides both on-line and in print about Knoppix remastering. At that point, some of you would have read the complicated set of steps and decided it just wasn't worth it. Knoppix fits all of its software within a compressed loopback filesystem on the disc. To remaster Knoppix, you must make a copy of the uncompressed contents of that filesystem, use chroot to access the copy as though it were the root filesystem, make any changes you want to make, and then re-create the new cloop filesystem and ISO image with a few gigantic commands on the command line.

Of course, I oversimplified and excluded a few steps there. Besides some of the more technical steps, I also left out that minor bug you undoubtedly introduced but didn't notice until you booted. That means you get to go through the remastering process (especially the time-consuming steps of creating a new cloop filesystem) all over again. I also left out the step where you notice that even though you made minor changes, your ISO inexplicably is much larger than it was before and won't fit on a CD, so back to the remastering process you go to free up more space.

The fact is, traditional Knoppix remastering is a time-consuming, error-prone, complex process full of trial and error, but there is a better way. There are a few different methods you can use to tweak the default Knoppix disc without using the remastering process. I like to refer to these methods as “remastering Knoppix without remastering”, because the default Knoppix cloop filesystem stays the same throughout the entire process.

I first became interested in low-impact Knoppix tweaks when I had to create a custom CD to include with my book, Knoppix Hacks. I didn't want to do much—just change the default background and add a few links to the desktop. Although I could have remastered the CD, I was concerned about introducing bugs, even with minor changes. Imagine if I had discovered a bug after the CD was printed and shipped around the country with the book! I figured if I could tweak Knoppix but leave the compressed image alone, I would reduce the risk considerably.

Ever since my first attempt to remaster Knoppix without remastering, I've used these methods so much that I actually prefer them and find them superior to remastering for most people's needs. Not only is the process much quicker and less complex, it's also simpler to migrate your changes once the next version of Knoppix is released.

Save Configuration

The first method you can use to change Knoppix involves a classic Knoppix tool called saveconfig. This tool creates and lets you choose a number of different files and settings from your user's configuration files, all of the files on the desktop, and network, graphics and other settings. saveconfig then creates a tarball called configs.tbz out of the files and stores it on a USB key, hard drive or other writeable media of your choice along with a custom knoppix.sh script.

To run saveconfig, click K→Knoppix→Configure→Save Knoppix Configuration or type saveconfig in a terminal. Then, you can choose which settings to save and to which hard drive to save them. The next time you boot your computer, type myconfig=/dev/sda1/ to point Knoppix to a particular saved config, or type myconfig=scan, and Knoppix will scan all available drives for you. At the end of the boot process, Knoppix will run the knoppix.sh script on the drive, which will extract the tarball and restore your settings.

For minor, mostly cosmetic changes, the standard saveconfig script might serve all your needs. Simply make your changes, and then tell saveconfig to back up your personal configuration. This is useful, but you might want to back up only a few files, or you might want to add files that saveconfig doesn't save by default. To do that, you can tweak either the configs.tbz or knoppix.sh file. First, run saveconfig to create a configs.tbz and knoppix.sh file. Then, uncompress the configs.tbz file:

$ bunzip2 configs.tbz

Now, use tar to add new files to the tarball:

$ tar -rpPf configs.tar /path/to/file

You also can use tar to delete files:

tar --delete -pPf configs.tar /path/to/file

Once you are finished with your changes, you can recompress the tarball:

$ BZIP2=-9 bzip2 configs.tar
$ mv configs.tar.bz2 configs.tbz

Another way to tweak saveconfig (or even bypass it altogether) is to realize that when you tell Knoppix to use a particular saved configuration, all it really does is execute the knoppix.sh file, which is a standard shell script. That shell script contains the tar commands to extract the file. This means anything you put in the knoppix.sh file will be executed at the end of the boot process. What's more, if you create a new Knoppix disc and place the knoppix.sh in the KNOPPIX/ directory on the disc, it will be executed automatically every time Knoppix boots. So, if there's an extra program you'd like Knoppix to have, just track down the .deb file for it and any of its dependencies, and add them to the disc. Then, in the knoppix.sh script you can put:

dpkg -i /cdrom/*.deb

Knoppix will install all of those programs at the end of the boot process. If you want to run any other commands or start any services, you also could put them in this script.

Example: Webcam

Here's an example of a tweaked knoppix.sh file that I made to turn Knoppix into an instant Webcam server. I first created a webcam directory on the CD-ROM and put a basic HTML file there that pointed to an image at /var/www/webcam.jpg. Then, I created a knoppix.sh script and put it in the KNOPPIX/ directory on the CD. Here is the script:

#!/bin/sh

echo Configuring Apache
cp /cdrom/webcam/index.html /var/www/index.html
/etc/init.d/apache start
echo Configuring Cron
echo '* * * * *  root gqcam -d /var/www/webcam.jpg > /dev/null' >>
/etc/crontab
/etc/init.d/cron start

As you can see, there isn't much to this script. I copy the HTML file to the default Apache document root, start Apache, and then add a job to the global crontab and start cron. The cron command will run gqcam every minute, which will capture an image and store it in /var/www/webcam.jpg. Now, I can just burn the CD and boot it on any machine with a Webcam and Knoppix will take care of the rest.

Persistent Disk Image

In addition to the saveconfig script, Knoppix provides an even more advanced script that creates a persistent disk. The main problem with a live CD is that all of your changes are erased whenever you reboot. Traditionally, the persistence script created a loopback filesystem on a USB key or hard drive of a specified size and then copied your entire home directory to it. On the next boot, if you selected it with a cheat code, Knoppix would then mount the filesystem and point your entire home directory to it. That meant any changes you made to your home directory were kept through all of your reboots.

The main limitation to that script (as with legacy Knoppix discs) was that you could write only to certain directories. Beginning with Knoppix 3.8, Knoppix added UnionFS (now replaced with AuFS). This tool essentially allows you to merge filesystems on top of each other, even if one is read-only and the other is read-write. If you wrote to the filesystem, UnionFS/AuFS transparently took care of putting the writes on the correct filesystem. With Knoppix, this now meant the ramdisk that was traditionally used for /home and /etc could now be merged on top of the entire filesystem. You now could essentially write anywhere you wanted, install programs using the standard Debian package management, and basically treat the system like any other installed Linux distribution, limited only by your ramdisk size.

With the addition of AuFS, Knoppix changed its persistence script. Now when you click K→Knoppix→Configuration→Create a persistent Knoppix disk image, Knoppix creates a complete copy of the entire ramdisk. This is a copy of all of the changes you have made to the system since you have booted. Within the script, you can choose the maximum size of the filesystem, where to store the image, and even whether to use encryption. The next time you boot, if the USB or local hard drive is present, Knoppix automatically detects it and prompts you with a few options:

  • Home: use only your persistent home directory.

  • System: replace the entire /ramdisk mountpoint with the persistent disk image.

  • Overwrite: off by default, but it will replace the /etc directory on your persistent image if you also enable the System option. You might want to do this if your system-wide graphics or network settings from a previous machine interfere with your current one.

  • Init: start any persistent init scripts.

If you don't select any of these options, Knoppix will boot normally. It's also possible that you might have multiple knoppix.img files on different drives (or multiple files on the same drive in different directories). This actually is a good way to create custom Knoppix settings that are stored in different directories on a USB key. Say I have a security-focused persistent image in the security directory on my USB key (which Knoppix detects as /dev/sda1), I could then boot Knoppix with the home=/dev/sda1/security/knoppix.img cheat code.

Tweak Boot Settings

The saveconfig and knoppix.sh tweaks provide a lot of functionality, but their main downside is that they execute at the end of the boot process. Sometimes you want to change Knoppix settings sooner than that—whether it's the default screen you see at boot time or Knoppix's default cheat codes. These options and more are stored in the boot/isolinux/ directory on the disc and can be changed without much effort. Below I cover some of the more important files and what you can change with them.

Boot messages: the boot.msg, f2 and f3 files are text files that define what shows up at the boot prompt, when you press F2 and F3, respectively. If you want to add special help or even change the text completely, you can do so in these files.

isolinux.cfg: this is one of the most useful files in this directory, as it defines all the different kernels that can be loaded, along with their default boot options. For instance, here is the section of the file that defines the default Knoppix settings and cheat codes if you let the boot prompt time out or just press Enter:

LABEL knoppix
KERNEL linux
APPEND ramdisk_size=100000 init=/etc/init lang=us apm=power-off vga=791
initrd=m
inirt.gz nomce loglevel=0 quiet BOOT_IMAGE=knoppix

For instance, you can see here that the default language is English, but if you wanted to change it to Spanish, you would change lang=us to lang=es. If you scroll down farther in the file, you can not only see other Knoppix types you can boot, but also listings for memtest and dos.

Tweak initrd

Along with the other boot files, under boot/isolinux/ is the default Knoppix initrd file called minirt.gz. This file is the initial root image that Knoppix mounts. Within the image are essential files for the boot process, including the init executable, but the file of most interest to Knoppix hackers is the linuxrc file, which acts as Knoppix's general startup script. First, make a copy of the minirt.gz file (in my example, I assume it was mounted under /cdrom), uncompress it and then mount the filesystem:

# cp /cdrom/boot/isolinux/minirt.gz .
# gunzip minirt.gz
# mkdir temp
# mount -t ext2 -o loop minirt ./temp

Now, if you look in the top-level directory of that mounted filesystem, you will see the linuxrc file. A lot of the script defines default settings, such as which filesystems are built in to Knoppix and the process Knoppix uses to mount them, but you also can see where Knoppix defines system-wide defaults and also allows you to override them. For instance, the following lines define the default Knoppix directory and cloop file and the ability to overwrite them via a cheat code:

KNOPPIX_DIR="KNOPPIX"
KNOPPIX_NAME="KNOPPIX"
case "$CMDLINE" in *knoppix_dir=*) KNOPPIX_DIR="$knoppix_dir"; ;; esac
case "$CMDLINE" in *knoppix_name=*) KNOPPIX_NAME="$knoppix_name"; ;; esac

If you want to add some extra functions to the boot process, read through the script to identify where would be best. For instance, at some points of the script, the KNOPPIX cloop filesystem isn't loaded yet, nor are many common modules. If you aren't sure where to add your changes, just add them to the end of the script before the comment #Give control to the init process. At that point, Knoppix should have major modules and filesystems loaded and mounted.

Once you are finished with your tweaks, unmount the minirt filesystem and recompress it. Then, you can overwrite the default version with your custom edition:

umount temp
gzip -9 minirt

These are only a few examples of how to remaster Knoppix without remastering. One of the great things about these methods is that they are typically easy to try, so if you make a mistake, you can fix it quickly. Plus, most of these methods lend themselves well to migration from one Knoppix disc to the next for when the next version of Knoppix is released. Finally, because most of your custom tweaks can be self-contained, if you want to share them with friends, you simply can share your knoppix.sh and configs.tbz files, for instance, instead of an entire ISO image.

Kyle Rankin is a Senior Systems Administrator in the San Francisco Bay Area and the author of a number of books, including Knoppix Hacks and Ubuntu Hacks for O'Reilly Media. He is currently the president of the North Bay Linux Users' Group.