什么是挂载(mount)?

官方文档是这么解释的:

All files accessible in a Unix system are arranged in one big tree, the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command serves to attach the filesystem found on some device to the big file tree. Conversely, the umount(8) command will detach it again. The filesystem is used to control how data is stored on the device or provided in a virtual way by network or another services.

在Unix系统中,所有可访问的文件都是被组织在一个文件树中,即文件层次结构(file hierarchy),其根为 /。mount命令用于把在某些设备上的文件系统附加到大文件树,这样的话,系统才可以访问这些设备上的文件。

mount命令的标准形式如下 :

mount -t type device dir

This tells the kernel to attach the filesystem found on device (which is of type type) at the directory dir.

可是,知道了mount的简单操作,我还是一头雾水,究竟什么是mount(概念)?执行这一命令的时候操作系统究竟发生了什么(动作)?为什么需要mount这样一个操作(场景)?

在Google了很久之后,结合这个回答这个回答,稍微有了一些了解,结合自己的理解,整理如下:

Unix操作系统只有一个单一的目录树结构,该目录树的最顶层就是 / , 所有可访问文件都必须与这个目录树结构关联。这一点和Windows操作系统不同,在Windows中,每一个硬盘都有独立的目录树结构,比如D:\workspace\algorithmC:\Users 这样的。

挂载(mount)这一动作就是把某个设备与目录树中的某个特定位置进行关联,以便于操作系统能够从根目录开始找到这个刚刚加入的设备,从而访问该设备的内的文件数据。(需要注意的是,这里的设备是泛指,既可以指usb、CD-ROM这样真实的设备,也可以是操作系统中的某个目录)。比如,当系统启动的时候,一个特殊的存储设备(通常被称为root 分区)就会与目录树的根进行关联,换句话说,把root分区挂载到了/ 上。

再比如说,现在你想访问存储在U盘上的数据,操作系统究竟发生了什么呢?根据前面说的,为了使操作系统找到它,我们必须把usb挂载到目录树结构的某个位置下(比如说,/media/usb/),假设usb设备是/dev/usb ,那么在把usb插入电脑的时候,对应执行的命令就是:

mount /dev/usb /media/usb

经过这行命令,usb中的文件就对操作系统可见了,比如在usb中的某个文件位于/dir/hello.txt,那么操作系统可访问的对应路径就是/media/usb/dir/hello.txt。当拔出usb时,就执行umount /dev/usbumount /media/usb (两者都可行)

Mounting applies to anything that is made accessible as files, not just actual storage devices. 比如Linux系统有一个特殊的文件系统(被称为 proc filesystem)被挂载在/proc 目录下,该文件系统就没有真实的存储设备:/proc 目录下存放的文件保存着正在运行的进程的一些信息。(关于 proc 可以看之前写的那篇文章)


参考:

  1. https://unix.stackexchange.com/questions/3247/understanding-mount-as-a-concept-in-the-os

  2. https://unix.stackexchange.com/questions/3192/what-is-meant-by-mounting-a-device-in-linux

  3. https://linuxize.com/post/how-to-mount-and-unmount-file-systems-in-linux/

  4. http://ultra.pr.erau.edu/~jaffem/tutorial/file_system_basics.htm

posted @ 2019-12-03 20:08  kkbill  阅读(12880)  评论(0编辑  收藏  举报