Making a bootable USB drive

May 27th, 2009

In this post, I will go over how to set up a USB disk in order to make it bootable. This is all pretty scary at first glance, but if you think before you do, and really examine what is going on here, it will be a bit more simple.

The only catch here is that the USB disk will come out as the same machine arch as the host box ( uname -m )

sdc should be replaced with the drive target of choice, this will be the correct drive if the USB key is the last drive plugged in, with two other internal drives, or one other flash drive. Please ( PLEASE ) check this before you begin, I don’t want to see anyone out there hose their Hard Drive!

First, we need to setup the device. Again, this is providing that sdc is your device.

This will wipe any data that is on the dive right now. First unmount the drive:

sudo umount `df | grep /dev/sdc | awk '{print $1}'`

If /dev/sdc is not mounted ( or any partition therein ) this will bomb with anger, but don’t worry. It won’t hurt your box. Best case is it runs without any output, and succeeds.

Then you will need to partition out the drive, use cfdisk to create a single partition on sdc, for use with the new live OS.

sudo cfdisk /dev/sdc

or, you can do it with fdisk if you are one of my more elite readers.

OK, so now that we have set up the partition table, it’s time to set up the device as ext3.

mkfs.ext3 /dev/sdc1

Great! So we have a usable flash drive again! ( Was it really that hard? )

OK, so now we need to mount the drive, and set the file system up.

Let’s first set up a folder to mount the flash drive. If you are like me and have one folder you always use ( and it’s not flash ) feel free to adjust this as necessary.
sudo mkdir /mnt/flash

OK, so now let’s actually mount the drive.
sudo mount /dev/sdc1 /mnt/flash

And a Kernel Interface ( proc ), along with the current Devices ( dev ).

Let’s create the correct folders, and mount filesystems.

sudo mkdir /mnt/flash/proc
sudo mkdir /mnt/flash/dev
sudo mount -t proc none /mnt/flash/proc
sudo mount -o bind /dev /mnt/flash/dev

OK, Awesome. Let’s check out some basic information about debootstrap. Debootstrap creates a basic debian install by downloading bleeding edge packages from the repo given. The basic syntax is debootstrap distro /place/to/point/

Debootstrap is not installed by default, so if you have not already done so, please install debootstrap. As we are using debootstrap, I figure you are on a debian system, so the natural command is

sudo apt-get install debootstrap

Debootstrap will create a fully chroot’able environment to use. Let’s get to it.

sudo debootstrap jaunty /mnt/flash/

Wahoo! Awesome. So we have a nice environment to use, lets move into it and set this guy up.

sudo chroot /mnt/flash

Let’s install GRUB. For some reason ( Please, someone comment about this, I would love a better way to do this ) files are not included. We will pull ‘em from the host box.

Well, let’s get to it.

sudo apt-get install grub

Then let’s exit the chroot, to copy in critical files.

exit

Let’s copy out the GRUB boot information / stage files.

sudo cp /boot/grub /mnt/flash/boot -r

Now that we have the boot files in the chroot, let’s go back and add finish the setup in the drive.

sudo chroot /mnt/flash

OK, first thing, lets set up the fabled device.map. This file scares small children, torments kids and frightens adults to tears when not setup right. This single file has accounted for more headache on my part then any other system config file. Let’s just set it up.

Vim is not installed by default, so if you would like to use vim, then install it with a simple apt-get. For the sake of simplicity, I will reference vi, but again, feel free to break from this guide and use vim.

vi /boot/grub/device.map

OK. Clear out this file, and insert this.

(hd0) /dev/sda

Or, if you are lazy, you can do this:

echo "(hd0) /dev/sda" > /boot/grub/device.map

So, now that we have the device map set up, let’s install the Linux image. This is, again, for Ubuntu Linux with a vanilla kernel. Feel free to change to the -rt kernel, PAE kernel, or roll your own, just deviate as necessary to make it work. If you get something cool working, feel free to email me, or throw a comment letting us all know!

apt-get install linux-image-generic

OK Let’s setup the menu.lst. First we need to remove whatever is in there. Remember, only do this in a chroot! Don’t hose your host box!

echo "" > /boot/grub/menu.lst

