電腦上的操作系統(tǒng)尸闸、應(yīng)用程序和應(yīng)用數(shù)據(jù)一般都需要保存在永久存儲(chǔ)器中(通常就是硬盤)烤蜕,這樣電腦斷電后應(yīng)用數(shù)據(jù)等就不會(huì)丟失蚕断。
為了更有效地組織磁盤上的數(shù)據(jù)信息眨攘,通常將磁盤預(yù)先劃分成一個(gè)或多個(gè)磁盤分區(qū)主慰,創(chuàng)建對(duì)應(yīng)的文件系統(tǒng)嚣州,以方便計(jì)算機(jī)對(duì)各分區(qū)分別進(jìn)行管理。
MacOS 系統(tǒng)自帶一個(gè)圖形化的磁盤管理工具(Disk Utility)共螺,同時(shí)還有一個(gè)命令行版本的 diskutil该肴。通過該命令的使用,可以很快捷地對(duì)本地磁盤進(jìn)行擦除數(shù)據(jù)藐不、調(diào)整分區(qū)大小匀哄、格式化等操作。
一雏蛮、verb
diskutil 命令的格式為:diskutil <verb> <options>
不帶任何選項(xiàng)的 diskutil 命令會(huì)列出該命令支持的 verb 及其對(duì)應(yīng)的介紹:
? ~ 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 主要分為以下幾類:
- 獲取磁盤和分區(qū)信息:如 list拱雏、info、activity 等
- 掛(卸)載磁盤或卷:如 mount底扳、eject铸抑、mountDisk 等
- 驗(yàn)證、修復(fù)磁盤分區(qū)或文件系統(tǒng):如 verifyVolume衷模、repairDisk 等
- 分區(qū)操作:如 splitPartitions鹊汛、mergePartitions 等
- 其他:如 appleRAID、apfs 等
如不清楚某個(gè) verb 的具體命令格式阱冶,可以直接使用 diskutil 命令加上該 verb 并且不帶任何其他選項(xiàng)刁憋,命令行即輸出該 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
二木蹬、獲取磁盤分區(qū)信息
1. list
可以使用 list 選項(xiàng)簡(jiǎn)要列出 MacOS 系統(tǒng)的磁盤及分區(qū)信息至耻,包括分區(qū)類型(TYPE)、分區(qū)名(NAME)镊叁、容量大谐就恰(SIZE)和標(biāo)志符(IDENTIFIER)等。如此時(shí)系統(tǒng)掛載了 dmg 映像文件晦譬,其信息也會(huì)顯示在列表中(下表中的 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 為內(nèi)置磁盤,/dev/disk2 為外置磁盤(U 盤敛腌,已在 Windows系統(tǒng)下格式化為 FAT32 格式)卧土,/dev/disk3 為 DMG 映像文件。而 /dev/disk1 其實(shí)就是 disk0s2 作為 APFS 文件系統(tǒng)容器的具體信息像樊。
2. info
info 選項(xiàng)可以列出指定磁盤或分區(qū)的詳細(xì)信息尤莺。如查看 disk2 (即 8 G 優(yōu)盤)的信息:
~ 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
輸出的信息包括設(shè)備標(biāo)志符(Device Identifier)、設(shè)備節(jié)點(diǎn)(Device Node)生棍、設(shè)備名(Device / Media Name)颤霎、容量大小(Disk Size)、塊大薪莅蟆(Block Size)等韩脑。
也可以查看某個(gè)分區(qū)的詳細(xì)信息:
~ 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
三、擦除磁盤或分區(qū)
eraseDisk 選項(xiàng)用于擦除整個(gè)磁盤并重新格式化粹污。該命令的格式為:
diskutil eraseDisk <format> <name> [APM|MBR|GPT] MountPoint|DiskIdentifier|DeviceNode
format 用于指定擦除數(shù)據(jù)后需要重新建立的文件系統(tǒng)類型段多。可以為 %noformat% 來跳過初始化文件系統(tǒng)的操作壮吩。其他支持的類型可以通過 listFilesystems 選項(xiàng)查看进苍。
? ~ 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+
用來測(cè)試的優(yōu)盤如下所示,已在 Windows 下格式化為 FAT32 格式鸭叙。
可以使用
diskutil eraseDisk ExFAT StarkyDisk disk2
命令將優(yōu)盤數(shù)據(jù)擦除并格式化為 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
此時(shí)的優(yōu)盤信息為:分區(qū)表變?yōu)?GPT 類型,且多了一個(gè) EFI 分區(qū)沈贝。
也可以在擦除磁盤時(shí)指定分區(qū)表類型:
? ~ 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
此時(shí)的優(yōu)盤分區(qū)表變?yōu)?MBR 類型:其他擦除命令如 eraseVolume (完全擦除整個(gè)磁盤或某個(gè)磁盤分區(qū)杠人,創(chuàng)建新的文件系統(tǒng))、zeroDisk (向整個(gè)磁盤或某個(gè)分區(qū)全部寫入 '0')
使用 zeroDisk 命令擦除磁盤(該過程會(huì)花費(fèi)很長(zhǎng)的時(shí)間宋下,我試了)后嗡善,該磁盤上的全部信息被抹除,同時(shí)也不再包含分區(qū)和文件系統(tǒng)信息:
則再次插入此優(yōu)盤會(huì)提示你『初始化』或『格式化』該磁盤学歧。
四罩引、創(chuàng)建磁盤分區(qū)
可以通過 partionDisk 選項(xiàng)完成對(duì)磁盤的分區(qū)操作。該命令的格式為:
diskutil partitionDisk MountPoint|DiskIdentifier|DeviceNode
[numberOfPartitions] [APM|MBR|GPT]
[part1Format part1Name part1Size part2Format part2Name part2Size
part3Format part3Name part3Size ...]
命令選項(xiàng)中的 Size 用來指定分區(qū)的大兄Ρ俊(以扇區(qū)數(shù)計(jì)量)袁铐,合法的值包括帶有指定后綴的浮點(diǎn)數(shù)。其中的后綴有 B(ytes), S(512-byte-
blocks), K(ilobytes), M(egabytes), G(igabytes), T(erabytes), P(etabytes)横浑,也可以是 % 來表示對(duì)整個(gè)磁盤的占比剔桨。
最后一個(gè)分區(qū)會(huì)自動(dòng)擴(kuò)展到占用整個(gè)磁盤的剩余空間,如果想為最后一個(gè)分區(qū)指定固定的大小伪嫁,可在其后再創(chuàng)建一個(gè)類型為『free space』的分區(qū)领炫。
? ~ 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
上面的命令在優(yōu)盤(disk2)上創(chuàng)建了 3 個(gè)分區(qū),第一個(gè)(F01)格式為 FAT32张咳,大小是 3 Gb。第二個(gè)(F02)格式為 JHFS+似舵,大小為 3 Gb脚猾。最后一個(gè)是『自由空間』,大小為剩余的容量砚哗。所以實(shí)際上只是分了兩個(gè)區(qū)龙助,整體的分區(qū)表類型為 MBR。
五、分割/合并磁盤分區(qū)
splitPartition 選項(xiàng)可以用來將已存在的某個(gè)分區(qū)再分割成數(shù)個(gè)更小的分區(qū)提鸟,注意原分區(qū)上的所有數(shù)據(jù)都會(huì)丟失军援。
該選項(xiàng)的第一個(gè)參數(shù)為需要分割的分區(qū)的掛載點(diǎn)/標(biāo)志符/設(shè)備節(jié)點(diǎn),其余參數(shù)和使用 partitionDisk 時(shí)相同称勋。
? ~ 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
上面的命令將優(yōu)盤的第二個(gè)分區(qū)(disk2s2)又分割成了兩個(gè)更小的分區(qū)胸哥,分別是 FAT32 格式的 F01(disk2s2),和 JHFS+ 格式的 F02(disk2s3)赡鲜。雖然命令中指定了 F02 的大小是 3G空厌,因?yàn)槭亲詈笠粋€(gè)分區(qū),所以自動(dòng)擴(kuò)展到占用剩余的磁盤空間银酬。最后它的實(shí)際大小是 4.5G嘲更。
mergePartitions 選項(xiàng)用來將多個(gè)已存在的分區(qū)合并為一個(gè)大的分區(qū)。該選項(xiàng)的格式為:
diskutil mergePartitions [force] format name DiskIdentifier|DeviceNode DiskIdentifier|DeviceNode
第一個(gè)分區(qū)參數(shù)為起始分區(qū)揩瞪,第二個(gè)分區(qū)參數(shù)為結(jié)束分區(qū)赋朦。這兩個(gè)分區(qū)之間的所有分區(qū)都將被合并。
如果 force 選項(xiàng)沒有被指定李破,且合并前的第一個(gè)分區(qū)是可調(diào)整大小的文件系統(tǒng)(如 JHFS+)北发,則第一個(gè)分區(qū)上的數(shù)據(jù)會(huì)保留到合并后的分區(qū)。
? ~ 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
六喷屋、調(diào)整分區(qū)大辛詹Α(無損)
resizeVolume 選項(xiàng)可以無損調(diào)整(增加或縮減)分區(qū)大小。
將 disk2s2 分區(qū)縮減為 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
此時(shí) disk2s2 內(nèi)的文件如下:
將 disk2s2 分區(qū)擴(kuò)展狱庇,并盡可能占用所有可用的自由空間。
? ~ 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
此時(shí) disk2s2 內(nèi)的文件如下:
參考文章
man diskutil