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

/etc/auto.mytest)。timeout 參數)。/etc/auto.master# vi /etc/auto.master
/misc /etc/auto.misc
/mytest /etc/auto.mytest
/mytest:父掛載點(自動建立)。/etc/auto.mytest:對應 map 檔案。/etc/auto.mytest# vi /etc/auto.mytest
abc -fstype=ext3 :/dev/sdb
abc:子目錄名(完整路徑 /mytest/abc)。-fstype=ext3:檔案系統類型與掛載選項。:/dev/sdb:裝置路徑(冒號開頭表示本地裝置)。# service autofs start
# chkconfig autofs on
# 觸發掛載
# cd /mytest/abc
# ls
# df -h /mytest/abc
首次 ls /mytest 顯示空目錄,進入 abc 才會觸發掛載並顯示內容。
# /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)。restorecon -Rv /mytest、檢查 audit.log。# systemctl enable --now autofs
# systemctl status autofs
# journalctl -u autofs -f
Autofs 讓儲存資源按需接入,是大規模 NFS 客戶端、移動介質、多租戶隔離的標準解決方案。