掛載

轉載

首先引用一句 wiki 上的定義來開篇:

Mounting takes place before a computer can use any kind of storage device (such as a hard drive, CD-ROM, or network share). The user or their operating system must make it accessible through the computer’s file system. A user can only access files on mounted media.

意思是說, “掛載” 發生在計算機想要使用任何類型的存儲設備 (如硬盤, CD-ROM, 網絡設備) 之前. 操作系統必須將這個設備納入自己的文件系統中去.

要注意的是, 這里的存儲設備不一定必須是外部的存儲設備, 也可以是你安裝系統的硬盤上的分區.

例子先

光看上面說的還不夠, 先看個例子吧, 這個例子摘自 man mount, 在 man 手冊中這個例子下的一句話非常好的解釋了 mount 到底是什么.

mount -t type device dir 

在這個例子下面有這么一句話:

This tells the kernel to attch the filesystem fount ondevice(which is of typetype) at the directorydir.

這句話非常重要, 我們一定要明白, 掛載操作, 實際上是把設備device中的文件系統附加到dir上, 然后我們就可以通過訪問dir來訪問這個設備.

明白了這一點, 我們就能明白 “掛載” 的本質了, 掛載的本質就是針對某一設備, 分析出其文件系統結構, 并根據其文件系統類型調用 linux 中相應的驅動, 處理其的元數據, 將這些信息附加到 linux 的目錄樹上呈現出來.

明白這一點之后, 后面的 bind mount, loop mount 以及 remount 的區別就能夠很清楚了.

掛載點

什么是掛載點呢? 還是先借用 Wiki 上的一句話:

A mount point is a physical location in the partition used as a root filesystem.

不幸的是, Wiki 上的這句話并不準確, 這句話的意思也就是說 “掛載點就是 root 分區中的一個位置”, 這句話錯在 “root 分區” 上.

我們知道在安裝 Linux 系統時可能會為磁盤分多個區, 最普遍的情況就是很多用戶會給 /home 目錄單獨分一個區. 而且有一部分用戶還會在 /home/username 目錄下建立一個專門用來掛載各種設備的目錄 (如 /home/username/mnt-point) 而不使用系統的 /mnt 目錄. 那么這時候, 難道說 /home/username/mnt-point 這個目錄就不是掛載點了嗎? 顯然它也是掛載點, 但它確并不是位于 root 分區 (即 / 分區).

國外有一篇文章, 用毫不裝逼的方式說出了”掛載點”的本質:

In simple words a mount point is a directory to access your data (files and folders) which is stored in your disks.

所以說白了, 掛載點就是一個目錄. 所以下文中當我應該說”掛載到某一掛載點”的時候我都直接說”掛載到某一目錄”.

假設備掛載 (loop mount)

loop device

明白 loop mount 之前, 最好先清除什么是 loop device, 有耐心的話可以參見維基百科中的條目, 比較長, 沒耐心的話可以直接看我下面的描述, 簡潔些.

簡單來說, loop device 能夠提供將一個檔案掛載到某一目錄的功能. 這和 bind mount (下文會介紹) 有些類似, 但并不相同. 原始的 mount 只是為了將正常的設備掛載, bind mount 使得可以掛載目錄, 而 loop device 使得可以掛載檔案.

在 linux 中, loop device 就是指 /dev/loop0, /dev/loop1, /dev/loop2 … 這些設備, 它們是虛假的設備(pseudo device), 不像 /dev/sda 在你的主機里物理存在. loop device 需要你在編譯內核的時候將其靜態編譯或者編譯為動態模塊, 然后需要使用modprobe加載其模塊(這個模塊包含了 loop device 的驅動程序以及 losetup 這種提供給用戶來操作 loop device 的程序), 這時其驅動程序就回創建 /dev/loop0, /dev/loop1 … 這幾個設備文件.

檔案

注意, 我在說檔案的時候, 指的是英文中的 archive, 它和文件 file 是不同的東西, 檔案 archive 是一個打包的文件集, 里面一般包含許多文件, 比如 tar, jar, iso 就是常見的檔案格式.

