grub on a usb drive

I typically use PXE to boot PCs when I am not booting them from the hard drive, but I really like the convenience of USB flash drives. They are extremely portable, cheap and most systems can boot from them. I have been playing around a lot with GRUB lately, so I thought I would spend some time setting up a USB flash drive with GRUB. The setup is pretty similar to setting GRUB up on a hard drive (which I have outlined in other post). This post outlines how to setup a USB flash drive to use GRUB to boot memtest86+, FreeDOS, a Fedora Install kernel, an Arch Linux Install kernel and Parted Magic Live.

Installing GRUB on a USB Flash Drive
Here are steps outlining how to install GRUB on a USB flash drive:

  1. Insert the drive into a USB slot on a Linux system (for me, this was my desktop system which is running Fedora 8)
  2. Create a partition on the USB flash drive large enough to fit the GRUB files as well as any kernels you want to boot (this will destroy any data on the drive, I typically create a single partition that spans the entire drive)
  3. Set the bootable flag on the partition (not all BIOS require this, but some do)
  4. Format the partition (this can be formatted as ext2/3, vfat or any other file system that GRUB supports, I typically use ext2, unless a Micro$oft system will need to access the drive, then I format it as vfat)
  5. Mount the partition
  6. Create directories to store the GRUB files (I create a boot directory in the partition's root directory and then a grub directory in the boot directory, boot/ and boot/grub/)
  7. Copy the GRUB stage1, stage 1.5 and stage2 files to mounted partition (I put them in the boot/grub directory, the stage1.5 file should be for the file system you used to format the partition)
  8. Unmount the partition
  9. Launch GRUB (run grub)
  10. Wait for grub to start (it make take a while to probe your hardware and you should eventually get the grub> prompt)
  11. Determine how GRUB identifies the USB flash drive - GRUB labels partitions with the format (hdX,Y), where X is the drive and Y is the partition, you can use the find command from within GRUB to determine the partition label, run:
    grub> find <filename>
    where <filename> is the full path and file name of a file that is on your grub partition, in my case I ran:
    grub> find /boot/grub/stage1
    This returned (hd1,0), so the partition where my my GRUB files are located is (hd1,0), note that if you have another partition anywhere on the system with the same path and file name, GRUB will return more than one partition, I have more than one stage1 file on my desktop system, but the partition on the USB flash drive is the only one whose path matches what I entered with the find command, so GRUB only returned one label
  12. Set the GRUB root device, run:
    grub> root (hdX,Y)
    where (hdX,Y) is the label for the partition you identified in the previous step
  13. Install GRUB on the USB flash drive, run:
    grub> setup (hdX)
    where (hdX) is the label for the drive you identified in the above step, GRUB will check that all the necessary files exist and will return successfully if everything has been correctly setup
  14. Exit GRUB (grub> quit)
  15. Remove the USB flash drive from the Linux system

At this point you should be able to boot into GRUB from the USB flash drive. Of course, there are no kernels to boot yet, so you will be limited to only running GRUB.

Next I will walk through setting up the USB flash drive with all the files needed for memtest86+, FreeDOS, a Fedora Install kernel, an Arch Linux Install kernel and Parted Magic Live. I typically create some directories to keep these files in, they can all be in the root directory, but that gets a little messy, especially if you want to use the USB flash drive to store other files. I typically create boot/, boot/grub (I already created both of these when I installed GRUB), boot/kernels/, boot/utils/. If I use the PXE images that are available for Parted Magic and Arch Linux these are all the directories I create, if I want to use the USB images, I also create pmagic/, pmagic/pmodules and boot/arch.

Adding memtest86+ Files
All that is needed to boot the memtest86+ utility is one file. The file is available from the memtest website and it is included with the Fedora and Arch Linux install images/ISOs. Grab the precompiled bootable binary from the memtest website (or get it from a distro image). Place a copy of the binary on the USB flash drive (I put it in the boot/utils directory).

Adding FreeDOS Files
Two files are needed to boot FreeDOS, a FreeDOS floppy image and the memdisk binary from the syslinux package. Grab a FreeDOS floppy image (images are available from the FreeDOS website, you may need to look around a bit, but they are there) and the memdisk binary (memdisk is available from the syslinux website). Place a copy of the FreeDOS floppy image and the memdisk binary on the USB flash drive (I put them both in the boot/utils directory).

Adding Fedora Install Kernel Files
Two files are needed to boot a Fedora Install kernel, the kernel itself and an initial RAM disk file. Both files are available from the Fedora installation media. Grab the kernel file and initial RAM disk file off of the Fedora installation media. Both files are located in the isolinux folder, vmlinuz is the kernel file and initrd.img is the initial RAM disk file. Place a copy of both files on the USB flash drive (I put them both in the boot/kernels directory and rename them vmlinuz-fedora and initrd-fedora.img, to distinguish them from the other kernel and initial RAM disk files that are copied to the same directory).

Adding Arch Linux Install Kernel Files
I have installed Arch Linux Install kernels two different ways. One way uses files from the archboot installation ISO (non-official image) and one using files from the official installation media. The archboot method is easier. Just like the Fedora method, it requires the kernel and initial RAM disk files to be copied onto the USB flash drive. Grab the kernel file and initial RAM disk file off of the archboot installation ISO. Both files are located in the isolinux folder, vmlinuz is the kernel file and initrd.img is the initial RAM disk file. Place a copy of both files on the USB flash drive (I put them both in the boot/kernels directory and rename them vmlinuz-archboot and initrd-archboot.img).

The other method, using the official installation media requires a few more files to be copied and also labeling the partition. It requires the kernel and initial RAM disk files as well as an isomount file and two squash file system files. Grab the kernel file and initial RAM disk file off of an official Arch Linux installation ISO/image. Both files are located in the boot folder, vmlinuz26 is the kernel file and archiso_pata.img is the initial RAM disk file. Place a copy of both files on the USB flash drive (I put them both in the boot/kernels directory and rename them vmlinuz-arch and initrd-arch.img). Grab the isomount file from the root directory of the official Arch Linux installation ISO/image and copy it to the root directory of the USB flash drive. Grab the two squash file system files, overlay.sqfs and root-image.sqfs from the root directory of the official Arch Linux installation ISO/image and copy them to the USB flash drive (I put them both in the boot/arch directory). Open the isomount file on the USB flash drive for editing. Update the entries for overlay.sqfs and root-image.sqfs to reflect the locations of the files on the USB flash drive. The final step is to label the USB flash drive partition that contains the Arch Linux files (I labeled my partition cruzer, since it is a SanDisk Cruzer USB flash drive, you can label it whatever you like). e2label can be used to label an ext2/3 partition, mlabel can be used to label a vfat partition.

Adding Parted Magic Live Files
I have also installed the Parted Magic Live kernels two different ways. One way uses the files from the Parted Magic PXE distribution, the other uses files from the Parted Magic USB distribution. The PXE method is easier. Just like the other Linux methods, it requires the kernel and initial RAM disk files to be copied onto the USB flash drive. Grab the kernel file and initial RAM disk file off of the Parted Magic PXE distribution. Both files are located in the pmagic-pxe-<version>/pmagic/ directory, bzImage is the kernel file and initramfs is the initial RAM disk file. Place a copy of both files on the USB flash drive (I put them both in the boot/utils directory and rename them bzImage-pmagic-pxe and initramfs-pmagic-pxe).

The other method, using the Parted Magic USB distribution requires the kernel and initial RAM disk files as well as a squash file system file. Grab the kernel file and initial RAM disk file off of the Parted Magic USB distribution. Both files are located in the pmagic-usb-<version>/pmagic/ directory, bzImage is the kernel file and initramfs is the initial RAM disk file. Place a copy of both files on the USB flash drive (I put them both in the boot/utils directory and rename them bzImage-pmagic-usb and initramfs-pmagic-usb). Grab the squash file system file, pmagic-<version>.sqfs located in the pmagic-usb-<version>/pmagic/pmodules directory copy it to the pmagic/pmodules directory on the USB flash drive.

Example menu.lst
At this point you should be able to boot into GRUB from the USB flash drive and run the commands to boot any of the kernels/utilities. To make it easier to boot one of the kernels/utilities you can configure GRUB to display a menu with each of the kernels/utilities as a selectable boot option. To do this you create a file named menu.lst (or grub.conf if you are using Fedora to install GRUB on the USB flash drive) in the same directory GRUB is located and add configuration information for each of the kernels/utilities you want it to display. Here is my menu.lst/grub.conf GRUB configuration file with entries for all the files discussed above.

timeout 5 # sets the timeout to 5 seconds
default 0 # sets 0th menu item (memtest+86) as the default boot option

# boot sections follow
# each is implicitly numbered from 0 in the order of appearance below

# (0) memtest+86
title memtest+86 V4.00
kernel /boot/utils/memtest86+-4.00.bin

# (1) FreeDOS
title FreeDOS
kernel /boot/utils/memdisk floppy
initrd /boot/utils/fdboot.img

# (2) Fedora
title Fedora
kernel /boot/kernels/vmlinuz-fedora
initrd /boot/kernels/initrd-fedora.img

# (3) Arch Linux using archboot files
title Arch Linux (archboot)
kernel /boot/kernels/vmlinuz-archboot
initrd /boot/kernels/initrd-archboot.img

# (4) Arch Linux using official media files
title Arch Linux Live CD
kernel /boot/kernels/vmlinuz-arch lang=en locale=en_US.UTF-8 archisolabel=cruzer ramdisk_size=75%
initrd /boot/kernels/initrd-arch.img

# (5) Parted Magic PXE files
title Parted Magic v4.5 (PXE)
kernel /boot/utils/bzImage-pmagic-pxe edd=off noapic load_ramdisk=1 prompt_ramdisk=0 rw sleep=10 loglevel=0 keymap=us
initrd /boot/utils/initramfs-pmagic-pxe

# (6) Parted Magic USB files
title Parted Magic v4.5 (USB)
kernel /boot/utils/bzImage-pmagic-usb edd=off noapic load_ramdisk=1 prompt_ramdisk=0 rw sleep=10 loglevel=0 keymap=us
initrd /boot/utils/initramfs-pmagic-usb

The value for archisolabel for the Arch Linux Live CD entry must match the label you gave your file system.

Testing Using VirtualBox
Testing and troubling shooting the USB flash drive can be a bit of a pain if you are using the same PC to create it as you are to test it. You make some changes, reboot the system, change the boot options to boot from USB, run some tests, reboot the system, change the boot options to boot the hard drive, etc. Since this can be somewhat time consuming (most of which is spent waiting for the system to (re)boot) I will often use VirtualBox during the testing phase. VirtualBox is a great x86 virtualization product that can be used to simulate a PC as well as simulate booting from a USB flash drive. To use VirtualBox to test booting from a USB flash drive:

  1. Install VirtualBox
  2. Create a virtual disk image (vmdk file) for the USB device, run:
    > VBoxManage internalcommands createrawvmdk -filename ~/.VirtualBox/usb.vmdk -rawdisk <usb flash drive device> -register
    where <usb flash drive device> is the Linux device label for the USB flash drive, in my case /dev/sdb, the user creating the vmdk file must have read and write permissions for that device
  3. Configure VirtualBox to boot the virtual disk
  4. Boot the virtual machine

At this point the virtual machine should boot to GRUB and you can test your GRUB install.

Resources
Arch Linux website: www.archlinux.org
Fedora Project website: fedoraproject.org
FreeDOS website: www.freedos.org
GRUB website: www.gnu.org/software/grub
memtest86+ website: www.memtest.org
Parted Magic website: partedmagic.com
syslinux website: syslinux.zytor.com
VirtualBox webstie www.virtualbox.org