macOS-APFS文件系统及其工具

APFS 苹果官方文档

About Apple File System

Apple File System Reference.pdf

  • diskutil apfs 命令集
  • gpt命令集
  • newfs_apfs 工具
  • /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util 工具
  • apfs_hfs_convert hfs 分区转换为 apfs 分区
  • newfs_apfs

将磁盘格式化为APFS

  1. 查看所有接入的 disk
    diskutil list

列出所有的apfs分区 diskutil apfs list

1
2
3
4
5
6
7
8
9
10
11
/dev/disk4 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: GUID_partition_scheme +10.0 GB disk4
1: EFI EFI 209.7 MB disk4s1
2: Apple_APFS Container disk5 9.8 GB disk4s2

/dev/disk5 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +9.8 GB disk5
Physical Store disk4s2
1: APFS Volume NodeModules 24.6 KB disk5s1
  1. 卸载磁盘
    diskutil unmountDisk disk5

  2. gpt命令删除所有分区。从后向前删除,空白磁盘可不需要这个步骤
    gpt remove -i 2 disk4
    gpt remove -i 1 disk4

  3. 清空原有分区表
    sudo dd if=/dev/zero of=/dev/rdisk4 bs=512k count=1

  4. 创建新gpt分区表
    sudo gpt create disk4

  5. 使用 diskutil 添加 APFS Container (可直接在空磁盘上执行这一步,分区是否对齐待定)
    diskutil apfs createContainer disk4

  6. diskutil list 查看新 APFS Container 对应的disk编号,这里是disk5

1
2
3
4
5
6
7
8
/dev/disk4 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: +10.0 GB disk4

/dev/disk5 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +10.0 GB disk5
Physical Store disk4
  1. 向 disk5 添加 APFS Volume
    diskutil apfs addVolume disk5 APFS TheAPFSName
1
2
3
4
5
6
7
8
9
/dev/disk4 (disk image):
#: TYPE NAME SIZE IDENTIFIER
0: +10.0 GB disk4

/dev/disk5 (synthesized):
#: TYPE NAME SIZE IDENTIFIER
0: APFS Container Scheme - +10.0 GB disk5
Physical Store disk4
1: APFS Volume TheAPFSName 24.6 KB disk5s1
  1. 禁用 Volume 上的 OwnerShip,这里以添加后的 Volume disk5s1 为例:
    sudo diskutil disableOwnership disk5s1

擦除 apfs volume

1
diskutil apfs eraseVolume disk5s1 -name ErasedApfsVolume

apfs.util 工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$ /System/Library/Filesystems/apfs.fs/Contents/Resources/apfs.util

APFS Utility tool
Usage: apfs.util <option> [<verb>] path|dir|device [<optional parameter>], as follows:

apfs.util <option> [<verb>] /dev/diskXsY
-a path : Get the absolute volume uuid for the volume at path.
-D get : Get defragmentation setting for volume or container.
-D set=yes/no : enable / disable defragmentation for volume or container.
-k : get volume UUID.
-p : probe for volume name.
-R SNAPSHOT : set the volume to revert to the snapshot named by SNAPSHOT on next mount.
-s : set container UUID and its volume(s) UUID(s) to new random values.

apfs.util <option> [<verb>] path|dir [<optional parameter>]
-A get : get atime update setting for volume.
-A set=yes/no : enable / disable atime update for volume.
-C -type type volume_path : clear the purgeability of files of the specified 'type' on the volume referred to by 'path'.
-E path : drop the extents for 'path' from the first snapshot.
-G path : get stats on the purgeable files on the volume specified by 'path'.
-m <verb> path : mark the file at path as purgeable with the various options specified.
'verb' could be one of: -low -med -high -su -photo -music -mail -document -data
-podcast -video -movie -message -book -sucontent -fault -pdir
-clear.
-M dir : flag the named directory as "maintain-dir-stats".
-O path [fs name] : override the fstypename for apfs to be "hfs" (or the optional name given).
-P <criteria> path : purge files meeting the criteria specified on the volume referred to by 'path'.
-K path : purge(kill) the single file at 'path'
-r dir : set the sync-root flag on the specified directory.
-S dir : get the directory stats from dir.
-Y dir : get the sync-root flag from the specified directory.
-y path : get the sync-root flag from the first parent that has one for the item identified by 'path'.
-z path : make the file at path exempt from copy-on-write semantics for snapshots.
-Z path : get the purgeable file flags for the specific file at 'path'.

apfs.util <option>
-t : stitches and creates synthetic objects on root volume group.
-h : show this help menu.

macOS-APFS文件系统及其工具
https://dnacore.github.io/post/a18f8a16-8fd7-4d1c-b4f6-2111f1d35e44.html
作者
DNACore
发布于
2024年10月16日
更新于
2024年10月31日
许可协议