Let’s open up your menu configuration.

vi /boot/grub/menu.lst

This is an example entry that I used to boot, again, if your setup has been different feel free to change, and apply salt to flavor.

title Flash Drive
kernel /vmlinuz root=/dev/sda1 ro quiet
initrd /initrd.img
quiet

Awesome. We are almost there. Let’s get GRUB all setup on this device block.

grub

OK, Now that we are in the GRUB shell, let’s use it. I have pasted both the command, and the response. I will be sure to separate them.

grub> find /boot/grub/stage1

And, the resultant spew is:

find /boot/grub/stage1
(hd0,4)
(hd2,0)

OK, now we need to set root to our drive. hd2,0 is my drive, and usually it is safe to go with the highest number.

grub> root (hd2,0)

And our spew:

root (hd2,0)

Next, we need to setup the drive to ensure GRUB will work.

grub> setup (hd2)

And the spew:

setup (hd2)
Checking if "/boot/grub/stage1" exists... yes
Checking if "/boot/grub/stage2" exists... yes
Checking if "/boot/grub/e2fs_stage1_5" exists... yes
Running "embed /boot/grub/e2fs_stage1_5 (hd2)"... 16 sectors are embedded.
succeeded
Running "install /boot/grub/stage1 (hd2) (hd2)1+16 p (hd2,0)/boot/grub/stage2 /boot/grub/menu.lst"... succeeded
Done.

Whoh, that was a lot. OK, well all is well, looks great. Let’s get outa here.

grub> quit

Let’s also get out of the chroot.

exit

Lastly, let’s unmount all of our devices.

sudo umount /mnt/flash/proc
sudo umount /mnt/flash/dev
sudo umount /mnt/flash/

There ya’ have it. A bootable flash drive, debian based, and all around nifty start. Remember, this is just a base, feel free to add on GNOME etc, make it your own

I’d like to thanks drs305 for reviewing this, catching a bunch of stuff I overlooked. Same goes to Bodsda, Thanks, guys!


7 Responses to “Making a bootable USB drive”

  1. GAC on May 29, 2009 8:44 pm

    You seem to be using /mnt/sdc in a couple of the examples above, shouldn’t this be /dev/sdc (and /dev/sdc1 for /mnt/sdc1)?

  2. tag on May 29, 2009 9:27 pm

    Yes! You are 100% right! Thank you for that catch! I will be sure to change that!

  3. Peng’s links for Saturday, 30 May « I’m Just an Avatar on May 30, 2009 7:07 pm

    [...] is an…Justin Dugger: A case study in the FAIL metric: Soul-FuMilo Casagrande: UDS: final roundPaul Tagliamonte: Making a bootable USB driveJonathan Riddell: The Kubuntu UDS CrewDavid Futcher: Taking A [...]

  4. JaneRadriges on June 13, 2009 5:12 pm

    The article is ver good. Write please more

  5. Roger on August 16, 2009 9:20 pm

    I merely use System > Administration > USB Startup disk creator :-)

    After booting the USB device is mounted read only at /cdrom. You just need to do “mount -o rw,remount /cdrom” and can then go in and modify to your heart’s content.

  6. Stanislav Antic on August 17, 2009 3:44 am

    Maybe you should mention that using non journaling FS expands lifetime of a flash disk.

  7. tag on August 17, 2009 3:25 pm

    @Stanislav Antic

    First of all, I’ll assume you meant a Journaling filesystem, not Non-Journaling. ext2 is Non-Journaling, and it is better for most cases on a flash drive. ext3 ( what I used ) is journaling, and therefore your point.

    Yes. You are right. Using a FS that adds to the R/W cycle of a NAND device will indeed expend lifetime.

    However, when you are using a persistent OS where data integrity is key, using something like ext2 is just not going to work. We can preform some tricks to reduce this damage, such as mounting it in RAM on boot from an ramfs. This, however is out of the scope of this article. If you’d like, I will write an article detailing this.

    @Roger:

    That will work great for a Live CD, but not a full distro, however that is worth mentioning! Thanks!

Trackback URI | Comments RSS

Leave a Reply

Name (required)

Email (required)

Website

Speak your mind

Donate to my Coffee Budget!