Thursday, October 16, 2008

Making a Bootable USB from DVD image

One of the big problems of making a bootable USB is file size. Almost all of the how-to's out there that allow to pass kernel commands during the boot process require the use of syslinux, which uses FAT16/FAT32 formated partitions. This comes with the drawback of limiting your maximum file size to 2 Gigabyte if using FAT32. A DVD release has it's main file, livecd.squashfs that can run up to nearly 4 Gigabyte, twice the maximum allowable size. In other words, Epic Fail. Here is a way around that.

This How-To will be covering use of a variation of a syslinux bootable USB drive, extlinux. As you might figure from it's name it allows you to use ext2/ext3 to format your partitions, thereby removing the 2 Gigabyte limit and allowing you to fit that monster livecd.squashfs onto your thumbdrive. For this example I will be using a 4 Gigabyte thumbdrive and Sabayon Linux 3.5 32 bit.

The first step to this is making sure you have syslinux installed. So depending on your distribution, apt-get install, emerge, urpmi -i, equo install ... etc etc etc, syslinux. This, if your distribution is any where near current, will also include extlinux.

You might as well go root and stay there. Yes, lots of this can be accomplished via GUI apps, and there are a plethora out there that you can use to accomplish a good chunk of these tasks. Command line though brings us to a common point that all your GUI apps are bolted on to anyway. That and because it's my preferance. From here on out I am going to assume that you are root (Ubuntu users this means 'sudo su -').

The next step to this is formatting the USB drive.
First identify your thumbdrive.

fdisk -l

If you read the output of this the first line of each device will tell you the size of the device, which makes it pretty easy to see which is a hard drive and which is not.

In my case /dev/sdb as shown below:

Disk /dev/sdb: 4016 MB, 4016045568 bytes
255 heads, 63 sectors/track, 488 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x0004adbb

Make sure to substitute your device, /dev/sd(?)
fdisk /dev/sdb

Command (m for help): d
Selected partition 1

Command (m for help): n
Command action
e extended
p primary partition (1-4)
Partition number (1-4): 1
First cylinder (1-16065, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1015, default 1015):
Using default value <hit enter here for defualt>

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 83

Command (m for help): a
Partition number (1-4): 1

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

mkfs.ext2 -j /dev/sdb1

Next is to install the syslinux boot sector, I know we said we were going to be using extlinux, but bear with it for a few. Thumbdrives don't come with a boot sector, so we have to make one and that's what we are doing here.

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

Please note that this is the location of the syslinux mbr.bin for Gentoo and Sabayon, your distribution may hide it somewhere else. The command 'find / -name mbr.bin' will pin it down for you, this command will scan your entire hard drive so it is kind of slow. Just substitute the absolute path to mbr.bin in the command above.

Now we need to make a few mount points and mount the ISO and the thumbdrive, remember to substitute your thumbdrive location for the one in the example.

mkdir /mnt/iso
mkdir /mnt/usb
mount /dev/sdb1 /mnt/usb
mount -o loop -t iso9660 /path/to/iso /mnt/iso

(as an example mount -o loop -t iso9660 /home/jim/Sabayon-Linux-x86-3.5.iso /mnt/iso)

Now we copy things over that we will need.

cp -rvp /mnt/iso/boot isolinux livecd livecd.squashfs README.txt /mnt/usb

This could take awhile with a DVD iso image. The livecd.squashfs is a monster. Go get a cup of coffee and check back in a few minutes. After it is finished copying we can continue on by running the following commands which set up the files for use by extlinux and make the needed changes to the boot sector.

cd /mnt/usb
mv isolinux extlinux

mv extlinux/isolinux.cfg extlinux/extlinux.conf
rm extlinux/isolinux.bin
cd /mnt
extlinux -i /mnt/usb/extlinux

Your done, wasn't that easy. The final piece to this is unmounting everything we mounted earlier.

umount /mnt/usb
umount /mnt/iso

Now you can reboot into the USB drive by selecting to boot from USB, the ways the various BIOS's handle this though are too numerous to list. Typically there will be an interupt option during the POST to select boot device. If not you'll probably have to go into the BIOS itself and find out where to select it.

Have Fun


Viktor said...

I am trying to make Linux MCE DVD ISO on USB
sudo cp -rvp /mnt/iso/boot isolinux livecd livecd.squashfs README.txt /media/usb0/
does not work for me:
cp: cannot stat `/mnt/iso/boot': No such file or directory
cp: cannot stat `isolinux': No such file or directory
cp: cannot stat `livecd': No such file or directory
cp: cannot stat `livecd.squashfs': No such file or directory
cp: cannot stat `README.txt': No such file or directory

so I am trying :
sudo cp -rvp /mnt/iso/ isolinux livecd livecd.squashfs README.txt /media/usb0

working god damn long time, will it work on the end? ;)

I am surprised that there is no easier way how to do it.

In Ubuntu there is a utility called UNetbootin which allows you to put ISO image to USB, worked for me with Ubuntu instalation, which's ISO has just 700MB, so that'll be a reason why I can't use DVD ISOS for it?

Azerthoth said...

viktor, I'd be happy to give you a hand. Feel free to catch me on IRC and we can work the issue your having out.

And of course you will be looking for Azerthoth.

I'm there in the evenings during the week and pretty much anytime during the weekends. GMT -5 until november then GMT -8 after.

Emanuele said...

firstlythanks for your good guide.
Secondly I want use it for making my old 20GB usb-hdd the device for installations.
I think it's better than unetbootin.
I'm going to use it for Sabayon 3.5
With unebootin , the bios doesn't start anything...
Tried with vista under vmwarefor the sake of info..
Otherwise unetbootin it's good for usb pen.

Thanks in advance