Section 9.7. Disk Images


9.7. Disk Images

Disk images (files with a .dmg extension) have become a frequent part of life on Mac OS X. Originally created to store the block-for-block contents of a floppy disk for ease of duplication, the disk images created and used by Mac OS X are based on a format called Universal Disk Image Format (UDIF). UDIF allows the storage of the same partition tables, disk drivers, and volumes found on physical disks, which lets disk images serve as intermediaries in the duplication of any kind of disk, including CDs and DVDs.

Beyond their origin as a disk duplication formatand because of their ease of usedisk images are used by many software vendors, including Apple, as a distribution format instead of .sit (StuffIt), .tar, or .zip files. When you double-click a disk image (or use Disk Utility to open it), Mac OS X uses the contents of the image as the data for a disk that it mounts into the filesystem, which shows up in the Finder as shown in Figure 9-11; Figure 9-12 shows the same disk image in Disk Utility.

Figure 9-11. A disk image mounted in the Finder


9.7.1. Types of Disk Images

Because they can be used for many different purposes, disk images have several different types . These are:


Read/write disk image

A fixed-size disk image that doesn't just contain data but also allows data to be written to it. These disk images are useful if you want to have an encrypted data

Figure 9-12. A disk image mounted in Disk Utility

storage point, or if you want to set up the contents of a disk image before converting it to one of the other types. This kind of disk image occupies the same amount of space on a disk as its capacity.


Read-only disk image

A fixed-size disk image that only allows data to be read from it. This kind of disk image occupies the same amount of space on a disk as its capacity.


Compressed disk image

A read-only disk whose contents have been compressed, resulting in the size of the disk image being much less than its capacity. This is the preferred format for distributing software across the Internet.


CD/DVD master disk image

A disk image whose internal format uses either ISO-9660 or UDF so its contents can be directly burned to a CD or DVD. CD and DVD master disk images end with the .cdr extension, though you may also find the .iso extension in use.


Sparse disk image

A read/write disk image format that starts out smaller on disk than its original capacity. The disk image grows in size as needed up to either its capacity or the size of the disk it is located on, whichever is less. Sparse disk images end with the .sparseimage extension.

9.7.1.1. Encryption and disk images

One attribute that disk images don't share with their physical cousins is that all the data on a disk image can be encrypted. This enables you to store data on public disks that can't be accessed by anybody who doesn't have the proper password. For example, you could store valuable data on a keychain USB flash drive using an encrypted disk image with the knowledge that if somebody gains access to your keys, he won't be able to gain access to the data stored on the USB drive. These encryption features are also used by FileVault to protect your Home directory, if you enable this feature in System Preferences.

FileVault Implementation

Mac OS X's FileVault, which encrypts all the data in your Home folder, uses disk images under the covers to perform its magic. When you enable FileVault for a user, all the user's data is placed into an encrypted sparse disk image . When a user logs into the machine, the image holding her data is mounted into the /Users folder. When she logs out, the disk image is unmounted, making any data in the user's Home directory inaccessible.

When you enable FileVault for a user, the data that was in her original Home folder is erased. It is, however, the same kind of erase that happens when you drag files to the Trash. This means that any data that was in a user's Home folder before FileVault was enabled could potentially be recovered. The best time to enable FileVault for a user is before any sensitive data is placed into her Home folder.


9.7.2. Working with Disk Images on the Command Line

In addition to Disk Utility, Mac OS X provides the hdiutil command-line tool to work with disk images. This tool has a wealth of options to create and manipulate disk images, many of which venture into very arcane territory. Some of the more commonly used features of hdiutil are covered here, so you can see how to manipulate disk images from the command line.

To mount a disk image from the Terminal, use the following command:

     hdiutil mount imagename

This command mounts the contents of the disk image into the /Volumes folder and it appears as a disk in the Finder's Sidebar. Example 9-7 shows a disk image named FlickrExport-1.2.6.dmg being mounted.

Example 9-7. Mounting a disk image using hdiutil
 $ hdiutil mount FlickrExport-1.2.6.dmg /dev/disk2        /Volumes/FlickrExport-1.2.6

When you mount a disk image, you'll see several items that should be familiar from looking at disks. There is a series of slices to the disk, including a partition map. The output of the hdiutil command also gives you the filesystem location where the disk image was mounted.

