Article may2007.tar

Create a Customized Live Linux CD or Bootable USB Thumb Drive Using Knoppix

John Ferrell

Earlier this year, I was asked to find a bootable CD that would allow PCs in a special purpose lab, many of which had bad hard drives, to access our Citrix environment. Basically, we wanted to turn these PCs into thin clients to extend their life. I had been experimenting with Knoppix and decided it was time to try customizing my own Knoppix CD for use in the lab. Ultimately, the CD proved so useful that I created a bootable USB thumb drive from it for my own use -- mainly accessing Citrix and Windows Terminal Servers. In this article, I will provide step-by-step instructions on how to create a customized Knoppix CD and how to use that customized CD to create a bootable USB thumb drive.

Knoppix Overview

Knoppix is probably one of the most popular Live Linux CDs at this time and rightly so; it works on all sorts of hardware, includes everything you need to get going (in most cases), and is freely distributed. Of course, because of licensing restrictions, some applications (such as the Citrix ICA client) cannot be included with the default Knoppix distribution. However, through a process known as remastering, you can create your own custom Knoppix CD that includes the Citrix ICA client and any other applications you may want.

Knoppix is based on Debian GNU/Linux. Debian's package manager, APT (Advanced Packaging Tool), makes adding and removing packages for your custom Knoppix CD a simple task. Software that is not pre-packaged for Debian, such as the Citrix ICA client, can generally be installed with relative ease. The following instructions are based on the "Knoppix Remastering How-to" and the "Bootable USB Key" articles found on the Knoppix wiki. I tried to streamline the process and have added information for installing the Citrix ICA client and customizing the user preferences.

I have also created a few scripts to help automate the remastering process: one to set up the remastering directories and copy the required files (Listing 1), one to remove packages (Listing 2), and a third to create the compressed Knoppix file system and ISO image (Listing 3). In order to get a better understanding of the remastering process, I recommend manually running through the process before using the scripts. I have tested these remastering instructions using Knoppix 3.8 through 5.0.1. However, I have only created a bootable thumb drive using a customized Knoppix 5.0.1 CD. To complete the process, you will need a machine with approximately 4GB of free space on an EXT2 or EXT3 partition and a connection to the Internet. I do all of my remastering using a VMware virtual machine. I have over written the wrong partition a few times, so being able to "undo" the changes has been helpful. I have a virtual machine with a minimal Red Hat installation that provides the EXT3 partition. Using VMware also mak

Yes it easy to test the customized Knoppix ISOs without burning the ISO to a CD. All listing files can be found on the Sys Admin Web site at: http://www.sysadminmag.com.

Initial Setup

1. Boot from the Knoppix Live CD.

2. Open a root shell from the K menu:

K > KNOPPIX > Root Shell
3. Verify the network configuration. By default, Knoppix uses DHCP to configure the network interface.

# ifconfig
# ping www.google.com
4. Use fdisk to list the hard disk partitions. You will need to select a partition to be used during the remastering process.

# fdisk -l

Disk /dev/sda: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot  Start   End   Blocks   Id  System
/dev/sda1   *       1    13   104391   83  Linux
/dev/sda2          14   111   787185   82  Linux swap / Solaris
/dev/sda3         112  1044  7494322+  83  Linux
5. Mount the partition that will be used during the remastering process. For this example I will use the partition /dev/sda3:

# mount -rw /dev/sda3 /mnt/sda3
6. Create a work directory for Knoppix remastering process:

# mkdir /mnt/sda3/knoppix.work
7. Create a swapfile. (A swapfile is required when compressing the Knoppix file system, if the PC has less than a gigabyte of RAM.)

# cd /mnt/sda3/knoppix.work
# dd if=/dev/zero of=swapfile bs=1M count=1024
# mkswap swapfile
# swapon swapfile
8. Create a source and master directory within the work directory. The source directory will be used to modify the Knoppix distribution files. The master directory is where the Knoppix compressed file system image will be written to, which will ultimately be used to create the bootable ISO image:

# mkdir -p /mnt/sda3/knoppix.work/source/KNOPPIX
# mkdir -p /mnt/sda3/knoppix.work/master/KNOPPIX
9. Copy the CD contents to the source directory:

# cp -Rpv /KNOPPIX/* /mnt/sda3/knoppix.work/source/KNOPPIX
10. Copy the Knoppix boot files to the master directory:

# cp -Rav /cdrom/boot /mnt/sda3/knoppix.work/master/boot
11. Copy the default HTML page. This is the Knoppix information page that is displayed after KDE is started:

# cp /cdrom/index.html /mnt/sda3/knoppix.work/master
12. Copy required files from /cdrom to the master directory. The following command copies everything but the KNOPPIX file (the compressed file system):

