Project67.Look after Your Disks


Project 67. Look after Your Disks

"Is there a command-line alternative to Apple's Disk Utility application?"

This project looks at the diskutil command. It's a command-line interface to Apple's Disk Management framework, providing the same functionality as the Disk Utility application. The project considers disk maintenance and presents examples verifying and repairing disks. Project 68 uses diskutil to mount and unmount local drives.

Take Command of Disks

We saw in Project 65 that there are Unix command-line equivalents to Apple's NetInfo Manager and the Accounts panel in System Preferences. There's also a Unix equivalent to the OS X Disk Utility tool: the diskutil command. In this case, the command-line tool can't claim more features than in its graphical counterpart, but its extra value lies in its capability to work within extended Unix commands and scripts.

The diskutil command can partition, format, mount and unmount drives, and verify and repair file systems. It can be used to manage any locally mounted drive, including internal drives, USB and Fire Wire drives, as well as removable media, such as CDs and DVDs. Where the Disk Utility application (in Applications:Utilities) is a graphical interface to Apple's Disk Management framework, diskutil is the command-line equivalent.

Get information on diskutil by typing either of the following.

$ man diskutil $ diskutil


For information on a specific diskutil command (or verb)verifyVolume, for exampletype

$ diskutil verifyVolume


Learn More

Project 66 discusses file systems and includes a sidebar, "Mount Points," that compares Unix mount points with mounted volumes as seen by the OS X Finder.


You'll notice that most verbs operate on a volume. A volume may be identified by its mount point, disk identifier, or device node. We get this information by applying the verb list. Type

$ diskutil list /dev/disk0   #:                   type name          size   identifier   0: Apple_partition_scheme              *233.8 GB disk0   1:    Apple_partition_map                31.5 KB disk0s1   2:              Apple_HFS OSX-sauron    233.6 GB disk0s3 /dev/disk1   #:                   type name          size   identifier   0: Apple_partition_scheme              *149.1 GB disk1   1:    Apple_partition_map                31.5 KB disk1s1   2:              Apple_HFS Macintosh HD   19.8 GB disk1s3   3:              Apple_HFS Backup-sauron 129.0 GB disk1s5


Let's examine this output.

Information is displayed for every disk drive that's attached (even if it's not mounted); each listed disk is followed by an indented list of the disk's slices.

A disk is identified by its entry in the device directory /dev in the form /dev/diskn, where n is the disk number. Here, we see the main system disk (/dev/disk0) and an external FireWire drive (/dev/disk1).

If a disk is partitioned (sliced), each slice also has an entry in the device directory, identified by /dev/disknsm, where n is (still) the disk number, m is the slice number, and the s in between is just shorthand for "slice." The diskutil command does not show the full /dev entry, or device node, for each slice; instead, it uses a (slightly) shorter device identifier of the form disknsm (without the leading /dev/).

We see that the system drive has one partition named OSX-sauron. Its device node is /dev/disk0s3; its device identifier (shown in the identifier column on the right) is disk0s3. The FireWire drive has two partitionsMacintosh HD and Backup-sauronat slices 3 and 5, respectively.

Tip

The commands df -l and mount also list information on local drives, including device-to-mount point mappings. They consider only mounted drives, not drives that are attached but unmounted.


We obtain more information about a disk or a partition with the diskutil verb info. Here's how we might get information on the system partition (disk 0, slice 3).

$ diskutil info disk0s3   Device Node:       /dev/disk0s3   Device Identifier: disk0s3   Mount Point:       /   Volume Name:       OSX-sauron      File System:       Journaled HFS+                      Journal size 8192k at offset 0xa701000   ...   Ejectable:         No


We could also have typed:

$ diskutil info / $ diskutil info /dev/disk0s3


Note

Every disk partition occupies one slice, but not every slice is a partition. A slice may contain device drivers or the partition map. The terms partition and volume are interchangeable.


Verify and Repair a Volume

If you need to perform preventative disk maintenance, or report on the status of your disk drives, the diskutil command makes an ideal tool to incorporate into a shell script. Let's look at some examples that verify and repair a file system, and verify the OS X Unix permissions on the system volume.

Continuing on the system cited in the preceding examples, let's verify the file system on "Backup-sauron", which is disk1s5, using diskutil verb verifyVolume. The argument following the verb specifies the target volume, in this case by its mount point (/Volumes/Backup-sauron). We also could have used its device node, /dev/disk1s5, or device identifier, disk1s5. (You cannot specify an entire disk such as disk1; you must specify a particular volume.)

$ diskutil verifyVolume /Volumes/Backup-sauron/ Started verify/repair on volume disk1s5 Backup-sauron Checking HFS Plus volume. Checking Extents Overflow file. Checking Catalog file. Checking multi-linked files. Checking Catalog hierarchy. Checking volume bitmap. Checking volume information. The volume Backup-sauron appears to be OK. Mounting Disk Verify/repair finished on volume disk1s5 Backup-sauron


Tip

If a volume is not mounted, it can still be verified and repaired, but you must identify it by its disk identifier or device node.


If diskutil reports errors, you should attempt to repair the volume by typing:

$ sudo diskutil repairVolume disk1s5 Password: ...


If you try to repair a mounted volume, diskutil will first attempt to un-mount the volume, which will fail if the volume is in use (a file is open or your current working directory lies within the volume). The system volume is always in use: To verify it, you must boot from an alternative system on another partition or drive, boot from a Mac OS X install disk, or use a third party disk repair program. (Note that prior to Mac OS X 10.4.3, it was not possible to verify the system volume.)

Verify Permissions

Verify the permissions on the system volume or on any volume that contains a Mac OS X system.

$ diskutil verifyPermissions / Started verify/repair permissions on disk disk0s3 OSX-sauron Determining correct file permissions. We are using special permissions for the file or directory ./Library/Widgets. New permissions are 16877 Permissions differ on./Library, should be drwxrwxr-t, they are drwxrwxr-x Permissions differ on./private/var/log/secure.log, should be -rw-------, they are -rw-r----- We are using special permissions for the file or directory ./usr/lib/php/build/acinclude.m4. New permissions are 33060 The privileges have been verified or repaired on the Verify/repair finished permissions on disk disk0s3 OSX-sa...


Tip

A disk that is badly damaged may be beyond the repair capabilities of diskutil. It's not lost forever, however: Third-party disk-repair utilities try much harder to fix disks and save data. The cost of such a program is usually less than the value of the recovered data.


If problems are reported, reissue the command as repairPermissions.

Be Smart with diskutil

You can use diskutil to check any mounted drive's SMART status (described in the "Be Smart" section of Project 37). Let's do so, by employing the grep command to filter the relevant information from diskutil output. We are interested in lines that contain the text SMARTso that's the text we'll match with grep.

$ diskutil info disk0 | grep SMART    SMART Status:       Verified $ diskutil info disk1 | grep SMART    SMART Status:       Not Supported


Tip

To manage disk images or burn them to optical media, look at Apple's hdiutil tool. Use the drutil tool to burn CDs and DVDs.


We check both drives and find that the external FireWire drive does not support SMART, but the main system drive does. The system drive's status is Verified, which is good, and the drive is healthy. If it reported About to Fail, it would be time to back up the disk and replace it.




Mac OS X UNIX 101 Byte-Sized Projects
Mac OS X Unix 101 Byte-Sized Projects
ISBN: 0321374118
EAN: 2147483647
Year: 2003
Pages: 153
Authors: Adrian Mayo

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