hdiutil - macOS


Overview

hdiutil is a command-line utility on macOS that manages disk images. Its primary functions are to create, convert, compress, and mount disk images. Common use cases include software distribution, system backup, secure data exchange, and creating bootable images. Its versatility makes it a valuable tool for system administrators and power users.

Syntax

The general syntax for hdiutil is as follows:

hdiutil <verb> [options] <parameters>

<verb> represents what operation you intend to perform, such as attach, detach, create, convert, etc. The options and parameters you include will vary depending on the verb used.

Options/Flags

This section highlights the most frequently used options in hdiutil:

  • -attach <image>: Mounts the specified disk image.
  • -detach <device>: Unmounts the specified disk image.
  • -create: Creates a new disk image.
  • -convert <source> -format <format>: Converts a disk image to a specified format.
  • -resize -size <size>: Resizes an existing disk image.
  • -verify <image>: Verifies the structure of a disk image.
  • -comp <compressionType>: Specifies the compression type (e.g., UDZO for zlib-compressed images).

Examples

Create a Blank Disk Image

hdiutil create -size 500m -volname "SampleVolume" -fs HFS+ -type SPARSE sample.dmg

Convert a Disk Image to Compressed Format

hdiutil convert input.dmg -format UDZO -o compressed.dmg

Mount a Disk Image

hdiutil attach sample.dmg

Verify a Disk Image

hdiutil verify sample.dmg

Common Issues

  • Permission Denied: Ensure you have the necessary permissions to access or modify a disk image.
  • Format Not Recognized: Verify that the disk image is not corrupted and that the format is supported by your macOS version.
  • Disk Image Busy: This usually occurs when trying to detach a disk image that is in use. Make sure to close all applications that might be using the disk image before detaching.

Integration

hdiutil can be combined with other commands for automated workflows:

# Create, mount, use, and unmount an image in a script
hdiutil create -size 100m -fs HFS+ -volname "ScriptDisk" script.dmg
mounted=$(hdiutil attach script.dmg | grep "Volumes" | cut -f 3)
cp mydata "$mounted"
hdiutil detach "Volumes/ScriptDisk"
  • diskutil: Used for disk and volume management on macOS.
  • asr: Apple Software Restore, useful for disk cloning.
  • dd: A low-level data duplication utility.

For further details and documentation, the man page (man hdiutil) on macOS is a comprehensive resource, or visit the Apple developer documentation.