How does file system mounting in macOS work

Solution 1:

That is (as is probably clear to you) not how mounting works.

The contents of the /dev folder is mainly device files (as the name implies). Device files are "special files" in that they're really nodes in the file system, but the contents of these "files" are not stored inside the file system.

Device files are divided into two main types: block devices and character devices. A block device is something you can read/write blocks of data from/to (a block is a number of bytes). In contrast, a character device is something you can read/write single characters (bytes) from/to.

In this case, the file systems mounted on a normal Mac are usually block devices. For example hard drives, SSD's, etc. are normally available as block devices.

As we've now know that device files does not store their content inside the file system, but merely are indicators of a block or character device - how does it then work?

It works by storing a kind of "pointer" to a kernel representation of the device inside the file system. This is done by having each device file contain 3 pieces of information that is stored inside the file system, and that you can see by running "ls" in the Terminal for example:

$ ls -l /dev/disk0
brw-r-----  1 root  operator    1,   0 Jun  5 10:48 /dev/disk0

In this example the first "b" on the line indicates that this is a block device. Had it been a character device, the letter would have been a "c"

Then after "operator" (the group), the numbers "1, 0" appear. This means that this block device (/dev/disk0) "points" to the internal kernel device that has major number 1 and minor number 0.

The major number indicates what type of device we're talking about. The disk drives in your Mac all have major number 1. The minor number then indicates which disk and partition or volume that it is.

Now when you actually mount a file system, you do give the device file as an argument to the mount command. However what is really stored inside the kernel when representing the mount is not the path you've given, but merely the device information (i.e. the major and minor numbers).

So when you unmount /dev/, you're essentially removing the device files themselves from the / root. I.e. you can no longer browse those device files. However the actual devices still exist within the kernel, and the already mounted file systems just refer to those by their major/minors numbers, and thus keep working.