# cd /cdrom
# find . -size -10000k -type f -exec cp -pv --parents '{}' \
/mnt/sda3/knoppix.work/master/ \;
13. Copy /etc/dhcpc/resolv.conf to the source directory. (This is done to enable DNS resolution while working in the chrooted environment.)

# cp /etc/dhcpc/resolv.conf \
/mnt/sda3/knoppix.work/source/KNOPPIX/etc/dhcpc/resolv.conf
14. Change root to the source/KNOPPIX directory. This is the root of the file system that will be on the custom CD. All changes that are made in the chrooted environment will be written to the compressed KNOPPIX file.

# chroot /mnt/sda3/knoppix.work/source/KNOPPIX
Customizing the CD -- Working within the Chrooted Environment

1. If you are customizing a version of Knoppix prior to 5.0 you will need to mount the /proc file system. This is required for networking support. (The warning, "warning: can't open /etc/fstab: No such file or directory", is normal since /etc/fstab does not exist.)

# mount -t proc /proc proc
2. Test network connectivity:

# ping www.google.com
3. Update the package repository list:

# apt-get update
4. Remove various packages as needed. For each package you plan on adding, you will probably need to remove another package to free up some space. Take a look at Listing 4 for some ideas of packages you may want to remove. The following commands will manipulate the packages.

List the packages currently installed:

# dpkg-query -l
Send a list of installed packages to a file:

# dpkg-query -l | awk '{print $2}' > /tmp/packages.txt
Remove an individual package:

# apt-get remove --purge <PackageName>
Remove a list of packages:

# dpkg -P `cat /tmp/packages.txt`
5. After you have removed the packages, you will want to remove orphaned packages. These are packages that are no longer needed, because there are no other packages dependent on them.

To list orphaned packages:

# deborphan
To remove all orphaned packages:

# deborphan | xargs apt-get -y remove --purge
6. Install new packages.

Install libmotif3, which is needed for the Citrix ICA client, and verify that krdc and rdesktop (packages for accessing Windows Terminal Servers) are installed and up to date. If you are planning on making a bootable thumb drive from the CD, you will also need syslinux.

# apt-get install libmotif3 krdc rdesktop syslinux
Search for packages by keyword:

# apt-cache search <keyword>
Install individual package:

# apt-get install <PackageName>
7. Clean up apt's cache:

# apt-get clean
8. Install the Citrix ICA client.

Download the Citrix ICA client for Linux from www.citrix.com and place it in /tmp. I usually download it to my SFTP server, so I can access it easily from within the chrooted environment.

Change to /tmp. Working out of /tmp makes it easy to clean up unwanted files before creating the final image.

# cd /tmp
Extract Citrix ICA client:

# tar xvzf linuxx86.tar.gz
Run Citrix ICA client installation script. The defaults are fine, but be sure to select "yes" when prompted to integrate ICA client with the Web browser.

# ./setupwfc
9. (Optional) If you have SSL certificates for your Citrix servers, you will want to copy the certificates to /usr/lib/ICAClient/keystore/cacerts/. I found that I needed to change the certificate extensions from .cer to .crt before the ICA client would recognize them.

10. Install other non-standard applications.

11. Test the newly installed applications from within the chrooted environment.

Set the display variable to that of the local X server:

# export DISPLAY=localhost:0.0
Launch applications from the command line. For example, use the following command to launch Firefox. The "&" tells the system to run Firefox in the background. If you omit the "&", the terminal will be "locked" until Firefox is closed.

# firefox &
12. Once you have finished installing and testing the applications, exit the chrooted environment by pressing Ctrl + D. If you mounted /proc, be sure to unmount it before exiting the chrooted environment.

Customize the User Settings (Optional)

1. Configure the various user preferences to your liking. Here you can add/remove desktop icons, modify the K menu, customize Firefox's settings, printers, etc.

2. Save the user settings by running the following command. This will give you the option to save the user preferences to a floppy drive or the hard drive. This will create a file, configs.tbz, containing the user preferences and a script, knoppix.sh, which is used to restore the preferences at boot and can be customized to modify other system settings. Take a look at Listing 5, a custom knoppix.sh, for some examples.

# /usr/sbin/saveconfig
3. Copy configs.tbz and knoppix.sh to:

/mnt/sda3/knoppix.work/master/KNOPPIX/
4. If you have a desktop background you want to use, copy it to:

/mnt/sda3/knoppix.work/master/KNOPPIX/background.jpg.
5. To prevent the Konqueror Web browser from being launched at startup, remove default Knoppix HTML files:

# rm -fv /mnt/sda3/knoppix.work/master/KNOPPIX/index*.html
# rm -fv /mnt/sda3/knoppix.work/master/index*.html
Create the Compressed Knoppix File System and ISO Image

1. Clean up /tmp within the source directory and remove any other files that you do not want on the CD:

