How to create a bootable OS X Mountain Lion (10.8) USB drive from Linux

Found this from here, since it was useful I decided to paste it here.

I saw a few guides on how to make a bootable OS X flash drive, but not many on how to do it without already having access to an OS X machine. Here are my ugly notes on creating a bootable OS X Mountain Lion (10.8)USB drive from/with Linux.

The copy of OS X 10.8 (Mountain Lion) I downloaded was in the form of a file named “InstallESD.dmg”. First, you’ll need to open it. You will need p7zip installed. If you try to dmg2img this file, you won’t get very far, so just extract it with 7-zip:

7z x InstallESD.dmg

The unrolled dmg file I found was 4348218934 bytes in size and had an md5sum of 8b4869920cd740414fe6b7e3f0b1be3e. Inside was another file with the same name (although it is slightly smaller) which is the actual install image. The internal dmg file (the one we really want) was 4333438336 bytes in size and had an md5sum of 68fd407bb74e4a2dd1913ce8bae80fc4. It must be converted prior to mounting, and for this you will need dmg2img installed.

cd InstallMacOSX.pkg 
dmg2img InstallESD.dmg 

The resulting img file was only 137322496 in bytes and had an md5sum of 05b5788f10f7300c457e2aed735eb83a. Now find the offset to properly mount the install partition in the resulting file.

hexdump -C InstallESD.img | grep “48 2b 00 04”
00008400  48 2b 00 04 80 00 01 00  31 30 2e 30 00 00 00 00  |H+……10.0….|

I got “00008400” which is hex for 33792 in decimal:

$echo $((0x00008400))
33792

We need to subtract 1024 from this number to find the target offset:

$echo ‘33792-1024’|bc
32768

Now make the loop device:

losetup -o 32768 /dev/loop0 /path/to/InstallMacOSX.pkg/InstallESD.img

And finally, mount it:

mount -t hfsplus /dev/loop0 /mnt/tmp/or/wherever/you/want

Next, I re-initialized an 8GB USB flash drive with an MSDOS partition table and created an HFS+ partition with gparted after installing these packages: hfsprogs hfsplus hfsutils.

Next, mount the HFS+ USB device R/W and cd to the /mount/point/of/loop/device and copy the contents of the mounted image:

tar cvf – * | tar xvf – -C /mount/point/to/hfs/usb/drive

Sync, unmount, insert USB drive into the Mac and power it on while holding down the OPTION key.