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

Recently, when I used macOS to load an external hard drive, mountI couldn’t install it using life and death. Many articles didn’t describe the various situations in detail, so I wrote a blog to record it.

How to mount and unmount hard drives (or partitions)

mountAnd umountit cannot be used on macOS. If it is used, it will display unknown special files or no permissions, 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 it is umountadded , sudoit will be displayed Resource busy, and the prompt is used diskutil unmount, as follows:

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

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

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

Using diskutil unmount /dev/disk3s2uninstallation will also succeed, as follows:

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

As a reminder, you don’t need to write the path here, diskutil unmount disk3s2you can write it directly. 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 load or unload all volumes in a partitionmountDisk , you must use 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 this option is available Disk, the operation is "one partition" instead of "one hard disk" . Because it is loaded according to a table in a hard disk partition.

How to obtain 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.

expand knowledge

What is the difference between the above two parts

It can be seen 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

/dev/disk2Stored in is the boot file of the hard disk, that is, the hard disk node instance (device node entry), which also includes the hard disk identifier (disk identifier) ​​of each part. disk2The TYPEwrite is also GUID_partition_scheme(GUID partition scheme).

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

If you try to load this container disk2s2it 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/disk3It is "APFS Container Scheme" (APFS container scheme), which contains various information of the volume, which is why the volume can be loaded directly through the NAMEpart backup⁩instead of having to use the device path.

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

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 file in the hard disk, then the files of this file /devare generally all in the file diskXs2.

By default, the resulting volumes are all in /Volumes/a directory and can be accessed here (equivalent in Linux /mnt):

$ ls /Volumes/
16TB		Macintosh HD	backup

Why is the container's shown in the second picture as 容器disk3(what is the "Physical Store diskXsX" part)

Sharp-eyed students may find that in the second picture, the container part is called 容器disk3, and the "device" part is also "disk3" instead of "disk3" 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:

General structure of APFS

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". This is why the physical storage area of ​​​​the container is disk2s2(is the third part of the hard disk), but due to further internal distinctions, further operations are required inside the container to allocate it disk3.

Hope to help those in need~

Guess you like

Origin blog.csdn.net/qq_33919450/article/details/131496913