什么是掛載(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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM