Autofs自动挂在实现

Autofs介绍:

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

Autofs特点:

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

Autofs常用配置:

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

[[email protected] ~]# cat /etc/redhat-release 
CentOS release 6.6 (Final)
[[email protected] ~]# uname -r
2.6.32-504.el6.x86_64
[[email protected] ~]#

客户端:

IP:192.168.112.129

[[email protected] ~]# cat /etc/redhat-release 
CentOS release 6.6 (Final)
[[email protected] ~]# uname -r
2.6.32-504.el6.x86_64
[[email protected] ~]#

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

[[email protected] ~]# yum install rpcbind nfs nfs-utils
[[email protected] ~]# service rpcbind start
Starting rpcbind: [  OK  ]
[[email protected] ~]#
[[email protected] ~]# service nfs start
Starting NFS services:  [  OK  ]
Starting NFS quotas: [  OK  ]
Starting NFS mountd: [  OK  ]
Starting NFS daemon: [  OK  ]
Starting RPC idmapd: [  OK  ]
[[email protected] ~]#

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

[[email protected] ~]# mkdir -p /cvxtmp
[[email protected] ~]# mkdir -p /cvxtmp2
[[email protected] ~]#

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

[[email protected] ~]# 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
[[email protected] ~]#

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

[[email protected] ~]# cat /etc/exports 
/cvxtmp   *(rw,no_all_squash,no_subtree_check,sync)
/cvxtmp2  *(rw,no_all_squash,no_subtree_check,sync)
[[email protected] ~]# 
[[email protected] ~]# showmount -e 192.168.112.130
Export list for 192.168.112.130:
/cvxtmp2 *
/cvxtmp  *
[[email protected] ~]#

3、安装autofs服务

[[email protected] ~]# yum install autofs -y

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

[[email protected] ~]# 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
[[email protected] ~]#

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

分区的目录写入其中:

[[email protected] ~]# cat /etc/auto.home 
/cvxtmp  -rw,soft,intr node03:/cvxtmp
/cvxtmp2  -rw,soft,intr node03:/cvxtmp2
[[email protected] ~]#

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

[[email protected] ~]# 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
[[email protected] ~]#

然后重启autofs服务:

[[email protected] ~]# service autofs restart
Stopping automount: [  OK  ]
Starting automount: [  OK  ]
[[email protected] ~]#

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

[[email protected] ~]# 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
[[email protected] ~]# 
接下来我们进入挂在的目录cvxtmp和cvxtmp2
[[email protected] ~]# cd /cvxtmp
[[email protected] cvxtmp]# cd ..
[[email protected] /]# cd cvxtmp2
[[email protected] cvxtmp2]#

然后再查看挂在情况:

[[email protected] 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
[[email protected] cvxtmp2]#

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

[[email protected] /]# echo cvxtmp > /cvxtmp/cvxtmp.txt
[[email protected] /]# echo cvxtmp2 > /cvxtmp2/cvxtmp2.txt
[[email protected] /]# cat /cvxtmp/cvxtmp.txt 
cvxtmp
[[email protected] /]# cat /cvxtmp2/cvxtmp2.txt 
cvxtmp2
[[email protected] /]#

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

[[email protected] ~]# cat /cvxtmp/cvxtmp.txt 
cvxtmp
[[email protected] ~]# cat /cvxtmp2/cvxtmp2.txt 
cvxtmp2
[[email protected] ~]#

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

时间: 2024-08-27 14:52:07

Autofs自动挂在实现的相关文章

Autofs 自动挂载文件系统服务

Linux下autofs的使用 Autofs与Mount/Umount的不同之处在于,它是一种看守程序.当用户用到资源时自动挂接,超出指定时间则自动卸载. 1./etc/auto.master文件 该文件设置了挂载点和挂载点配置文件. -------------------------------------------------- #挂载点 配置文件 /mnt /etc/auto.misc ---------------------------------------------------

Android之——自动挂断电话的实现

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47072451 通过<Android之--AIDL小结>与<Android之--AIDL深入>两篇博文,相信大家已经对Android AIDL有了一定的了解,下面,我们就利用Android的AIDL实现自动挂断电话的功能,好了,不多说了,我们直接进入主题. 1.准备AIDL文件 挂断电话的AIDL文件都是Android自带的文件,我们可以从Android的源代码中找到

autofs自动挂载

autofs:自动挂载器自动挂载器是一个监视目录的守护进程,并在目标子目录被引用时,自动执行预定义的NFS挂载自动挂载器由autofs服务脚本管理自动挂载器由auto.master配置文件进行配置,该文件引用了一个按惯例称作/etc/auto.misc或其他类似名称的二级配置文件autofs与NFS两者之间配后用的还是比较多的 mount命令参数非常多,如下为与NFS相关的参数.(1)-a:把/etc/fstab中列出的路径全部挂载.(2)-t:需要mount的类型,如nfs等.(3)-r:将m

autofs 自动挂载.

autofs 自动挂载. 操作环境:redhat 6 一.autofs 说明 自动挂载器是一个监视目录的守护进程,并在目标子目录被引用时,自动执行预定义的挂载 自动挂载器由autofs服务脚本管理 自动挂载器由auto.master配置文件进行配置,该文件引用了一个按惯例称作/etc/auto.misc 二.安装autofs [[email protected] home]# yum install autofs 三.配置autofs [[email protected] home]# rpm

NFS网络文件系统+autofs自动挂载

(1)实例:服务器端使用NFS服务输出/cxm目录为只读,/cxm下的目录为读写,并且开机自启动:客户端使用autofs服务自动挂载服务器输出的目录,并且1分钟无操作自动卸载. 服务器端:rhel6.3(192.168.200.122) [[email protected] 桌面]# mkdir /cxm   #新建父目录 [[email protected] 桌面]# mkdir /cxm/zyk  #新建子目录 [[email protected] 桌面]# ll -d /cxm  #查看父

Android开发之黑名单来电自动挂断

本实例允许用户动态添加号码到黑名单,并实现黑名单来电自动挂断.程序通过创建PhoneStateListener监听器来监听TelephonyManager的通话状态来实现该功能. 由于自Android 10之后Android不再对外公开挂断电话的API,如果需要挂断电话必须使用AIDL与电话管理Service进行通信,并调用服务中的API实现结束电话. 为了调用远程的AIDL Service,开发者需要将Android源码中的如下两个文拷到指定位置: com.android.internal.t

Linux 第八周上课笔记(2) nfs,ldap网络帐号,autofs自动挂载服务

######################NFS############################ nfs 手动挂载方式 1)yum install nfs-utils 2)showmount -e ip                       ##识别该ip下的共享 3)mount ip:/sharedir /mountpoint        ##挂载点 挂载目录 永久挂载方式 方法一 vim /etc/fstab 172.25.254.250:/nfsshare/nfs1 /m

RHCE7.0答案之Autofs自动挂载

Autofs自动挂载: yum -y install autofs vim /etc/auto.master  在文件中添加下面行 /home/guests /etc/auto.tianyun vim /etc/auto.tianyun 子挂载点监控 ldapuser0 -rw,sync classroom:/home/guests/ldapuser0 systemctl enable autofs.service systemctl restart autofs.service ls /hom

Autofs 自动挂载服务

autofs  自动挂载服务 需求:把下面这两条命令做成自动挂载 172.16.2.6:/share/soft /share/soft 172.16.2.6:/share/iso /share/iso 客户端: 1.# mkdir/share (挂载点) 2.# vim /etc/auto.master /share    /etc/auto.share  --后面这个文件不存在,自定义的名字 3.# vim /etc/auto.share soft    -      172.16.2.6:/