用過 dd 的人應該知道, 這個強大的命令可以將整個磁盤或者磁盤分區克隆下來, 放到一個文件里, 一般, 這樣的文件我們都以 .img 后綴為其命名并稱這樣的文件為鏡像文件. 我所說的檔案也包含這類情況.

loop mount

ok, 明白了什么是 loop device, 也明白了檔案是什么, 那么到底如何把一個檔案掛載到某個目錄下呢?

實際上 loop mount 采取了一個瞞天過海的方式, 它先將這個檔案映射到某個 loop device 上, 像這樣:

# losetup /dev/loop0 xxxx.iso 

通過這種方式來欺騙mount命令, 讓mount命令以為 /dev/loop0 上面真的有設備. 這時運行mount就行了:

# mount -t iso9660 /dev/loop0 /path/to/mount/point 

這么看起來, 當你想掛載某一個檔案的時候(比如某個 iso), 你首先得把這個檔案和某一個 loop device 關聯起來, 使用 losetup 命令. 然后使用 mount 命令將這個 loop device 設備掛載到某個目錄上. 實際上不必這樣,mount命令自身其實就有一個能把這兩步合并的功能, 那就是這樣:

# mount -t iso9660 -o loop /dev/loop0 /path/to/mount/point 

最后我們再來想一想, 是不是所有的檔案都可以用這種方式掛載? 顯然不是的, 根據mount命令有個 -t 參數來看, 在掛載的時候是需要指定文件系統的類型的(不指定的話mount命令會自動識別), 還記得上面說的掛載的本質嗎?

"掛載操作, 實際上是把設備 _device_ 中的**文件系統**附加到 _dir_ 上,". 

不被識別的文件系統是不能被掛載的, 如果你沒有加載 ReiserFS 模塊, 那么掛載具有 ReiserFS 文件系統的設備時就會報 “unknown file system” 錯誤. 像上面說的 tar, jar, zip 這樣的檔案, 它們只是一種打包/壓縮格式, 本身就不是一種文件系統格式, 當然是不能被 linux 識別的. 它們雖然可以映射到某一個 loop device, 但并不能被掛載.

但是像 .iso 文件, 它一般包含 iso 9660 文件系統, 都知道這是一種 CD 上采用的文件系統. 還有就是你可以使用 dd, mkfs 命令來創建一個 ext2, ext3 等文件系統的檔案. 這樣的檔案才是可以被掛載的.

loop mount 一直以來是 Unix-like 系統下很有用的特性, 能幫助你當你拿到一個 iso 文件后, 不必將其刻錄到 CD/DVD 里就能查看里面的內容. windows 下直到 windows 7 才支持這一特性, 在此之前都需要借助第三方軟件如 Daemon Tools 來實現虛擬光驅的功能.

綁定式掛載 (bind mount)

上面所說的 “掛載” 都是指讓你將某個設備掛載到某一目錄, 不管這個設備是真實的物理設備, 還是假的 loop 設備, 它都是設備. 而 “綁定式掛載” 能夠允許你將已經的存在目錄掛載到另一目錄. 比如:

 #  mount --bind / /home/username/mnt-point 

這樣, 你的 mnt-point 目錄下也會有 etc, opt, usr 等目錄, 這一過程我們稱作 “將根目錄綁定到 /home/username/mnt-point 上”, 所以, 你在一處改變目錄下的內容的話, 在另一處也能夠看到改變.

需要注意的一點是如果根目錄樹下有某個目錄是掛載到另一個磁盤分區的話, 那么它可能不會被綁定到新的目錄下. 比如說如果 /usr 和 / 處于不同的磁盤分區(/ 在 sda1, /usr 在 sda2), 那么你可能會發現 /home/username/mnt-point/usr 是空的, 那么這時可以額外掛載一次來使得 /usr 也出現在 /home/username/mnt-point/usr:

 #  mount --bind /usr /home/username/mnt-point/usr 

不過你也可以在一開始就執行:

 #  mount --rbind / /home/username/mnt-point 

關于綁定式掛載,man 2 mount中的描述是 “使一個文件, 或者一個目錄樹在另一個目錄上可見”. 這地方不太理解, 就我所知, 只能將目錄綁定到目錄, 不能將文件綁定到目錄的. 我嘗試過將一個普通的文件綁定到目錄, 但報錯了. 不知道 man 手冊里這個說法是什么意思. 我只能這么理解: 目錄也是文件, 所以這種說法沒錯吧….

