将不胜感激有关该主题的意见。我有 macOS 的输出(High Sierra;文件系统:APFS) 命令 - diskutil apfs list
。示例输出如下:
+-- Container disk5 1B5FE22B-6F4F-4EB9-8AA3-D326E4E940DF
| ====================================================
| APFS Container Reference: disk5
| Size (Capacity Ceiling): 250140434432 B (250.1 GB)
| Minimum Size: 192333017088 B (192.3 GB)
| Capacity In Use By Volumes: 186220453888 B (186.2 GB) (74.4% used)
| Capacity Not Allocated: 63919980544 B (63.9 GB) (25.6% free)
| |
| +-< Physical Store disk4s2 E6FF882B-995C-4C60-B164-76923667F8A1
| | -----------------------------------------------------------
| | APFS Physical Store Disk: disk4s2
| | Size: 250140434432 B (250.1 GB)
| |
| +-> Volume disk5s1 7BB363F8-A658-4B6C-A4B4-778AC782785E
| | ---------------------------------------------------
| | APFS Volume Disk (Role): disk5s1 (No specific role)
| | Name: Macintosh HD (Case-insensitive)
| | Mount Point: Not Mounted
| | Capacity Consumed: 181760897024 B (181.8 GB)
| | FileVault: Yes (Locked)
| |
| +-> Volume disk5s2 48A79DA5-1228-4F5A-8851-5178B91D2310
| | ---------------------------------------------------
| | APFS Volume Disk (Role): disk5s2 (Preboot)
| | Name: Preboot (Case-insensitive)
| | Mount Point: Not Mounted
| | Capacity Consumed: 66613248 B (66.6 MB)
| | FileVault: No
| |
| +-> Volume disk5s3 D46C3FA0-5B9D-4E13-9327-EEFB483F692E
| | ---------------------------------------------------
| | APFS Volume Disk (Role): disk5s3 (Recovery)
| | Name: Recovery (Case-insensitive)
| | Mount Point: Not Mounted
| | Capacity Consumed: 1033371648 B (1.0 GB)
| | FileVault: No
| |
| +-> Volume disk5s4 3E852266-E0C3-48AB-ACAD-D73CEE0134F1
| ---------------------------------------------------
| APFS Volume Disk (Role): disk5s4 (VM)
| Name: VM (Case-insensitive)
| Mount Point: Not Mounted
| Capacity Consumed: 3221250048 B (3.2 GB)
| FileVault: No
我的目标是:
- 输出磁盘容器以及 fileVault 状态“是(已锁定)”的 GUID。
- 将 fileVault 状态“是(已锁定)”的 GUID 存储到变量中。
我想出了以下几点:
diskutil apfs list|awk '/[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}$|.l/{print}'
输出:
+-- Container disk5 1B5FE22B-6F4F-4EB9-8AA3-D326E4E940DF
| Size (Capacity Ceiling): 250140434432 B (250.1 GB)
| Capacity In Use By Volumes: 186220453888 B (186.2 GB) (74.4% used)
| Capacity Not Allocated: 63919980544 B (63.9 GB) (25.6% free)
| +-< Physical Store disk4s2 E6FF882B-995C-4C60-B164-76923667F8A1
| | APFS Physical Store Disk: disk4s2
| +-> Volume disk5s1 7BB363F8-A658-4B6C-A4B4-778AC782785E
| | APFS Volume Disk (Role): disk5s1 (No specific role)
| | FileVault: Yes (Locked)
| +-> Volume disk5s2 48A79DA5-1228-4F5A-8851-5178B91D2310
| | APFS Volume Disk (Role): disk5s2 (Preboot)
| | FileVault: No
| +-> Volume disk5s3 D46C3FA0-5B9D-4E13-9327-EEFB483F692E
| | APFS Volume Disk (Role): disk5s3 (Recovery)
| | FileVault: No
| +-> Volume disk5s4 3E852266-E0C3-48AB-ACAD-D73CEE0134F1
| APFS Volume Disk (Role): disk5s4 (VM)
| FileVault: No
欢迎任何纠正/修改代码的输入:)。提前致谢
答案1
一起去马克·普洛特尼克的建议转换结构化 XML 输出
diskutil apfs list -plist
转换为 JSON 并用jq
.该jq
实用程序可从 macOS 上的 Homebrew 获取。
plutil
由于需要读取常规文件,因此必须分两步完成:
diskutil apfs list -plist >list.xml
plutil -convert json -o list.json list.xml
从生成的 JSON 文件中,我们可以提取启用了 FileVault 且已锁定的所有卷的 APFS 卷 UUID,使用
jq -r '.Containers[].Volumes[] | select(.FileVault == true and .Locked == true) | .APFSVolumeUUID' list.json
在一个“单个”命令中将其分配给一个变量:
locked_uuids=$(
diskutil apfs list -plist >list.xml
plutil -convert json -o list.json list.xml
jq -r '.Containers[].Volumes[] | select(.FileVault == true and .Locked == true) | .APFSVolumeUUID' list.json
rm -f list.xml list.json
)
请注意,上述命令会覆盖并删除当前目录中的list.xml
和两个文件。list.json
您可能想使用mktemp
以下命令创建临时文件:
locked_uuids=$(
tmpxml=$(mktemp)
tmpjson=$(mktemp)
diskutil apfs list -plist >"$tmpxml"
plutil -convert json -o "$tmpjson" "$tmpxml"
jq -r '.Containers[].Volumes[] | select(.FileVault == true and .Locked == true) | .APFSVolumeUUID' "$tmpjson"
rm -f "$tmpxml" "$tmpjson"
)
并且,为了方便起见,您可能希望将这些命令放入 shell 函数中并调用它(bash
在此处使用):
list_locked_vaults () {
local tmpxml=$(mktemp)
local tmpjson=$(mktemp)
diskutil apfs list -plist >"$tmpxml"
plutil -convert json -o "$tmpjson" "$tmpxml"
jq -r '.Containers[].Volumes[] | select(.FileVault == true and .Locked == true) | .APFSVolumeUUID' "$tmpjson"
rm -f "$tmpxml" "$tmpjson"
}
locked_uuids=$( list_locked_vaults )