AutoFs自动挂载服务
不论咱们是想使用前面学习的Samba还是NFS服务,都要把挂载信息写入到/etc/fstab中,这样远程共享资源就会自动的随服务器开机而挂载,虽然这样一劳永逸确实很方便,但如果挂载的远程资源很多的话,毕竟会或多或少消耗着服务器的一定网络带宽和CPU计算性能。如果挂载后长期不去使用这些服务,那么无疑这笔损耗是白白被浪费掉了,当然还会有同学说可以每次使用前都执行下mount命令手动的挂载上,这虽然是个不错的选择,但每次都要去挂载一下再使用,这样是不是又很麻烦呢?详情请关注《linux就该这么学》。
操作方法
- 01
AutoFs服务程序与Mount命令不同之处在于它是一种守护进程,只有检测到用户试图访问一个尚未挂载的文件系统时才自动的检测并挂载该文件系统。换句话说,把挂载信息填入/etc/fstab文件后系统将在每次开机时都自动把其挂载,而运行AutoFs服务后则是当用户需要使用该文件系统了才会动态的挂载,节约服务器的网络与系统资源。 [root@linuxprobe ~]# yum install autofs Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. rhel | 4.1 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package autofs.x86_64 1:5.0.7-40.el7 will be installed --> Processing Dependency: libhesiod.so.0()(64bit) for package: 1:autofs-5.0.7-40.el7.x86_64 --> Running transaction check ---> Package hesiod.x86_64 0:3.2.1-3.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: autofs x86_64 1:5.0.7-40.el7 rhel 550 k Installing for dependencies: hesiod x86_64 3.2.1-3.el7 rhel 30 k Transaction Summary ================================================================================ Install 1 Package (+1 Dependent package) Total download size: 579 k Installed size: 3.6 M Is this ok [y/d/N]: y Downloading packages: -------------------------------------------------------------------------------- Total 9.4 MB/s | 579 kB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : hesiod-3.2.1-3.el7.x86_64 1/2 Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2 Verifying : hesiod-3.2.1-3.el7.x86_64 1/2 Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2 Installed: autofs.x86_64 1:5.0.7-40.el7 Dependency Installed: hesiod.x86_64 0:3.2.1-3.el7 Complete!
- 02
生产环境中一般都会同时管理着很多设备的挂载工作,如果把这些设备挂载信息都写入到autofs服务主配置文件中的话肯定无疑会让主配置文件臃肿不堪,不利于服务执行效率,也不利于日后修改里面的配置内容,所以在autofs服务程序的主配置文件中需要按照“挂载目录 子配置文件”的格式写入参数。挂载目录是设备要挂载位置的上一级目录,例如咱们的光盘设备一般是挂载到/media/cdrom目录中的,那么此处就应该写成/media即可,而对应的子配置文件则是对这个目录内挂载设备信息的进一步说明,子配置文件是需要用户自行定义的,文件名字没有严格要求,但后缀需以.misc结束,具体的配置参数如第7行加粗字所示。 [root@linuxprobe ~]# vim /etc/auto.master # # Sample auto.master file # This is an automounter map and it has the following format # key [ -mount-options-separated-by-comma ] location # For details of the format look at autofs(5). #/media /etc/iso.misc/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 # +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
- 03
子配置文件中应按照“挂载目录 挂载文件类型及权限 :设备名称”的格式写入参数,例如想要把设备挂载到/media/iso目录中,则此时写iso即可,而-fstype为文件系统格式参数,iso9660为光盘系统设备格式,ro、nosuid及nodev为光盘设备具体的权限参数,最终/dev/cdrom则是定义要挂载的设备名称,配置完成后顺手再把autofs服务程序启动并加入到开机启动项中吧: [root@linuxprobe ~]# vim /etc/iso.misc iso -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom [root@linuxprobe ~]# systemctl start autofs [root@linuxprobe ~]# systemctl enable autofs ln -s '/usr/lib/systemd/system/autofs.service' '/etc/systemd/system/multi-user.target.wants/autofs.service'
- 04
接下来会就要发生一幕非常有趣的事情了,先来查看下当前的设备挂载情况,确认光盘设备目前是没有被挂载使用的,而且在/media目录中根本就没有一个iso子目录,但却可以通过cd命令切换进去,同时光盘设备会被立即自动挂载上,咱们也就能顺利的查看到光盘内的所有内容了。 [root@linuxprobe ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 18G 3.0G 15G 17% / devtmpfs 905M 0 905M 0% /dev tmpfs 914M 140K 914M 1% /dev/shm tmpfs 914M 8.9M 905M 1% /run tmpfs 914M 0 914M 0% /sys/fs/cgroup /dev/sda1 497M 119M 379M 24% /boot [root@linuxprobe ~]# cd /media [root@linuxprobe media]# ls [root@linuxprobe media]# cd iso [root@linuxprobe iso]# ls -l total 812 dr-xr-xr-x. 4 root root 2048 May 7 2017 addons dr-xr-xr-x. 3 root root 2048 May 7 2017 EFI -r--r--r--. 1 root root 8266 Apr 4 2017 EULA -r--r--r--. 1 root root 18092 Mar 6 2012 GPL dr-xr-xr-x. 3 root root 2048 May 7 2017 images dr-xr-xr-x. 2 root root 2048 May 7 2017 isolinux dr-xr-xr-x. 2 root root 2048 May 7 2017 LiveOS -r--r--r--. 1 root root 108 May 7 2017 media.repo dr-xr-xr-x. 2 root root 774144 May 7 2017 Packages dr-xr-xr-x. 24 root root 6144 May 7 2017 release-notes dr-xr-xr-x. 2 root root 4096 May 7 2017 repodata -r--r--r--. 1 root root 3375 Apr 1 2017 RPM-GPG-KEY-redhat-beta -r--r--r--. 1 root root 3211 Apr 1 2017 RPM-GPG-KEY-redhat-release -r--r--r--. 1 root root 1568 May 7 2017 TRANS.TBL [root@linuxprobe ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel-root 18G 3.0G 15G 17% / devtmpfs 905M 0 905M 0% /dev tmpfs 914M 140K 914M 1% /dev/shm tmpfs 914M 8.9M 905M 1% /run tmpfs 914M 0 914M 0% /sys/fs/cgroup /dev/cdrom 3.5G 3.5G 0 100% /media/iso /dev/sda1 497M 119M 379M 24% /boot