Linux之autofs自动挂载服务

目录

Linux之autofs自动挂载服务

产生原因

安装

配置文件分析

文件路径

作用

etc/auto.master文件内容格式

挂载参数

案例

案例1 --- 服务器创建共享目录,客户端实现自动挂载

案例2 --- 自动挂载光盘


Linux之autofs自动挂载服务

产生原因

        在一般NFS文件系统的使用过程中,如果客户端要使用服务端所提供的文件系统,可以在 /etc/fstab中设置开机时自动挂载,也可以在登录系统后手动利用mount来挂载

        由于网络的问题,NFS服务器与客户端的连接不会一直存在,当我们挂载了NFS服务器之后,任何一方脱机都可能造成另外一方等待超时,如果在资源挂载后长期不使用,也会造成服务器硬件资源的浪费

        为了解决这样的问题,就出现了下面的想法:

                当客户端在有使用NFS文件系统的需求时才让系统自动挂载

                当NFS文件系统使用完毕后(autofs默认自动卸载时间为300s即5分钟),让NFS自动卸载

        autofs 自动挂载服务可以帮我们解决这一问题,该服务是运行在客户端的一种 Linux 系统守护进程,在用户需要使用该文件系统时才去动态挂载,从而节约了网络资源和服务器的硬件资源

安装

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

 

配置文件分析

文件路径

        /etc/auto.master

作用

        处于生产环境中的 Linux 服务器,一般会同时管理许多设备的挂载操作。如果把这些设备挂载信息都写入到 autofs 服务的主配置文件中,无疑会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容,则需要将挂载的设备独立配置形成子文件主配置文件只存储配置挂载设置的文件名

etc/auto.master文件内容格式

        挂载目录 子配置文件

        挂载目录 --- 不需要事先存在,因为autofs会主动建立该目录

        子配置文件 --- 文件名可自定义       

        例子 --- /nfs    /etc/auto.nfs

子配置文件由自己创建,内容格式:

        本地挂载目录 [-挂载参数] 服务器地址:目录

        例子 --- testmnt    192.168.48.130:/data

挂载参数

参数 功能
fg/bg 当执行挂载时,该挂载行为会在前台(fg)还是后台(bg)执行,若在前台执行,则mount会持续尝试挂载,直到成功或time out为止;若为后台执行,则mount会在后台持续多次进行mount,而不会影响到前台的程序运行。
soft/hard hard表示当两者之间的任何一台主机脱机,则RPC会持续地呼叫,直到对方恢复连接为止。如果是soft的话,那RPC会在time out后重复呼叫,而非持续呼叫
intr 当使用上面提到的hard方式挂载时,若加上intr这个参数,则当RPC持续呼叫时,该次的呼叫是可以被中断的
rsize/wsize 读出(rsize)与写入(wsize)的区块大小。这个设置值可以影响客户端与服务器

案例

案例1 --- 服务器创建共享目录,客户端实现自动挂载

第一步:服务端server主机新建共享目录并编辑nfs配置文件

[root@localhost ~]# mkdir /data
[root@localhost ~]# chmod -Rf 777 /data/
[root@localhost ~]# ls /data/
[root@localhost ~]# echo "this is test " > /data/file.txt
[root@localhost ~]# ls /data/
file.txt
[root@localhost ~]# vim /etc/exports
#编写以下内容
/data           *(rw,sync,all_squash)

第二步: 服务端server主机启动服务,注意:先启动rpcbind服务

[root@localhost ~]# systemctl  start   rpcbind
[root@localhost ~]# systemctl  start   nfs-server
[root@localhost ~]# systemctl  enable  rpcbind
[root@localhost ~]# systemctl  enable  nfs-server

 第三步:客户端node1操作,编辑自动挂载的主配置文件

        计划本地挂载目录:/nfs/testmnt

[root@localhost ~]# vim /etc/auto.master
# 编辑第7行,修改为如下
/nfs    /etc/auto.nfs

# /nfs为最终挂载目录的第一级
# /etc/auto.nfs为自动挂载的自配置文件,文件名任意命名

[root@localhost ~]# showmount -e 192.168.149.128

[root@localhost nfs1]# vim /etc/auto.nfs    # 编辑自动挂载的子配置文件
#编辑以下内容
testmnt   192.168.149.128:/data

第四步:客户端node1主机重启服务

[root@localhost ~]# systemctl start autofs
[root@localhost ~]# systemctl enable autofs

第五步:客户端node1主机测试

[root@localhost nfs]# df -h   # 查看系统挂载信息

[root@localhost nfs]# cd /nfs #自动创建该目录
[root@localhost nfs]# ls
[root@localhost nfs]# cd testmnt
[root@localhost testmnt]# ls
file.txt

案例2 --- 自动挂载光盘

第一步:修改autofs的主配置文件及子配置文件

# 计划本地光盘挂载目录:/media/cdrom
[root@localhost ~]# vim /etc/auto.master
/media  /etc/iso.aa
# /media为计划挂载目录的第一级别  
# /etc/iso.aa为子配置文件

第二步:编辑子配置文件

[root@localhost ~]# vim /etc/iso.aa
cdrom   -fstype=iso9660,ro,nosuid,nodev  :/dev/sr0    # 冒号前有空格

 第三步:启动服务

[root@localhost ~]# systemctl restart autofs
[root@localhost ~]# systemctl enable autofs

第四步:测试,注意 --- 若光盘已经挂载需要先卸载

[root@localhost ~]# df  -h   # 查看光盘是否已经挂载
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               4.0M     0  4.0M    0% /dev
tmpfs                  968M     0  968M    0% /dev/shm
tmpfs                  388M  9.5M  378M    3% /run
/dev/mapper/rhel-root   16G  4.2G   12G   27% /
/dev/nvme0n1p1         395M  235M  160M   60% /boot
tmpfs                  194M  104K  194M    1% /run/user/0
/dev/sr0               8.5G  8.5G     0  100% /run/media/root/RHEL-9-1-0-BaseOS-x86_64  # 显示已挂载
[root@localhost ~]# umount  /dev/sr0  # 先卸载光盘设备
[root@localhost ~]# df  -h  # 再次查看

[root@localhost ~]# cd  /media/
[r[root@localhost media]# cd  cdrom   # 触发自动挂载
[r[root@localhost cdrom]# df  -h
文件系统               容量  已用  可用 已用% 挂载点
devtmpfs               4.0M     0  4.0M    0% /dev
tmpfs                  968M     0  968M    0% /dev/shm
tmpfs                  388M  9.5M  378M    3% /run
/dev/mapper/rhel-root   16G  4.2G   12G   27% /
/dev/nvme0n1p1         395M  235M  160M   60% /boot
tmpfs                  194M  104K  194M    1% /run/user/0
/dev/sr0               8.5G  8.5G     0  100% /media/cdrom  # 已经自动挂载

猜你喜欢

转载自blog.csdn.net/qq_57289939/article/details/132697606