How to create a disk image from the command line

disk-utility

Currently part of my build process is as follows;

  • Start Utilities/Disk Utility
  • Select New Image
  • Set Save As to widget
  • Set Name to widget
  • Set Size to custom size 50 mb
  • Set Format Mac OS Extended

but is it possible to do all this from the command line ?

Best Answer

You can do this with the hdiutil tool. The appropriate configuration in your case would be:

hdiutil create -size 50m -fs HFS+ -volname Widget /path/to/save/widget.dmg

Obviously change the "Widget" and path to whatever you need.

A few additional options that may be useful:

  • -srcfolder /path/to/source This will create the disk image with the data in the specified folder as the contents.
  • -megabytes 50 used instead of -size 50m. This will use binary sized megabytes (2^20 bytes) instead of decimal (10^6 bytes).
  • -srcdevice /dev/diskXsY This is like srcfolder, but makes a block-based copy from another device, like a hard drive partition or DVD. Useful for making clones and images of install disks, etc.