Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

12.3 AutoFs auto-mount service


May 24, 2021 That's what Linux should learn



Whether it's Samba or NFS, write the mount information to /etc/fstab so that remote shared resources are automatically mounted with the server powered on. A lthough this is convenient, if you mount too many remote resources, you can put a lot of load on network bandwidth and server hardware resources. I f you do not use the resource for a long time after it is mounted, it can also result in a waste of server hardware resources. T here may be readers who say, "You can manually mount by executing the mount command before each use." It's a good choice, but you don't find it a hassle to mount and use it every time?

Autofs auto-mount service can help us solve this problem. U nlike the mount command, the autofs service program is a Linux system daemon that automatically mounts when a user is detected trying to access a file system that has not yet been mounted. In other words, when we fill in the /etc/fstab file with mount information, the system automatically mounts it every time it is turned on, while the autofs service program installs it dynamically when the user needs to use the file system, saving network resources and server hardware resources.

[root@linuxprobe ~]# yum install autofs Loaded plugins: langpacks, product-id, subscription-manager This system is not registered to Red Hat Subscription Management. Y ou can use subscription-manager to register. r hel | 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

  1. Package Arch Version Repository Size
  2. ================================================================================
  3. Installing:
  4. autofs x86_64 1:5.0.7-40.el7 rhel 550 k
  5. Installing for dependencies:
  6. hesiod x86_64 3.2.1-3.el7 rhel 30 k
  7. Transaction Summary
  8. ================================================================================
  9. Install 1 Package (+1 Dependent package)
  10. Total download size: 579 k
  11. Installed size: 3.6 M
  12. Is this ok [y/d/N]: y
  13. Downloading packages:
  14. --------------------------------------------------------------------------------
  15. Total 9.4 MB/s | 579 kB 00:00
  16. Running transaction check
  17. Running transaction test
  18. Transaction test succeeded
  19. Running transaction
  20. Installing : hesiod-3.2.1-3.el7.x86_64 1/2
  21. Installing : 1:autofs-5.0.7-40.el7.x86_64 2/2
  22. Verifying : hesiod-3.2.1-3.el7.x86_64 1/2
  23. Verifying : 1:autofs-5.0.7-40.el7.x86_64 2/2
  24. Installed:
  25. autofs.x86_64 1:5.0.7-40.el7
  26. Dependency Installed:
  27. hesiod.x86_64 0:3.2.1-3.el7
  28. Complete!

Linux servers in a production environment typically manage mount operations on many devices at the same time. I f these device mount information is written to the main configuration file of autofs service, it will undoubtedly make the main configuration file bloated, not conducive to service execution efficiency, and not conducive to future modification of the configuration content inside, so in the main configuration file of autofs service program needs to be filled in the format of "mount directory sub-profile". T he mount directory is the next level of the device mount location. F or example, cd devices are typically mounted to the /media/cdrom directory, and the mounted directory is written as /media. T he corresponding sub-profile provides further explanation of the mounted device information in this mounted directory. S ubfiles need to be user-defined, file names are not strictly required, but suffixes are recommended to end with .misc. The specific configuration parameters are shown in bold words on line 7.

[root@linuxprobe ~]# vim /etc/auto.master #

Sample auto.master file

  1. # This is an automounter map and it has the following format
  2. # key [ -mount-options-separated-by-comma ] location
  3. # For details of the format look at autofs(5).
  4. #
  5. /media /etc/iso.misc
  6. /misc /etc/auto.misc
  7. #
  8. # NOTE: mounts done from a hosts map will be mounted with the
  9. # "nosuid" and "nodev" options unless the "suid" and "dev"
  10. # options are explicitly given.
  11. #
  12. /net -hosts
  13. #
  14. # Include /etc/auto.master.d/*.autofs
  15. #
  16. +dir:/etc/auto.master.d
  17. #
  18. # Include central master map if it can be found using
  19. # nsswitch sources.
  20. #
  21. # Note that if there are entries for /net or /misc (as
  22. # above) in the included master map any keys that are the
  23. # same will not be seen as the first read key seen takes
  24. # precedence.
  25. #
  26. +auto.master

In the sub-profile, you should fill in the format of Mount Directory Mount File Type and Permissions: Device Name. F or example, to mount a disc device to the /media/iso directory, the mount directory can be written as iso, while -fstype is a file system format parameter, iso9660 is a disc device format, ro, nosuid, and nodev are specific permission parameters for the optical disc device, and /dev/cdrom is defined as the device name to be mounted. Once configured, start the autofs service program and add it to the system startup item:

(root@linuxprobe. s. root@linuxprobe . root@linuxprobe The next very interesting thing that will happen next is the systemctl enable autofs ln -s'/usr/lib/systemd/system/autofs.service' '/etc/system/system/multi-user.target.wants/autofs.service'. L et's first look at the current CD device mount and make sure that the disc device is not mounted and that there is no iso subdirecte in the /media directory at all. H owever, we can use the cd command to switch to this iso subdirect directory, and the disc device is automatically mounted immediately. We'll also be able to view the contents of the disc smoothly.

[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