You aren't limited to mounting disk images into the /Volumes folder. The following command can be used to mount the contents of a disk image into any directory in the filesystem:

     hdiutil mount -mountpoint directory imagename

The only catch here is that the directory you are mounting the disk drive into has to exist in the filesystem first. The contents of the directory that you are mounting the disk image into will also be hidden by the contents of the disk image. Example 9-8 shows an example of this command.

Example 9-8. Mounting a disk image into the filesystem
 $ hdiutil mount -mountpoint /Foo test.dmg Password: Initializing... Verifying... Checksumming DDM... DDM: verified CRC32 $70362E53 Checksumming Apple (Apple_partition_map : 0)... Apple (Apple_partition_map : 0): verified C RC32 $DFA2282D Checksumming (Apple_Free : 1)... (Apple_Free : 1): verified CRC32 $00000000 Checksumming Apple_HFS_Untitled_2 (Apple_HFS : 2)... .............................................................................. Apple_HFS_Untitled_2 (Apple_HFS : 2): verified CRC32 $2877B09E Checksumming (Apple_Free : 3)... (Apple_Free : 3): verified CRC32 $00000000 Verification completed... verified CRC32 $3CAEE635 Attaching... Finishing... Finishing... /dev/disk5 Apple_partition_scheme /dev/disk5s1 Apple_partition_map /dev/disk5s3 Apple_HFS /Foo

To unmount a disk image, use the following command:

     hdiutil unmount mountpoint

Here, the mountpoint is the path of the folder where the disk image is mounted.

9.7.3. Creating a New Disk Image

To create a new empty read/write disk image using Disk Utility:

  1. Use Disk Utility's File New Blank Disk Image menu, or the New Image button on Disk Utilitys toolbar. A dialog window opens, as shown in Figure 9-13.

  2. Select the size of the disk image. This size governs the disk image's capacity. As with hard drives, keep in mind that the size of a disk image doesn't equal how much data it can actually hold because of filesystem overhead.

  3. Select whether you want to encrypt the disk image.

  4. Select the kind of disk image to create. When you create a new disk image, you have your choice of a read/write disk image or a sparse disk image. For most purposes, the standard read/write disk image is appropriate.

Figure 9-13. Options for creating a disk image


When you hit the Create button, a disk image is created and mounted in the Finder and into the filesystem in /Volumes. By default, the disk image contains a single HFS+ volume. To change the format of the volume to HFS or to UFS, simply highlight the volume in Disk Utility and use the Erase tab, just as you would with an actual physical disk.

To create a disk image on the command line, use the following hdiutil command:

     hdiutil create -volname volumename -fs fstype -size size imagename

where volumename is the name of volume you want to create, fstype is one of the filesystem types listed in Table 9-2, and size is the size of the disk image expressed as a number followed by either b (which stands for sectors, not bytes), k (for kilobytes), m (for megabytes), g (for gigabytes), t (for terabytes), p (for pentabytes), or e (for exabytes).

Table 9-2. Filesystem type codes used by hdiutil

Filesystem type code

Description

HFS+J

Journaled HFS+

HFS+

HFS+ (non-journaled)

HFS

Original HFS

MS-DOS

The FAT filesystem used by MS-DOS

UFS

The Unix filesystem (Berkeley Fast File System)


Example 9-9 shows how to create a 10 MB disk image.

Example 9-9. Creating a new disk image
 $ hdiutil create -volname Foo -fs HFS+ -size 10m foo.dmg Initializing... Creating... ............................................................................... Finishing... created: /Users/duncan/foo.dmg

To create an encrypted disk image, add the -encryption argument to the command. Applied to the command in Example 9-9, it looks like:

     hdiutil create -encryption -volname Foo -fs HFS+ -size 10m foo.dmg

By default hdiutil create creates read/write disk image files. Creating other kinds of disk image files requires the use of the -format argument along with a four-letter code. The complete list of codes is given in Table 9-3. For example, to create a sparse disk image, you would use the following command:

     diskutil create -format UDSP -voname Foo -fs HFS+ size 10m foo.dmg

Table 9-3. diskutil disk image format codes

Code

Description

UDRW

Read/write

UDRO

Read-only

UDZO

Compressed

UDTO

DVD/CD master


9.7.4. Creating a Disk Image from a Folder

