电脑上的操作系统、应用程序和应用数据一般都需要保存在永久存储器中(通常就是硬盘),这样电脑断电后应用数据等就不会丢失。
为了更有效地组织磁盘上的数据信息,通常将磁盘预先划分成一个或多个磁盘分区,创建对应的文件系统,以方便计算机对各分区分别进行管理。
MacOS 系统自带一个图形化的磁盘管理工具(Disk Utility),同时还有一个命令行版本的 diskutil。通过该命令的使用,可以很快捷地对本地磁盘进行擦除数据、调整分区大小、格式化等操作。
一、verb
diskutil 命令的格式为:diskutil <verb> <options>
不带任何选项的 diskutil 命令会列出该命令支持的 verb 及其对应的介绍:
➜ ~ diskutil
Disk Utility Tool
Utility to manage local disks and volumes
Usage: diskutil [quiet] <verb> <options>, where <verb> is as follows:
list (List the partitions of a disk)
info[rmation] (Get information on a specific disk or partition)
listFilesystems (List file systems available for formatting)
activity (Continuous log of system-wide disk arbitration)
u[n]mount (Unmount a single volume)
unmountDisk (Unmount an entire disk (all volumes))
eject (Eject a disk)
mount (Mount a single volume)
mountDisk (Mount an entire disk (all mountable volumes))
rename[Volume] (Rename a volume)
verifyVolume (Verify the file system data structures of a volume)
repairVolume (Repair the file system data structures of a volume)
verifyDisk (Verify the components of a partition map of a disk)
repairDisk (Repair the components of a partition map of a disk)
eraseDisk (Erase an existing disk, removing all volumes)
eraseVolume (Erase an existing volume)
reformat (Erase an existing volume with same name and type)
eraseOptical (Erase 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)
partitionDisk ((re)Partition a disk, removing all volumes)
resizeVolume (Resize a volume, increasing or decreasing its size)
splitPartition (Split an existing partition into two or more)
mergePartitions (Combine two or more existing partitions into one)
appleRAID <verb> (Perform additional verbs related to AppleRAID)
coreStorage <verb> (Perform additional verbs related to CoreStorage)
apfs <verb> (Perform additional verbs related to APFS)
diskutil <verb> with no options will provide help on that verb
上面列出的 verb 主要分为以下几类:
- 获取磁盘和分区信息:如 list、info、activity 等
- 挂(卸)载磁盘或卷:如 mount、eject、mountDisk 等
- 验证、修复磁盘分区或文件系统:如 verifyVolume、repairDisk 等
- 分区操作:如 splitPartitions、mergePartitions 等
- 其他:如 appleRAID、apfs 等
如不清楚某个 verb 的具体命令格式,可以直接使用 diskutil 命令加上该 verb 并且不带任何其他选项,命令行即输出该 verb 的使用介绍。如 eraseDisk 的使用介绍:
➜ ~ diskutil eraseDisk
Usage: diskutil eraseDisk format name [APM[Format]|MBR[Format]|GPT[Format]]
MountPoint|DiskIdentifier|DeviceNode
Completely erase an existing whole disk. All volumes on this disk will be
destroyed. Ownership of the affected disk is required.
Format is the specific file system name you want to erase it as (HFS+, etc.).
Example: diskutil eraseDisk JHFS+ UntitledUFS disk3
二、获取磁盘分区信息
1. list
可以使用 list 选项简要列出 MacOS 系统的磁盘及分区信息,包括分区类型(TYPE)、分区名(NAME)、容量大小(SIZE)和标志符(IDENTIFIER)等。如此时系统挂载了 dmg 映像文件,其信息也会显示在列表中(下表中的 disk3 )。
➜ ~ diskutil list
/dev/disk0 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *121.3 GB disk0
1: EFI EFI 209.7 MB disk0s1
2: Apple_APFS Container disk1 121.1 GB disk0s2
/dev/disk1 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +121.1 GB disk1
Physical Store disk0s2
1: APFS Volume Mac OS 78.4 GB disk1s1
2: APFS Volume Preboot 22.5 MB disk1s2
3: APFS Volume Recovery 517.8 MB disk1s3
4: APFS Volume VM 3.2 GB disk1s4
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *7.8 GB disk2
1: Windows_FAT_32 UNTITLED 7.8 GB disk2s1
/dev/disk3 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: Apple_partition_scheme +39.1 MB disk3
1: Apple_partition_map 32.3 KB disk3s1
2: Apple_HFS Wireshark 39.1 MB disk3s2
其中的 /dev/disk0 为内置磁盘,/dev/disk2 为外置磁盘(U 盘,已在 Windows系统下格式化为 FAT32 格式),/dev/disk3 为 DMG 映像文件。而 /dev/disk1 其实就是 disk0s2 作为 APFS 文件系统容器的具体信息。
2. info
info 选项可以列出指定磁盘或分区的详细信息。如查看 disk2 (即 8 G 优盘)的信息:
~ diskutil info disk2
Device Identifier: disk2
Device Node: /dev/disk2
Whole: Yes
Part of Whole: disk2
Device / Media Name: DataTraveler 2.0
Volume Name: Not applicable (no file system)
Mounted: Not applicable (no file system)
File System: None
Content (IOContent): FDisk_partition_scheme
OS Can Be Installed: No
Media Type: Generic
Protocol: USB
SMART Status: Not Supported
Disk Size: 7.8 GB (7807696896 Bytes) (exactly 15249408 512-Byte-Units)
Device Block Size: 512 Bytes
Read-Only Media: No
Read-Only Volume: Not applicable (no file system)
Device Location: External
Removable Media: Removable
Media Removal: Software-Activated
Virtual: No
输出的信息包括设备标志符(Device Identifier)、设备节点(Device Node)、设备名(Device / Media Name)、容量大小(Disk Size)、块大小(Block Size)等。
也可以查看某个分区的详细信息:
~ diskutil info disk1s1
Device Identifier: disk1s1
Device Node: /dev/disk1s1
Whole: No
Part of Whole: disk1
Volume Name: Mac OS
Mounted: Yes
Mount Point: /
Partition Type: 41504653-0000-11AA-AA11-00306543ECAC
File System Personality: APFS
Type (Bundle): apfs
Name (User Visible): APFS
Owners: Enabled
OS Can Be Installed: Yes
Booter Disk: disk1s2
Recovery Disk: disk1s3
Media Type: Generic
Protocol: PCI
SMART Status: Verified
Volume UUID: E9D63DEC-29D7-3EE0-B9BB-3614E31EA747
Disk / Partition UUID: E9D63DEC-29D7-3EE0-B9BB-3614E31EA747
Disk Size: 121.1 GB (121123069952 Bytes) (exactly 236568496 512-Byte-Units)
Device Block Size: 4096 Bytes
Volume Total Space: 121.1 GB (121123069952 Bytes) (exactly 236568496 512-Byte-Units)
Volume Used Space: 80.0 GB (79982071808 Bytes) (exactly 156214984 512-Byte-Units) (66.0%)
Volume Free Space: 41.1 GB (41140998144 Bytes) (exactly 80353512 512-Byte-Units) (34.0%)
Allocation Block Size: 4096 Bytes
Read-Only Media: No
Read-Only Volume: No
Device Location: Internal
Removable Media: Fixed
Solid State: Yes
Hardware AES Support: No
三、擦除磁盘或分区
eraseDisk 选项用于擦除整个磁盘并重新格式化。该命令的格式为:
diskutil eraseDisk <format> <name> [APM|MBR|GPT] MountPoint|DiskIdentifier|DeviceNode
format 用于指定擦除数据后需要重新建立的文件系统类型。可以为 %noformat% 来跳过初始化文件系统的操作。其他支持的类型可以通过 listFilesystems 选项查看。
➜ ~ diskutil listFilesystems
Formattable file systems
These file system personalities can be used for erasing and partitioning.
When specifying a personality as a parameter to a verb, case is not considered.
-------------------------------------------------------------------------------
PERSONALITY USER VISIBLE NAME
-------------------------------------------------------------------------------
APFS APFS
(or) APFSI
Case-sensitive APFS APFS (Case-sensitive)
ExFAT ExFAT
Free Space Free Space
(or) FREE
MS-DOS MS-DOS (FAT)
MS-DOS FAT12 MS-DOS (FAT12)
MS-DOS FAT16 MS-DOS (FAT16)
MS-DOS FAT32 MS-DOS (FAT32)
(or) FAT32
HFS+ Mac OS Extended
Case-sensitive HFS+ Mac OS Extended (Case-sensitive)
(or) HFSX
Case-sensitive Journaled HFS+ Mac OS Extended (Case-sensitive, Journaled)
(or) JHFSX
Journaled HFS+ Mac OS Extended (Journaled)
(or) JHFS+
用来测试的优盘如下所示,已在 Windows 下格式化为 FAT32 格式。
可以使用
diskutil eraseDisk ExFAT StarkyDisk disk2
命令将优盘数据擦除并格式化为 ExFAT 格式。
➜ ~ diskutil eraseDisk ExFAT StarkyDisk disk2
Started erase on disk2
Unmounting disk
Creating the partition map
Waiting for partitions to activate
Formatting disk2s2 as ExFAT with name StarkyDisk
Volume name : StarkyDisk
Partition offset : 411648 sectors (210763776 bytes)
Volume size : 14835712 sectors (7595884544 bytes)
Bytes per sector : 512
Bytes per cluster: 32768
FAT offset : 2048 sectors (1048576 bytes)
# FAT sectors : 2048
Number of FATs : 1
Cluster offset : 4096 sectors (2097152 bytes)
# Clusters : 231744
Volume Serial # : 5ad7f879
Bitmap start : 2
Bitmap file size : 28968
Upcase start : 3
Upcase file size : 5836
Root start : 4
Mounting disk
Finished erase on disk2
此时的优盘信息为:分区表变为 GPT 类型,且多了一个 EFI 分区。
也可以在擦除磁盘时指定分区表类型:
➜ ~ sudo diskutil eraseDisk ExFAT StarkyDisk MBR disk2
Password:
Started erase on disk2
Unmounting disk
Creating the partition map
Waiting for partitions to activate
Formatting disk2s1 as ExFAT with name StarkyDisk
Volume name : StarkyDisk
Partition offset : 2 sectors (1024 bytes)
Volume size : 15249406 sectors (7807695872 bytes)
Bytes per sector : 512
Bytes per cluster: 32768
FAT offset : 2048 sectors (1048576 bytes)
# FAT sectors : 2048
Number of FATs : 1
Cluster offset : 4096 sectors (2097152 bytes)
# Clusters : 238207
Volume Serial # : 5ad80e37
Bitmap start : 2
Bitmap file size : 29776
Upcase start : 3
Upcase file size : 5836
Root start : 4
Mounting disk
Finished erase on disk2
此时的优盘分区表变为 MBR 类型:其他擦除命令如 eraseVolume (完全擦除整个磁盘或某个磁盘分区,创建新的文件系统)、zeroDisk (向整个磁盘或某个分区全部写入 '0')
使用 zeroDisk 命令擦除磁盘(该过程会花费很长的时间,我试了)后,该磁盘上的全部信息被抹除,同时也不再包含分区和文件系统信息:
则再次插入此优盘会提示你『初始化』或『格式化』该磁盘。
四、创建磁盘分区
可以通过 partionDisk 选项完成对磁盘的分区操作。该命令的格式为:
diskutil partitionDisk MountPoint|DiskIdentifier|DeviceNode
[numberOfPartitions] [APM|MBR|GPT]
[part1Format part1Name part1Size part2Format part2Name part2Size
part3Format part3Name part3Size ...]
命令选项中的 Size 用来指定分区的大小(以扇区数计量),合法的值包括带有指定后缀的浮点数。其中的后缀有 B(ytes), S(512-byte-
blocks), K(ilobytes), M(egabytes), G(igabytes), T(erabytes), P(etabytes),也可以是 % 来表示对整个磁盘的占比。
最后一个分区会自动扩展到占用整个磁盘的剩余空间,如果想为最后一个分区指定固定的大小,可在其后再创建一个类型为『free space』的分区。
➜ ~ sudo diskutil partitionDisk disk2 3 MBR MS-DOS F01 3G JHFS+ F02 3G "Free Space" F03 0
Started partitioning on disk2
Unmounting disk
Creating the partition map
Waiting for partitions to activate
Formatting disk2s1 as MS-DOS (FAT) with name F01
512 bytes per physical sector
/dev/rdisk2s1: 5847920 sectors in 730990 FAT32 clusters (4096 bytes/cluster)
bps=512 spc=8 res=32 nft=2 mid=0xf8 spt=32 hds=255 hid=2 drv=0x80 bsec=5859376 bspf=5711 rdcl=2 infs=1 bkbs=6
Mounting disk
Formatting disk2s2 as Mac OS Extended (Journaled) with name F02
Initialized /dev/rdisk2s2 as a 3 GB case-insensitive HFS Plus volume with a 8192k journal
Mounting disk
Finished partitioning on disk2
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *7.8 GB disk2
1: DOS_FAT_32 F01 3.0 GB disk2s1
2: Apple_HFS F02 3.0 GB disk2s2
上面的命令在优盘(disk2)上创建了 3 个分区,第一个(F01)格式为 FAT32,大小是 3 Gb。第二个(F02)格式为 JHFS+,大小为 3 Gb。最后一个是『自由空间』,大小为剩余的容量。所以实际上只是分了两个区,整体的分区表类型为 MBR。
五、分割/合并磁盘分区
splitPartition 选项可以用来将已存在的某个分区再分割成数个更小的分区,注意原分区上的所有数据都会丢失。
该选项的第一个参数为需要分割的分区的挂载点/标志符/设备节点,其余参数和使用 partitionDisk 时相同。
➜ ~ sudo diskutil list | grep disk2
/dev/disk2 (external, physical):
0: GUID_partition_scheme *7.8 GB disk2
1: EFI EFI 209.7 MB disk2s1
2: Apple_HFS starky 7.5 GB disk2s2
➜ ~ sudo diskutil splitPartition disk2s2 2 MS-DOS F01 3g JHFS+ F02 3g
Started partitioning on disk2s2 starky
Splitting
Unmounting disk
Waiting for partitions to activate
Formatting disk2s2 as MS-DOS (FAT) with name F01
512 bytes per physical sector
/dev/rdisk2s2: 5845824 sectors in 730728 FAT32 clusters (4096 bytes/cluster)
bps=512 spc=8 res=32 nft=2 mid=0xf8 spt=32 hds=255 hid=411648 drv=0x80 bsec=5857280 bspf=5709 rdcl=2 infs=1 bkbs=6
Mounting disk
Formatting disk2s3 as Mac OS Extended (Journaled) with name F02
Initialized /dev/rdisk2s3 as a 4 GB case-insensitive HFS Plus volume with a 8192k journal
Mounting disk
Finished partitioning on disk2s2 starky
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *7.8 GB disk2
1: EFI EFI 209.7 MB disk2s1
2: Microsoft Basic Data F01 3.0 GB disk2s2
3: Apple_HFS F02 4.5 GB disk2s3
上面的命令将优盘的第二个分区(disk2s2)又分割成了两个更小的分区,分别是 FAT32 格式的 F01(disk2s2),和 JHFS+ 格式的 F02(disk2s3)。虽然命令中指定了 F02 的大小是 3G,因为是最后一个分区,所以自动扩展到占用剩余的磁盘空间。最后它的实际大小是 4.5G。
mergePartitions 选项用来将多个已存在的分区合并为一个大的分区。该选项的格式为:
diskutil mergePartitions [force] format name DiskIdentifier|DeviceNode DiskIdentifier|DeviceNode
第一个分区参数为起始分区,第二个分区参数为结束分区。这两个分区之间的所有分区都将被合并。
如果 force 选项没有被指定,且合并前的第一个分区是可调整大小的文件系统(如 JHFS+),则第一个分区上的数据会保留到合并后的分区。
➜ ~ sudo diskutil list | grep disk2
/dev/disk2 (external, physical):
0: GUID_partition_scheme *7.8 GB disk2
1: EFI EFI 209.7 MB disk2s1
2: Apple_HFS F01 2.9 GB disk2s2
3: Microsoft Basic Data F02 4.5 GB disk2s4
➜ ~ sudo diskutil mergePartitions JHFS+ Starky disk2s2 disk2s4
Merging partitions into a new partition
Start partition: disk2s2 F01
Finish partition: disk2s4 F02
Started partitioning on disk2
Merging partitions
Waiting for partitions to activate
Growing disk
Finished partitioning on disk2
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *7.8 GB disk2
1: EFI EFI 209.7 MB disk2s1
2: Apple_HFS F01 7.5 GB disk2s2
六、调整分区大小(无损)
resizeVolume 选项可以无损调整(增加或缩减)分区大小。
将 disk2s2 分区缩减为 4g 大小,腾出的空间作为『free space』:
➜ ~ diskutil list | grep disk2
/dev/disk2 (external, physical):
0: GUID_partition_scheme *7.8 GB disk2
1: EFI EFI 209.7 MB disk2s1
2: Apple_HFS F01 7.5 GB disk2s2
➜ ~ sudo diskutil resizeVolume disk2s2 4g
Resizing to 4000000000 bytes
Started partitioning on disk2s2 F01
Verifying the disk
Verifying file system
Volume was successfully unmounted
Performing fsck_hfs -fn -x /dev/rdisk2s2
Checking Journaled HFS Plus volume
Checking extents overflow file
Checking catalog file
Checking multi-linked files
Checking catalog hierarchy
Checking extended attributes file
Checking volume bitmap
Checking volume information
The volume F01 appears to be OK
File system check exit code is 0
Restoring the original state found as mounted
Resizing
Shrinking file system
Modifying partition map
Finished partitioning on disk2s2 F01
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *7.8 GB disk2
1: EFI EFI 209.7 MB disk2s1
2: Apple_HFS F01 4.0 GB disk2s2
此时 disk2s2 内的文件如下:
将 disk2s2 分区扩展,并尽可能占用所有可用的自由空间。
➜ ~ sudo diskutil resizeVolume disk2s2 R
Resizing to full size (fit to fill)
Started partitioning on disk2s2 F01
Verifying the disk
Verifying file system
Volume was successfully unmounted
Performing fsck_hfs -fn -x /dev/rdisk2s2
Checking Journaled HFS Plus volume
Checking extents overflow file
Checking catalog file
Checking multi-linked files
Checking catalog hierarchy
Checking extended attributes file
Checking volume bitmap
Checking volume information
The volume F01 appears to be OK
File system check exit code is 0
Restoring the original state found as mounted
Resizing
Modifying partition map
Growing file system
Finished partitioning on disk2s2 F01
/dev/disk2 (external, physical):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme *7.8 GB disk2
1: EFI EFI 209.7 MB disk2s1
2: Apple_HFS F01 7.5 GB disk2s2
此时 disk2s2 内的文件如下:
参考文章
man diskutil