Disk Utility and diskutil

Disk Utility may seem the most mundane of all the utilities, but together with its command shell partners packs a real punch in several important areas.

If you seldom use Disk Utility, and then only for a quick check of disk integrity, or to repair permissions, you are missing out on its powers to package files up into disk images, burn optical disks, and more. If you know how to do those in Disk Utility, you should see what its Command Shell relatives – diskutil, hdiutil, hdid and drutil – can do for you.

Disk images

At their heart is the disk image, an essential tool for distributing software, collections of documents, or burning optical disks. Their versatility is reflected in the range of different types.

UDIF are the most widely used, being of fixed size, and can be read-write or read-only, uncompressed or use ADC, zlib or Bzip2 compression; thus a Bzip2-compressed image is known as ‘UDBZ’.

Special types of disk image include ‘UDTO’, which are CD-R or DVD-R masters for export, ‘UDSP’ sparse images that grow in physical size according to their contents, up to 128 petabytes maximum, and ‘UDSB’ sparsebundle images that are similar but backed by a directory bundle, up to just less than 8 exabytes.

Types now of historical interest for backward compatibility include ‘Rdxx’ that are read-only images accessible from Disk Copy 6.3.3, and ‘DC42’ for the even older Disk Copy 4.2. You may experience delicate compatibility issues when working with older systems: UDIF images created under OS X 10.4.7 or later will only be backward compatible if their metadata are flattened, whilst sparsebundle images created under 10.5 and later are not recognised by earlier versions of OS X.

Another special type is the ‘IDME’ image using the internet-enable verb of hdiutil; when downloaded, this is post-processed to copy the image’s visible contents into the folder into which the image is located, then the image is moved to the Trash with IDME disabled. For a period this was a popular method of delivering software and updates, but security concerns have led to its fall from favour.

Disk Utility

hdiutil may give you fullest access to all the features and foibles of disk images, but the more accessible Disk Utility should be able to cater for more everyday needs.

Disk Utility offers a lot of options when you create a new disk image.
Disk Utility offers a lot of options when you create a new disk image.

Create new disk images from its File menu, using the Blank Disk Image… command if you are happy to assemble the contents yourself, or either of the Disk Image From commands if they are more appropriate, perhaps if you simply want to put a complete folder or optical disk into the image.

New images can be created in many of the formats more fully available from the command line. Those for burning to optical disk should normally be sized to that of the optical medium being used, and small images can simply employ regular UDIF fixed-size format. If the image is larger, perhaps destined for a server or network storage, it is worth opting for either of the sparse formats, as they will grow to accommodate the files that you put inside them, but will only occupy the space that they need.

Unless you are creating a master image for optical disk, try to keep to Mac OS Extended (HFS+, journalled) volume format to ensure greatest compatibility and features.

You can also convert disk images between different formats, without resorting to the command line.
You can also convert disk images between different formats, without resorting to the command line.

Once created, a disk image will be attached, its volume(s) mounted and accessible in Disk Utility and the Finder. Strictly speaking, images are not themselves mounted, but are attached and detached, whilst the file system volumes contained in an image are mounted and unmounted. You can now copy files and folders over to the image in the Finder, or in Terminal, and a selection of other useful commands is provided in the Images menu.

When you have completed preparing the disk image, any of the regular means of ejecting it from Disk Utility, such as the Eject command in its File menu, suffices to unmount volumes and detach the image.

Using hdiutil and friends

Fixed size images can still be resized in Terminal using hdiutil’s resize verb, although to optimise an image before burning to optical media consider using diskutil resize first.

The create verb makes a new disk image of given capacity and type, attaching it automatically with the -attach option. At the outset you can use the -srcfolder option to copy file-by-file the entire contents of a folder into the image. For instance hdiutil create -srcfolder \~/Documents MyDocs.dmg creates a new fixed-size UDIF image and copies the contents of your Documents folder into it.

Read-write images can be of three types, specified by the -type option: UDIF (default), SPARSE, or SPARSEBUNDLE. The -fs option specifies the file system to be used in the image, normally HFS+, but can be enhanced with Joliet support, any of the HFS+ variants, MS-DOS, or UDF. If you want, the -layout option allows control over partitions, supporting multiple volumes within an image, although the default is now a GUID Partition Scheme map with a single entry, for compatibility with both PowerPC and Intel Macs.

To create images with hybrid file systems, use the makehybrid verb instead of create, which brings in features of the DiskRecording framework to make a read-only image from a source folder or existing disk image. This is versatile, supporting many different image types, including El Torito style bootable ISO 9660 disks for Intel hardware, blessed HFS+ partitions to boot Macs, Joliet and UDF file systems.

