dx4.org :: linux :: modifydmg

If you use Mac OS X, you're probably familiar with the .dmg disk image file format. They're really just HFS+ filesystems, but they're wrapped in Apple proprietary, undocumented garbage which makes them a pain to work with. For example, most .dmg files you download are read-only and it's not easy to figure out how to get around that.
Because .dmg is a proprietary format, it's not clear whether read-only images are fundamentally different from read-write (as in CD-ROM ISO filesystems which are not designed to be written to) or if they simply have a flag set somewhere. Either way, I ended up having to convert the read-only image into read-write and back to do what I wanted.

All of these operations are performed at the command line (Applications > Utilities > Terminal for you GUI types) using the hdiutil program. Needless to say, this program is only available on OS X.

First, convert your read-only image into a read-write one. You evidently can't convert it in place, so you have to make a copy instead.

hdiutil convert ro.dmg -format UDRW -o rw.dmg

Now we've got a read-write image, but we can't just start sticking new files in there - the image is the exact right size for the amount of data already in it. We've got to expand the image. First, see how large the image currently is.

hdiutil resize -limits rw.dmg

Take the first number that command returns and add some to it. How much depends on what you want to add to the image. It doesn't matter if you go over, since you're going to shrink the image again before you're done.

hdiutil resize -sectors <sectors> rw.dmg

Now mount the disk image, make your changes, and umount it. You can do that through the GUI, or with hdiutil.

hdiutil attach rw.dmg
hdiutil detach rw.dmg

Check how much data is on the disk image now. This time, the number you're interested in is the second one, which indicates the minimum number of sectors the image can be resized to.

hdiutil resize -limits rw.dmg

Resize the image to that number.

hdiutil resize -sectors <sectors> rw.dmg

Finally, convert the image back to read-only. The following command uses maximum zlib compression, which is a popular read-only format.

hdiutil convert rw.dmg -format UDZO -imagekey zlib-level=9 -o ro.dmg