Saturday, December 13, 2008

Using diskutil

The diskutil program is a native Apple command line utility for manipulating disks, partitions, and RAID sets. This includes magnetic hard disks, CDs/DVDs, and flash drives. Most options, except "list", require root access.

Finding out about disks in your system

For information on all available disks and their partitioning, use:
diskutil list

For more detailed information on a particular disk or partition, use:
diskutil info disk-or-partition

The default Apple partitioning scheme uses the last physical partition on a disk for storing data. Here is sample output from diskutil list showing a hard disk and a CD. The UNIX device name is shown first, along with the contents of each partition:

/dev/disk0
#: TYPE NAME SIZE IDENTIFIER
0: Apple_partition_scheme *74.5 Gi disk0
1: Apple_partition_map 31.5 Ki disk0s1
2: Apple_HFS Macintosh HD 74.4 Gi disk0s3
/dev/disk1
#: TYPE NAME SIZE IDENTIFIER
0: CD_partition_scheme *718.1 Mi disk1
1: Apple_partition_scheme 625.3 Mi disk1s1
2: Apple_partition_map 31.5 Ki disk1s1s1
3: Apple_HFS Dungeon Siege Disc 2 625.0 Mi disk1s1s2

Here is sample output from diskutil info on a disk partition:

diskutil info /dev/disk0s3
Device Identifier: disk0s3
Device Node: /dev/disk0s3
Part Of Whole: disk0
Device / Media Name: Untitled 3

Volume Name: Macintosh HD
Mount Point: /
File System: Journaled HFS+
Journal size 8192 KB at offset 0x256000
Owners: Enabled

Partition Type: Apple_HFS
Bootable: Is bootable
Media Type: Generic
Protocol: ATA
SMART Status: Verified
Volume UUID: EE8C4FFD-6C4B-302D-B096-DCF81D4E13FB

Total Size: 74.4 Gi (79892103168 B) (156039264 512-byte blocks)
Free Space: 26.8 Gi (28731383808 B) (56115984 512-byte blocks)

Read Only: No
Ejectable: No
Whole: No
Internal: Yes

Checking partitions for integrity and fixing them

You can use diskutil to check the file system data structure of a partition (e.g., /dev/disk0s3) with:
diskutil verifyVolume partition

If errors are you found, you can fix them with:
diskutil repairVolume partition

Checking partitions for UNIX permission problems and repairing them

You can use diskutil to check the UNIX permissions on a partition with:
diskutil verifyPermissions partition

If errors are you found, you can fix them with:
diskutil repairPermissions partition

If permissions get accidentally changed on some system files, it could cause strange behavior or disable certain features of the system.

Finding out about RAID sets

RAID is usually used in servers to provide additional protection from hard disk failure. For information on RAID sets, use:
diskutil listRAID

Other diskutil options

In addition to the options listed above, diskutil can be used to reformat disks or partitions, erase writable CDs/DVDs, securely erase data, etc. Here are some of the other features:

  • u[n]mount - Unmount a single volume
  • unmountDisk - Unmount an entire disk (all volumes)
  • eject - Eject a removable disk
  • mount - Mount a single volume
  • mountDisk - Mount an entire disk (all mountable volumes)
  • eraseDisk - Erase an existing disk, removing all volumes
  • eraseVolume - Erase an existing volume
  • reformat - Reformat an existing volume
  • eraseOptical - Erase an optical media (CD/RW, DVD/RW, etc.)
  • zeroDisk - Erase a disk, writing zeros to the media
  • randomDisk - Erase a disk, writing random data to the media
  • secureErase - Securely erase a disk or freespace on a volume
  • resizeVolume - Resize a volume, increasing or decreasing its size

See the man page for even more!