Autofs自動挂載服務
Autofs簡介
mount是用來挂載檔案系統的,可以在系統啟動的時候挂載也可以在系統啟動後挂載。對于本地固定裝置,如硬碟可以使用mount挂載;而CD光牒、軟碟、NFS、SMB等檔案系統具有動态性,即需要的時候才有必要挂載。光驅和軟碟我們一般知道什麼時候需要挂載,但NFS和SMB共享等就不一定知道了,即我們一般不能及時知道NFS共享和SMB什麼時候可以挂載。而autofs服務就提供這種功能,好像windows中的光驅自動打開功能,能夠及時挂載動态加載的檔案系統。免去我們手動挂載的麻煩。要實作光驅,軟碟等的動态自動挂載,需要進行相關的配置
Autofs特點
-
優點
autofs是一個守護程序,它會在背景檢測使用者是否要通路某一個還未挂載的檔案系統,autofs會自動檢測該檔案系統是否存在,如果存在則進行挂載,如果使用者過了一段時間還沒有使用該檔案系統,autofs自動将其解除安裝,節省人力維護的成本,以及節省伺服器資源
-
缺點
當使用者請求時候才挂載檔案系統,如果高并發的業務場景,大量的使用者并發通路檔案系統,autofs突然進行大量挂載,會給伺服器造成很大的壓力,一次一些高并發場景下,甯願保持持續挂載,也不用
安裝Autofs服務
[root@localhost ~]# yum -y install autofs
Updating Subscription Management repositories.
Last metadata expiration check: 0:09:58 ago on Sun 14 Mar 2021 08:41:40 PM CST.
Dependencies resolved.
====================================================================================
Package Architecture Version Repository Size
====================================================================================
Installing:
autofs x86_64 1:5.1.4-43.el8 base 781 k
====================================================================================
Install 1 Package
Total download size: 781 k
Installed size: 3.6 M
Downloading Packages:
autofs-5.1.4-43.el8.x86_64.rpm 1.8 MB/s | 781 kB 00:00
------------------------------------------------------------------------------------
Total 1.8 MB/s | 781 kB 00:00
Running transaction check
Running transaction
Preparing : 1/1
Installing : autofs-1:5.1.4-43.el8.x86_64 1/1
Installed products updated.
Installed:
autofs-1:5.1.4-43.el8.x86_64
Complete!
//設定開機自啟
[root@localhost ~]# systemctl start autofs
[root@localhost ~]# systemctl enable autofs
Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.
修改配置檔案(主配置+子配置)
autofs服務程式的主配置檔案中需要按照挂載目錄 子配置檔案的格式寫入參數。挂載目錄是裝置要挂載位置的上一級目錄
定義主配置檔案
[root@localhost ~]# vim /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
/media /etc/cdrom.misc #添加這行配置
定義子配置檔案
[root@localhost ~]# vim /etc/cdrom.misc
cdrom -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
//重新開機autofs
[root@localhost ~]# systemctl restart autofs
測試
//檢視挂載情況
#cdrom沒有被挂載
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 883M 0 883M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 17M 884M 2% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 47G 1.9G 46G 4% /
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 181M 0 181M 0% /run/user/0
//進入media/cdrom觸發挂載
[root@localhost ~]# cd /media/cdrom
[root@localhost cdrom]# ls
AppStream EULA images RPM-GPG-KEY-redhat-beta
BaseOS extra_files.json isolinux RPM-GPG-KEY-redhat-release
EFI GPL media.repo TRANS.TBL
#可以看到cdrom被挂載
[root@localhost cdrom]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 883M 0 883M 0% /dev
tmpfs 901M 0 901M 0% /dev/shm
tmpfs 901M 17M 884M 2% /run
tmpfs 901M 0 901M 0% /sys/fs/cgroup
/dev/mapper/rhel-root 47G 1.9G 46G 4% /
/dev/sda1 1014M 179M 836M 18% /boot
tmpfs 181M 0 181M 0% /run/user/0
/dev/sr0 7.9G 7.9G 0 100% /media/cdro