Absolutely foolproof way to get a USB flash drive to boot Linux

Written by Ian on 03/06/10


Introduction

There are so many USB flash drives out there, and often the instructions only work with one type of drive.
• What is needed is a definitive, one-off guide to getting this working.

Aim

To prepare a thumbdrive to boot Linux, from within Linux using only free tools.
• This only needs to be done once (unless you accidentally re-format it!).

Tools

A Linux PC.
• I prefer kubuntu, but any *ubuntu will work.
• I've tried this on kubuntu 9.10, 8.04 and 6.04.
• The version isn't so important.

Most other Debian-based systems will work OK.
• Red Hat based (i.e. Fedora, Mandrake, CentOS etc...) can also work, but you'll have to download and install the tools using yum or rpm instead of apt-get.

Method

1. Open a terminal window on your PC.

2. Plug in your flashdrive.
• If an icon comes up saying something like 'new media detected', just ignore it. DO NOT CLICK ON IT!

3. Next we find out the name the OS gives to the thumbdrive.
• Enter the following command:

dmesg

Look down the bottom, and you'll see something has been detected with a name like /dev/sdb.
• Ignore any numeral.
• In other words, if you see /dev/sdb1, just consider that to be /dev/sdb.

4. Next we create a new partition table on the device.
• This will probably WIPE EVERYTHING THAT'S ON THERE. Also, make sure you get the device name correct.
• If you use the wrong device name you could end up wiping your hard disk (we will check for this later):

We use the fdisk utility (with the 'name' of your thumbrive from step 3):

sudo
• fdisk
• /dev/sdb

It will ask for your user password. sudo temporarily gives you root access to run that command.
• If you are on a machine administered by someone else, you might not be allowed to do this.
• You'll need to ask them for sudo access.

In the fdisk utility, we do the following:

p
• (to print the partition table).
• It should show the size of your thumbdrive... if you see something much too big (like hundreds of gigabytes, then you're looking at your hard disk - quickly press Ctrl-C to exit: you read the wrong name in step 3).

 

If all is OK:

 

o
• (to create a new partition table)

n
• (to create a new partition).
• When asked, make it a physical partition, number 1, maximum size (default options).

t
• (to change partition type).
• Set your new partition (no.1) to type 'b', which is W95 FAT.
• This is the most common modern type to be recognised by the computer bios.

a
• (to make the new partition bootable).
• Apply to partition 1.

p

• (to print out the partition table).
• Check that it's correct as per the steps above. If not, try again.
• To quit without making ANY changes to the flash drive, just press q.
• Otherwise:

w

• (to write the new partition table to the flash drive)

5. Next, we will use the syslinux package.
• In *ubuntu it's easy to install this if you have internet access:

sudo
• apt-get
• install
• syslinux

If asked, enter your user password. If asked, reply Y to install this.

5.1 Once syslinux is installed, we will manually place the bootloader on the drive master boot record (MBR).
• Remember,
• replace sdb with the actual name of your flashdrive:

sudo
• dd if=/usr/lib/syslinux/mbr.bin of=/dev/sdb

6. Next we will format the drive as a DOS drive. Note again to replace the 'sdb' with the actual name of your flashdrive - but don't forget to add the number '1' at the end to show you are formatting the first partition on the device (we could have used fdisk to create more partitions if we wanted to):

sudo
• mkdosfs
• /dev/sdb1

7. Then we run syslinux to install the bootloader.
• Note again to replace the 'sdb':

sudo
• syslinux -f
• /dev/sdb1

8. Finally, we should fix the filesystem up.
• It seems that this is not always needed, but we will make sure we do it here to prevent possible problems later (again, replace the 'sdb' if necessary):

sudo
• fsck.vfat -r -v /dev/sdb1

If asked to give an option, select 1 to copy the main file allocation table to the backup.
• Select Y to actually perform the backup.

9. Now, remove the flash drive from your PC.
• Wait about 5 seconds, then reinsert it.

This time, if you get an icon to click, you can use this to mount the device and look inside. There should be something called isolinux.sys (or similar).
• This is the actual bootloader installed by systlinux.

10. Next step would be to copy over something to boot.
• If you want to try it out, you can get a VERY simple booting kernel image online (search for simple Linux boot), and copy to your USB device.

11. Note: when you try this, insert into your PC and reboot.
• If it doesn't automatically boot up (you will know if it works - there's a big splash screen with a beautiful photo on it), you will need to adjust your PCs bios options to allow it to boot from USB.
• Pretty much all modern machines (i.e. 5 years old or newer) can support this.