diskutil macOS 工具使用总结
- 获取磁盘和分区信息:如 list、info、activity 等
- 挂(卸)载磁盘或卷:如 mount、eject、mountDisk 等
- 验证、修复磁盘分区或文件系统:如 verifyVolume、repairDisk 等
- 分区操作:如 splitPartitions、mergePartitions 等
- 其他:如 appleRAID、apfs 等
apfs
apfs模拟的是一个完整的硬盘(个人理解)
列出apfs列表
diskutil apfs list /dev/disk3
删除apfs容器
删除容器目标应该是以disk硬盘为目标
diskutil apfs deleteContainer disk3
HFS
格式化/擦除整个磁盘格式化
eraseDisk
diskutil eraseDisk <format> <name> [APM|MBR|GPT] MountPoint|DiskIdentifier|DeviceNode
format 用于指定擦除数据后需要重新建立的文件系统类型。可以为 %noformat% 来跳过初始化文件系统的操作。其他支持的类型可以通过 listFilesystems
选项查看。
可以使用 diskutil eraseDisk ExFAT StarkyDisk disk2
命令将优盘数据擦除并格式化为 ExFAT 格式。
eraseVolume
完全擦除整个磁盘或某个磁盘分区,创建新的文件系统
zeroDisk
向整个磁盘或某个分区全部写入 ‘0’,该磁盘上的全部信息被抹除,同时也不再包含分区和文件系统信息
创建磁盘分区
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
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 分区扩展,并尽可能占用所有可用的自由空间。
➜ ~ 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