重新掛載 (remount)

借助于綁定式掛載, 可以實現有趣的效果, 比如說, 你可以將 / 綁定到 /, 將 /tmp/test/ 綁定到 /tmp/test/ (運行 mount 命令就能看到效果). 不過… 這么干有個鳥用啊!! 誰這么無聊會去這么干啊!!

這就是 remount 存在的原因, 我們雖然可以通過綁定式掛載耍點小聰明, 將自己綁定到自己上, 但這與沒綁定沒有任何區別啊; 然而借助 remount, 我們就可以在重新掛載的時候修改掛載的參數.

remount 最常用的情況就是將一個文件系統由只讀重新掛載為讀寫, 或者相反. 比如:

# mount -o remount,rw / 

關于 remount 的詳情, 可以看一下 man 手冊, 這里就不多介紹了.

supermount

“超級掛載”, 這個項目的目的是讓你能夠免去手動 mount/umount 的過程, 達到 “插上 U 盤就開始拷文件” 以及 “拷完文件就拔掉 U 盤” 的效果.

Update:

經 Ubuntu 中文論壇 @astolia 大俠指正, 關于 “綁定式掛載”, 雖然不能將一個文件掛載到目錄, 但確是可以將一個文件掛載到另一個文件的! 比如說這樣:

# touch /tmp/test # touch /tmp/test2 # echo "hello mount" > /tmp/test # mount --bind /tmp/test /tmp/test2 

然后你再去查看 /tmp/test2, 會發現它也有 “hello mount” 這個內容. 所以這樣一開始說的 “掛載點就是目錄” 這個說法也不太對了….. 再次感謝 @astolia ~~~

非常好的一篇文章

