mount bind使用方法

2021-09-12 23:04:00 字數 3014 閱讀 8874

我們可以通過mount --bind命令來將兩個目錄連線起來,mount --bind命令是將前乙個目錄掛載到後乙個目錄上,所有對後乙個目錄的訪問其實都是對前乙個目錄的訪問,如下所示:

## test1 test2為兩個不同的目錄

linux-umlhem:/home/test/linux # ls test1

11.test 1.test

linux-umlhem:/home/test/linux # ls test2

22.test 2.test

linux-umlhem:/home/test/linux # ls -lid test1

1441802 drwx------ 2 root root 4096 feb 13 09:50 test1

linux-umlhem:/home/test/linux # ls -lid test2

1441803 drwx------ 2 root root 4096 feb 13 09:51 test2

## 執行mount --bind 將test1掛載到test2上,inode號都變為test1的inode

linux-umlhem:/home/test/linux # mount --bind test1 test2

linux-umlhem:/home/test/linux # ls -lid test1

1441802 drwx------ 2 root root 4096 feb 13 09:50 test1

linux-umlhem:/home/test/linux # ls -lid test2

1441802 drwx------ 2 root root 4096 feb 13 09:50 test2

linux-umlhem:/home/test/linux # ls test2

11.test 1.test

## 對test2的訪問或修改實際上是改動test1目錄

linux-umlhem:/home/test/linux # cd test2

linux-umlhem:/home/test/linux/test2 # touch 3.test

linux-umlhem:/home/test/linux/test2 # ls

11.test 1.test 3.test

linux-umlhem:/home/test/linux/test2 # cd ..

linux-umlhem:/home/test/linux # ls test1

11.test 1.test 3.test

## 解掛載後,test1目錄保持修改,test2保持不變

linux-umlhem:/home/test/linux # umount test2

linux-umlhem:/home/test/linux # ls test1

11.test 1.test 3.test

linux-umlhem:/home/test/linux # ls test2

22.test 2.test

## 將test2掛載到test1上

linux-umlhem:/home/test/linux # ls -lid test2

1441803 drwx------ 2 root root 4096 feb 13 09:51 test2

linux-umlhem:/home/test/linux # mount --bind test2 test1

linux-umlhem:/home/test/linux # ls -lid test1

1441803 drwx------ 2 root root 4096 feb 13 09:51 test1

linux-umlhem:/home/test/linux # ls -lid test2

1441803 drwx------ 2 root root 4096 feb 13 09:51 test2

linux-umlhem:/home/test/linux # ls test1

22.test 2.test

mount --bind test1 test2為例,當mount --bind命令執行後,linux將會把被掛載目錄的目錄項(也就是該目錄檔案的block,記錄了下級目錄的資訊)遮蔽,即test2的下級路徑被隱藏起來了(注意,只是隱藏不是刪除,資料都沒有改變,只是訪問不到了)。同時,核心將掛載目錄(test1)的目錄項記錄在記憶體裡的乙個s_root物件裡,在mount命令執行時,vfs會建立乙個vfsmount物件,這個物件裡包含了整個檔案系統所有的mount資訊,其中也會包括本次mount中的資訊,這個物件是乙個hash值對應表(hash值通過對路徑字串的計算得來),表裡就有 /test1 到 /test2 兩個目錄的hash值對應關係。

命令執行完後,當訪問 /test2下的檔案時,系統會告知 /test2 的目錄項被遮蔽掉了,自動轉到記憶體裡找vfs,通過vfsmount了解到 /test2 和 /test1 的對應關係,從而讀取到 /test1 的inode,這樣在 /test2 下讀到的全是 /test1 目錄下的檔案。

1.mount --bind連線的兩個目錄的inode號碼並不一樣,只是被掛載目錄的block被遮蔽掉,inode被重定向到掛載目錄的inode(被掛載目錄的inode和block依然沒變)。

2.兩個目錄的對應關係存在於記憶體裡,一旦重啟掛載關係就不存在了。

在韌體開發過程中常常遇到這樣的情況:為測試某個新功能,必需修改某個系統檔案。而這個檔案在唯讀檔案系統上(總不能為乙個小小的測試就重刷韌體吧),或者是雖然檔案可寫,但是自己對這個改動沒有把握,不願意直接修改。這時候mount --bind就是你的好幫手。 

假設我們要改的檔案是/etc/hosts,可按下面的步驟操作: 

1. 把新的hosts檔案放在/tmp下。當然也可放在硬碟或u盤上。 

2. mount --bind /tmp/hosts /etc/hosts       此時的/etc目錄是可寫的,所做修改不會應用到原來的/etc目錄,可以放心測試。測試完成了執行 umount /etc/hosts 斷開繫結。 

pythonpip使用方法 pip使用方法整理

匯出專案已安裝的pip包 pip list 檢視專案中安裝的包 pip freeze requirements.txt 將專案中所用到的第三方庫輸出到requirements.txt中 pip install 版本號 pip install i 本次使用清華源進行安裝 離線安裝第三方庫 一鍵安裝整個...

使用方法 離型劑使用方法

1 使用離型劑前必須確保模具乾淨,無粉塵 蠟垢 樹脂垢 油汙等殘留物質。可以使用專用的模具潔模劑進行清洗,不可用其他液體清洗,以免清洗不乾淨或損壞模具。另外,如果您的模具是新的,在清洗乾淨後,務必要對模具進行初步處理,用離型劑在模具表面噴 刷塗5 6次,之後方可正常投入生產。2 使用離型劑時,首先要...

使用方法 卡丁車輪胎使用方法

輪胎的使用方法很重要 卡丁車輪胎是卡丁車場日常執行中的主要易損件,但是輪胎的使用方法很重要。哪些情況下會縮短輪胎的使用壽命呢?一 跑道地面粗糙 有的跑道在鋪瀝青時,或者選錯了瀝青中石子的規格,或者碾壓得不夠平整,導致路面粗糙。有的跑道使用現成的水泥地,但表面粗糙或不平整。有的室內跑道,為了提高輪胎的...