# rm -rfv /mnt/sda1/knoppix.work/source/KNOPPIX/tmp/*
2. Create the compressed file system image:

# mkisofs -R -U -V "KNOPPIX" -publisher "KNOPPIX" \
-hide-rr-moved -cache-inodes -no-bak \
-pad /mnt/sda3/knoppix.work/source/KNOPPIX \
| nice -5 /usr/bin/create_compressed_fs \
- 65536 > /mnt/sda3/knoppix.work/master/KNOPPIX/KNOPPIX
3. Update MD5 hashes, which are used for integrity checking:

# cd /mnt/sda3/knoppix.work/master
# find -type f -not -name md5sums -not -name boot.cat \
-not -name isolinux.bin \
-exec md5sum '{}' \; > KNOPPIX/md5sums
4. Create the ISO using the files in the master directory. (In this example, the ISO file is written to /mnt/sda3/knoppix.work/knoppix.iso.)

# mkisofs -pad -l -r -J -v -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 \
-o /mnt/sda3/knoppix.work/knoppix.iso \
/mnt/sda3/knoppix.work/master
5. Test your new ISO image. Once you verify that it works properly, you can create a bootable USB thumb drive from it.

Creating a Bootable USB Thumb Drive Using the Customized Knoppix CD

These instructions were tested on a 1GB PNY USB thumb drive; however, they should work on other thumb drives. If your thumb drive is larger than 1GB, you will probably want to review the "Bootable USB Key" article on the Knoppix wiki as it provides information for larger drives.

1. Boot from the custom Knoppix CD.

2. Once KDE is loaded, connect the USB thumb drive.

3. Open up a root shell: K Menu > Knoppix > Root Shell

4. Find the device entry for the USB thumb drive using dmesg, it should be the last part the dmesg's output. My PNY thumb drive shows up as /dev/sdb, because my hard disk is /dev/sda. If you have other SCSI devices, the USB drive may show up as /dev/sdc or /dev/sdd, etc.

# dmesg | tail -15
usb-storage: device found at 2
usb-storage: waiting for device to settle before scanning
  Vendor: PNY       Model: USB 2.0 FD    Rev: PMAP
  Type:   Direct-Access                  ANSI SCSI revision: 00
SCSI device sda: 2007040 512-byte hdwr sectors (1028 MB)
sdb: Write Protect is off
sdb: Mode Sense: 23 00 00 00
sdb: assuming drive cache: write through
SCSI device sdb: 2007040 512-byte hdwr sectors (1028 MB)
sdb: Write Protect is off
sdb: Mode Sense: 23 00 00 00
sdb: assuming drive cache: write through
 sdb: sdb4
sd 1:0:0:0: Attached scsi removable disk sdb
usb-storage: device scan complete
5. Erase the thumb drive. Be sure you have the correct device entry for the USB thumb drive and that you have backed up your data first:

# dd if=/dev/zero of=/dev/sdb
6. Format and partition the thumb drive. The following command will create a FAT16 partition and make it USB-ZIP compliant. This is done for better compatibility with various BIOSes:

# mkdiskimage -4 /dev/sdb 0 64 32
7. Run the SYSLINUX installer on the thumb drive. Note the 4th partition: this is for the USB-ZIP compatibility:

# syslinux -s /dev/sdb4
8. Mount the thumb drive:

# mount /media/sdb4
9. Copy the boot files from the CD to the thumb drive, rename isolinux.cfg to syslinux.cfg, and remove isolinux.bin (since we are now using SYSLINUX to boot rather than ISOLINUX):

# cp -av /cdrom/boot/isolinux/* /media/sdb4
# mv -v /media/sda4/isolinux.cfg /media/sda4/syslinux.cfg
# rm -fv /media/sda4/isolinux.bin
10. Copy the rest of the files on the CD to the thumb drive and then remove the /boot directory from the thumb drive:

# cp -av /cdrom/* /media/sdb4
# rm -rfv /media/sdb4/boot
11. Sync the drive and unmount it. It is now ready to be tested:

# sync
# umount /media/sdb4
# sync
Conclusion

I have just touched on the basics of customizing a Knoppix CD. In my environment, all I needed was the Citrix ICA client and the Windows Terminal Server client. You, however, may want to add other software depending on your environment and needs.

From extending the life of aging hardware to providing a handy portable admin tool, I hope you find customizing Knoppix as useful as I have.

References

Knoppix Remastering How-to -- http://www.knoppix.net/wiki/Knoppix_Remastering_Howto

Bootable USB Key -- http://www.knoppix.net/wiki/Bootable_USB_Key

John Ferrell is a System Administrator at the University of Maryland, College Park. He maintains a mixed network of Windows, Netware, Solaris, and Linux systems and can often be found writing Perl or Python scripts to automate as many tasks as he can. John can be reached by email at: jdferrell3@yahoo.com.