1. 程式人生 > >MacOS 磁碟管理工具 diskutil 介紹

MacOS 磁碟管理工具 diskutil 介紹

https://www.jianshu.com/p/6a1f365617ad

電腦上的作業系統應用程式應用資料一般都需要儲存在永久儲存器中(通常就是硬碟),這樣電腦斷電後應用資料等就不會丟失。
為了更有效地組織磁碟上的資料資訊,通常將磁碟預先劃分成一個或多個磁碟分割槽,建立對應的檔案系統,以方便計算機對各分割槽分別進行管理。
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 主要分為以下幾類:

  • 獲取磁碟和分割槽資訊:如 listinfoactivity
  • 掛(卸)載磁碟或卷:如 mountejectmountDisk
  • 驗證、修復磁碟分割槽或檔案系統:如 verifyVolumerepairDisk
  • 分割槽操作:如 splitPartitionsmergePartitions
  • 其他:如 appleRAIDapfs

如不清楚某個 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 格式。

U 盤 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

 

 

此時的優盤資訊為:

U 盤 ExFAT

 

分割槽表變為 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 型別:

U 盤 MBR

其他擦除命令如 eraseVolume (完全擦除整個磁碟或某個磁碟分割槽,建立新的檔案系統)、zeroDisk (向整個磁碟或某個分割槽全部寫入 '0')
使用 zeroDisk 命令擦除磁碟(該過程會花費很長的時間,我試了)後,該磁碟上的全部資訊被抹除,同時也不再包含分割槽和檔案系統資訊:

zeroDisk


則再次插入此優盤會提示你『初始化』或『格式化』該磁碟。

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

mergePatitions 合併前

 

mergePatitions 合併後

六、調整分割槽大小(無損)

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 內的檔案如下:

 

resizeVolume

將 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 內的檔案如下:

 

resizeVolume

參考文章

man diskutil