萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

RHEL 5 使用 Autofs 自動掛載 NFS/本地磁碟:配置 auto.master 與 map 檔案實戰

Autofs 按需自動掛載/卸載檔案系統,減少常駐掛載開銷、解決開機掛載順序依賴、支援 NFS/CIFS/本地裝置。本文以將 /dev/sdb 掛載至 /data/abc 為例,示範 /etc/auto.master 與 map 檔案配置、觸發掛載驗證、超時自動卸載行為。

Autofs 架構圖

核心架構

  • automount 守護進程:監聽掛載點存取,觸發掛載。
  • /etc/auto.master:主映射表,定義頂層掛載點對應 map 檔案。
  • Map 檔案:定義子目錄與裝置/網路路徑對應(如 /etc/auto.mytest)。
  • 超時卸載:預設 300 秒無存取自動卸載(可調 timeout 參數)。

配置步驟

1. 編輯主映射 /etc/auto.master

# vi /etc/auto.master
/misc  /etc/auto.misc
/mytest /etc/auto.mytest
  • /mytest:父掛載點(自動建立)。
  • /etc/auto.mytest:對應 map 檔案。

2. 建立 map 檔案 /etc/auto.mytest

# vi /etc/auto.mytest
abc -fstype=ext3 :/dev/sdb
  • abc:子目錄名(完整路徑 /mytest/abc)。
  • -fstype=ext3:檔案系統類型與掛載選項。
  • :/dev/sdb:裝置路徑(冒號開頭表示本地裝置)。

3. 啟動 autofs 並驗證

# service autofs start
# chkconfig autofs on

# 觸發掛載
# cd /mytest/abc
# ls
# df -h /mytest/abc

首次 ls /mytest 顯示空目錄,進入 abc 才會觸發掛載並顯示內容。

NFS 遠端掛載範例

# /etc/auto.nfs
data  -fstype=nfs4,rw,soft,intr  nfs-server:/export/data
backup -fstype=nfs,vers=3,ro     192.168.1.100:/backup

主映射加入:/nfs /etc/auto.nfs。

進階選項

選項說明
--timeout=600自動卸載等待秒數(預設 300)
--ghost建立空目錄佔位,ls 即可見
browse允許瀏覽未掛載目錄
nobind禁止 bind 掛載

常見問題

  • 掛載點忙無法卸載:lsof /mytest/abc 找出佔用進程、fuser -km /mytest/abc 強制釋放。
  • key "abc" not found in map:map 檔案語法錯誤、權限不足(需 644)、未重載 autofs(service autofs reload)。
  • SELinux 阻擋:restorecon -Rv /mytest、檢查 audit.log。

systemd 時代 (RHEL 7+)

# systemctl enable --now autofs
# systemctl status autofs
# journalctl -u autofs -f

Autofs 讓儲存資源按需接入,是大規模 NFS 客戶端、移動介質、多租戶隔離的標準解決方案。

copyright © 萬盛學電腦網 all rights reserved