Installing live ISO images on USB sticks

Note: this was more or less state of the art around October 2014, and a lot of it is still relevant. Please see the update for March 2021 for more up-to-date information.

Rationale: why yet another tutorial for that?

Of course, there are cons:

Installing the bootloader

This procedure uses the GNU GRUB version 2 bootloader. On Debian-based distros, it comes in the grub-pc package. On Red Hat-based distros, it seems to be the grub2 package. Installing it from sources should work too.

Let us assume that the USB stick you want to make bootable is /dev/sdc and mounted in /media/sdc1.

  1. Do not be root. If you make a mistake in this procedure as a simple user, it will probably just fail. If you make a mistake with root privileges, you may make your system unbootable.
  2. If there is a boot directory on the root of your stick, remove it or clean it up, especially boot/grub.
  3. Issue the following command line (adapting the paths to your system):
    /usr/sbin/grub-install --root-directory=/media/sdc1 /dev/sdc
    If all goes well, it will print something like "Installation finished. No error reported.".
    With some Debian versions, it may report a warning about /boot/grub/grub2-installed: ignore it.
  4. With your favorite text editor, create the file /media/sdc1/boot/grub/grub.cfg; do not put anything in it yet.

If you followed this procedure, your USB stick is now bootable: you can test it.

Testing the stick

Once the stick is supposed to be bootable, it may be a good idea to test it. The simple way is to simply stick it in a computer and boot it.

There is a less tiring way using QEMU. On a reasonably recent box, QEMU is fast enough to test GRUB comfortably. To start QEMU on your USB stick, unmount it and type:

qemu -hda /dev/sdc

Once the computer (emulation) is started, you should see a prompt similar to this one:

	                       GNU GRUB  version 1.98+20100804-14
	
	   Minimal BASH-like line editing is supported. For the first word, TAB
	   lists possible command completions. Anywhere else TAB lists possible
	   device or file completions.
	
	grub>
      

If you know GRUB, you can try a few commands.

Installing ISO images

General method

To install a live ISO image, just copy the ISO file on your stick in the boot/ directory (or anywhere you want, in fact). The next step is to figure out the GRUB commands to boot it.

Put the GRUB commands in the boot/grub/grub.cfg file. The stance to boot an ISO image looks like that:

	menuentry "name of the entry" {
	  loopback loop /boot/filename.iso
	  linux (loop)/path/vmlinuz boot options
	  initrd (loop)/path/initrd.gz
	  boot
	}
	

With GRUB, a device or partition holding a filesystem is designated using parentheses; for example (hd0,msdos1) is the first MSDOS-style partition on the first hard drive. A file in this device can be accessed by prefixing its name with the name of the device.

The loopback command defines a new virtual device, called here loop with the contents of a file. The next commands access the files in the ISO-9660 filesystem stored in the ISO file.

The hard part with this method is to figure out the path to the kernel and especially the boot options. Remember that once you have started the kernel, it has no way of knowing that you are expecting it to dig in an ISO file, let alone which one if there are several.

To help you figure out the path of the kernel and the command line options, examining the contents of the ISO file can be helpful. This can be done without privileges using the isoinfo tool that comes with the genisoimage/mkisofs suite:

isoinfo -R -i /media/sdc1/boot/file.iso -f
isoinfo -R -i /media/sdc1/boot/file.iso -x /isolinux/isolinux.cfg

The first command lists the contents of the ISO image; the second command dumps the contents of the requested file.

Live ISO images usually use the ISOLINUX bootloader: to find out the kernel options, reading its configuration is probably the first step.

Distros with a GRUB configuration (GRML)

Some live distros include a GRUB configuration specifically made foor loopback boot. This is true, for example, for GRML. You recognize this when you see a /boot/grub/loopback.cfg file in the ISO image. The GRUB stance will then look like that:

	menuentry "GRML 2010.12 i386" {
	  iso_path=/boot/grml_2010.12.iso
	  export iso_path
	  loopback loop $iso_path
	  set root=(loop)
	  configfile /boot/grub/loopback.cfg
	}
	

Casper-based distros (Ubuntu)

A lot of live distros use the Casper boot system. This apply especially to the Ubuntu live systems.

Casper includes support for loopback boot, it only requires the name of the ISO image. The GRUB stance will look like that:

	menuentry "Ubuntu 10.04 Netbook i386" {
	  loopback loop /boot/ubuntu-10.04-netbook-i386.iso
	  set root=(loop)
	  echo "Loading kernel..."
	  linux /casper/vmlinuz boot=casper iso-scan/filename=/boot/ubuntu-10.04-netbook-i386.iso
	  echo "Loading initrd..."
	  initrd /casper/initrd.lz
	  echo "Booting..."
	  boot
	}
	

Converting the menu system

I have a vague project of a tool to convert the ISOLINUX configuration in an ISO file into a GRUB configuration, but nothing is written yet.

Update (March 2021)

All this was more or less the state of the art around October 2014. As of March 2021, a few things have evolved.

UEFI has become the norm. I have written a page on installing GRUB on a hybrid UEFI/MBR bootable stick. It is better than this one.

Most live distributions, including Debian, Ubuntu, GRML, Kali, seem to be providing a /boot/grub/loopback.cfg file with good submenus. It makes configuring them much easier.

I have tried to configure Debian Live directly and enable persistence.