【Linux】autofs自动挂载



前言

在Linux中,若要访问硬件资源,则需使用mount命令对其进行挂载(或者写入/etc/fstab文件开机自动挂载),将硬件资源与本地目录相关联,然后才能使用该存储介质。除此之外,使用Samba或NFS之类的服务,也需要对远程存储设备进行挂载。
但若挂载的资源过多,则会给网络资源和服务器资源造成一定的负载,从而降低了服务器性能。


一、简介

为了解决这个问题,可以使用autofs服务。
autofs服务是一种系统守护进程,我们可以把挂载信息写入其配置文件中。若用户不访问对应的存储介质,则系统不会进行挂载;而当用户去尝试访问该存储介质时,autofs则会自动对其进行挂载操作(即动态挂载)。整个过程对用户而言是透明的。

二、autofs安装与配置

这边先对autofs进行安装。配置本地镜像库(此处不做介绍),手动挂载镜像文件,以YUM方式安装autofs:

[root@service ~]# mount /dev/sr0 /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@service ~]# yum install -y autofs

服务安装完成后,会得到这些文件或目录:

[root@service ~]# ls /etc/auto*
/etc/autofs.conf            /etc/auto.master  /etc/auto.net
/etc/autofs_ldap_auth.conf  /etc/auto.misc    /etc/auto.smb

/etc/auto.master.d:

autofs服务的主配置文件是/etc/auto.master,内容如下:

[root@service ~]# 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
#
# Include /etc/auto.master.d/*.autofs
# The included files must conform to the format of this file.
#
+dir:/etc/auto.master.d
#
# 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

我们在auto.master文件中添加自定义挂载的信息

[root@service ~]# vi /etc/auto.master
...
/media   /etc/iso.misc            # 添加镜像挂载,指向自定义的子文件
/misc   /etc/auto.misc
...
...

之后,我们创建/etc/iso.misc文件,并在其写入挂载点、挂载格式,设置位置等信息

[root@service ~]# vi /etc/iso.misc
# 挂载名称   磁盘类型               指定挂资源文件位置
iso       -fstype=iso9660,defaults :/dev/sr0

配置完成后,就需要将服务拉起(或服务重启):

[root@service ~]# systemctl start autofs
[root@service ~]# systemctl enable autofs
Created symlink from /etc/systemd/system/multi-user.target.wants/autofs.service to /usr/lib/systemd/system/autofs.service.
[root@service ~]# systemctl status autofs
● autofs.service - Automounts filesystems on demand
   Loaded: loaded (/usr/lib/systemd/system/autofs.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-03-22 22:53:49 CST; 4s ago

三、挂载效果验证

接下来,对autofs服务的挂载验证。
先来看下磁盘空间,此时并未有镜像文件挂载上去:

[root@service ~]# df -h
Filesystem                     Size  Used Avail Use% Mounted on
devtmpfs                       282M     0  282M   0% /dev
tmpfs                          294M     0  294M   0% /dev/shm
tmpfs                          294M  5.6M  288M   2% /run
tmpfs                          294M     0  294M   0% /sys/fs/cgroup
/dev/mapper/centos_asdfv-root   17G  2.9G   15G  17% /
/dev/sda1                     1014M  137M  878M  14% /boot
tmpfs                           59M     0   59M   0% /run/user/0

当我们使用cd命令进入到自定义的挂载目录后,此时再看磁盘空间,发现已自动将/dev/sr0挂载到/media/iso目录上:

[root@service ~]# cd /media/iso
[root@service iso]# df -h        
Filesystem                     Size  Used Avail Use% Mounted on
devtmpfs                       282M     0  282M   0% /dev
tmpfs                          294M     0  294M   0% /dev/shm
tmpfs                          294M  5.6M  288M   2% /run
tmpfs                          294M     0  294M   0% /sys/fs/cgroup
/dev/mapper/centos_asdfv-root   17G  2.9G   15G  17% /
/dev/sda1                     1014M  137M  878M  14% /boot
tmpfs                           59M     0   59M   0% /run/user/0
/dev/sr0                       4.4G  4.4G     0 100% /media/iso
[root@service iso]# ls
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

autofs的默认挂载超时时间为300秒,可在配置文件/etc/autofs.conf 中看到:

# timeout - set the default mount timeout in secons. The internal
#           program default is 10 minutes, but the default installed
#           configuration overrides this and sets the timeout to 5
#           minutes to be consistent with earlier autofs releases.
#
timeout = 300

此外,我们也可以在auto.master设置自定义挂载点时同时指定超时时间(10秒):

[root@service ~]# vi /etc/auto.master
/media  /etc/iso.misc --timeout=10

[root@service ~]# systemctl restart autofs

四、总结

对于经常需要挂载的介质资源,我们就可以使用autofs服务来进行自动挂载,既节省挂载操作时间又能节省相关网络或服务器性能资源。服务的配置较为简单,简单几行就能立马实现自动挂载,方便我们日常的运维操作。


  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值