Autofs自动挂在实现

Autofs介绍:

1
2
3
4
5
6
mount是用来挂载文件系统的,可以在系统启动的时候挂载也可以在系统启动后挂载。对于本地固定设
备,如硬盘可以使用mount挂载;而光盘、软盘、NFS、SMB等文件系统具有动态性,即需要的时候才有
必要挂载。光驱和软盘我们一般知道什么时候需要挂载,但NFS和SMB共享等就不一定知道了,即我们
一般不能及时知道NFS共享和SMB什么时候可以挂载。而autofs服务就提供这种功能,好像windows中的
光驱自动打开功能,能够及时挂载动态加载的文件系统。免去我们手动挂载的麻烦。要实现光驱,软盘
等的动态自动挂载,需要进行相关的配置。

Autofs特点:

1
2
3
4
  Autofs与Mount/Umount的不同之处在于,它是一种看守程序。如果它检测到用户正试图访问一个尚未
  挂接的文件系统,它就会自动检测该文件系统,如果存在,那么Autofs会自动将其挂接。另一方面,
  如果它检测到某个已挂接的文件系统在一段时间内没有被使用,那么Autofs会自动将其卸载。因此一
  旦运行了Autofs后,用户就不再需要手动完成文件系统的挂接和卸载。

Autofs常用配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Autofs需要从/etc/auto.master文件中读取配置信息。该文件中可以同时指定多个挂接点,由Autofs
来挂接文件系统。文件中的每个挂接点单独用一行来定义,每一行可包括3个部分,分别用于指定挂接
点位置,挂接时需使用的配置文件及所挂接文件系统在空闲多长时间后自动被卸载。例如在文件中包括
了如下一行:  
  /auto /etc/auto.misc --timeout 60
  
  其中第一部分指定一个安装点为/auto,第二部分指定该挂接点的配置文件为/etc/auto.misc,
  第三部分指定所挂接的文件系统在空闲60秒后自动被卸载。 
  文件/etc/auto.misc的示例如下:
  cd -fstype=iSO9660,ro :/dev/cdrom
  fd -fstype=msdos :/dev/fd0
  
  文件每一行都说明某一个文件系统如何被挂接。其中第一行指定将/dev/cdrom挂接在/auto/cd中,
  第二行指定将/dev/fd0挂接在/auto/fd中。每一行的第二个值-fstype是一个可选项,用来表明所
  挂接的文件系统的类型和挂接选项,在mount命令中能使用的挂接选项同样适用于-fstype。


废话不多说了,下面直接给出实战演示步骤


实验环境:

NFS 服务器:

IP:192.168.112.130

1
2
3
4
5
6
7
8
9
10
[root@node03 ~] # cat /etc/redhat-release 
CentOS release 6.6 (Final)
[root@node03 ~] # uname -r
2.6.32-504.el6.x86_64
[root@node03 ~] #
[root@node03 ~] # getenforce
Disabled
[root@node03 ~] # service iptables status
iptables: Firewall is not running.
[root@node03 ~] #

客户端:

IP:192.168.112.129

1
2
3
4
5
6
7
8
9
10
[root@node02 ~] # cat /etc/redhat-release 
CentOS release 6.6 (Final)
[root@node02 ~] # uname -r
2.6.32-504.el6.x86_64
[root@node02 ~] #
[root@node02 ~] # getenforce
Disabled
[root@node02 ~] # service iptables status
iptables: Firewall is not running.
[root@node02 ~] #

1、NFS服务器安装nfs服务:

1
2
3
4
5
6
7
8
9
10
11
[root@node03 ~] # yum install rpcbind nfs nfs-utils
[root@node03 ~] # service rpcbind start
Starting rpcbind: [  OK  ]
[root@node03 ~] #
[root@node03 ~] # service nfs start
Starting NFS services:  [  OK  ]
Starting NFS quotas: [  OK  ]
Starting NFS mountd: [  OK  ]
Starting NFS daemon: [  OK  ]
Starting RPC idmapd: [  OK  ]
[root@node03 ~] #

创建两个共享目录:cvxtmp和cvxtmp2

1
2
3
[root@node03 ~] # mkdir -p /cvxtmp
[root@node03 ~] # mkdir -p /cvxtmp2
[root@node03 ~] #

将磁盘/dev/sdb和/dev/sdc分别挂在到cvxtmp和cvxtmp2

1
2
3
4
5
6
7
8
[root@node03 ~] # df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2       ext4    97G  3.3G   89G   4% /
tmpfs          tmpfs  236M   12K  236M   1%  /dev/shm
/dev/sda1       ext4   976M   27M  898M   3%  /boot
/dev/sdb        ext4    50G   94M   47G   1%  /cvxtmp2
/dev/sdc        ext4   4.9T  8.4G  4.6T   1%  /cvxtmp
[root@node03 ~] #

