How to Manage Order of Mounting in CentOS/RHEL 7,8 using systemd
CentOS/RHEL 6 and earlier versions, were mounting the filesystems in an order defined in the file /etc/fstab. But the CentOS/RHEL 7 and 8 versions no longer uses /etc/fstab to determine the order of mounting the filesystems, instead, all the filesystems are now considered as systemd mount unit. A mount unit specifies how a file system can be mounted on a specific directory.
Can we still use /etc/fstab in CentOS/RHEL 7,8?
Yes, off course, but with a glitch. When you configure the mount points in /etc/fstab, they are automatically converted into dynamic “mount” unit types. The dynamic mount units are genrated in the directory “/run/systemd/generator”.
So basically you can change the order of mounting using either of the below 2 methods: 1. systemd mount options in /etc/fstab 2. Modifying the dynamic mount units
The preferred method is using the systemd mount options in /etc/fstab.
1. Using systemd mount options in /etc/fstab
1. Lets create two sample mount point first for the testing.
Mount Point: /test
# pvcreate /dev/nvme1n1
# vgcreate test_vg /dev/nvme1n
# lvcreate -n test_lv -L 1g test_vg
# mkfs.xfs /dev/mapper/test_vg-test_lv
# mkdir /test
Mount Point: /data
# lvcreate -n data_lv -L 1g test_vg
# mkfs.xfs /dev/mapper/test_vg-data_lv
# mkdir /data
2. So, here we want /data mount point to be mounted after /test mount point. Lets see how this can be achived with systemd mount options in /etc/fstab. Lets add the systemd option requires-mounts-for=/mount_point_name, where /mount_point_name is the expected mount point.
3. The /etc/fstab configuration should look like:
# vi /etc/fstab
/dev/mapper/test_vg-test_lv /test xfs defaults 0 0
/dev/mapper/test_vg-data_lv /data xfs defaults,x-systemd.requires-mounts-for=/test 0 0
4. Reload systemd to refresh the configuration and unit:
# systemctl daemon-reload
5. Verify the new mount units created in the /run/systemd/generator/ directory for the 2 new mount points /data and /test.
# cat /run/systemd/generator/test.mount
# Automatically generated by systemd-fstab-generator
[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
Before=local-fs.target
[Mount]
Where=/test
What=/dev/mapper/test_vg-test_lv
Type=xfs
# cat /run/systemd/generator/data.mount
# Automatically generated by systemd-fstab-generator
[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
Before=local-fs.target
RequiresMountsFor=/test
[Mount]
Where=/data
What=/dev/mapper/test_vg-data_lv
Type=xfs
Options=defaults,x-systemd.requires-mounts-for=/test
Note the RequiresMountsFor option in the mount unit /run/systemd/generator/data.mount which confirms the dependancy of /test to be mounted before the /data can be mounted.
2. Modifying the Dynamic Mount Units
If you do not want to modify the /etc/fstab file, the dynamic mount files can also be edited. Make sure you remove the corresponding mount point entry from the /etc/fstab file.
1. Copy the ‘mount’ unit files for each mount point from /run/systemd/generator to /etc/systemd/system/. In our case:
# cp /run/systemd/generator/data.mount /etc/systemd/system/data.mount
cp /run/systemd/generator/test.mount /etc/systemd/system/test.mount
2. Modify the /etc/systemd/system/data.mount file and add the “RequiresMountsFor=/test” and “Options=defaults,x-systemd.requires-mounts-for=/test option in the file.
# vi /etc/systemd/system/data.mount
# Automatically generated by systemd-fstab-generator
[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
Before=local-fs.target
RequiresMountsFor=/test
[Mount]
Where=/data
What=/dev/mapper/test_vg-data_lv
Type=xfs
Options=defaults,x-systemd.requires-mounts-for=/test
3. Remove the corresponding mount entry from /etc/fstab file.
# vi /etc/fstab
### remove the /data and /test mount entries from here
Conclusion
Filesystems are system “unit” type. More specifically they are a unit of type “mount”. When the filesystem names are given in the /etc/fstab, the system will convert these entries into dynamic “mount” unit types. Dynamically created mount unit types are in /run/systemd/generator/ location. You can either add systemd mount options in /etc/fstab or copy the dynamic mount files in /etc/systemd/system directory and modify them. The earlier is the preferred way of defining the order of mounting in CentOS/RHEL 7,8 systems.