Sunday, January 7, 2007

Boot Camp 101

It's time for me to update my USB version of the live distribution, Knoppix. I was very excited some months ago when I discovered that entire Linux distributions could be booted from a USB memory device. It had occurred to me before, but I suppose I had never really considered that ramifications and uses of such a setup. Many advantages sprang to mind... including security and being able to take all your data with you and turn any terminal in to your terminal. The best part? When you are done, just unplug and walk, no local record at all. There is one problem with "USB Linux" however. Only new generation computers can actually boot from USB. In my experience... systems with just a couple of years on them just don't have the ability. Of course BIOS updates can sometimes take care of that, but it does limit the effectiveness vs CD/DVD booting, which almost ALL systems can boot.

The first step is to obtain a USB memory stick large enough for your requirements. Remember there are two versions of Knoppix, the CD version and the DVD version. I would say a 1GB stick is large enough to hold the CD distribution and any user data you may need to store. I use a Lexar JumpDrive FireFly 1GB for Knoppix. I'll be using the Knoppix 5.1.1 release, make sure you get the ISO image.

A few IMPORTANT notes before we continue. I'll sometimes refer to the USB memory stick as just the usb stick or usb device or just device. These terms are interchangeable. I'll also be working a lot from the mount point of the device, in my case /media/disk/ please remember to change this to match your own mount point.

Without further introduction, let us partition. You may find that your device is assigned a mount point as soon as it is inserted. For most cases very useful, but for our case... not so. Go ahead and umount using the device's mount point (or device address). You can check a list of mounted devices and their mount points by running mount.

In my case, the device address was /dev/sdb1 and the mount point was /media/disk. So my command was umount /dev/sdb1. Good! Now your device is free to be worked on. (Sidebar:: You can umount from both the device address and the mounted address. I prefer to use the device address because it is a constant.)

Just a quick reminder. sdb1 refers to a partition, not the drive itself... when you want to refer to the drive as a whole (as we will when we use the disk partitioner), you must remove the partition identifier, in this case 1.

So the address of my USB stick would actually be /dev/sdb. Commands such as fdisk are powerful, and sometimes lack idiot guards. If you try to partition a partition, the program will allow you to do so, but the results will be extremely erratic!

So... run the disk partitioner with the device address, in my case fdisk /dev/sdb. You will be greeted with a simple command prompt. Go ahead and type print. You will see a list of partitions. On a USB device you will probably only see one, but regardless, use the delete command until all partitions have been erased. When you have an empty partition table, it's always a good idea to write your changes and restart the disk partitioner. This is not a requirement, but it allows the partitioner to make a clean start.

Now we can create the new partition using the new command. Choose primary and partition number 1. You will at this point be asked to specify first and last cylinders. Just press enter to accept the defaults (which will use all space on the drive). Viola! Our partition has been created, but it needs a little editing for our purposes... it needs to be bootable! So use the active command to set partition 1 as active. We can now write the changes.

Just a quick note. Sometimes when writing your partition table changes, fdisk will report that it will make the changes on the next boot. The reason for this? Your wonderful distribution may try to mount the device mid partition. To avoid this, drop down to a low run level (1 perhaps), and use the write command there instead. No interference!

What's next? Well we have to format the partition. Run mkfs.ext3 /dev/sdb1 to format the partition with the ext3 file system. It will give you some verbose information and (hopefully) complete without error.

Linux's default behavior with all storage devices causes it to run a disk check over a certain period of time and a certain amount of mounts. We need to disable this check, because the device is removable. To do this we can run tune2fs -c 0 -i 0 /dev/sdb1. The -c switch in this command sets the mount count and the -i switch sets the interval count. As you might have guessed, 0 is disabled.

So now we have a clean slate on which to begin the installation of the Knoppix image. Half way there!

First, let us mount the device. My command would be mount /dev/sdb1 /media/disk, but if you have a distribution that supports automatic mounting it might be easier to just remove and reinsert your device.

You should know where your Knoppix ISO is. We are going to (in a round about way) mount the ISO image by creating a loop (long story, but trust me). Browse to the location of your ISO file and run losetup /dev/loop0 KNOPPIX_V5.1.1CD-2007-01-04-EN.iso (adjust as needed). This will create a loop device at /dev/loop0. Now this loop device must be mounted. Create an empty directory anywhere (/media or /mnt is a good place) with mkdir. Now mount the loop device in to that directory. I used mount /dev/loop0 /media/loop.

Now we can simply copy across the contents of the image to the USB device by using a standard copy command. I used cp -r /media/loop/* /media/disk. Remember the -recursive option as the copy command will have to traverse directories!

After the copy process appears to be completed, clean up! Run a sync command to make sure no stray files are waiting to be copied to the USB drive. Umount the loop device with umount /dev/loop0 and remove it with losetup -d /dev/loop0.

The next stage is to install the boot loader. The first step of installation is to tell Grub that this device is a BIOS drive. Use echo '(hd0) /dev/sdb' > /media/disk/boot/grub/device.map. This will create the relevant device.map file. You may need to create the grub directory beforehand.

Now we can use grub-install... maybe. The Grub Install command is a little hit and miss. Sometimes if will work flawlessly, othertimes it will return with wonderful innocuous error messages such as /media/disk/boot/grub/stage1 not read correctly. If you want to go ahead and run the installer, use grub-install --root-directory=/media/disk --no-floppy '(hd0)' where /media/disk is the location of your mounted device.

If this does not work, copy the grub files directly from your distribution. I would run cp /boot/grub/* /media/disk/boot/grub/. Remember to change the device.map file (should only contain one line (hd0) /dev/sdb (or the hardware address of your device)).

Final step! We need to create a menu.lst file. This will be stored in /media/disk/boot/grub/. This is the boot loader menu, which passes certain boot options to the kernel. Sometimes these can be very specific, and change with each new release. Take a look at /media/disk/boot/isolinux/isolinux.cfg. On the second line down, you will see the boot configuration, in the case of Knoppix 5.1.1:

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

The above string will need a little work to bring it in to Grub compliance and should be used in menu.lst:

title Knoppix
kernel /boot/isolinux/linux ramdisk_size=100000 init=/etc/init lang=us apm=power-off vga=791 initrd=minirt.gz nomce loglevel=0 quiet BOOT_IMAGE=knoppix
initrd /boot/isolinux/minirt.gz

Hopefully you'll see how I converted the string. Just remember that when booting, your device's mount point will become irrelevant. If you are booting from the USB device, it will become /, so /boot... is all that's needed.

Phew! All done. Now just umount the device and test it out. If all goes well, Knoppix will boot. This tutorial was a little challenging. It never hurts to start from scratch remember... you may have made bad calls early on... sometimes you have to remove all assumptions... start from scratch. To help me along the way with this project I found various articles... the problem however was that a good deal of the time no alternatives are suggested and commands you are executing are not explained. Here is my main source article.

0 comments: