Explaining to systemd that /var is symlinked to /home/var

2

Suppose an installation of Debian unstable, using systemd for init, with two filesystem partitions, / and /home. Suppose further that, for reasons to do with the physical disks, I have moved the contents of /var to /home/var and replaced the /var directory with an appropriate symlink. (Please do not try to talk me out of moving /var into the /home partition, or turn this into a systemd argument ;-)

With this configuration, it is necessary to inform systemd that any unit which requires anything in /var cannot be started until after /home is mounted. The one I know to be broken (as it tries to access a file in /var/lib very early in the boot sequence) is systemd-random-seed.service, but there could easily be any number of others that I haven't happened to notice yet.

What is the best way to configure the general rule that "anything needing something from /var cannot be started until after /home is mounted"? I will accept an answer of the form "add Requires= and After= directives to every single unit file affected" only if you can demonstrate that there is no superior alternative.

The version of systemd currently in Debian unstable is 224.

zwol

Posted 2015-08-14T18:17:53.953

Reputation: 1 130

Answers

3

Well, init cannot really know all by itself which files a given service needs, so "this service uses /var" dependencies still need to be declared somewhere anyway.

Of course, that ought to be done by the developers & packagers, not by you. For example, the aforementioned systemd-random-seed.service already has all the necessary dependencies:

$ systemctl cat systemd-random-seed
# /usr/lib/systemd/system/systemd-random-seed.service
#  This file is part of systemd.
...
DefaultDependencies=no
RequiresMountsFor=/var/lib/systemd/random-seed
...

So in your case, the "superior alternative" is to use a bind mount in place of the symlink. That will hook naturally into systemd's .mount unit dependencies while providing identical functionality to a symlink otherwise.

That is, if you have a bind mount on /var, then all units which already depend on var.mount will automatically (indirectly) depend on home.mount.

# /etc/fstab
/home/var  /var  none  bind  0  0

(If that's not acceptable, perhaps compiling a custom systemd version with the dependency hacked in would fulfill your 'requirements' better.)


If some of your .service units lack proper dependencies, there is another option – you can turn /var into an automount using systemd's autofs4 support.

With automounts, any process trying to access files under /var will block until the filesystem is mounted. So a global 'dependency' is created without having to edit individual service units.

To do this, add the x-systemd.automount option into fstab. (Or, if you prefer var.mount over fstab, then create a corresponding var.automount as well.)

# /etc/fstab
/home/var  /var  none  bind,x-systemd.automount  0  0

Of course this again requires that /var be a bind mount rather than a symlink.

user1686

Posted 2015-08-14T18:17:53.953

Reputation: 283 655

Aha! The possibility of a bind mount occurred to me after I posted this question, but I couldn't find anything in the documentation to indicate whether or not it would work. I'll accept this after confirming it does work (the computer is in the middle of a very large package install right now). – zwol – 2015-08-14T19:18:22.777

Some care needs to be taken with the automount approach: it might work for startup, but it doesn't help at all with ordering the shutdown process... I think it's only mainly a problem with ordering networked filesystem shutdown; if a local mount fails to stop because it's still in use, then systemd-shutdown is supposed to be able to handle it. – sourcejedi – 2017-09-19T17:16:19.410

0

More than a year later with the version of systemd (229)now shipping with ubuntu 16.04 there is support in fstab for dependency mounting like this.

so it's as easy as doing this.

# /etc/fstab
home/var /var x-systemd.requires=/home,x-systemd.automount,none bind 0 0

got the idea from this post https://copyninja.info/blog/systemd_automount_entry.html

DKebler

Posted 2015-08-14T18:17:53.953

Reputation: 180