Bootable Linux USB Drive – How To

Author: Andrew Rapo

URL: www.rapo.org/linux

Version: 0.1

 

[NOTE: This is a rough draft, so it is sparse and may contain errors]

 

Intro

 

In retrospect, it is pretty straightforward and easy to create a bootable USB drive, but it took me a while to put all the pieces together.  This How To is intended to save someone some time.

 

Besides being a useful thing to have, creating a bootable USB drive is an excellent way to familiarize oneself with the Linux boot process.  Linux is appealing because of its underlying simplicity, but in an effort to accommodate the widest range of potential users, most current distributions have fairy complex boot-up scenarios.  For an individual new to Linux, tracing the boot-up process can be time-consuming and confusing.

 

This How To can be viewed as an exercise to acquire a thorough understanding of Linux’s essential boot-up components.  It assumes only a limited familiarity with Linux.

 

The Hardware

 

San Disk Cruzer Micro – 256MB

HP d530 – Pentium 4, 3GHz, 2GB RAM

BIOS supports booting from USB device

Intel 82801EB USB Universal Host Controller

 

The Software

 

KNOPPIX 3.6 bootable Linux CD-ROM –

http://www.knoppix.org/

Provides a full-featured running Linux without the need to install

Nice tools for setting up devices

Good Linux kernel

 

ttylinux - ttylinux-4.3.tar.gz –

http://www.minimalinux.org/ttylinux/

Provides a basic running Linux feature set

Can be installed in 4MB

Can boot completely into a 4MB RAM disk

Perfect for the USB drive’s initrd

A good starting point for the USB drive installation

 

Brief Overview

 

Phase 1 - Assemble The Pieces

 

  1. Make a partition on the built-in hard drive to serve as a staging area

                                                               i.      Use KNOPPIX

                                                             ii.      Create and ext2 filesystem on /dev/hda1

  1. Copy ttylinux onto /dev/hda1
  2. Add a USB-ready kernel – from KNOPPIX
  3. Modify the ttylinux  initrd

                                                               i.      Include USB kernel modules

                                                             ii.      Make a custom linuxrc to handle the initial boot sequence

  1. Modify /etc/lilo.conf to use the new kernel and initrd
  2. Install LILO on the staging partition
  3. Reboot from the hard drive to make sure it is working
  4. Mount the USB drive to make sure the kernel and modules are working
  5. Copy all of the kernel modules from KNOPPIX to the hard drive

 

Phase 2 - Build the USB drive

 

  1. Copy everything from the hard drive to the USB drive
  2. Modify the initrd on the USB drive

                                                               i.      Modify linuxrc

  1. Modify /etc/lilo.conf on the USB drive
  2. Install LILO on the USB drive
  3. Reboot from the USB drive

 

Steps

 

Phase 1

 

1. Make a partition on the built-in hard drive to serve as a staging area

For simplicity, let’s assume you have a spare computer to work with and you can format its drive(s) without losing anything important.  Boot the KNOPPIX 3.6 CD-ROM and at the “boot:” prompt type:

 

knoppix 2

 

This will bring up KNOPPIX Linux in “run level 2”, giving you a command prompt (without having to login) and all of the tools you will need.  If you don’t provide any parameters at the boot prompt (just hit return) KNOPPIX will load a nice windowed desktop.  This is fine, but more than we need.

 

At the command prompt type:

 

cfdisk

 

This will bring up a user-friendly disk partitioning tool.  Use the arrow keys to make menu choices.   [Setup a root partition and a swap partition on /dev/hda.]  You should end up with a root partition on /dev/hda1 and a swap partition on /dev/hda2.  Reboot KNOPPIX, and again type “knoppix 2” at the boot: prompt.

 

At the command prompt type:

 

mke2fs /dev/hda1

 

This will create an ext2 file system on the root partition of your hard drive.  ttylinux can read ext2, and that is important for the next step.  Now mount /dev/hda1 by typing:

 

mount /dev/hda1 /mnt/hda1

 

2. Copy ttylinux onto /dev/hda1

 

[Download ttylinux-4.3.tar.gz] and gunzip/untar it into /tmp:

 

cd /tmp

gunzip ttylinux-4.3.tar.gz

tar -xvf ttylinux-4.3.tar

cd ttylinux-4.3

ls

 

You should see rootfs.gz. Then type:

 

cp /tmp/ttylinux-4.3/rootfs.gz  /mnt/hda1

cd /mnt/hda1

ls -al

 

You should see one empty directory named “lost+found” and rootfs.gz.  Continue by typing:

 

gunzip rootfs.gz

mkdir initrd

mount -o loop rootfs initrd

