The correct way to eject a APFS dmg from the command line

apfsdisk-utilitymount

Create a APFS DMG using Apple's Disk Utility application and call it test_apfs.dmg.

One can mount it with:

hdiutil attach -plist ~/Desktop/test_apfs.dmg

which provides the following output:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>system-entities</key>
    <array>
        <dict>
            <key>content-hint</key>
            <string>GUID_partition_scheme</string>
            <key>dev-entry</key>
            <string>/dev/disk2</string>
            <key>potentially-mountable</key>
            <false/>
            <key>unmapped-content-hint</key>
            <string>GUID_partition_scheme</string>
        </dict>
        <dict>
            <key>content-hint</key>
            <string>Apple_APFS</string>
            <key>dev-entry</key>
            <string>/dev/disk2s1</string>
            <key>potentially-mountable</key>
            <false/>
            <key>unmapped-content-hint</key>
            <string>7C3457EF-0000-11AA-AA11-00306543ECAC</string>
        </dict>
        <dict>
            <key>content-hint</key>
            <string>41504653-0000-11AA-AA11-00306543ECAC</string>
            <key>dev-entry</key>
            <string>/dev/disk3s1</string>
            <key>mount-point</key>
            <string>/Volumes/Untitled</string>
            <key>potentially-mountable</key>
            <true/>
            <key>unmapped-content-hint</key>
            <string>41504653-0000-11AA-AA11-00306543ECAC</string>
            <key>volume-kind</key>
            <string>apfs</string>
        </dict>
        <dict>
            <key>content-hint</key>
            <string>EF57347C-0000-11AA-AA11-00306543ECAC</string>
            <key>dev-entry</key>
            <string>/dev/disk3</string>
            <key>potentially-mountable</key>
            <false/>
            <key>unmapped-content-hint</key>
            <string>EF57347C-0000-11AA-AA11-00306543ECAC</string>
        </dict>
    </array>
</dict>
</plist>

After I try to detach with:

hdiutil detach /Volumes/Untitled

diskutil list, returns:

/dev/disk2 (disk image):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        +100.0 MB   disk2
   1:                 Apple_APFS Container disk3         100.0 MB   disk2s1

/dev/disk3 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +100.0 MB   disk3
                                 Physical Store disk2s1
   1:                APFS Volume Untitled                65.5 KB    disk3s1

and going back to Apple's Disk Utility application, I see the Untitled volume listed, but it is grayed out.

I can only really fully eject it if I then do:

hdiutil detach /dev/disk2

So, mounting is one command, fully ejecting is two.

Is this the only / correct way to eject this kind of volume from the command line? What I would like to be able to do is fully eject it with a single command. Is that possible?

Best Answer

Use the following command.

diskutil eject disk2

From the man pages for hdiutil, the following appears:

 detach dev_name [-force]
            detach a disk image and terminate any associated process.
            dev_name is a partial /dev node path (e.g. "disk1").  As of
            Mac OS X 10.4, dev_name can also be a mountpoint.  If Disk
            Arbitration is running, detach will use it to unmount any
            filesystems and detach the image.  If not, detach will attempt
            to unmount any filesystems and detach the image directly
            (using the `eject' ioctl).  If Disk Arbitration is not run-
            ning, it may be necessary to unmount the filesystems with
            umount(8) before detaching the image.  eject is a synonym for
            detach.  In common operation, detach is very similar to
            diskutil(8)'s eject.

            Options:
            -force   ignore open files on mounted volumes, etc.

So the documentation states you may need to unmount filesystems first. I assume this is what the hdiutil detach /Volumes/Untitled command did.

From the man pages for diskutil, the following appears:

 eject device
            Eject a disk.  Media will become offline for the purposes of
            being a data store for file systems or being a member of con-
            structs such as software RAID or direct data.  Additionally,
            removable media will become eligible for safe manual removal;
            automatically-removable media will begin its physical (motor-
            ized) eject sequence.

Here, there is no mention of having to run any other commands first.

Note: For the diskutil eject disk2 command to work, the Untitled volume can not be in use.