How to create a dmg image

In a previous post I showed you how to mount a .dmg image from the command line.

Have you ever wondered how you can create such an image? Continue reading if you want to know.

Let's assume you want to create an image with a size of 10Mb that is formatted with a case sensitive HFS+ filesystem, with a Volume name Test stored in a file with the name Test.dmg.
Here is the command line you have to type in the shell:

  1. hdiutil create -size 10m -fs "Case-sensitive HFS+" -volname Test Test.dmg

If you want to put files on it you have to attach it first:

  1. hdiutil attach Test.dmg

Or you could just add -attach to the list of parameters of the hdiutil command and it will also attach it after it creates it:

  1. hdiutil create -size 10m -fs "Case-sensitive HFS+" -volname Test -attach Test.dmg

Now you can copy your files to /Volumes/Test and when you're done you can detach the image in case you want to transfer/copy/move it:

  1. hdiutil detach /Volumes/Test

If you want a quick way to create an image from the contents of a folder you can use something like this:

  1. hdiutil create -fs "Case-sensitive HFS+" -volname Test -srcfolder /path/to/source/folder Test.dmg

With that you can also specify a format for the image like compressed or read-only images.
Note that in this case I have not used that -size specifier anymore because the image will be as large as the contents of the source folder.

hdutil create --help for more details about image formats encryption and other goodies :)

Leave a Reply