Pages

Sunday, 30 May 2021

Mount order: When /etc/fstab works with systemd

In systemd, mount units, just like other units are generally loaded in parallel. However, in practice, some dependencies are needed. How can systemd handle this?

1 /etc/fstab is the preferred way

When using systemd, we have two options to configure mount points. One is to create mount units directly, while the other is to use traditional /etc/fstab.

Under the hood, systemd scans /etc/fstab at boot time and generates mount units automatically into /run/systemd/generator.

One big advantage of the second option is that systemd considers the dependencies for us. E.g

$ cat /etc/fstab

/sda3   /a    ext4  defaults 0 0
/sda4   /a/b  ext4  defautls 0 0

In this case, systemd will make sure "/a" is mounted before "/a/b", by setting dependencies in the automatically generated mount units.

Of course, we can manually create mount units and set their dependencies as well. But as systemd can do it for us, why not let it go?

2 How to set explicit dependencies in /etc/fstab

There are other dependencies that systemd cannot handle directly. e.g.

$ cat /etc/fstab

/sdb1    /data     ext4   defaults 0 0
/data    /data1    none   bind     0 0
/data    /data2    none   bind     0 0

In such a case, usually, the intention is to let data2 and data1 bind to /sdb1. But this is not guaranteed by systemd. E.g. /data2 may be mounted before /data.

The solution is to set explicit dependencies as below.

$ cat /etc/fstab

/sdb1    /data     ext4   defaults 0 0
/data    /data1    none   x-systemd.requires=/data,bind     0 0
/data    /data2    none   x-systemd.requires=/data,bind     0 0

3 Man pages

man systemd.mount

No comments:

Post a Comment