Thursday, May 28, 2009

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

This is a TIP supported post. If you found this post useful consider leaving Doc a tip. The suggested Tip amount for this post is 25¢ to 75¢. Use the button below to tip Doc.


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
:: DO NOT PHYSICALLY REMOVE THE DRIVE!

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.

40 Folk(s) added to this Tech Note.:

  1. you don't need to choose GUID from the options menu first?
    ReplyDelete
  2. This is the best explanation of the process I've seen, thanks
    ReplyDelete
  3. Thanks Anonymous. That's what I try and do...make sense out of all of this so anyone can figure it out.

    ----------
    :: Steven "Doc" Combs, Ph.D.
    :: http://www.docstechnotes.com
    :: http://feeds2.feedburner.com/DocsTechNotes
    :: steven@docstechnotes.com
    ReplyDelete
  4. What it it's in .iso format instead of .img ?
    ReplyDelete
  5. I agree with the above poster: this is the best article on this topic that I have found.
    Thanks, Steve!
    ReplyDelete
  6. This comment has been removed by a blog administrator.
    ReplyDelete
  7. @Ivan, works well with .iso as good as .img :)
    ReplyDelete
  8. You should eject the disk before removing it:
    diskutil eject /dev/disk2
    ReplyDelete
  9. If your having trouble with the path of your iso or img file - using finder you can drag and drop the file into terminal. This can be a time saver...
    ReplyDelete
  10. Nice post!!! Thanks, btw this one looks even better: http://www.youtube.com/watch?v=yet-7pmdwUQ
    ReplyDelete
  11. @Riki
    We got your link the first time but thanks anyway.
    ReplyDelete
  12. Thanks Anonymous for keeping an eye on the comments. I’ve deleted the first reference to the link to keep things cleaned up. - Doc
    ReplyDelete
  13. umount. didn't work for me. apparently it's unreliable, but this command did work fine:
    sudo diskutil unmountDisk /dev/disk1
    ReplyDelete
  14. Thank you. This was easy and well explained. I had been pulling my hair out over the last few days about how to do this.
    ReplyDelete
  15. Great article, and it appears to be working (the light on my flash drive is flashing), but the process has been going for 2+ hours already (3GB .img on an 8GB flash drive).
    ReplyDelete
  16. It can take some time for large images depending on your Mac and your USB drive. Patience is good! :)
    ReplyDelete
  17. Doing it with an iso image is as easy as:
    You have to convert the iso image with "hdiutil" e.g. "hdiutil convert -format UDRW -o ./outputfile.dmg ./inputfile.iso"
    ReplyDelete
  18. Thanks for adding that great tip Surfin-Jeri! I’ll have to give that a shot myself.
    ReplyDelete
  19. using /dev/rdiskN instead of /dev/diskN is supposed to speed this process up dramatically
    ReplyDelete
  20. I can confirm that using /dev/rdiskN instead speeds up the writing dramatically.
    ReplyDelete
  21. Thanks a ton Steve, got it working with my iso after searching through a lot of brutal tutorials.
    ReplyDelete
  22. Followed these instructions as well as the hdiutil instruction (I used the /dev/rdiskN as well without problems). I have yet to run into a single problem, and the install is running perfectly. Thanks so much!
    ReplyDelete
  23. What am I doing wrong? I go to boot off the usb sticks and it can't find a bootloader.
    ReplyDelete
  24. Shoe,

    Normally this happens when you have a non-supported USB drive. Try another drive and it may work. It's been hit or miss for me on compatible USB drives. Once I find one, I stick to it and use it for these types of projects.

    Doc
    ReplyDelete
  25. Well I know the USB stick can be bootable. I can create boot disks on a windows machine. But I am trying to do this on my mac as I am lazy! Plus I know how to write bash scripts. I'll play around with it some more.
    ReplyDelete
  26. Let us know what you find out and if you get it working.

    Doc
    ReplyDelete
  27. What about a .oem file?
    ReplyDelete
  28. Chris,

    See this page about .oem files.

    http://filext.com/file-extension/OEM

    I am unfamiliar with .oem files as I moved away from Windows years ago. From my short time researching it does not seem as if this is an archive that can be transferred to a USB drive. Maybe readers with more extensive Windows backgrounds will chime in. If you find an answer, be sure to stop back by and let us know.
    ReplyDelete
  29. Hey my mac i just got is imac g4 and the disk utility has no "options" tab so i cant make it guid..therefore i waill try this and get back with results:)...btw does this work with .toast files? thanx in advance
    ReplyDelete
  30. you can convert an ios to a dmg in disk utility :)
    ReplyDelete
  31. @Anonymous. I have not tried this, but here’s some good discussion on the topic:

    http://macosx.com/forums/mac-os-x-system-mac-software/268316-converting-iso-dmg.html
    ReplyDelete
  32. Fantastic guide, had trouble with this in the past and these steps are very simple to follow. Well done! Quick question... If I wanted to use this technique to make a bootable USB drive to be used on a PowerPC based mac, would I just click the 'options' button in disk utility and select 'Apple Partition Map', then follow the same procedure?
    ReplyDelete
  33. @Andy E, Thanks again for the kudos and glad you found the guide easy to use. Now, about your other question, in theory the method you suggest should work; however, (and a caveat as it has been years since I used a PowerPC-based Mac), the bigger issue may be, can you boot from USB on the PowerPC-based Mac?
    ReplyDelete
  34. Bravo Great guide simple detailed to the point ,why don't more people have the sense that everyone isn't a coder. This is valuable information that is hard to find but it would be nice to have someway to list the comments organized on their technical value.Saving time not to read through all of them like this one here for instance.
    ReplyDelete
  35. Thanks for this article - it helped me no end.
    ReplyDelete
  36. Thanks! Worked fine with my PNY Attache 8GB and ClearOS image.
    ReplyDelete
  37. Beautiful! Works nice dude :D
    Thanks for sharing!
    ReplyDelete
  38. To convert an iso to an img on a mac use hdiutil in Terminal:

    check out instructions here:

    http://crazedmonkey.com/blog/links/convert-iso-to-img-using-hdiutil-under-mac-os-via-installationfromusbstick-for-ubuntu.html
    ReplyDelete