Should you use a disk image or a volume?

Disk images are one of the oldest features of computing, dating back well over half a century, still in widespread use today. They are files, or bundles (a structured folder posing as a file), that contain files and folders as if they were a disk. They’re commonly used to distribute software, to protect sensitive documents using encryption, and to provide a virtual file system for Time Machine backups on other host systems with networked storage.

Advantages

Because they’re plain files or bundles, they are mobile and can be hosted on other file systems, the reason for their use to distribute software and for storing backups on networked storage. They contain their own file system, these days most commonly APFS, where they can use its full features such as sparse files and extended attributes, even though the file system on which they’re stored doesn’t support them. They can also be encrypted, and so provide secure storage on an unencrypted disk.

For storing and distributing files which aren’t going to change, plain disk images have a lot going for them, and few disadvantages. Read speeds are close to those you’d expect of the host storage, and they’re straightforward to use.

Disadvantages

Where disk images become more dubious is in situations that you want to write to them, particularly if you want them to make efficient use of space by growing and shrinking in size. There are two formats of disk image intended to support such use:

  • sparse image, a flat file that has variable size, UDSP;
  • sparse bundle, a bundle that has variable size, UDSB.

Both will happily support changing files, and grow with their contents up to their intended maximum size. Sparse images and bundles don’t automatically recover space when contents are deleted. To do that, you’ll need to compact them with a utility or hdiutil. Generally, sparse bundles compact better than sparse images, but both types can accumulate lost space over time, and in the longer-term can need their contents copied to a new disk image to restore their space efficiency.

Write performance is also significantly worse than that of the host storage, and can vary substantially. In tests I carried out last summer, sparse bundles performed much better than sparse images. Write speed is worst when a disk image has been freshly made and hasn’t yet been unmounted; once written to and unmounted for the first time, it usually improves noticeably. Adding encryption to either format inevitably worsens write speed too. In some tests, write speed was as little as 1% of that of the host storage.

APFS volumes

If you don’t need the data to be mobile, and it can be stored on a local disk, it’s worth considering whether an additional APFS volume would be a better choice. This is particularly true for storing sensitive files encrypted: add an APFS (Encrypted) volume to a local disk, and it should be both more flexible and far quicker to write to.

In the past, before the advent of APFS, adding an encrypted HFS+ volume required repartitioning the disk, and those volumes had fixed sizes, unless you went back in and partitioned them again. Because APFS volumes can grow and shrink in size as freely as you want, and share space with other volumes in the same container, they’re more flexible, efficient and faster than any form of disk image.

The only compelling reasons for preferring a disk image are when you need to be able to move it around between Macs, or to host it on a NAS or other system that can’t support APFS natively.

Tools

  • Disk Utility is straightforward to use, but exposes relatively few of the options available.
  • hdiutul is more extensive, but far more complex to use.
  • C-Command’s excellent DropDMG caters for almost every need, giving full access to nifty features such as background pictures and software licence agreements, used by many developers to distribute their products.
  • My own free Spundle only works with sparse bundles, but gives ready access to hdiutil features including the ability to set band file size.