背景
你是否曾经 把 NFS写入到 /etc/fstab中, 当远程 NFS 服务器宕机导致服务器无法开机的烦恼?
你是否曾经 把磁盘分区挂载 写入到 /etc/fstab中, 当磁盘损坏导致设备无法开机的困惑?
现在,有了 autofs 你不必再为以上问题而苦恼!
autofs 基于 特定目录监控,真正做到按需挂载,即时挂载,无访问不挂载,再访问再挂载。
autofs 文件系统自动挂载, 可以是本地磁盘/U盘/LVM/CDROM, 也可以是远程 NFS/SAMBA/FTP
准备演示使用的NFS
本机IP 192.168.105.22, 创建一个路径为/tmp/nfs_home的NFS
1 2 3 4 5 6 7 8 9 10 [root@localhost ~]# systemctl enable nfs [root@localhost ~]# systemctl start nfs [root@localhost ~]# vim /etc/exports #新加一行 /tmp/nfs_home 192.168.0.0/16(rw) [root@localhost ~]# systemctl restart nfs [root@localhost ~]# exportfs /tmp/nfs_home 192.168.0.0/16 [root@localhost ~]# # 加入 测试文件 [root@localhost ~]# echo "HelloWorld" > /tmp/nfs_home/hello
开始配置autofs
配置重点说明:
访问本地 /mnt/share/google 等效为访问 192.168.105.22:/tmp/nfs_home
1 2 3 4 5 6 7 [root@localhost ~]# systemctl enable autofs [root@localhost ~]# systemctl start autofs [root@localhost ~]# vim /etc/auto.master #新增一行 /mnt/share /etc/auto.nfs [root@localhost ~]# vim /etc/auto.nfs # 创建文件 google -rw 192.168.105.22:/tmp/nfs_home [root@localhost ~]# systemctl restart autofs # 重启autofs
测试
1 2 3 4 5 6 7 8 [root@localhost ~]# mount | grep google [root@localhost ~]# ll /mnt/share/ total 0 [root@localhost ~]# cat /mnt/share/google/hello HelloWorld [root@localhost ~]# mount | grep google /dev/mapper/centos-root on /mnt/share/google type xfs (rw,relatime,attr2,inode64,noquota) [root@localhost ~]#
十几分钟后 没有访问 动作,/mnt/share/google 会自动卸载。再次访问再次加载。