下面是關於linux下面自動掛載器的討論,node
自動掛載器的介紹(automounter)linux
自動掛載器就是當咱們網絡中有一臺NFS Server的時候,客戶端須要去掛載NFS Server上面的資源,若是將NFS的掛載信息寫在/etc/fstab這個文件裏面,那麼客戶端只要是啓動計算機,就是自動掛載NFS資源。可能有些客戶端不須要去掛載NFS的資源,這樣就會浪費資源。而自動掛載器就能夠幫咱們實現,當客戶端須要去訪問的時候,才掛載,不須要訪問的時候,資源是斷開的,因此學習自動掛載器是頗有必要的。vim
要想實現自動掛載的功能,必須安裝autofs這個包網絡
自動掛載是經過兩個文件來實現的less
/etc/auto.master學習
/etc/auto.miscui
如今咱們打開/etc/auto.master這個文件,看下里面是怎麼定義的。spa
[root@localhost ~]# vim /etc/auto.masterrest
# $Id: auto.master,v 1.4 2005/01/04 14:36:54 raven Exp $orm
#
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
/misc/etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
#"nosuid" and "nodev" options unless the "suid" and "dev"
#options are explicitly given.
#
/net-hosts
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
能夠看到,在/etc/auto.master裏面是這樣定義的,
咱們先看這句話
/misc/etc/auto.misc
將/etc/auto.misc裏面的內容掛載到/misc這個目錄下面去。
咱們再來開到/etc/auto.misc這個文件
[root@localhost ~]# vim /etc/auto.misc
#
# $Id: auto.misc,v 1.2 2003/09/29 08:22:35 raven Exp $
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage
cd-fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
# the following entries are samples to pique your imagination
#linux-ro,soft,intrftp.example.org:/pub/linux
#boot-fstype=ext2:/dev/hda1
#floppy-fstype=auto:/dev/fd0
#floppy-fstype=ext2:/dev/fd0
#e2floppy-fstype=ext2:/dev/fd0
#jaz-fstype=ext2:/dev/sdc1
#removable-fstype=ext2 :/dev/hdd
~
在/etc/auto.misc文件定義了將cdrom裏面的東西掛載到cd目錄下面去
注意,這裏的cd目錄是以/misc爲根的,也就是/misc/cd目錄。
咱們將這兩個文件整合一下,意思就是說將cdrom裏面的東西掛載到/misc/cd目錄下面去,就是這個意思。
如今咱們將一張光盤放入光驅裏面,看下可否自動掛載起來。
[root@localhost ~]# cd /misc/
[root@localhost misc]# ls
[root@localhost misc]#
咱們如今已經進入了這個目錄,能夠看到,這個目錄裏面什麼東西都沒有,剛纔哪兩個文件定義說,是掛載到/misc/cd目錄下面,如今咱們進入cd目錄。
[root@localhost ~]# cd /misc/
[root@localhost misc]# ls
[root@localhost misc]# cd cd
[root@localhost cd]# ls
AUTORUN.INFHD4.GHOPESETUPREADME.TXTWINPE.XPE
AXPEOSGHOST.EXEPESOFTSYSTEMWNPEFONT.BIN
[root@localhost cd]# cd ..
[root@localhost misc]# ls
cd
[root@localhost misc]#
能夠看到,咱們光盤裏面的東西就被掛載到/misc/cd目錄下面去了。
這個自動掛載的功能是系統默認就有了的,下面咱們根據剛纔的例子來學習如何利用自動掛載器的功能來實現掛載NFS網絡資源。
首先編輯/etc/auto.master這個文件,
auto.master 文件定義本地掛載點. 本地掛載點目錄/mnt
編輯auto.master 配置文件以下:
#
# $Id: auto.master,v 1.4 2005/01/04 14:36:54 raven Exp $
#
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
/misc/etc/auto.misc
/mnt/etc/auto.nfs
/etc/auto.nfs這個文件能夠本身命名,在/etc/auto.misc這個文件定義也是能夠的。
複製一個模板文件auto.misc成auto.nfs
這個文件名必定要與/etc/master中定義的一致。
[root@localhost ~]#
[root@localhost ~]# cp /etc/auto.misc /etc/auto.nfs
編輯/etc/auto.nfs配置文件以下
#
# $Id: auto.misc,v 1.2 2003/09/29 08:22:35 raven Exp $
#
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# Details may be found in the autofs(5) manpage
nfs192.168.0.254:/var/ftp/pub
這個nfs目錄在/mnt下面不須要有
重啓一下服務
[root@localhost ~]# service autofs restart
Stopping automount:[OK]
Starting automount:[OK]
[root@localhost ~]#
服務啓動成功。
[root@localhost ~]# df -h
FilesystemSizeUsed Avail Use% Mounted on
/dev/sda33.9G3.8G0 100% /
/dev/sda6494M11M458M3% /home
/dev/sda23.9G118M3.6G4% /var
/dev/sda199M14M81M15% /boot
tmpfs188M0188M 0% /dev/shm
[root@localhost ~]#
如今能夠df –h 能夠看到的nfs網絡資源是沒有掛載過來的
如今進入/mnt目錄裏面什麼東西也沒有
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
[root@localhost mnt]#
當咱們進入nfs目錄的時候,NFS網絡資源是否會掛載過來呢
[root@localhost mnt]# cd nfs
[root@localhost nfs]# ls
ClusterClusterStorage fstabgrub.confServerserver.repoVT
[root@localhost nfs]# cd ..
[root@localhost mnt]# ls
nfs
[root@localhost mnt]#
當進入nfs目錄的時候,網絡資源已經被自動的掛載過來了。經過df- h 命令也能看到的nfs網絡資源被掛載到/mnt/nfs下面了
[root@localhost ~]# df -h
FilesystemSizeUsed Avail Use% Mounted on
/dev/sda33.9G3.8G0 100% /
/dev/sda6494M11M458M3% /home
/dev/sda23.9G118M3.6G4% /var
/dev/sda199M14M 81M15% /boot
tmpfs188M0188M0% /dev/shm
192.168.0.254:/var/ftp/pub
9.5G2.9G6.2G32% /mnt/nfs
[root@localhost ~]#
如今咱們查看一下autofs的全局配置文件,
[root@localhost ~]# vim /etc/sysconfig/autofs
# TIMEOUT - set the default mount timeout (default 600).
#
TIMEOUT=300
#
在這裏有個TIMEOUT=300,就是當300S內不作任何操做,那麼自動掛載就將消失,當再次進入nfs目錄的時候,NFS網絡資源又會被自動掛載過來。