Suppose, hypothetically, that you have a service that works on stuff in a mounted filesystem and will malfunction if that filesystem goes away (for example, a media server that's working from a NAS-mounted drive). You'd like to immediately stop the service if the (remote) filesystem goes away for any reason so the service won't freak out (or has minimal chances of doing so). It's possible to do this in systemd but it's not quite obvious what you need to do and what the drawbacks are.
The simple and obvious approach is to set '
RequiresMountsFor=
' for the directory you need. As the documentation says, this adds a
Requires=
and
After=
to the relevant systemd.mount unit for the filesystem. Unfortunately, this appealing basic solution has a number of limitations. The biggest one is that it only works at all if someone does an explicit 'systemctl stop <fs>.mount', not if the mount disappears through other means (for example, 'umount'). And it only works at all if the mount is explicitly listed (in /etc/fstab or as a .mount file), not if the mount is created on the fly by some other service (for example, if it's a ZFS mount). As far as I can tell, you can't fix this by setting
After=
to the service unit that winds up mounting the filesystem; you're just stuck.
(If you set
ConditionPathExists=
or one of its friends to something appropriate, your service won't start when the filesystem is missing. If it's mounted by a service, you'll definitely want to set an
After=
.)
If you directly set Requires= and After= to the .mount unit and the filesystem is mounted on the fly by some service, your own service won't start automatically (even after the mount appears, because systemd doesn't have triggering ), but at least now 'systemctl stop <fs>.mount' will correctly stop your service. This is perfectly sensible on systemd's part; you told it that your service requires something that systemd doesn't know about and thus isn't there, so it can't start. To get your service to start on boot, you need an /etc/fstab entry or a .mount file.
Even with an /etc/fstab entry or .mount file, your service still won't stop if the filesystem is unmounted outside systemd. Systemd will correctly show the status of the <fs>.mount as 'inactive (dead)', but it won't stop your service despite the 'Requires='. It will stop your service if you then do an explicit 'systemctl stop <fs>.mount', even though this doesn't visibly change the status of the <fs>.mount unit.
To actually get your service to stop when the filesystem is unmounted outside of systemd, you need to set
BindsTo=
instead of
Requires=
. As the systemd documentation says, this is a very strong dependency (and also you want to have an /etc/fstab entry or a .mount file for the filesystem), but finally we have something that works.
If you have a
BindsTo=
to a .mount unit for a filesystem that's mounted by some other service and so doesn't have an /etc/fstab entry or a .mount file, your service won't start automatically (you have to arrange to start it after the filesystem and its .mount unit exist) but your service will shut down even if something does an 'umount <fs>'.