How to use the diskutil command on the terminal to mount and unmount the external hard disk on Mac

Recently, when using macOS to mount an external hard drive, I couldn’t mount it using mount. Many articles didn’t describe various situations in detail, so I wrote a blog to record it.

How to mount and unmount hard disk (or partition)

mount and umount cannot be used on macOS, if used, it will display unknown special file or no permission, as follows:

$ mount /dev/disk3s2
mount: /dev/disk3s2: unknown special file or file system.
$ umount /dev/disk3s2
umount: unmount(/Volumes/backup): Operation not permitted

If you add sudo to umount, Resource busy will be displayed, prompting to use diskutil unmount, as follows:

$ sudo umount /dev/disk3s2
Password:
umount(/Volumes/backup): Resource busy -- try 'diskutil unmount'

In some cases, if you follow diskutil mount /dev/disk3s2, you can find that it can be mounted successfully, as follows:

$ diskutil mount /dev/disk3s2
Volume backup on /dev/disk3s2 mounted

Use diskutil unmount /dev/disk3s2 to uninstall successfully, as follows:

$ diskutil unmount /dev/disk3s2
Volume backup on disk3s2 unmounted

As a reminder, you don’t need to write the path here, you can directly write diskutil unmount disk3s2. If you know the hard disk or volume name, you can directly use the name to load or unload, as follows:

$ sudo diskutil mount backup
Volume backup on backup mounted

It should be noted that the “partition” in macOS is not the same concept as the partition in Windows.

But if you want to mount or unmount all volumes in a partition, you must use mountDisk and unmountDisk, otherwise the following content will be displayed:

$ diskutil unmount disk2
disk2 was already unmounted or it has a partitioning scheme so use "diskutil unmountDisk" instead

Note that although there is Disk in this option, the operation is “one partition”, not “one hard disk”. Because it is loaded according to a table in a hard disk partition.

How to get the hard disk path and the name of the hard disk volume

You can get information about connected hard drives through “System Information” and “Disk Utility”, but this is too much trouble.

You can use `diskutil list to view all the hard drives connected to the Mac, as follows (only the first external hard drive is kept):

/dev/disk2 (external, physical):
   #: TYPE NAME SIZE IDENTIFIER
   0: GUID_partition_scheme *1.0 TB disk2
   1: EFI?EFI? 209.7 MB disk2s1
   2: Apple_APFS ?Container disk3? 1000.0 GB disk2s2

/dev/disk3 (synthesized):
   #: TYPE NAME SIZE IDENTIFIER
   0: APFS Container Scheme - + 1000.0 GB disk3
                                 Physical Store disk2s2
   1: APFS Volume ?backup? 323.2 GB disk3s2

At this time, you can get the names of the locations of each partition and volume.

If you just want to know how to mount and unmount, you can read here. The following is to solve some curious babies’ questions and record some APFS knowledge, such as what is the underlying operation of mounting APFS partitions.

Extended knowledge

What is the difference between the above two parts

You can see that in the above content, a hard disk actually generates two parts: /dev/disk2 (external, physical) and /dev/disk3 (synthesized). What is the difference between these two?

/dev/disk2 (external, physical)part

This section represents the physical portion of the hard drive. That is, this part of “Disk Utility”:

Please add a picture description

The boot file of the hard disk is stored in /dev/disk2, that is, the hard disk node instance (device node entry), which also includes the hard disk identifier (disk identifier) of each part. The TYPE of disk2 also writes GUID_partition_scheme (GUID partition scheme).

The disk2s2 part is the container part you see in the “Disk Utility” (if the sharp-eyed readers will find that this part is called container disk3, the “device” part is also “disk3”, the next chapter will explain why):
Please add a picture description

If you try to mount the container disk2s2 it will display the following message:

$ sudo diskutil mount disk2s2
Volume on disk2s2 failed to mount because it appears to be
an APFS Physical Store (or is the APFS Container already mounted?)
Note that for this mount operation, Disk Arbitration reports that
the operation is not supported (kDAReturnUnsupported)

But you can load the EFI part (if you need it):

$ sudo diskutil mount disk2s1
Volume EFI on disk2s1 mounted

/dev/disk3 (synthesized) part

This part is the part of the volume (Volume) in the container above.

/dev/disk3 is “APFS Container Scheme” (APFS container scheme), which contains various information of the volume, which is why the volume can be loaded directly through backup? in the NAME part, instead of using the device path.

/dev/disk3s2 is the APFS volume contained in the container. If you load the /dev/disk3s2 part, you can access the hard disk content in APFS format by accessing the file named NAME in the loading location (the default is /Volumes/) (all devices in Unix are files).

This part is what you see in Disk Utility (this is my Time Machine hard drive):

Please add a picture description

If there is only one volume in the hard disk, then this volume is generally diskXs2 in /dev.

By default, generated volumes are located in the /Volumes/ directory and can be accessed here (equivalent to /mnt in Linux):

$ ls /Volumes/
16TB Macintosh HD backup

Why is the container displayed as container disk3 in the second picture (what is the “Physical Store diskXsX” part)

Sharp-eyed students may find that in the second picture, the container part is called container disk3, and the “device” part is also “disk3” instead of disk2s2, but the “physical storage area” is disk2s2.

This is because in APFS a partition (partition) contains a single container (the container is responsible for space management and garbage protection). A container or partition can contain multiple volumes. This is different from some other file systems. Most of the partitions of other file systems are directly the file system layer (File System Layer). The following is the hierarchy of the hard disk with multiple partitions on the hard disk in “Disk Utility”:

Please add a picture description

The following figure shows the space division of the APFS partition on the hard disk, and the space division inside the container is on the right:

APFS general structure

That is to say, the “Apple File System container” part is the general name of the right part in the above figure, and the space on the hard disk is also the sum of the right side, and there is no separate part called “Apple File System container”. That’s why the physical storage area of the container is disk2s2 (the third part of the hard disk), but due to further internal distinctions, further operations are required inside the container to allocate it to disk3.

Hope to help those in need~