Manipulating disk images with hdiutil

Let's have fun with HDUTIL(1). Ok the word "fun" is maybe too strong. Let's play with hdiutil to create and manipulate disk images.

Hdiutil is an command line utility to manipulate disk images on MacOS X. To use hdiutil you type at the shell prompt:
$ hdiutil action_verb [options]
$ hdiutil action_verb [options]

Common action verbs are attach, detach, verify, create, convert, and burn. In this article we are going to use hdiutil to backup our data.

Let's walk through hdiutil with a concrete example. Everyone knows backup is an important task and should be done on the regular basis. If I don't have a fancy backup tool hdituil can help you to do this task.

I have to backup my Documents directrory. I can use hdiutil to make a disk image of that directory.

$ hdiutil create -srcfolder ~/Documents /tmp/documents.dmg
......................................................................
created: /tmp/documents.dmg
$ ls -lh /tmp/documents.dmg
-rw-rw-r--   1 fred  wheel       484M Oct 20 11:32 /tmp/documents.dmg

$ hdiutil create -srcfolder ~/Documents /tmp/documents.dmg
......................................................................
created: /tmp/documents.dmg
$ ls -lh /tmp/documents.dmg
-rw-rw-r-- 1 fred wheel 484M Oct 20 11:32 /tmp/documents.dmg

After a while, depending on the size of the source directory the disk image is created. Here in our example the resulting disk image is stored in a file named /tmp/documents.dmg his size is 484M.

Using hdiutil attach, you can now mount this image. A new disk volume icon named Documents will appear in your desktop.

$ hdiutil attach -noverify /tmp/documents.dmg
/dev/disk3              Apple_partition_scheme         
/dev/disk3s1            Apple_partition_map            
/dev/disk3s2            Apple_HFS                     /Volumes/Documents

$ hdiutil attach -noverify /tmp/documents.dmg
/dev/disk3 Apple_partition_scheme
/dev/disk3s1 Apple_partition_map
/dev/disk3s2 Apple_HFS /Volumes/Documents

The option -noverify tell hdiutil to attach the volume without verifying it first.

To detach and eject this volume just drag the icon into the trash bin. You can also use hdiutil with the verb detach to do that.

$ hdiutil detach /Volumes/Documents
"disk3" unmounted.
"disk3" ejected.

$ hdiutil detach /Volumes/Documents
"disk3" unmounted.
"disk3" ejected.

You can also use hdutil to burn this image on a CDRom or a DVD. That's pretty simple just use the verb burn and insert a blank media at the prompt. If you specify -noverify hdiutil will skip the verification at the and and cut the time need to burn the media by almost half.

$ hdiutil burn /tmp/documents.dmg
Please insert a disc: 
Preparing data for burn
Opening session
Opening track
Writing track
............................................................................
Closing track
............................................................................
Closing session
Finishing burn
Verifying burn...
Verifying
............................................................................
Burn completed successfully
............................................................................
hdiutil: burn: completed

$ hdiutil burn /tmp/documents.dmg
Please insert a disc:
Preparing data for burn
Opening session
Opening track
Writing track
............................................................................
Closing track
............................................................................
Closing session
Finishing burn
Verifying burn...
Verifying
............................................................................
Burn completed successfully
............................................................................
hdiutil: burn: completed

If you want more information on HDUTIL(1) you can read the MacOS X man pages.
 

Leave a message

(Required)
(Required and not displayed)
(Optional)
obfuscated letters Enter the text shown in the image