To create a disk image that holds the contents of a folder using Disk Utility:

  1. Use Disk Utility's File New Disk Image From Folder, or the New Image button on Disk Utilitys toolbar. An Open dialog window appears, giving you the chance to select a folder to create the disk image. Select the folder you want to create an image from and hit the Open button.

  2. A dialog window, confusingly titled Convert Image, opens, letting you set the options for the disk image, as shown in Figure 9-14.

  3. Select the kind of image to create. Since you are creating an image with data, you can select from read-only, compressed, DVD/CD master, and read/write.

  4. Select whether you want to encrypt the contents of the disk image.

When you hit the Create button, the disk image is created. Unlike when you create an image, it will not be mounted.

Figure 9-14. Options for creating a disk image from a directory


To create a disk image from a folder on the command line, use the following hdiutil command:

     hdiutil create -srcfolder folder imagename

where folder is the path to the folder that you want to image and imagename is the name of the resulting image. Example 9-10 shows the creation of a disk image from the Documents directory in the Home directory.

Example 9-10. Creating a disk image with the contents of a directory
 $ hdiutil create -srcfolder ~/Documents Documents.dmg ................................................................ created: /Users/jldera/Documents.dmg

By default, the disk images that hdiutil creates from a directory are read-only. To create a read/write disk image, use the following syntax for the hdiutil command:

     hdiutil create -srcfolder folder -format UDIF imagename

9.7.5. Creating a Disk Image from a Device

Since disk images can hold all the data on a disk drive, including the partition map, it's natural that you can create a disk image directly from either a disk drive or a volume on it. By creating a disk image from a drive, you can later restore that image back onto the drive or onto any other drive. You can use this for backup purposes or to create a master disk image for a department or company, making it easy for you to clone one installation onto every machine. To create a disk image from a disk drive using Disk Utility, select the drive and then select File New Disk Image from diskname from the menu bar. Here, diskname is the name of the volume youve selected in Disk Utility's left pane.

Keep in mind that if you want to clone the drive or partition that contains your installation of Mac OS X, you shouldn't use Disk Utility to accomplish this task while the system is running. Instead, you should either boot from Tiger's Install DVD, or put your Mac into target mode and mount its drive(s) via FireWire on another system.


Creating an image of a device from the command line is a bit trickier. There isn't a direct command in hdiutil, so you have to use the convert command and convert from the disk's device in the /dev directory. Example 9-11 shows how to create a compressed disk image of an external FireWire drive mounted at /dev/disk1. Since this command accesses the raw device of the disk, the command must be run by a user with administrative privileges.

Example 9-11. Creating a disk image from a device
 $ sudo hdiutil convert /dev/disk1 -format UDZO -o /FirewireBackup.dmg Preparing imaging engine... Reading DDM... (CRC32 $70362E53: DDM) Reading Apple_partition_map (0)... (CRC32 $DFA2282D: Apple_partition_map (0)) Reading Apple_Free (1)... (CRC32 $00000000: Apple_Free (1)) Reading Apple_HFS (2)... .............................................................................. (CRC32 $15DC1C94: Apple_HFS (2)) Reading Apple_Free (3)... ............................................................................... (CRC32 $00000000: Apple_Free (3)) Terminating imaging engine... Adding resources... ............................................................................... Elapsed Time: 11.784s (1 task, weight 100) File size: 1433395 bytes, Checksum: CRC32 $43A4EE39 Sectors processed: 58605120, 62161 compressed Speed: 2.6Mbytes/sec Savings: 100.0% created: FirewireBackup.dmg

9.7.6. Converting a Disk Image

Once you have worked with a disk image in one format, such as read/write, you may want to convert it to read-only format, so that it is compressed and ready to post to the Internet, or convert it into a CD master that is ready for duplication. To convert an image using Disk Utility, use the Images Convert menu option, select the disk you want to convert, and then use the Covert Disk dialog box that appears to select the kind of image you want to convert it to. This dialog box is shown in Figure 9-15.

Figure 9-15. Converting a disk image


To convert a disk image on the command line, use the hdiutil convert command as follows:

     hdiutil convert -format format -o outputfile imagefile

where format is the four-letter code that the disk image will be converted to, outputfile is where the new disk image will be saved, and imagefile is the disk image to convert. Example 9-12 shows how to use this command to convert a disk image to a compressed disk image.

