2.13. autofs自动挂载服务

autofs服务程序是一种Linux系统守护进程,当检测到用户视图访问一个尚未挂载的文件系统时,
将自动挂载该文件系统。换句话说,我们将挂载信息填入/etc/fstab文件后,
系统在每次开机时都自动将其挂载,而autofs服务程序则是在用户需要使用该文件系统时才去动态挂载,
从而节约了网络资源和服务器的硬件资源。
[root@localhost ~]# vim /etc/auto.master
  1 #
  2 # Sample auto.master file
  3 # This is an automounter map and it has the following format
  4 # key [ -mount-options-separated-by-comma ] location
  5 # For details of the format look at autofs(5).
  6 #
  7 /misc   /etc/auto.misc
  8 /media /etc/cdrom.misc

子配置文件中应按照“挂载目录 挂载文件类型及权限 :设备名称”的格式写入参数, 例如想要把设备挂载到/media/cdrom目录中,

则此时写iso即可,而-fstype为文件系统格式参数,iso9660为光盘系统设备格式,ro、nosuid及nodev为光盘设备具体的权限参数, 最终/dev/cdrom则是定义要挂载的设备名称, 配置完成后顺手再把autofs服务程序启动并加入到开机启动项中吧:

[root@localhost ~]# vim /etc/cdrom.misc
cdrom   -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

[root@localhost ~]# systemctl restart autofs
[root@localhost ~]# systemctl enable autofs
ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'
[root@localhost ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   57G  3.5G   54G    6% /
devtmpfs                 475M     0  475M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  7.7M  479M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/sda1               1014M  149M  866M   15% /boot
tmpfs                     98M     0   98M    0% /run/user/0
[root@localhost ~]# cd /media/
[root@localhost media]# ls
[root@localhost media]# cd cdrom
[root@localhost cdrom]#
[root@localhost cdrom]# ll
总用量 686
-rw-rw-r-- 1 root root     14 11 26 00:01 CentOS_BuildTag
drwxr-xr-x 3 root root   2048 11 26 00:20 EFI
-rw-rw-r-- 1 root root    227 8  30 2017 EULA
-rw-rw-r-- 1 root root  18009 12 10 2015 GPL
drwxr-xr-x 3 root root   2048 11 26 00:21 images
drwxr-xr-x 2 root root   2048 11 26 00:20 isolinux
drwxr-xr-x 2 root root   2048 11 26 00:20 LiveOS
drwxrwxr-x 2 root root 663552 11 26 07:52 Packages
drwxrwxr-x 2 root root   4096 11 26 07:53 repodata
-rw-rw-r-- 1 root root   1690 12 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root   1690 12 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r-- 1 root root   2883 11 26 07:54 TRANS.TBL
[root@localhost cdrom]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   57G  3.5G   54G    6% /
devtmpfs                 475M     0  475M    0% /dev
tmpfs                    487M     0  487M    0% /dev/shm
tmpfs                    487M  7.7M  479M    2% /run
tmpfs                    487M     0  487M    0% /sys/fs/cgroup
/dev/sda1               1014M  149M  866M   15% /boot
tmpfs                     98M     0   98M    0% /run/user/0
/dev/sr0                 4.3G  4.3G     0  100% /media/cdrom

autofs服务可以和NFS服务搭档工作,开机自动挂载NFS服务器的目录,

当然也可以直接

vim /etc/fstab