[ FloobyDust ][ Previous | Next ]
Knoppix Linux and Grub Bootloader on USB Flash
Updated 2007.11.30
Introduction
This exercise put a Knoppix Linux "Live CD" version 4.0.2 distribution on Sandisk Cruzer Mini 1G USB flash drive and made it bootable. I have tested bootability on ThinkPads R40, T43, and T61, a Sun W1100Z (Opteron), and on a "white-box" P4 system with an Intel D945GCZ motherboard. Other older machines I tested in offered to boot the fob but failed to do so, with varying error messages (including none). YMMV.
I chose to use grub as the bootloader since I am familiar with it. It allows specifying multiple boot options including other OS installations in its boot menu. It also has an "on the fly" command line capability that allows manally entering boot instructions or editing the pre-defined ones.
Here is a different approach to putting Knoppix/Linux on a USB fob using the syslinux bootloader: rz-obrian.rz.uni-karlsruhe.de/knoppix-usb
ATTENTION! The procedure described below worked for me. It may not work for you. I may have errors in this report. If you copy/install/set up to the wrong drive your system may be unbootable. Winged monkeys may fly out of your posterior.
Why Bother
It was an interesting challenge. It also has potential for being quite useful as a recovery/rescue tool. Knoppix Linux seems to have quite extensive hardware detection and driver support. On the few machines I have booted it on it has come up quickly with full support for graphics, networking (including wifi!) and optical drives. It can read and write most filesystem types (probably shouldn't try to write NTFS with pre-5.0 Knoppix) and save recovered data over the network, to another USB drive, to CD/DVD, or to the USB fob itself. It can also manipulate hard drives and potentially be used to repair broken operating systems.
The beauty of Knoppix in this application is that it is a "Live CD" installation that runs read-only on its install media. USB flash drives have a limited number of write cycles before they fail. Since Knoppix doesn't write to its install media (in normal operation) this problem is avoided. Knoppix runs in local RAM and possibly swap space on a local hard drive. Otherwise local drives are not altered unless by the user manually.
Knoppix is available here, and a great deal of additional information here.
Make Drive Bootable
The USB flash drive needs to be made bootable. That may involve partitoning, marking a partition active, formatting and installing a bootloader in the MBR. In the case of the a SanDisk Cruzer Micro 1G fob I tested, just installing the grub bootloader (as described below) and running fdisk to set the partition acitve made the out-of-the-box flash fob bootable on all of my test machines. It was already paritioned and formatted with a single FAT partition.
There are apparently two different ways to format these things: "whole device" or partitioned. The whole device approach leaves the partition table blank and formats the fob as a "super floppy". The partitioned approach makes it look (and boot) more like a hard drive. It varies from BIOS to BIOS which kind is bootable. Older machines may require an unparitioned device while newer ones want to treat flash like a hard dive. The fobs I have experimented with all come with one FAT parition already formatted. I don't know (yet) if grub will install on a "whole device".
I stuck with a single FAT partition for this exercise. This isn't strictly necessary since it is possible to create other partition types and filesystems on the fob. SanDisk tech support says "not supported" but what do they know. Sticking with one FAT partition/filesystem solves some potential issues with windows (below). It also maximizes the range of machines likely to find the fob bootable. Grub and Knoppix seem perfectly happy to run from a FAT partition.
NOTE: For larger fobs, FAT32 is a better choice. The cluster size is quite large for FAT on a big fob, and there will be a lot of wasted space if you put many files on it. For a Knoppix-only install it doesn't manke much difference since the vast majority of files are "inside" the compressed file system which is a single file on the FOB. It only makes a difference if you keep the rest of your life on the fob as I do.
NOTE: I found out the hard way that any reformatting of the filesystem could make the fob unbootable on some systems. Formatting with Linux mkdosfs or with Windows XP's format before installing grub made the fob unbootable on the Intel system. It could still boot OK on my other systsems. The Intel BIOS seems to be very sensitive to FAT settings. I haven't been able to track down the exact problem.
SUBNOTE: It may be possible to use Linux fdisk to partition a bootable fob (in Intel's terms) if the expert options are used to select head and sectors/cylinder numbers that keep it happy. I haven't explored this enough yet to have any definitive information.
Update 2006.06.19: an email from "P.R." who used my notes to put Knoppix 5.0.1 on a USB fob pointed out that Linux mkdosfs would format a 1G fob as FAT32. Since the partition ID is 6 (FAT 16) the "-F16" flag should be used when formatting with mkdosfs if FAT16 is desired. It may not be appropriate for larger fobs. See the note above. I thought the ID confusion might explain why it would boot on some boxen and not others, but further experiments show that even with the "-F16" flag, it still won't boot on my P4 if formatted with mkdosfs. (Both FAT16 and FAT32 seem to work OK when using the HP formatting tool).
Update 2006.06.23: I finally realized that the "unbootable disk" error message with the Linux partition & format version of the fob when plugged into my Intel box was coming from the fob's boot sector (not BIOS or MBR). Duh. For some reason this BIOS is booting the active partition directly, not the MBR as the other BIOSen do (and as the Intel box does when the fob is formatted with the HP utility). The brute-force workaround for this situation is to install Grub to both the MBR and the active partition's boot sector (see Installing Grub and Knoppix, below):
(in my example, your values may differ)
grub> root (hd2,0)
grub> setup (hd2)
grub> setup (hd2,0)
Update 2006.12.11: The order of file installation can affect bootability on some BIOSen. See "Grub Error 18" below.
HP Flash Drive Formatting Tool:
Running an HP program - which has to be done in windows - will make the flash drive bootable (again) on all my test systems. The utility can be used to just reformat the fob (which also seems to set the drive's partition active even though it isn't being asked to make it bootable) or it can be used to make a bootable DOS diskette. This latter requires the DOS boot files IO.SYS, MSDOS.SYS, amd COMMAND.COM which the user has to provide. Just the reformat option is sufficient when followed by a Knoppix and grub install. The utility says it is for HP DriveKeys but seems to work on other manufacturer's devices as well. FAT32 is probably the best choice for drives 1G and larger.
Update 2007.11.30: The HP link may already be dead. I've added a few other possibilities. I have not tested any of these except the origianl HP one. They may or may not work. They may or may not contain malware.
h18007.www1.hp.com/support/files/hpcpqdt/us/download/20306.html
www.bay-wolf.com/utility/usbkey/usbmemkeyboot.zip
downloads.pcworld.com/pub/new/utilities/peripherals/SP27608.exe
Using Linux Fdisk to Set Active:
If the HP utility isn't needed, Linux Fdisk can be used to set the partition active. The "p" command shows the partition table before and after setting the partition active with the "a" command. The "*" flags the active partition. The "w" command writes the updated partition table back to the fob. Fdisk can also be used to create and delete partitions, but that may adversely affect bootability on some machines.
# fdisk /dev/sda in my example
Command (m for help): p
Disk /dev/sda: 1024 MB, 1024966656 bytes
32 heads, 63 sectors/track, 993 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes
Device Boot Start End Blocks Id System
/dev/sda1 1 992 999813+ 6 FAT16
Command (m for help): a
Partition number (1-4): 1
Command (m for help): p
Disk /dev/sda: 1024 MB, 1024966656 bytes
32 heads, 63 sectors/track, 993 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 992 999813+ 6 FAT16
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
FYI - Solaris Bootable Fob:
Interestingly, going through the process described here
to install a bootable Solaris/grub on a USB fob also produced a stick bootable on all my test machines.
Installing grub and Knoppix
This was a 1G flash drive so I just installed the entire Knoppix CD. It is also possible to make the installation considerably smaller by first remastering the CD and going from there. Just removing Open Office will free up almost 200MB. There are excellent recipees for remastering on the Knoppix site:
I did this once as an exercise. It took a little time but was quite easy. I'm going to do it again and this time customize the splash screens and desktop background. Those files are "outside" the compressed system and can be changed at any time.
Update 2006.06.19: Did a remster to customize backgrounds and splashes, add some tools, and remove some packages to reduce the installed size. Also customized the KDE menu. See Remaster Knoppix. The remastered version was used on my "final" install to USB flash.
Format if necessary and set partition active. Since I had reformatted the fob many times during the experimental phase of this effort It needed to be restored to "maximum bootability" by running the HP format tool. This partitioned and formatted the fob, and also set its partiton active. If the fob hadn't been screwed up by my fiddling, I could have used Linux Fdisk (above) after booting Knoppix from CD.
Boot Knoppix from CD and plug in the flash drive. CD copying will probably run faster if DMA is enabled. It seems to be enabled by default for 5.0 and later. For pre-5.0 Knoppix, type "knoppix dma" at the isolinux prompt to enable DMA on all drives at boot time, or wait until Knoppix is running and a root shell is open and use:
#hdparm -d1 /dev/hd<whatever the cdrom is>
Or from the KDE menu:
KNOPPIX->Utilities->Hard Disk/CD/DVD DMA Acceleration
Mount/remount the flash drive read-write. From the desktop cause the drive to be automounted by left clicking on it. (This doesn't always work with Knoppix 5.1.1 and maybe other versions. See "Automount" below.) Then right click and select
Action-> Change Read Write mode
Alternatively create a mount point and mount it (or remount it) manually. It shows up as sda1 in my example.
#mount -rw /dev/sda1 /mnt/sda1 (to mount read/write)
#mount -o remount,rw /mnt/sda1 (to remount read/write)
Copy the CD contents to the fob. This took about 12 minutes (without enabling DMA - so turn on DMA!). With lots of RAM the command prompt will return fairly quickly but the copy is not complete until the fob LED stops flashing.
#cp -R /mnt/cdrom/* /mnt/sda1
NOTE: the cdrom is mounted @ /mnt/cdrom and /cdrom in 4.0.2. In 5.0.1, it is available only at /cdrom.
Copy the grub files from the running Knoppix CD to the fob. (Or from the fob to the fob...)
#cd /mnt/sda1/boot
#mkdir grub
#cd grub
#cp /lib/grub/i386-pc/stage1 .
#cp /lib/grub/i386-pc/stage2 .
#cp /lib/grub/i386-pc/fat_stage1_5 .
Put a distinct marker on the fob so the grub files can be readily located when running the grub install. Without this convenience running "find /boot/grub" as often suggested finds all Linux installations including Knoppix.
#touch fob_flag
NOTE: steps 4 & 5 (and maybe 6) are the simplest way to copy and create the boot files, but may limit bootability with some BIOS implementations. See "Grub Error 18" below.
Install grub to the fob's MBR. The setup command uses "(hdX)" to put the 1st stage in the MBR. "(hdX,Y)" would put it in the boot sector of partition "Y". For most BIOSen, installing to the MBR is sufficient. Also installing to the partition's bootsector also allows booting on some confused BIOS versions.
#grub
grub> find /boot/grub/fob_flag
(hd2,0) -- the result of the find in my example
grub> root (hd2,0) -- using the result of the find in my example
grub> setup (hd2) -- install mbr -- my example paramters - if you get this wrong you will trash some other drive!!!
hopefully grub anounces success
grub> setup (hd2,0) -- install bootsector - again, per my example
success, again
grub> quit
Create /boot/grub/menu.lst on the fob. I used one derived from the fob's /boot/isolinux/isolinux.cfg. Isolinux is used when booting the CD version. It would be convenient to boot from grub to isolinux and pick up the boot options directly but I haven't figured out how to do that - or if it is even possible. Failing that the boot options can be used in grub's menu.
This menu has options to boot to a full-graphics system with DMA forced on (BEEZIX 5.1.1B0.1 X DMA), with DMA forded off (BEEZIX 5.1.1B0.1 X NODMA), a command line only option (BEEZIX 5.1.1B0.1 CMD LINE), a full-GUI boot that doesn't try to bring up the network in DHCP mode - since a bug in 4.0.2 may hang the boot on some configurations 5.0+ should be OK with DHCP - (BEEZIX 5.1.1B0.1 X NO DHCP), a "failsafe" mode (BEEZIX5.1.1B0.1 FAILSAFE, a guess at a Windows boot (WINDOWS MAYBE), a boot direct to a memory test (MEMTEST), a free DOS implementation, and some drive diagnosis and paritioning tools.
The drive tools and DOS are floppy images and may not/will not boot from USB fob on most machines. There are here on the off chance that they will boot, and for consistency with the isolinux menu in the CD version, where they _do_ boot and run.
NOTE: with the exception of the first entry, these menu entries do not explicitly enable hard drive and optical drive DMA. DMA off seems to default to OFF for pre 5.0 Knoppix and ON for 5.0 and later. For pre 5.0, add "dma" (without the quotes) to the "kernel" lines to enable dma for all drives at boot time or use one of the options described below ("Bugs and Features") to enable DMA once Knoppix is running.
For Knoppix 4.0.2, I noticed what seems to be a bug in passing boot options when done this way. The "lang=us" doesn't always work unless it is the last option on the line.
Further, the "failsafe" choice may be more likely to cause booting to fail! It is here because it was in isolinux.cfg. I don't know how useful it is. The "memtest" option is slick. It lets you boot to a memory test ASAP. This menu also attempts to make a Windows boot available. It assumes that if you have Winxx it is installed on the first partition of the first drive. The makeactive command may not be appropriate for all installations. If your system boots via a partition other than the first one on the first drive this may set your system to boot directly to windows. The "map" commands are necessary since at boot the USB fob is drive (hd0).
Here's my menu. This is actually the one I used for the final BEEZIX version, not the simple remasterd Knoppix. (the browser's line wrapping will probably foul this up. The lines that start with "kernel" continue on one line through the "lang=us" boot option. The next line starts with "initrd". Here's a full-page version that may be easier to read: Knoppix/grub menu.lst or raw text file for download if that's convenient: menu.lst
# menu.lst for BEEZIX 5.1.1B0.1 installation on USB thumb drive
default=0
timeout=10
splashimage=(hd0,0)/boot/grub/bootsplash.xpm.gz
title BEEZIX 5.1.1B0.1 X DMA
root (hd0,0)
kernel /boot/isolinux/linux ramdisk_size=100000 init=/etc/init apm=power-off vga=791 nomce dma lang=us
initrd /boot/isolinux/minirt.gz
title BEEZIX 5.1.1B0.1 X NO DMA
root (hd0,0)
kernel /boot/isolinux/linux ramdisk_size=100000 init=/etc/init apm=power-off vga=791 nomce nodma lang=us
initrd /boot/isolinux/minirt.gz
title BEEZIX 5.1.1B0.1 CMD LINE
root (hd0,0)
kernel /boot/isolinux/linux ramdisk_size=100000 init=/etc/init apm=power-off vga=791 nomce 2 lang=us
initrd /boot/isolinux/minirt.gz
title BEEZIX 5.1.1B0.1 X NO DHCP
root (hd0,0)
kernel /boot/isolinux/linux ramdisk_size=100000 init=/etc/init apm=power-off vga=791 nomce nodhcp lang=us
initrd /boot/isolinux/minirt.gz
title BEEZIX 5.1.1B0.1 FAILSAFE
root (hd0,0)
kernel /boot/isolinux/linux ramdisk_size=100000 init=/etc/init vga=normal atapicd nosound noapic noacpi pnpbios=off acpi=off nofstab noscsi nodma noapm nousb nopcmcia nofirewire noagp nomce nodhcp xmodule=vesa lang=us
initrd /boot/isolinux/minirt.gz
title WINDOWS MAYBE!!!!!
map (hd0) (hd1)
map (hd1) (hd0)
rootnoverify (hd1,0)
makeactive
chainloader +1
title DOS
root (hd0,0)
kernel /boot/isolinux/memdisk
initrd /boot/isolinux/balder.img
title MEMTEST
root (hd0,0)
kernel /boot/isolinux/memtest
title SPINTOOL generic disk tools
root (hd0,0)
kernel /boot/isolinux/memdisk
initrd /boot/isolinux/spintool.img
title DISKTOOL Hitachi & generic disk tools
root (hd0,0)
kernel /boot/isolinux/memdisk
initrd /boot/isolinux/disktool.img
title SC TOOLS 1
root (hd0,0)
kernel /boot/isolinux/memdisk
initrd /boot/isolinux/scfloppy1.img
title SC TOOLS 2
root (hd0,0)
kernel /boot/isolinux/memdisk
initrd /boot/isolinux/scfloppy2.img
How to boot
This varies all over the map by BIOS. My newest machines will present boot options when a particular function key is pressed during boot. It is then simply a matter of selecting the USB fob.
My slightly older machines require a particularly annoying tap-dance. With the USB fob plugged in, I have to hit a function key to get the BIOS configuration pages ("CMOS") and go to the "boot' page. From there I select "hard drives" then select the USB fob and move it to the top of the hard drive list. Then save and exit, and the machine boots to the fob. Sheesh. My Sun W1100Z Opteron box requires this even though it has an on-the-fly boot options selection that lists "hard drives" with a "+" next to it to expand the list - it just won't actually expand.
Update 2006.03.29: I spent some time wandering around BestBuy and Staples trying the fob in a few laptops - with mixed results. Knoppix booted up all the way on an HP widescreen, a Toshiba booted part way to the dreaded "Can't find Knoppix filesystem... dropping you to a (very) limited shell", and a Sony just said "GRUB". The missing filesystem error looks like a USB problem. Either the Knoppix drivers don't work with that chipset, or maybe the drivers need more time to load. The hang at "GRUB" smells like BIOS vs LBA vs USB vs ??? problem. I need to go back and get specific machine model numbers and dig further. Input welcome!
Update 2006.06.19: There is a fix for the "missing filesystem" problem, at least in some cases. See the USB 2.0 bug, below.
Update 2006.12.11: The order of file installation can affect bootability on some BIOSen. See "Grub Error 18" below.
Knoppix Bugs and "Features" - Vary with version
So far just the two mentioned above and one that really isn't a bug:
lang=us
The "lang=us" boot option has to go at the end of the kernel line
DHCP boot hang.
Booting with DHCP enabled will hang the boot on some systems. Seems to be fixed in 5.0.1.
DMA not enabled in pre-5.0.1.
DMA isn't enabled by default (apparently). Some docs indicate that DMA should be enabled by default on all hard drives and optical drives. This doesn't seem to be the case on any of my machines so maybe that isn't true. Disabling DMA by default is probably the conservative choice since some chipsets can cause read errors or file system corruption under Linux with DMA enabled.
Drive reads and writes will run faster (!) if DMA is enabled. Type "knoppix dma" at the isolinux prompt (or add dma to the kernel options in the grub menu) to enable DMA on all drives at boot time, or wait until Knoppix is running and a root shell is open and use:
#hdparm -d1 /dev/hd<whatever>
Or from the KDE menu:
KNOPPIX->Utilities->Hard Disk/CD/DVD DMA Acceleration
USB 2.0 driver not loaded.
As mentioned in the 4.0.2 bug list, there is a typo in linuxrc in the compressed miniroot used to boot Knoppix 4.0.2. Which causes the USB 2.0 driver not to load. This causes slow USB operation (and booting) in most cases, and in some cases the "Can't find Knoppix filesystem" boot failure in others. My P4 wouldn't boot my fob after a BIOS upgrade, and some boxen just don't boot the fob at all.
In the "boots but no USB 2.0 driver" situation, just issue
modprobe echi-hcd
and that will load the missing module. The failure to boot case requires repairing and replacing the miniroot. See "Bugs & Fixes" on my Remaster Knoppix page for a description of the fix for 4.0.2. Knoppix 5.0.1 seems to have fixed the problem.
Grub Error 18.
Some BIOSen that can boot from USB can't access disk addresses beyond 8MB. If grub stage 1_5 or the "kernel" that grub tries to boot (minirt.gz in the case of Knoppix) is beyond that address grub will halt with "Error 18". (It's OK if the main kernel is above 8MB, but the initially booted "kernel" can't be in the case of a limited BIOS capability).
To avoid this problem and maximize "bootability", keep /boot/ at the beginning (lowest addresses) of the USB fob. Starting with an empty formatted fob, copy the /boot directory from the cdrom to the fob, then create /boot/grub and copy the grub files and create the fob_flag file, then copy the rest of the cdrom - excluding /boot/.
/boot and its sub directories take up about 7MB currently and fit within the limit.
Free DOS won't boot. (5.1.1)
A missing "memsdisk" entry prevents Free Dos from booting. See the Remaster Knoppix page.
No CDROM icon on desktop. (5.1.1)
The CDROM icon doesn't appear until some other drive icon is clicked.
USB fob doesn't always automount. (5.1.1)
This varies. A FAT32 will sometimes not automount when plugged in. The error is "can't determine file system type". It can still be manually mounted: mount -t vfat /dev/something /mnt/something
XP issues
Just a few of many..... XP only recognizes the first partition on the fob. I wanted to make an ext2 parition for Knoppix and a FAT partition for my own files. I could do that fine with Linux fdisk. Linux would happily mount both of them. XP will show both in "Disk Management" but only the first is enabled.
Perhaps even worse, even though the 1st partition is ext2 XP doesn't indicate that it is foreign (and just not mount it). It shows it as "unformatted" and offers to format it. Typical.
If you touch the fob's partiton(s) with XP's format utility, it will probably become unbootable on some machines (even after grub is installed and the parttion is set active. The only "sure thing" formatter I've found so far is the HP format tool above.
Fill In The Blanks
This page is obviously incomplete and perhaps inaccurate. Feel free to send me corrections and additions: williamDOTwaddingtonATbeezmoDOTcom
[ FloobyDust ][ Previous | Next ]