Example 9-12. Converting a disk image to a compressed disk image
 $ hdiutil convert -format UDZO -o Compressed.dmg Documents.dmg Preparing imaging engine... Reading DDM... (CRC32 $EEB3C0C1: DDM) Reading Apple_partition_map (0)... (CRC32 $5A2C9F73: Apple_partition_map (0)) Reading Apple_HFS (1)... .............................................................................. (CRC32 $C7097C7F: Apple_HFS (1)) Reading Apple_Free (2)... ............................................................................... (CRC32 $00000000: Apple_Free (2)) Terminating imaging engine... Adding resources... ............................................................................... Elapsed Time: 23.061s (1 task, weight 100) File size: 48394357 bytes, Checksum: CRC32 $79BEA1E4 Sectors processed: 258898, 238321 compressed Speed: 5.0Mbytes/sec Savings: 63.5% created: Compressed.dmg

9.7.7. Burning a Disk Image to CD or DVD

Burning an image to CD or DVD from Disk Utility is simple; just select the disk image and click the Burn button. Make sure that the image doesn't contain more data than the disc you are burning to can hold. CDs can hold up to 700 MB and DVDs can hold up to 4.7 GB.

Burning to disc is just about as easy from the command line. Simply use the hdiutil burn command as shown in Example 9-13.

Example 9-13. Using the hdiutil burn command
 $ hdiutil burn 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

9.7.8. Restoring a Disk Image to a Drive

To restore the contents of a disk from a disk image, use Disk Utility's Restore panel, as shown in Figure 9-16, and use the following process:

Figure 9-16. Using Disk Utility to restore a disk image to a drive


  1. Either drag a disk image to the Source text field or click the Image button and select the image you want to restore from.

  2. Drag the disk that you want to restore to from the left column to the Destination text field.

  3. Optionally, check the Erase Destination checkbox if you want to replace the contents of the disk with the disk image.

  4. Click the Restore button.

You should be aware that it is possible to restore the contents of a drive to another drive that is either larger or smaller than the original drive. As long as there is enough space on the target drive to hold the data being moved to it, Disk Utility will take care of the rest.


9.7.9. Moving the Contents of One Drive to Another

You can use the Restore panel of Disk Utility to copy one disk to another. This is useful when you want to move a perfect copy of your boot disk to another drive. For example, you could copy a disk to an external FireWire drive and then use that drive as a master disk image for restoring the disk onto other machines. To do this using the Restore panel, select a disk as the source instead of a disk image.

This feature is especially handy when you want to upgrade the boot drive in a machine. For example, if you wanted to upgrade the hard drive in your PowerBook, you could use the following process:

  1. Boot your system using the Mac OS X Install DVD. Instead of proceeding with the install, use the Utilities Disk Utility menu option to launch Disk Utility.

  2. Click the Restore tab of Disk Utility (refer to Figure 9-16).

  3. Set the Source of the restore to your internal boot drive.

  4. Set the Destination of the restore to your external FireWire drive.

  5. Click Restore and go have a coffee at your local coffee shop while Disk Utility copies the data from the internal drive to the external.

  6. When Disk Utility is done, exit the Installer and power down your system.

  7. Replace the drive in your computer. For help, you should refer to Apple's documentation for your model of Mac.

  8. Once the new drive is installed, boot your system with the Mac OS X Install DVD again. And once again, launch Disk Utility.

  9. Verify that the new disk drive appears in the drive list. Make sure that it is formatted to use Journaled HFS+.

  10. Using the Restore tab, make the external FireWire drive the Source and the new internal hard drive the Destination.

  11. Click Restore and go have some more coffee.

When Disk Utility finishes, you're almost done. You just need to check to make sure that your disk is set up to boot. Usually Disk Utility does the right thing if each disk has only a single partition on it, but when you are moving data around on disks with multiple partitions, things might not always work out so well. You can check the boot status of your drive with the Startup Disk tool. To open the Startup Disk tool, select Utilities Startup Disk. Here, make sure that the correct Mac OS install is chosen and then click the Restart button to boot the system and test out the restoration process.




Running Mac OS X Tiger
Running Mac OS X Tiger: A No-Compromise Power Users Guide to the Mac (Animal Guide)
ISBN: 0596009135
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net