Introduction
A .dmg file is a disk image for macOS.
Most Mac software downloaded from the web comes as a DMG: opening it mounts a virtual
disk in the Finder, and the app is dragged from there to the Applications folder.
The format is Apple’s Universal Disk Image Format (UDIF). An image can be read-only or writable, compressed with one of several methods, and encrypted with a password. Apple has not published UDIF. Everything here comes from independent documentation and from reading real images: chiefly Jonathan Levin’s “Demystifying the DMG File Format”, and the sources of dmg2img, libdmg-hfsplus and 7-Zip.
Structure
A UDIF image is a virtual disk of 512-byte sectors, stored as runs. Each run of
sectors is stored raw or compressed on its own, and a few kinds of run store nothing
at all. The file is read from the end. Its last 512 bytes are a trailer, which starts
with the signature koly and says where everything else is.
The trailer’s fields are big-endian. They give the position and length of the
data fork (the runs), and of an XML property list,
and the disk’s size in sectors. The property list’s blkx array holds one
entry for each partition, and each entry’s Data element is a
block table in base64: a header that starts with mish,
then one 40-byte record for each run. Each run record has a type, a sector range and
the offset and length of the run’s bytes in the data fork.
0Data fork the disk’s sectors, as runs, each stored raw or compressedvaries…Property list XML, with ablkxblock table per partitionvariessize−512Trailerkoly, offsets, lengths, checksums, sector count512 bytes
To read a sector: find which partition’s table covers it, find the run in that table, read the run’s bytes from the data fork, decompress them, and take the sector from the result. Compressed runs are small, usually one mebibyte each, so a reader decompresses only what it needs.
Compression methods
A run says how it is stored. An image usually uses one method for all its data, named in the format code hdiutil reports:
| Format | Run data |
|---|---|
UDZO | zlib |
UDBZ | bzip2 |
ULFO | LZFSE |
ULMO | LZMA, in the xz container |
UDCO | Apple Data Compression (ADC) |
UDRO | raw |
The rest of the disk is runs that are zeros, or unused space that stores nothing. Many images mix several: a zlib partition can have raw runs for data it could not compress.
What holds the data
The decompressed disk is an ordinary disk: usually a partition map, Apple Partition
Map on older images and GUID Partition Table on newer ones, and a partition holding
an HFS+ or APFS volume. Each table in the plist is one partition of that map.
Software from hdiutil create and from Disk Utility is a device image of
this shape.
Encrypted images
An encrypted image wraps the whole disk in encryption with a password chosen when it
was made. Version 2 images start with the eight bytes encrcdsa. Version 1
images have cdsaencr at the very end of the file instead. Without the
password only the header can be read. The explorer asks for the password and, given
the right one, decrypts the blocks and opens the disk image inside; the key
derivation, the key blob and the per-block IV rule are described in
Encrypted images.
Other things named .dmg
Not every .dmg is UDIF. A file with no koly trailer can be
a raw disk image with the partition map or file system at its start, which hdiutil
reports as raw read/write. A sparse image (hdiutil format UDSP) starts with
sprs, and an old image in New Disk Image Format (NDIF) keeps its block
map in a resource fork, which only a Mac file system preserves. See
Sparse and segmented images.
In the explorer
Drop a .dmg on this site to look inside it. Nothing is uploaded, and only
the parts of the file that are needed are read. The Runs tab shows each run’s
compression, ratio and what lies under it, and each run opens as the sectors it
decompresses to. The Files tab lists the folders and files of HFS+, APFS, ISO 9660 and
FAT volumes, and each opens as a file of its own, with file-system compression
undone and resource forks and hard links followed. An APFS container shows its
volumes and snapshots. See Volumes.
Not described
This reference is incomplete, because Apple has not published UDIF. The parts below are not yet described, or not yet read by the file explorer, which shows them as they are, without a claimed meaning.
Known, not yet covered
- Viewer: the contents of encrypted APFS volumes (the explorer says the volume is encrypted and shows no files), snapshots beyond their names and dates, and exFAT and NTFS volumes (identified only).
- Viewer: a file system with more than 2,000,000 files and folders (HFS+, APFS, ISO 9660 or FAT) is listed only as far as its first 2,000,000; the file list then starts with a line that says so.
- Viewer: sparse bundles and the other parts of segmented images, which are separate files, and old NDIF images, which need their resource fork.
- Viewer: an encrypted image locked with a certificate instead of a password; and a decrypted image over 256 MB, which shows its header but is not opened. See Encrypted images.
Unknown
- Reference: the trailer’s flags other than bit 0 (flattened) and bit 2 (internet-enabled, set by a removed hdiutil command), the run comment, and the block table’s buffer and descriptor counts.
- Reference: the contents of the
plst,cSum,nsizandsizeresources. - Reference: the sparse image’s undocumented fields.
- Reference and viewer: files compressed with LZBITMAP, which Apple has not published; the runs are named and their bytes shown.
- Reference: the version 1 encrypted header (
cdsaencr) and old version 2 key wrapping, which no image on hand could check. See Encrypted images.
Links
Software
- Finder – mounts a DMG file as a disk when it is opened (macOS)
- Disk Utility – creates and converts disk images (macOS)
- hdiutil – the command-line tool for disk images, built into macOS (macOS)
- 7-Zip – free archiver that extracts files from many DMG files (Windows)
Developers
- dmg2img – converts a UDIF image to a raw image (C)
- libdmg-hfsplus – reads and writes DMG and HFS+ (C)
Reference
- Demystifying the DMG File Format – Jonathan Levin’s description of UDIF
- Technical Note TN1150 – Apple’s specification of HFS+