8

I currently use hdiutil to create my DMGs.

The command I use is hdiutil create -size xxxg myImage.dmg -fs HFS+. I'm always left fidgeting around with the size until I get it close enough.

Is there a better method that either shrinks the DMG to size or creates it size?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user870130
  • 565
  • 1
  • 8
  • 16
  • 1
    @DanielA.White: Creating disk images is often a task that's part of packaging up OS X software for distribution. While it's not programming *per se*, it's closely related! –  Jan 10 '12 at 18:04
  • @DanielA.White, I tend to agree with duskwuff, this would probably fall under "software tools commonly used by programmers" – CNeo Jan 10 '12 at 18:15
  • @DanielA.White: How is asking for tips on how to create an optimized disk image file (`.dmg`), which has been a standard file format for distributing software in Mac OS X for over 11 years, off topic? – NSGod Jan 10 '12 at 19:00

2 Answers2

5

Yes:

hdiutil create myImage.dmg -srcfolder SourceDir

This will automatically size the image to fit.

  • I tried this which I thought was going to be perfect. I pointed it to ~7 GBs worth of data and it created a DMG with 212.5 MB available. If I resize or just specify the size when I create it I can get it closer. Maybe I'm splitting hairs in the end it's going to be a 7 GB download for the user one way or the other. Thanks. – user870130 Jan 10 '12 at 18:46
3

You can create a larger dmg, add the files, then resize it:

hdiutil resize -size xxxg myImage.dmg

or create a sparseimage, and then convert it

hdiutil convert -format UDZO Source.sparseimage -o myImage.dmg

I've never tried the latter, not sure if it'll pick the right size for the dmg.

Source: http://nickcharlton.net/post/converting-a-sparseimage-to-a-dmg

Note that sparseimages need to be compacted if files are deleted from them, as they don't release the space from deleted files automatically:

hdiutil compact Source.sparceimage
CNeo
  • 736
  • 1
  • 6
  • 10
  • Creating an oversized image and compacting it isn't a great solution, as there's a significant amount of FS overhead on large images. –  Jan 10 '12 at 18:07
  • True, though when packaging up a small binary for distribution this will be less of an issue. Sparceimage compact won't take that long (presumably there won't be much wasted space), and the resize will pretty much copy the entire dmg. That being said, I didn't know about your solution, which is definitely cleaner and faster :) +1. – CNeo Jan 10 '12 at 18:16
  • Huh, the overhead actually isn't as bad as I thought... a 1GB image compresses down to 164 KB. –  Jan 10 '12 at 18:22