A USB stick, grub and Ubuntu

Warning: for Linux geeks and my notes only. Not general interest!
I experienced an especially painful Ubuntu upgrade over the weekend, made worse by the installer refusing to read packages from a CD. I suspect that whatever driver the installer employs to read from the CD doesn’t like my CD-DVD drive, as the CD disc itself was just fine. Anyway, I then spent a long afternoon figuring out how to boot and install from a USB stick. Here’s how.

1. Prepare the USB stick
This assumes that you have a working machine on which to mount the stick. All operations should be run as su or sudo.

  1. Plug it in and determine the device either by typing “mount” or looking at “tail /var/log/syslog”. The device is pretty likely to be “/dev/sda”.
  2. Run fdisk: “fdisk /dev/sda”.
  3. Type “p” to see existing partitions. Type “d” and select partition numbers until all existing partitions are gone.
  4. Type “n” to create a new partition. Accept the defaults (“enter” twice for one partition on the whole stick).
  5. Type “t” for the partition type and “6” for FAT16.
  6. Type “a” to toggle the flag of the partition to bootable.
  7. Type “w” to write the partition and exit.
  8. Now create the FAT16 filesystem using “mkdosfs /dev/sda1”.
  9. “sudo apt-get install mbr” if you don’t have it
  10. Add a master boot record to the stick: “install-mbr /dev/sda”.

You should now have a bootable stick with a single FAT16 partition (/dev/sda1).

2. Add GRUB bootloader to the stick
Mount the stick again. We’re assuming that the mount point is /media/usbdisk.

  1. Create /boot/grub on the stick: “mkdir -p /media/usbdisk/boot/grub”.
  2. Copy over grub files: “cp /lib/grub/i386-pc/* /media/usbdisk/boot/grub/”.
  3. Create grub device map: ‘echo “(hd0) /dev/sda” > /media/usbdisk/boot/grub/device.map’.
  4. Install grub to the stick: “grub-install ––root-directory=/media/usbdisk /dev/sda”.

Now use an editor (e.g. nano) to open /media/usbdisk/boot/grub/menu.lst and add these lines:

title           Ubuntu
root            (hd0,0)
kernel          /vmlinuz root=/dev/rd/0 /init=/linuxrc rw
initrd          /initrd.gz
boot

You can also add a “ramdisk_size=” parameter to the kernel line. These lines tell grub which files to use when booting. We don’t have these files yet, so…

3. Copy required files to stick

  1. Head over to http://archive.ubuntu.com/ubuntu/dists/dapper/main/installer-i386/current/images/hd-media/ (or the appropriate directory for your distro and architecture).
  2. Grab the files “boot.img.gz”, “vmlinuz”, “initrd.gz” and copy to the root directory of the stick.
  3. Finally, get yourself any Ubuntu ISO image. If your stick is 1 GB or larger, the normal installer images (something like ubuntu-6.06-alternate-i386.iso) will fit just fine.

4. Boot and install
You’re ready to go. You might have to enter the BIOS to make sure that your machine can boot from a USB device and change the boot order. My BIOS has a “USB-HDD” option which works for sticks – this is quite common, but there are variations.

In theory the machine will now boot from your stick, GRUB will start up, find the kernel and ramdisk images, locate the ISO and the installer will run as it would from a CD. These instructions derived in large part from Rene Mayrhofer.

4 thoughts on “A USB stick, grub and Ubuntu

  1. Hi,

    a lot like the debian install guide. I followed that guide for some debian installation from a USB stick. It took 5mn with the debian-businesscard.iso (20MB) and then, netinstall once you could boot up from USB. I am not sure that ubuntu provides a net-install scheme (I am a debian user and am thiniking about using ubuntu soon, that’ why I don’t really know).

    Moreover, instead of using fdisk, use cfdisk (curses based) so that you don’t need to know all these letters for acting on your partitions. Just type cfdisk /dev/sda, and you’ll get a nice interface :)

  2. Yes, there are lots of similar guides around. These notes are just my attempt to condense the other guides into something that I can use, for future reference. I wouldn’t normally even bother, were it not for the problem with the CD drive.

    Ubuntu does provide a net install – after all Ubuntu is very similar to Debian, just better packaged and more up to date.

  3. Thanks alot for this. It helped me install onto a laptop that had no boot mechanism. I have blogged about this blog on my blog, I hope that’s ok and that’s too much “blog” in one sentence

  4. Much thanks. Having problems w/ a laptop, and was unsuccessful at making a bootable usb drive. These notes are much clearer than other guides, so I believe I see my problem. Going to try again this evening.

Comments are closed.