Thursday, May 28, 2009

Create bootable USB drive (.img or .raw) using the OS X terminal

I frequently hack around with live images of other OSs and install them on my Acer Aspire One netbook (I still haven't located a distro that I like). Instead of burning CD images, I have found that bootable USB drives are faster and more reliable. The images I use most frequently come in either .raw or .img formats.

Using these image types, here's how to create a bootable USB drive using OS X and a couple of terminal commands (WARNING: Performing these steps will completely erase the contents of your USB drive). Remember, you need a USB drive large enough to hold the image contents. Let's burn an image using the following steps found in the tech note after the click below.

Download an image to your Desktop
:: Locate an a disk image you want boot from USB
:: If you're experimenting, Google these distros: Moblin, Fedora 10, Ubuntu, CentOS, Mandriva - remember, you are looking for live bootable images usually in the .img format

Partition the USB drive
Note: Not all USB drives are created equally. Some will function as bootable drives and some will not. You may have to experiment with different brands before you find one that works. I have found that often cheaper, no-brand drives work the best. My trusty Kodak 2Gb USB Drive works for most applications, but I make no guarantees for your application.

:: Fire up the Disk Utility application
:: Select the options in order and as shown in the image below (click for larger view)

Create a bootable USB drive
:: Click the Apply button (READ WARNING AGAIN!!!) - the disk will be partitioned as free space and with no file format

Determine the USB drive device name

:: Insert the USB drive into one of the USB slots
:: Fire up the Terminal (I prefer the free iTerm)
:: In ther Terminal type:
diskutil list
:: A list of all devices will be displayed as shown in the image below
:: Locate the USB drive inserted and jot this info to paper or brain - in this example, highlighted below, the USB drive inserted is /dev/disk2 and was identified by the USB drive name

Write an Image file to a USB drive using Mac OS X
Unmount the USB drive
:: In the Terminal, type the following command
sudo umount /dev/disk2
:: Enter the system password and press Return

Burn the image to the USB drive
:: In the Terminal, use the dd command using the following format:
sudo dd if=location/name.img  of=/dev/disk2
:: In this example the command was formatted as follows:
sudo dd if=/Users/stevencombs/Desktop/karmic-netbook-remix-i386.img  of=/dev/disk2
Raw Support
In the example above an .img image was used. If your image is in the. raw format, use the following format for the dd command: 
sudo dd if=location/name.raw of=/dev/rdisk2 bs=1m
:: OS X will begin burning the image to the USB drive (as indicated by the flashing USB drive light) - this operation can take several minutes depending on the .img size and the write speed of the USB drive - once complete, the light will stop flashing the the terminal prompt will reappear
:: Remove the USB drive

The USB drive is now ready to boot that laptop or desktop using the USB boot option. This same process can be used for most linux distributions. What I have not determined is how to easily do this with an .iso file on a Mac. So if you have a tip for me regarding this process, drop me a comment and add to these notes.