Install iso to usb in Linux


usb deviceHow to install iso to usb in linux. The Ubuntu site gives you instructions for creating bootable USB media from Windows or from Ubuntu. But what about other distributions?

Let’s start by taking a 50k foot view of the process to get a general idea of whats going on; then we’ll go through it step-by-step.

1. Download the iso.
2. Create 2 partitions on the USB device.
3. Set the boot flag on the 1st partition in fdisk.

Now you have two choices, write the iso to the bootable partition and you’re done. This assumes the iso was built the allow this and your hardware supports it. When it’s mounted, it will look like an iso9660 file system because thats what you’re writing!

4. Write the iso image to the 1st partition.
5. Done.

or copy all the files from inside the iso and install the bootloader manually.

6. Mount the 1st USB partition.
7. Create file systems on the 2 partitions.
8. Mount the iso.
9. Copy the contents of the mounted iso to USB.
10. Install syslinux
11. Edit the bootloader definition file.
12. Done.

It’s not that hard really, just a bunch of lower-level manual tasks. The beauty of this approach is it works from almost any system, and it just works.

There’s no complications of 3rd party software that dies in a fire assuming your configuration to be one thing or another or trying to wrap everything up in a pretty gui with a big red button to push.

Granted, when those apps work right, they’re great and they make some people wonder why anyone would torture themselves tip-toeing through a command line approach. But that’s a discussion for another time.

You’ll be using a lot of root priveleges here, so either prepend the following commands with sudo or just become root.

Assuming you’ve downloaded the iso, start by mounting it somewhere on your system. I like /mnt because there’s never any daemon’s keeping watchdogs on that area; mounting new devices as they’re connected. Just don’t use /media, /tmp, or another system directory. Or you’ll quickly run into a new set of problems.

Now figure out which device is the USB device and use that name instead of sdx from the
examples to follow. (Use ‘lsusb’, ‘dmesg’, or ‘fdisk -l’ to find this.)

Starting with using fdisk on my USB device:

Write a partition table with two partitions to the USB device. The first one is for ubuntu itself to boot from, the second is for persistent storage of settings, documents, etc.

My USB device is empty so it’s ready to have new partitions created. If yours has something on it, either delete them one at a time from fdisk or use dd to wipe out the first 512 bytes first.

Press ‘n’ to create a new partition.

Press ‘p’ to make it primary partition.

Choose 1 to make it the first partition.

Start at the default, 1.

Enter a size with a leading plus sign to specify that it’s an overall size and not a cylinder number. I chose ‘+750M’ because the iso i’m using is just over 700M in size.

Do the same thing for the second partition, but don’t make it bootable. I use the rest of the storage for the second partition, so when it asks for a size or cylinder, I just take the default which is the last cylinder.

Flag a partition as bootable.

Choose the 1st partition when it asks which one to make bootable.

Change the partition type of the bootable first partition. Make it FAT16 for maximum compatibility.

Finalize your changes by choosing ‘w’ to actually write the partition table to disk. Up until this point you could break out of what you were doing and no changes would have been made.

Feel free to shout out, ‘KA-BLAM!!!’ as the partition gets flushed to disk for emphasis.

Now you’re ready to create the file systems. It doesn’t really matter what labels you give your boot device and storage device so make something up.

First the bootable file system.

And now the second file system for storage persistence.

Make another mount point on your system so you can mount the USB’s first partition. Then mount it.

Remember the iso image you mounted using the loopback option on /mnt? Copy the contents of the iso mount to the usb mount. If you’re using rsync or cp, make sure you get the trailing slashes right.

Softlink failures are expected, please ignore. They are not supported by FAT file systems and they are not needed in this case anyway.

Install syslinux on the first partition. If you don’t have this binary already, it should be easy to find. It’s probably called ‘syslinux’ in your debian or rpm repositories. Unmount /dev/sdx1 before you do this. (You could probably skip unmounting but let’s do it to be safe)

At this point you could boot from the usb device and get syslinux to start loading, but it would not find a kernel to boot. You need to change the configuration slightly because it’s not a cdrom you’re booting.

Mount the 1st partition again and make a copy of the syslinux directory and its contents, then rename the .cfg and .bin files.

mount /dev/sdx1 /mnt1
cd /mnt1
cp -a isolinux syslinux

Now remove ‘/cdrom’ from txt.cfg to fix the boot path. You want to change the parts that have ‘file=/cdrom/preseed/ubuntu.seed’ to ‘file=/preseed/ubuntu.seed’. You can do this in a simple sed one-liner.

Reboot and either change your boot order in the BIOS to try USB first or press F12 before it boots to select a device to boot from this time.

If all goes well, you’ll see syslinux loading, then the grub boot menu.

[Edit: 5/21/2012]
Depending on the iso, you may need to (re)install grub. If the USB device does not boot after the above steps, go back and remount the device /dev/sdx1 at /mnt1 and run grub-install.

If it boots into the grub menu but won’t boot the os, go into the grub command line and probe around to see how the device is laid out, then go back and check the grub config to make sure the root path is correct.


Posted on May 3rd, by admica in howto, ubuntu.

3 thoughts on “Install iso to usb in Linux

  1. Hi,

    Don’t you need also to put a bootable bootloader in the pendrive MBR area?

    Something like:
    Check if the usbstick has an valid mbr, else
    dd if=mbr.bin of=/dev/sdX
    (mbr.bin is in the syslinux package, usually installed in /usr/lib/syslinux/mbr.bin)

  2. I got jolios working without (re)installing grub so it depends on the source image and how you’ve copied it to the USB device. (http://distrowatch.com/table.php?distribution=jolios)

    A plain file system copy from mounted iso to mounted partition will not install grub, only grub’s config files, so you would need to install grub. My bad. But this should be easy enough to fix. You can use grub-install to install the bootloader while you’ve got it mounted like this:

    grub-install –root-directory=/mnt1 /dev/sdx

  3. Nice tutorial. I think there’s an error in the step where you’re renaming the cfg and bin files. Shouldn’t the second syslinux.bin be syslinux.cfg?

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">