The segment verb segments an NDIF or UDIF image, for example allowing a very large image to be burned to a set of DVD-R disks.

The attach verb attaches an existing disk image as a device, normally mounting any contained file systems; indeed the mount verb is a synonym although mounting file systems can be prevented. Attachment can be read-only, or read-write, and the -nobrowse option makes the image invisible to the Finder.

Working with read-only images is facilitated by the -shadow option, creating a local shadow image, with default extension .shadow and read-write access enabled. You can thus make changes to the shadow, and write them out to an updated final version of the disk image, then made read-only. By default, when images are attached their integrity is also verified against any contained checksum, but that can be turned off.

The separate verb verify computes the checksum of read-only or compressed images and compares it against that stored in the image, whilst checksum computes the checksum of images of all types. You are spoilt for choice of checksum method, including variants of CRC-32, MD5, and SHA.

Disk image encryption is fully supported, by default using the AES cipher with a 128-bit key, or 256-bit if you prefer. Options available to recover keys from keychains, use access certificates, and to support public and private keys are elaborate and need careful study to use reliably. The udifrez verb allows you to embed resources such as the software licence agreement that you often see shown when a disk image is attached.

The convert verb converts a disk image from one type to another, writing out the conversion to a new image file. Sparse and sparsebundle images are optimised in size using the compact verb.

Finally the detach verb removes an attached image, and normally (as Disk Arbitration services are running) unmounts its volumes automatically; this can be forced so that open files are ignored.

hdiutil commands are designed to be embedded in other software. They can generate output in XML property list format rather than text, and the -puppetstrings option delivers progress report output for commands that take time to complete.

Although the hard disk image backing store service hdid is mainly for backward compatibility, it has the unusual ability to mount images over HTTP. For instance, the command hdid http://www.myserver.co.uk/files/myimage.dmg can mount the specified image from a web server, although servers normally limit the size that can be used; Apache 1.x constrains images to 2 GB, Apache 2.x to 4 GB. Web servers also need to be specially tuned to overcome resulting performance issues.

Having created a disk image, hdiutil burn offers comprehensive options to burn data disks using optical burner drives, although lacking support for Audio format. You can constrain burning speed with the -speed option, which can be useful if you suspect problems with media.

Direct access to both internal and external optical burners is even more extensive in the drutil command, which uses the DiskRecording framework. This allows you to specify which burner to use, for example -drive internal, or you can use multiple drives simultaneously.

Its burn verb encompasses a fuller range of disk types and formats, including Red Book Audio, and content can include file lists, folders, and images (including .dmg and .iso types). To obtain an estimate of the burned size of a given file path using a specified format, use its size verb. Those who need to bulk erase RW media will also find the bulkerase quick or full verbs of value.

This whistlestop tour of the vast range of features accessible from the command line is best supported by online documentation, using the normal man command for each of hdiutil, diskutil, hdid, and drutil.

Technique: RAM Disks

The idea behind a RAM disk is simple: set aside an area of memory to act as if it is a very high-performance disk. Before the advent of virtual memory, this made a lot of sense, and back in the days of Mac OS 7 was an effective trick for improving performance in many circumstances if your Mac had ample memory.

Now that OS X has sophisticated virtual memory, which automatically and intelligently caches what should be in physical memory to hidden files on your hard disk, RAM disks seem at best naive if not positively disadvantageous. However in the right circumstances a RAM disk can still bring worthwhile performance improvements, for example when accessing many different disk files intensively.

Until the advent of OS X 10.5, making RAM disks was messy. A typical script to create and mount one of 63 MB size might have read:
mydev=`hdid -nomount ram://$NUMSECTORS`
newfs_hfs $mydev
mkdir /tmp/mymount
mount -t hfs $mydev /tmp/mymount

Thankfully this has become much simpler in Leopard and later, where the single command
diskutil erasevolume HFS+ “ramdisk” `hdiutil attach -nomount ram://128000`
will do the same job, as will a tool like RAM Disk Creator.

Once created and mounted, access and test this RAM disk as if it were any other volume. However its big drawback, of course, is that it is transient. If your Mac suffers a kernel panic or a power outage, the contents of your RAM disk are blown away, and they do not survive even orderly restarting.

If you decide that a particular task can be accelerated using a RAM disk, you need to consider these issues, try periodically writing the contents of the RAM disk to a disk image using hdiutil, although that in itself will have performance effects.

Updated from the original, which was first published in MacUser volume 29 issue 08, 2013.