cp -a initrd/* /mnt/hda1

 

3. Add a USB-ready kernel – from KNOPPIX

 

Type:

cd /mnt/hda1/boot

cp /boot/vmlinuz-2.4.27 /mnt/hda1/boot

 

4. Modify the ttylinux initrd

 

Add the required USB kernel modules - from KNOPPIX - to the ttylinux initrd:

 

cd /mnt/hda1/initrd/lib

 

mkdir modules

mkdir modules/2.4.27

mkdir modules/2.4.27/kernel

mkdir modules/2.4.27/kernel/drivers

mkdir modules/2.4.27/kernel/drivers/usb

mkdir modules/2.4.27/kernel/drivers/usb/storage

mkdir modules/2.4.27/kernel/drivers/usb/host

 

cd modules/2.4.27/kernel/drivers/usb/

cp /lib/modules/2.4.27/kernel/drivers/usb/usbcore.o .

 

cd storage

cp /lib/modules/2.4.27/kernel/drivers/usb/storage/usb-storage.o .

 

cd ../host

cp /lib/modules/2.4.27/kernel/drivers/usb/host/usb-uhci.o .

 

Now, create a startup script for the initrd.  Type:

 

cd /mnt/hda1/initrd

 

Make a mount point for the main hard drive:

 

mkdir mnt/hda1

 

And use vi (or another editor) create a file called, “linuxrc”.  Copy the following into linuxrc:

 

#!/bin/sh

 

echo "linuxrc:" $1

echo "linuxrc: loading usb moduules"

 

/sbin/insmod /lib/modules/2.4.27/kernel/drivers/usb/usbcore.o

/sbin/insmod /lib/modules/2.4.27/kernel/drivers/usb/storage/usb-storage.o

/sbin/insmod /lib/modules/2.4.27/kernel/drivers/usb/host/usb-uhci.o

sleep 10

 

echo "linuxrc: mounting /dev/hda1"

 

mount -n -t ext2 /dev/sda1 /mnt/hda1

 

cd /mnt/hda1

/sbin/pivot_root . initrd

 

shift 1

exec /usr/sbin/chroot . /sbin/init $*

 

Save linuxrc and the make it executable by typing:

 

chmod +x linuxrc

 

Now unmount and gzip rootfs

 

cd /mnt/hda1

umount /mnt/hda1/initrd

gzip rootfs

mv rootfs.gz boot

 

5. Modify /etc/lilo.conf to use the new kernel and initrd

 

cd /mnt/hda1/etc

 

Use vi to edit the file “lilo.conf” so that it looks like:

 

# global settings

boot=/dev/hda

map=/boot/map

install=/boot/boot.b

prompt

timeout=50

default=linux_HD

 

# per-image settings

image=/boot/vmlinuz-2.4.27

       label=linux_HD

       initrd=/boot/rootfs.gz

       root=/dev/ram0

       append=”init=/linuxrc

 

6. Install LILO on the staging partition

 

Now, to install LILO type:

 

cd /mnt/hda1

chroot .

lilo -v

 

You should get some messages including, “Added linux_HD *”

 

Type: <CTRL>-d to exit chroot

 

7. Reboot from the hard drive to make sure it is working

 

Type: reboot

 

The KNOPPIX CD-ROM will eject.  Leave it out and let the computer boot from the hard drive.  There may be some errors regarding iptables, but you should get a login prompt.  Login as root, password: root.

 

8. Mount the USB drive to make sure the kernel and modules are working

 

The errors are generated because only the USB modules are currently available for loading.  But because they are present, you should be able to insert a blank USB drive, format it, and mount it.  Insert a USB drive, wait a few seconds, and type:

 

mke2fs /dev/sda1

mount /dev/sda1 /mnt/sda1

cd /mnt/sda1

ls

 

You should see the one, default directory, “lost+found”.  If so, then you have assembled all the necessary somponents are are just about ready to set up the USB drive, itself. 

 

If you need to troubleshoot, try the command:

 

/sbin/lsmod

 

This should list all of the kernel modules in memory.  If you don’t see: usbcore, usb-storage, and usb-uhci, then they may not have been referenced correctly in the initrd’s linuxrc script.  You can try re-loading them manually.  Remember, they should be in the lib/modules tree on the initrd:

 

/sbin/insmod /initrd/lib/modules/2.4.27/kernel/drivers/usb/usbcore.o

/sbin/insmod /initrdlib/modules/2.4.27/kernel/drivers/usb/storage/usb-storage.o

/sbin/insmod /initrd/lib/modules/2.4.27/kernel/drivers/usb/host/usb-uhci.o

 

9. Copy all of the kernel modules from KNOPPIX to the hard drive

 

The initrd contains a few, specific USB modules so that - eventually - the USB drive will be mounted automatically during the boot sequence.  In phase 2, you will want to have a complete set of kernel modules.  Type:

 

cd /mnt/hda1/lib

mkdir modules

cd modules

cp -a /lib/modules/2.4.27 .

 

Phase 2

 

10. Copy everything from the hard drive to the USB drive

 

Load the KNOPPIX CD-ROM and type: reboot

 

Again, at the boot prompt type: knoppix 2

 

At the command prompt mount the hard drive:

 

mount /dev/hda1 /mnt/hda1

mount /dev/sda1 /mnt/sda1

cd /mnt/sda1

cp -a /mnt/hda1/* .

 

11. Modify the initrd on the USB drive

 

mkdir /mnt/initrd

cd /mnt/sda1/boot

gunzip rootfs.gz

mount -o loop rootfs /mnt/initrd

cd /mnt/initrd

mkdir /mnt/usb

 

Use vi to edit the file, linuxrc, so it looks like:

 

#!/bin/sh

 

echo "linuxrc:" $1

echo "linuxrc: loading usb moduules"

 

/sbin/insmod /lib/modules/2.4.27/kernel/drivers/usb/usbcore.o

/sbin/insmod /lib/modules/2.4.27/kernel/drivers/usb/storage/usb-storage.o

/sbin/insmod /lib/modules/2.4.27/kernel/drivers/usb/host/usb-uhci.o

sleep 10

 

echo "linuxrc: mounting /dev/sda1"

 

mount -n -t ext2 /dev/sda1 /mnt/usb

 

cd /mnt/usb

/sbin/pivot_root . initrd

 

shift 1

exec /usr/sbin/chroot . /sbin/init $*

 

Now unmount and gzip rootfs

 

umount /mnt/initrd

cd /mnt/sda1/boot

gzip rootfs

 

12. Modify /mnt/sda1/etc/lilo.conf on the USB drive so it looks like

 

# global settings

boot=/dev/sda

map=/boot/map

install=/boot/boot.b

prompt

timeout=50

default=linux_USB

 

# per-image settings

image=/boot/vmlinuz-2.4.27

       label=linux_RAM

       initrd=/boot/rootfs.gz

       root=/dev/ram0

#      read-only

 

# per-image settings

image=/boot/vmlinuz-2.4.27

       label=linux_USB

       initrd=/boot/rootfs.gz

       root=/dev/ram0

       append="init=/linuxrc usb"

#      read-only

 

13. Install LILO on the USB drive

 

cd /mnt/sda1

chroot .

lilo -v

 

14. Reboot from the USB drive

 

reboot

 

Remove the KNOPPIX CD-ROM

 

During the BIOS check, hit F9 (or whatever your BIOS requires) to get a list of boot options.  Choose Boot From USB Device

 

At the LILO prompt choose “linux_USB”.  linux_RAM” is another useful mode which boots completely into RAM.  This can be very helpful for troubleshooting.  If linux_USB doesn’t work, try linux_RAM and try to mount the USB drive manually.

 

Disk Images

 

I have provided two iso disk images of working installations.

 

HD.iso - An image of the hard drive as set up in Phase 1

 

USB.iso - An image of the USB drive as set up in Phase 2

 

To use the USB.iso image (for example):

 

Boot KNOPPIX, copy the USB.iso to an accessible location, and mount the iso:

 

mkdir /mnt/iso

mount -o loop USB.iso /mnt/iso

cd /mnt/iso

 

Prepare the USB drive by completely erasing it!!!  This assumes that the USB drive is partitioned with a root partition at /dev/sd1 and a swap partition at /dev/sda2 - or some comparable setup.  Insert the USB drive, then:

 

mke2fs /dev/sda1

mount /dev/sda1 /mnt/sda1

cp -a /mnt/iso/* /mnt/sda1

 

Run LILO on the USB drive:

 

cd /mnt/sda1

chroot .

lilo -v

 

Reboot using BIOS controls to boot from the USB drive, etc.

 

Glossary (TBD)

 

initrd -

linuxrc -

 /etc/lilo.conf -

kernel -

LILO -

ext2 -

run level 2 -

/dev/had -

/dev/ram0 -

iso -

mount point -

mount -

cd -

ls -

filesystem  -

command prompt -

pwd

directory

root directory

 

 

Topics (TBD)

 

Partitioning disks

Burning iso files to CD-ROM

Using vi

cp -a

Getting ttylinux-4.3.tar.gz

 

This can be accomplished by running Lynx from the command line of the KNOPPIX CD.  For example:

 

Type:

 

lynx

 

When Lynx has started type:

 

g

 

Then type:

 

www.minimalinux.org/ttylinux/showpage.php?pid=4

 

Hit the down arrow a few times until the link for ttylinux-4.3.tar.gz is highlighted (red).  Then type:

 

d

 

Choose “Save To Disk” and specify the filename as:

 

“/tmp/ttylinux-4.3.tar.gz”

 

To quit Lynx, type:

 

q

 

The to make sure the file was downloaded, type:

 

ls -al /tmp

 

You should see “ttylinux-4.3.tar.gz” in the listing.