A brief guide to disk image files

Disk images, including both single-file and bundle forms, aren’t novel. A disk image is just a file or bundle (a specially structured folder posing as a file) which can be loaded into memory and function as a whole disk, with volumes, folders and more. They’re commonly used to distribute software, to protect sensitive documents with encryption, and to provide a virtual file system for Time Machine backups on other host systems such as networked storage. What is complicated about disk images are their many variants and options. This article is a short introduction to what is on offer in macOS.

There are two main types of disk image: regular images, which include not only the data stored in them but also all free space, and sparse images/bundles which only store the data without any free space. Create a normal 10 GB disk image to contain 1 GB of files, and that .dmg file will be 10 GB in size, even though most of the file is empty. Create a 10 GB sparse image or bundle, put 1 GB inside it, and it will be little more than 1 GB in size, as it doesn’t store any empty space.

When you open an image or bundle, macOS first attaches it, then inspects it for file systems, which it tries to mount. This is performed at a low level, in the kernel and its extensions, just as if the image/bundle were an external storage device.

Thus, the image file or bundle is merely a container for a file system and files. There are many variants and options for those: images can be read-only or writable, encrypted, compressed, HFS+, APFS or use other file systems, use case-sensitive names or not, and can support other specific features such as journalling for HFS+.

If you intend making your disk image/bundle writable, so that you can add further items to it, then you should consider using either of the sparse formats, which grow as the data in them grows. Of those, the sparse bundle is probably the more useful so long as it can be stored where its bundled structure is respected.

A sparse bundle consists of a folder, the bundle, with the extension .sparsebundle. Inside that is an Info.plist property list, a duplicate copy named Info.bckup, an empty file named token, and a folder named bands which contains the file system and files forming the virtual disk. As a minimum, there are two numbered bands, but these can grow into as many as needed to contain the files stored in the sparse bundle. By default, each has a maximum size of 8.4 MB; when the sparse bundle needs more storage than can be accommodated within its existing bands, another will be added, up to the maximum capacity of the sparse bundle.

sparsebuns1

Disk Utility’s support for sparse bundles isn’t as flexible or complete as it should be. To get full control, you’ll either need to use the hdiutil command in Terminal, or my own free utility Spundle.

spundle113

As flat files, disk images tend to travel better, but aren’t as flexible for changes in the amount of data they contain.

When deciding which type of disk image/bundle to use, the most important question is whether you intend to continue saving to it, or want to make it read-only. In the latter case, a normal non-sparse disk image is likely to be your best choice, allowing you to format and customise it to achieve exactly what you want. This can be done using the bundled Disk Utility, hdiutil in Terminal’s command line, or best of all using C-Command’s excellent DropDMG. The latter gives full access to nifty features such as background pictures and software licence agreements, as are used by many developers to distribute their products.

Finally, disk images support some specific cross-platform formats, most importantly DVD/CD-R master images widely used to burn to optical media.

These are some of the most popular disk image/bundle options:

  • for software delivery – read-only disk image using HFS+, compressed using zlib, authored with DropDMG;
  • to contain sensitive files – read-write sparse bundle using HFS+ or APFS, encrypted, created with Spundle;
  • as an optical disk master – DVD/CD-R master, read only, uncompressed.