How to Use the 'diskutil partitionDisk' Command (with Examples)
- Osx
- December 17, 2024
The diskutil partitionDisk
command is a utility on macOS systems that allows users to manage disk partitions. With this command, users can format volumes, partition disks, and list supported filesystems. It provides flexibility in setting partition schemes like APM (Apple Partition Map), MBR (Master Boot Record), and GPT (GUID Partition Table) depending on the user’s needs and the targeted operating system compatibility. Let’s dive into the various use cases showing how this command can be practically applied.
Use case 1: Reformat a Volume Using APM/MBR/GPT Partitioning Scheme
Code:
diskutil partitionDisk /dev/disk_device 0 APM|MBR|GPT
Motivation:
Sometimes a disk needs to be prepared for a fresh environment. For example, a user might want to completely erase and repartition a drive before setting up a new operating system. The ability to remove all partitions signifies starting with a clean slate, making it ideal in cases where previous data and configurations need to be discarded.
Explanation:
/dev/disk_device
: Specifies the target disk device which will undergo reformatting. It’s crucial to identify the correct disk using commands likediskutil list
to avoid mistakenly formatting the wrong disk.0
: Denotes that no partitions will be created after reformatting. The disk will essentially be left as unallocated space.APM|MBR|GPT
: The partitioning scheme to implement. APM is specifically for older macOS systems, MBR for older DOS and Windows systems, and GPT for modern systems offering advanced features like larger disk sizes and more partitions.
Example Output:
Started partitioning on disk /dev/disk_device
Unmounting disk
Creating the partition map
Waiting for the disks to reappear
Finished partitioning on disk /dev/disk_device
Use case 2: Reformat a Volume and Create a Single Partition Filling All Free Space
Code:
diskutil partitionDisk /dev/disk_device 1 APM|MBR|GPT partition_filesystem partition_name
Motivation:
A straightforward approach to allocating an entire disk to a particular filesystem. This is useful when a user wants maximized storage for a specific purpose, like dedicating a drive for media storage in a compatible filesystem format.
Explanation:
1
: Suggests that only one partition is to be created, utilizing all available space on the disk.partition_filesystem
: Specifies the desired filesystem for this new partition such asHFS+
,APFS
, orexFAT
. This choice often depends on system requirements and compatibility needs.partition_name
: The label or name for the created partition, which might be used for easy identification.
Example Output:
Started partitioning on disk /dev/disk_device
Unmounting disk
Creating the partition map
Partition 1 created with format HFS+ and named PartitionExample
Waiting for the disks to reappear
Finished partitioning on disk /dev/disk_device
Use case 3: Reformat a Volume and Create a Single Partition with a Specific Size
Code:
diskutil partitionDisk /dev/disk_device 1 APM|MBR|GPT partition_filesystem partition_name partition_size
Motivation:
This use case is advantageous when specific disk space management is needed. Users might reserve parts of a disk for different roles, ensuring they control the space allocation precisely. This allows balancing storage capacity across different needs, like separating system files from general data storage.
Explanation:
partition_size
: Denotes how much space the partition will take. It can be expressed in sizes like16G
for 16GB, or percentages like50%
, offering flexibility in disk management while ensuring space optimization.
Example Output:
Started partitioning on disk /dev/disk_device
Unmounting disk
Creating the partition map
Partition 1 created with format APFS and named Data with size 16GB
Waiting for the disks to reappear
Finished partitioning on disk /dev/disk_device
Use case 4: Reformat a Volume and Create Multiple Partitions
Code:
diskutil partitionDisk /dev/disk_device 2 APM|MBR|GPT partition_filesystem1 partition_name1 partition_size1 partition_filesystem2 partition_name2 partition_size2
Motivation:
In situations where a user might want to segregate the usage of a disk, creating multiple partitions is immensely useful. For instance, one might reserve one partition for system files and another for personal data or backups. This strategy enhances management and organizational efficiency of data.
Explanation:
number_of_partitions
: Indicates that multiple partitions will be created. Users need to specify details for each partition.partition_filesystemX
: The desired filesystem for each respective partition, catering to various use cases for differing storage demands.partition_nameX
: Names designated for each partition, useful for organizing and identifying the role of each partition.partition_sizeX
: The size specification for each partition, allowing allocation that respects the specific functions and space needs.
Example Output:
Started partitioning on disk /dev/disk_device
Unmounting disk
Creating the partition map
Partition 1 created with format HFS+ and named System with size 100GB
Partition 2 created with format exFAT and named Backup with size 200GB
Waiting for the disks to reappear
Finished partitioning on disk /dev/disk_device
Use case 5: List All Supported File Systems for Partitioning
Code:
diskutil listFilesystems
Motivation:
Knowing what filesystems are supported can guide users in making informed decisions about disk formatting and partitioning aligned with system compatibility and performance requirements. Prior knowledge can prevent potential rework associated with selecting incompatible filesystems.
Explanation:
The command simply provides a list of all compatible filesystems for diskutil partitioning, aiding users in pre-selecting appropriate types based on their specific environmental constraints and usage needs.
Example Output:
Formattable file systems
------------------------
APFS
Case-sensitive APFS
HFS+
Case-sensitive HFS+
FAT32
exFAT
Conclusion:
The diskutil partitionDisk
command is a powerful Swiss Army knife for managing disks and partitions on macOS systems. Whether preparing a disk for a fresh OS installation, organizing data management through partitions, or ensuring compatibility across different platform needs, this utility offers flexibility and precise control. By understanding each use case and its requisites, users can optimize disk management tailored to their specific scenarios.