Categories: Top ::

Apple: How to make disk image files (.dmg) from the command line
Sun, 14 Aug 2005

On my development Mac (a G4 tower from a few years ago - when the G5's were announced, the prices on G4's dropped by 1/3rd overnight - how could I refuse?) I run Mac OSX 10.2. I've often considered upgrading to 10.3 or 10.4, but in the end I do almost all of my work through my laptop (as a gateway to my other two development boxes) so don't really need the update. Further, as a developer, I find it wise to hang back on versions so that your dev environment shares more with the average user than the bleeding edge user. Anyway, people tell me that 10.4 is a bad update and to stick with 10.3, so I might as well stick with 10.2 right?

Of course, Shadow Plan development marches ever on and I release Shadow Plan updates as .dmg files to make installation of the desktop components a breeze - just drag and drop or even run-from-disk-image. Creating .dmg files from the command line is a little screwy though, so I thought I'd note it down here in case anyone needs to know. Read on, spiderfriends.


The way I've done it there are three main stages:

As you can see, the middle stage is a matter of dragging things around from my build-directories into the blank disk image. The tricky parts for purposes of this posting are steps 1 and 3, so I built a couple shell scripts so I wouldn't have to remember much.

Creating an empty read-write disk image

The steps aren't many, but took me awhile to figure out the options back months ago -- when you've no idea what values to plug in, trial and error gets tedious fast :)

The 'hdiutil' tool can be used to create and modify disk images and such. See below where I use it to create an empty 15MB disk image with no layout. -zeroImage is commented out and not-needed. Later I define a variable DISK to hold the disk identifier as spitout by 'hdid' tool and run 'newfs' (new filesystem, an old Unix favourite) to lay down a HFS format onto the disk. Surprisingly, the next thing to do is eject the disk (since we've screwed with it, the OS will be confused) and then try mounting it again so that we can (at last) drag stuff into it. The newfs step requires root due to permissions, and so you'll have to enter a password in. The whole thing is wrapped up here:

#!/bin/sh

# make a disk image
hdiutil create -megabytes 15 blank.dmg -layout NONE # -zeroImage

# mount it without system notify
DISK=`hdid -nomount blank.dmg`;

#echo $DISK

# format and name it
sudo newfs_hfs -v "My Blank Disk of Evil" $DISK

# eject it
hdiutil eject $DISK

# mount it (with notify to finder)
hdid blank.dmg

Copy the goodies..

At this point, proceed to copy all that you like into the disk image. When you're done moving things around, unmount the disk image by dragging the virtual disk over the Eject option (where Trash usually is) in the dock bar.

Prepare for distribution..

The last step is to change the disk image into something we wish to distribute -- in my case its read only, and uncompressed since OSX 10.0 can't handle compressed disk images. You can also prepare it as compressed using GZIP, since 10.1 and later can work with this format and it greatly reduces file sizes.

Again, I've wrapped it all up in a shell script to make remembering it easier. Note that I include two commands, but only one is used.. one reformats with compression and the other does not. (# is the comment character in unix shell scripts.)

#!/bin/sh

# convert to read only - no compression since 10.0 can't deal
hdiutil convert -format UDCO blank.dmg -o DownloadableImage.dmg

# convert to read only - compress for 10.1+
# hdiutil convert -format UDZO blank.dmg -o DownloadableImage.dmg -noext

There you go. Happy hunting.

[ Category: / technology / apple ] [link] [Comments]