Table of Contents
In my last article I explained with the steps to mount file system without fstab by using systemd mount unit files. Now I will continue that article by explaining with the steps to automount file system using systemd automount unit files. Similar to mount unit files we also have automount systemd unit files in CentOS/RHEL 7 and 8 Linux.
How to automount file system in Linux?
A unit configuration file whose name ends in "
.automount" encodes information about a file system automount point controlled and supervised by systemd. Automount units may be used to implement on-demand mounting as well as parallelized mounting of file systems. If an automount point is beneath another mount point in the file system hierarchy, a dependency between both units is created automatically.
Dependency with /etc/fstab
Automount units may either be configured via unit files, or via
If an automount point is configured in both
/etc/fstab and a unit file, the configuration in the latter takes precedence.
Sample mount systemd unit file
In our last article we created our systemd mount unit file, now we will create our custom automount systemd unit file under the same location as we created our
[root@rhel-8 system]# pwd /usr/lib/systemd/system
For the sake of this article I will again show the output snippet of my
tmp_dir.mount which I created in my older article
[root@rhel-8 system]# cat tmp_dir.mount # This file is part of systemd. [Unit] Description=Test Directory (/tmp_dir) DefaultDependencies=no Conflicts=umount.target Before=local-fs.target umount.target After=swap.target [Mount] What=/dev/disk/by-uuid/cea0757d-6329-4bf8-abbf-03f9c313b07f Where=/tmp_dir Type=ext4 Options=defaults [Install] WantedBy=multi-user.target
.automount" service will always look for mapping "
.mount" service for respective partition.
Sample automount systemd unit file
Automount units must be named after the automount directories they control. Example: the automount point
/home/lennart must be configured in a unit file
home-lennart.automount. Here my pount point is under root directory hence my automount systemd unit file will be
Below is my sample automount systemd unit file to automount file system
[root@rhel-8 system]# cat tmp_dir.automount [Unit] Description=Sample automount partition ConditionPathExists=/tmp_dir [Automount] Where=/tmp_dir TimeoutIdleSec=10 [Install] WantedBy=multi-user.target
Where= Takes an absolute path of a directory of the automount point. If the automount point does not exist at time that the automount point is installed, it is created. This string must be reflected in the unit filename. This option is mandatory. DirectoryMode= Directories of automount points (and any parent directories) are automatically created if needed. This option specifies the file system access mode used when creating these directories. Takes an access mode in octal notation. Defaults to 0755. TimeoutIdleSec= Configures an idleness timeout. Once the mount has been idle for the specified time, systemd will attempt to unmount. Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass 0 to disable the timeout logic. The timeout is disabled by default. With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.
Once you have created your automount systemd unit file to automount file system, reload the systemd daemon
[root@rhel-8 system]# systemctl daemon-reload
AutoMount file system (Start systemd service)
Now let us start
[root@rhel-8 system]# systemctl start tmp_dir.automount
Let us check the service status. As it shows the service is running but is in
(waiting) state since we had given a Time Out value of 10 seconds for idle session
[root@rhel-8 system]# systemctl status tmp_dir.automount ● tmp_dir.automount - Sample automount partition Loaded: loaded (/usr/lib/systemd/system/tmp_dir.automount; disabled; vendor preset: disabled) Active: active (waiting) since Mon 2019-09-16 18:45:59 IST; 6s ago Where: /tmp_dir Sep 16 18:45:59 rhel-8.example systemd: Set up automount Sample automount partition.
mountcommand will still not show
/tmp_diras mounted yet because automount will mount this filesystem only once someone attempts to access the mount point which is the beauty of this service.
As you see
df command does not shows
/tmp_dir partition yet
[root@rhel-8 ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 900M 0 900M 0% /dev tmpfs 915M 0 915M 0% /dev/shm tmpfs 915M 8.5M 907M 1% /run tmpfs 915M 0 915M 0% /sys/fs/cgroup /dev/mapper/rhel-root 15G 2.1G 12G 16% / /dev/sdc1 976M 2.6M 907M 1% /second_part /dev/sda1 483M 258M 225M 54% /boot tmpfs 183M 0 183M 0% /run/user/0
Now if I access this partition using
df command explicitly
[root@rhel-8 ~]# df -h /tmp_dir/ Filesystem Size Used Avail Use% Mounted on /dev/sdb1 976M 2.6M 907M 1% /tmp_dir
then the partition will get mounted automatically.
Also if you check the service status, the output snippet will give you a hint of what was happening in the backend
[root@rhel-8 ~]# systemctl status tmp_dir.automount ● tmp_dir.automount - Sample automount partition Loaded: loaded (/usr/lib/systemd/system/tmp_dir.automount; disabled; vendor preset: disabled) Active: active (waiting) since Mon 2019-09-16 18:45:59 IST; 17min ago Where: /tmp_dir Sep 16 18:45:59 rhel-8.example systemd: Set up automount Sample automount partition. Sep 16 19:00:54 rhel-8.example systemd: tmp_dir.automount: Got automount request for /tmp_dir, triggered by 3868 (df) Sep 16 19:01:09 rhel-8.example systemd: tmp_dir.automount: Got automount request for /tmp_dir, triggered by 1318 (bash) Sep 16 19:02:04 rhel-8.example systemd: tmp_dir.automount: Got automount request for /tmp_dir, triggered by 3903 (df)
Lastly I hope the steps from the article to automount file system using systemd automount unit file on CentOS/RHEl 7 and 8 Linux was helpful. So, let me know your suggestions and feedback using the comment section.