Using hdiutil to create a blank .dmg image in the current directory

As a workaround to this issue, I want to use the hdiutil to create a blank image in the current directory. The settings should be identical to these of the GUI Disk Utility (File > New Image > Blank Image):

So far I have figured out the below options for hdiutil create:

  • -size 100m or -megabytes 100
  • -type SPARSEBUNDLE --> It seems like -type SPARSE and -type UDIF should be possible. I am not sure what is the difference though, or which one is better.
  • -fs 'Case-sensitive APFS' --> That seems to be more performant than the conventional -fs 'Case-sensitive Journaled HFS+' file system

the part I can't figure out are:

  • how to make a blank image, not from a folder
  • how to place it in the current directory
  • what other options should I specify

I would appreciate it if you could help me figure out the above issues and, if possible, answer my other questions.


Solution 1:

You got it all right. Just give it a name for the image file (the .dmg/.sparseimage), and maybe a name for the virtual volume, if you don't want it named "untitled":

hdiutil create -size 100m -type SPARSE -fs 'Case-sensitive APFS' -volname myVolName myFileName

You probably don't want a sparsebundle, because under the hood it's a folder full of separate files. It was created for performance optimization for remote Time Machine backups on a networked hard drive (or other NAS or fileserver), but for local use, you probably want something that is just a single file. If you want to preallocate the whole 100MiB on disk even before you write anything to the virtual disk, then use UDIF (UDRW) and you'll get a ~100MiB .dmg file containing a ~100MiB virtual filesystem. If you want the image file to start out small and only grow to ~100MiB when you fill up its virtual filesystem, then use SPARSE, and you'll get a small .sparsefile image that grows up to ~100MiB as you copy files into the virtual filesystem.

By specifying the size you want it to be (and not giving it an input folder to build the disk image around), it assumes you want a blank image with a filesystem of that size.

As is standard for the Unix command line, if you specify the name of a new file without a path, it's assumed you want it created in the directory you're currently in; that is, in the shell's current working directory, as reported by the pwd (print working directory) command.