macOS-磁盘镜像管理程序hdiutil

功能类似的工具

hdiutil diskutil

hdiutil创建磁盘镜像文件

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
# sparse-band-size 后面值中的 32 表示 32MiB 如果需要其它大小可直接修改这个值
hdiutil create -size 120g\
-type SPARSEBUNDLE\
-layout GPTSPUD\
-fs APFS\
-volname "Ins"\
-nospotlight\
-verbose\
-anyowners\
-imagekey sparse-band-size=$((1024*2*32))\
./Ins.sparsebundle

# 创建加密磁盘映像
hdiutil create -size 120g\
-type SPARSEBUNDLE\
-fs APFS\
-volname "CacheLogDisk"\
-layout GPTSPUD\
-nospotlight\
-verbose\
-anyowners\
-imagekey sparse-band-size=$((1024*2*32))\
-encryption AES-128\
./CacheLog.sparsebundle


# 创建加密稀疏磁盘映像
hdiutil create -size 120g\
-type SPARSE\
-fs APFS\
-volname "CacheLogDisk"\
-layout GPTSPUD\
-nospotlight\
-verbose\
-anyowners\
-encryption AES-128\
./CacheLog.sparse
  • -imagekey sparse-band-size
    以512字节大小为单位 65536 * 521 = 33554432 Bytes = 32 MiB

  • -type
    可选的有:UDIF|SPARSE|SPARSEBUNDLE

  • -fs

hdiutil 挂载映像文件

  • 方法1:
1
hdiutil attach "$HOME/Documents/OF.sparseimage" -mountpoint "$HOME/OpenFOAM"

参考:https://apple.stackexchange.com/a/136689

  • 方法2:直接使用 open 命令
1
/usr/bin/open /full/path/to/OF.sparseimage

参考:https://apple.stackexchange.com/a/107115

hdiutil 压缩 sparseimage 映像

  • 运行命令
1
hdiutil compact ~/Desktop/a.spraseimage

如果提示hdiutil: compact failed - 功能未实现或者hdiutil: compact failed - 功能未实现可能是当前正在使用电池供电,此时需要执行

1
hdiutil compact -batteryallowed ~/Desktop/some.spraseimage

参考:
https://superuser.com/questions/275148/shrink-a-sparseimage
https://apple.stackexchange.com/questions/132147/hdiutil-compact-failed-function-not-implemented

校验映像

1
2
3
# 计算 checksum
hdiutil checksum -type UDIF-CRC32 some.dmg
hdiutil verify some.dmg

详细支持的校验方式请看:https://ss64.com/osx/hdiutil.html

查看当前挂载的映像信息hdiutil info

默认情况下执行hdiutil info会展示 framework 和 driver 的版本;
如果当前有镜像挂载,执行hdiutil info后会展示挂载映像的信息;
执行hdiutil info -plist会以plist格式输出挂载信息,方便进行其它处理。

注意:有时候提示没有可挂载的文件系统,原因是映像文件在非英文路径下,将映像文件移动到英文路径下即可。


macOS-磁盘镜像管理程序hdiutil
https://dnacore.github.io/post/2169df4e-0e27-4b70-ab32-3daa4a878fec.html
作者
DNACore
发布于
2019年10月28日
更新于
2025年3月4日
许可协议