2、修改nfs配置文件,将cvxtmp和cvxtmp2共享出去

1
2
3
4
5
6
7
8
9
[root@node03 ~] # cat /etc/exports 
/cvxtmp    *(rw,no_all_squash,no_subtree_check, sync )
/cvxtmp2   *(rw,no_all_squash,no_subtree_check, sync )
[root@node03 ~]
[root@node03 ~] # showmount -e 192.168.112.130
Export list  for  192.168.112.130:
/cvxtmp2  *
/cvxtmp   *
[root@node03 ~] #

3、安装autofs服务

[root@node02 ~]# yum install autofs -y

安装完成之后,在/etc/下会有几个文件,如下所示:

1
2
3
4
5
6
7
8
[root@node02 ~] # ls -l /etc/auto.*
-rw-r--r-- 1 root root   78 Aug  3 16:49  /etc/auto .home
-rw-r--r-- 1 root root  690 Aug  3 17:09  /etc/auto .master
-rw-r--r-- 1 root root  524 Mar 23 05:59  /etc/auto .misc
-rwxr-xr-x 1 root root 1260 Mar 23 05:59  /etc/auto .net
-rwxr-xr-x 1 root root  687 Mar 23 05:59  /etc/auto .smb
You have new mail  in  /var/spool/mail/root
[root@node02 ~] #

其中auto.master是主配置文件,之后我们要创建一个文件auto.home,然后将要挂载到本地的

分区的目录写入其中:

1
2
3
4
[root@node02 ~] # cat /etc/auto.home 
/cvxtmp   -rw,soft,intr node03: /cvxtmp
/cvxtmp2   -rw,soft,intr node03: /cvxtmp2
[root@node02 ~] #

此时编辑/etc/auto.master,将auto.home文件添加进去,autofs在启动时加载auto.home,"/-    /etc/auto.home"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@node02 ~] # cat /etc/auto.master 
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(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
/-       /etc/auto .home
#
# 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
[root@node02 ~] #

然后重启autofs服务:

1
2
3
4
[root@node02 ~] # service autofs restart
Stopping automount: [  OK  ]
Starting automount: [  OK  ]
[root@node02 ~] #

查看挂在情况,结果发现我们配置的挂在分区居然没有挂在过来,难道配置错误?

1
2
3
4
5
6
7
8
9
10
11
[root@node02 ~] # df -Th
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2       ext4    97G  3.4G   89G   4% /
tmpfs          tmpfs  236M   12K  236M   1%  /dev/shm
/dev/sda1       ext4   976M   27M  898M   3%  /boot
[root@node02 ~]
接下来我们进入挂在的目录cvxtmp和cvxtmp2
[root@node02 ~] # cd /cvxtmp
[root@node02 cvxtmp] # cd ..
[root@node02 /] # cd cvxtmp2
[root@node02 cvxtmp2] #

然后再查看挂在情况:

1
2
3
4
5
6
7
8
[root@node02 cvxtmp2] # df -Th
Filesystem      Type   Size  Used Avail Use% Mounted on
/dev/sda2        ext4    97G  3.4G   89G   4% /
tmpfs           tmpfs  236M   12K  236M   1%  /dev/shm
/dev/sda1        ext4   976M   27M  898M   3%  /boot
node03: /cvxtmp   nfs    4.9T  8.4G  4.6T   1%  /cvxtmp
node03: /cvxtmp2  nfs     50G   94M   47G   1%  /cvxtmp2
[root@node02 cvxtmp2] #

发现挂在居然已经生效。此时到NFS服务端向这里两个目录分别写入文件cvxtmp.txt和cvxtmp2.txt

1
2
3
4
5
6
7
[root@node03 /] # echo cvxtmp > /cvxtmp/cvxtmp.txt
[root@node03 /] # echo cvxtmp2 > /cvxtmp2/cvxtmp2.txt
[root@node03 /] # cat /cvxtmp/cvxtmp.txt 
cvxtmp
[root@node03 /] # cat /cvxtmp2/cvxtmp2.txt 
cvxtmp2
[root@node03 /] #

然后再切换到挂载端查看。

1
2
3
4
5
[root@node02 ~] # cat /cvxtmp/cvxtmp.txt 
cvxtmp
[root@node02 ~] # cat /cvxtmp2/cvxtmp2.txt 
cvxtmp2
[root@node02 ~] #

 

发现挂在确实生效,至此autofs自动挂在功能已经实现。




      本文转自027ryan  51CTO博客,原文链接:http://blog.51cto.com/ucode/1953389,如需转载请自行联系原作者







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值