這篇文章也是 參考1 中提到的文章, 由于寫的太好, 為了防止其丟失, 我把它轉到這里了, 下面那篇英文就是. 我之所以敢轉這篇文章是因為這篇文章的版權聲明停留在 2006 年, 它的主人大概是忘了更新. 我從這篇文章里摘取了幾個比較重要的點作為筆記.

  • 在 “掛載” 的概念中, “設備” 可以是一個分區 (如 /dev/sda9), 可以是另一塊磁盤, 可以是 CDROM, 軟盤, USB, 磁帶等等.
  • “掛載點” 是一個目錄, 而且往往是一個空目錄, 但這不是必須的. 如果這個目錄不是空的, 那么掛載之后, 這個目錄中以前的內容會被 “隱藏” 起來變得不可訪問.
  • Mounting Definition

    Mounting is the attaching of an additional filesystem to the currently accessible filesystem of a computer.

    A filesystem is a hierarchy of directories (also referred to as a directory tree) that is used to organize files on a computer or storage media (e.g., a CDROM or floppy disk). On computers running Linux or other Unix-like operating systems, the directories start with the root directory, which is the directory that contains all other directories and files on the system and which is designated by a forward slash ( / ). The currently accessible filesystem is the filesystem that can be accessed on a computer at a given time.

    In order to gain access to files on a storage device, the user must first inform the operating system where in the directory tree to mount the device. A device in a mounting context can be a partition (i.e., a logically independent section) on a hard disk drive (HDD), a CDROM, a floppy disk, a USB (universal serial bus) key drive, a tape drive, or any other external media. For example, to access the files on a CDROM, the user must inform the system to make the filesystem on the CDROM appear in some directory, typically /mnt/cdrom (which exists for this very purpose).

    The mount point is the directory (usually an empty one) in the currently accessible filesystem to which a additional filesystem is mounted. It becomes the root directory of the added directory tree, and that tree becomes accessible from the directory to which it is mounted (i.e., its mount point). Any original contents of a directory that is used as a mount point become invisible and inaccessible while the filesystem is still mounted.

    The /mnt directory exists by default on all Unix-like systems. It, or usually its subdirectories (such as /mnt/floppy and /mnt/usb), are intended specifically for use as mount points for removable media such as CDROMs, USB key drives and floppy disks.

    On some operating systems, everything is mounted automatically by default so that users are never even aware that there is any such thing as mounting. Linux and other Unix-like systems can likewise be configured so that everything is mounted by default, as a major feature of such systems is that they are highly configurable. However, they are not usually set up this way, for both safety and security reasons. Moreover, only the root user (i.e., administrative user) is generally permitted by default to mount devices and filesystems on such systems, likewise as safety and security measures.

    In the simplest case, such as on some personal computers, the entire filesystem on a computer running a Unix-like operating system resides on just a single partition, as is typical for Microsoft Windows systems. More commonly, it is spread across several partitions, possibly on different physical disks or even across a network. Thus, for example, the system may have one partition for the root directory, a second for the /usr directory, a third for the /home directory and a fourth for use as swap space. (Swap space is a part of HDD that is used for virtual memory, which is the simulation of additional main memory).

    The only partition that can be accessed immediately after a computer boots (i.e., starts up) is the root partition, which contains the root directory, and usually at least a few other directories as well. The other partitions must be attached to this root filesystem in order for an entire, multiple-partition filesystem to be accessible. Thus, about midway through the boot process, the operating system makes these non-root partitions accessible by mounting them on to specified directories in the root partition.

    Systems can be set up so that external storage devices can be mounted automatically upon insertion. This is convenient and is usually satisfactory for home computers. However, it can cause security problems, and thus it is usually not (or, at least, should not be) permitted for networked computers in businesses and other organizations. Rather, such devices must be mounted manually after insertion, and such manual mounting can only be performed by the root account.

    Mounting can often be performed manually by the root user by merely using the mount command followed by the name of the device to be mounted and its mounting destination (but in some cases it is also necessary to specify the type of filesystem). For example, to mount the eighth partition on the first HDD, which is designated by /dev/hda8, using a directory named /dir8 as the mount point, the following could be used:

      mount /dev/hda8 /dir8 

    Removing the connection between the mounted device and the rest of the filesystem is referred to as unmounting. It is performed by running the umount (with no letter n after the first u) command, likewise followed by the name of the device to be unmounted and its mount point. For example, to unmount the eighth partition from the root filesystem, the following would be used:

      umount /dev/hda8 /dir8 

    A list of the devices that are currently mounted can be seen by viewing the /etc/fstab file. This plain text configuration file also shows the mount points and other information about the devices, and it is employed during the boot process to tell the system which partitions to automatically mount. It can be safely viewed by using the cat command, i.e.,

      cat /etc/fstab 

    偶英語不好 ,看不懂 Σ(oдo艸)掛載 。

本文來自投稿,不代表Linux運維部落立場,如若轉載,請注明出處:http://www.www58058.com/97251

(0)
lhl123456lhl123456
上一篇 2018-04-29 19:51
下一篇 2018-04-29 21:21

相關推薦

  • linux下find的用法及練習

    find命令詳解

    Linux筆記 2018-04-15
  • 馬哥教育 – 第二周作業

    一、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。 <1>命令:CP 概念:用來將一個或多個源文件或者目錄復制到指定的目的文件或目錄。它可以將單個源文件復制成一個指定文件名的具體的文件或一個已經存在的目錄下。cp命令還支持同時復制多個文件,當一次復制多個文件時,目標文件參數必須是一個已經存在的目錄,否則將出現錯誤。 語法…

    2018-05-21
  • Linux用戶與組管理詳解

    在linux系統上,用戶管理是基于用戶名和密碼的方式進行資源的分配,了解和掌握用戶與組的管理是從事運維工作所必須具備的能力,也是將來從事運維行業的重要工作之一。

    2018-04-03
  • linux基礎知識

    第一周基礎知識作業

    2018-05-10
  • fff

    Linux筆記 2018-04-08
  • 遷移/home目錄到新的分區上步驟

    遷移/home目錄到新的分區上步驟 1、 安裝一個新的硬盤 2、 查看新硬盤的設備名,如下圖所示: 3、 在新硬盤上創建一個主分區,如下圖所示: 4、 將新創建的分區格式化為ext4文件系統,并加上/home卷標,如下圖所示: 5、 創建/home目錄臨時掛載點,并將分區掛載到臨時掛載點上,如下圖所示: 6、 切換單用戶,將除了root用戶之外的用戶踢出,如…

    2018-04-27
欧美性久久久久