Your requirement

You want to boot a Linux system from a USB stick.

The procedure

Partitioning

GRUB is well suited as a bootloader. The following procedure illustrates how to install a working bootloader on a USB stick:

First we overwrite the complete stick with zero bytes. If there are no SCSI, SATA or other USB storage devices connected to the system, the device used for the USB stick is usually /dev/sda. In the following we assume that /dev/sda is the same as the USB key.

Attention: the following command irretrievably deletes all data on this device! Make sure beforehand which device file your USB stick really addresses!

root@linux# cat /dev/zero > /dev/sda
cat: write error: No space left on device

Now we create a partition table with one partition and activate it (bootable flag). This can be done with fdisk /dev/sda or cfdisk /dev/sda, for example.

Important: For the kernel to really interpret the new partition table correctly, it may be necessary to unplug and re-plug the USB stick once.

Create file system

The new partition is called /dev/sda1. On this partition we create a file system, e.g. of type ext2:

root@linux# mkfs.ext2 /dev/sda1

Install GRUB

Now we prepare a directory structure for GRUB and install it on the USB stick. First we mount the new partition after /mnt:

root@linux# mount /dev/sda1 /mnt

Grub is stored in the directory /boot/grub. We create this directory and copy the bootloader parts into it, most easily from our development system’s hard drive – which hopefully also boots with GRUB:

root@linux# mkdir -p /mnt/boot/grub
root@linux# cp /boot/grub/*stage* /mnt/boot/grub

Since GRUB names hard disks differently to the Linux kernel, we have to create a file by telling GRUB that /dev/sda is its first hard disk:

root@linux# echo '(hd0)  /dev/sda' > /mnt/boot/grub/device.map

Now you can install GRUB in the MBR on the USB disk:

root@linux# grub-install --root-directory=/mnt /dev/sda

Important: Do not forget to unmount:

root@linux# umount /mnt

Test

If so far you have done everything correctly, and nothing wrong, you should now be able to boot a PC from this USB stick. If successful, you will be greeted by the prompt from GRUB:

    GNU GRUB  version 0.96  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename. ]

grub> 

Jetzt fehlt natürlich noch der Kernel, eine initial Ramdisk und eine Konfiguration für GRUB unter /boot/grub/menu.lst. Das Vorgehen ist aber exakt wie bei der Festplatte. Die Bezeichnung für die Partition /dev/sda1 ist bei Grub (hd0,0).

Hinweis:

Manche USB-Sticks verhalten sich gegenüber Linux nicht transparent. Sie missbrauchen scheinbar bestimmte Speicherbereiche, um Information zur Verwaltung des Flashspeichers abzulegen. Dabei gehen sie z.B. implizit davon aus, dass der USB-Stick mit einem Windows-Dateisystem beschrieben wird, was man aus unserer Sicht durchaus als "Bug" bezeichnen kann. Leider steht sowas nie auf der Packung...

Das oben beschriebene Verfahren kann dazu führen, dass es bei solchen USB-Sticks zu Fehlern kommt. Unter Umständen muss der USB-Stick mit einem speziellen Verfahren neu dann initialisiert werden. Uns ist nicht bekannt, dass es zu einem permanenten Defekt kommen kann, schließen aber jede Haftung in so einem Fall aus.

Linux knowledge

These articles were written by the founder of Checkmk many years ago.
They are still valid though and thus we keep them on our website.
Mathias has since then developed a monitoring software called Checkmk

Find out more