I'm trying to automate creating an encrypted APFS image with files in it. What I have so far:
>>> hdiutil create -megabytes 1 -layout GPTSPUD -fs apfs -volname encryption encryption.dmg
created: .../encryption.dmg
>>> hdiutil attach -stdinpass encryption.dmg
/dev/disk2 GUID_partition_scheme
/dev/disk2s1 Apple_APFS
/dev/disk3 EF57347C-0000-11AA-AA11-0030654
/dev/disk3s1 41504653-0000-11AA-AA11-0030654 /Volumes/encryption
>>> diskutil apfs deleteVolume disk3s1
Started APFS operation
Deleting APFS Volume from its APFS Container
Unmounting disk3s1
Deleting Volume
Finished APFS operation
>>> diskutil apfs addVolume disk3 APFS encryption -nomount -stdinpassphrase
Exporting new encrypted APFS Volume "encryption" from APFS Container Reference disk3
Started APFS operation on disk3
Preparing to add APFS Volume to APFS Container disk3
Creating APFS Volume
Created new APFS Volume disk3s1
Disk from APFS operation: disk3s1
Finished APFS operation on disk3
>>> hdiutil detach -quiet /dev/disk3
>>> hdiutil detach -quiet /dev/disk2
>>> hdiutil attach -stdinpass encryption.dmg
hdiutil: attach failed - no mountable file systems
But the final attach command failed.
If I try to open the resulting DMG in Finder, it too tells me that there are no mountable file systems, so clearly I failed to add the volume, even though the addVolume
command succeeded and said it successfully created the volume.
Maybe it didn't though, so does anyone know how to do this?