Managing Local Disks and Volumes Using `diskutil` (with examples)
- Osx
- December 17, 2024
The diskutil
command is a powerful utility on macOS systems designed to manage local disks and volumes. This tool allows users to perform various operations related to disk management, making it an essential command for system administrators and power users. With diskutil
, you can list disks, repair filesystems, unmount volumes, and even eject CDs or DVDs, among other tasks. Below, we illustrate some practical examples to demonstrate the capabilities of diskutil
.
Use case 1: Listing All Currently Available Disks, Partitions, and Mounted Volumes
Code:
diskutil list
Motivation:
The ability to list all available disks, partitions, and mounted volumes is extremely useful, especially when managing multi-drive systems or troubleshooting storage issues. Identifying the structure of connected storage devices allows users to make informed decisions about modifying or repairing volumes.
Explanation:
diskutil
: The main command, used to manage disk and volume tasks.list
: A subcommand that provides a detailed list of all disks, their partitions, and mounted volumes on the system.
Example Output:
Upon execution, the command outputs information about each disk, such as:
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *512.1 GB disk0
1: EFI EFI 209.7 MB disk0s1
2: Apple_APFS Container disk1 511.8 GB disk0s2
This output details the disk identifier, type, name, size, and partition scheme.
Use case 2: Repairing the Filesystem Data Structures of a Volume
Code:
diskutil repairVolume /dev/disk_device
Motivation:
Repairing the filesystem of a volume is a critical task when you suspect data corruption or errors are impeding system performance. Periodically running a repair can help maintain system stability and data integrity.
Explanation:
diskutil
: The overarching command for disk operations.repairVolume
: A subcommand instructingdiskutil
to attempt repairs on the filesystem data structures of the specified volume./dev/disk_device
: A placeholder representing the specific disk or volume you wish to repair. Replace this with the appropriate device identifier found usingdiskutil list
.
Example Output:
The command will output a series of status messages indicating the progress and result of the repair process, such as:
Started file system repair on disk2s1 MacBook
Checking file system
Checking Journaled HFS Plus volume
Checking extents overflow file
Checking catalog file
Checking multi-linked files
Checking catalog hierarchy
Checking extended attributes file
Use case 3: Unmounting a Volume
Code:
diskutil unmountDisk /dev/disk_device
Motivation:
Unmounting a volume is necessary when you need to safely disconnect a drive from the system, perform disk maintenance, or prevent further data writes to a disk that is about to be repaired or reformatted.
Explanation:
diskutil
: The primary command for managing disks and volumes.unmountDisk
: A subcommand to unmount all volumes on the specified disk./dev/disk_device
: Specifies the disk or volume that should be unmounted. Use the correct identifier fromdiskutil list
.
Example Output:
The command returns a confirmation such as:
Unmount of all volumes on /dev/disk2 was successful
Use case 4: Ejecting a CD/DVD
Code:
diskutil eject /dev/disk_device1
Motivation:
Ejecting a CD/DVD ensures data integrity and prevents any running processes from accessing the drive. This is particularly important before removing physical media to avoid corruption.
Explanation:
diskutil
: Core command for disk management operations.eject
: A subcommand dedicated to safely ejecting media from a drive./dev/disk_device1
: Represents the device identifier for the CD/DVD drive. Check the listing fromdiskutil list
to use the correct identifier.
Example Output:
Execution will yield a result of confirmation like:
Disk /dev/disk3 ejected
Conclusion:
The diskutil
command is an essential utility under macOS for handling a wide array of disk management tasks. From listing available volumes to repairing and unmounting them, diskutil
provides the tools needed to maintain and troubleshoot disk-related issues. Whether you are engaged in routine maintenance or resolving a specific problem, understanding these use cases prepares you for whatever disk management challenges may arise.