Debian – bind mounts by systemd don’t magically work with systemd-tmpfiles

bind-mountdebiansystemd

I have an weird challenge from on my Debian 8 box.

The background is I want to mount some directories as tmpfs, to avoid physical IO (disk wakeup / flash wear).

Probably I should just mount a separate tmpfs for each directory. However what I tried first was bind-mounts under /tmp/mnts. (My previous task was to move IO from disk to a small flash storage, to avoid spinups, so I just tried using the same pattern).

So I want to create directories on the tmpfs at boot time. I.e. systemd-tmpfiles. And then bind-mount them in various places under /var.

# /etc/tmpfiles.d/tmpfs-mnts.conf snippet
# Type Path    Mode UID  GID  Age Argument
d /tmp/mnts/var-lib-icinga-spool-checkresults 0750 nagios nagios -

# /etc/fstab snippet
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/tmp/mnts/var-lib-icinga-spool-checkresults /var/lib/icinga/spool/checkresults none bind

systemd-tmpfiles --create + mount -a works fine. But it doesn't work at boot time, so there's a race condition or something. But the failure is a bit interesting – findmnt shows the source directory has been deleted.

# findmnt|grep /var/lib/icinga/spool/checkresults
└─/var/lib/icinga/spool/checkresults                     tmpfs[/mnts/var-lib-icinga-spool-checkresults//deleted] tmpfs       rw
# cd /var/lib/icinga/spool/checkresults/
# mkdir ./test
mkdir: cannot create directory ‘./test’: No such file or directory
# ls --inode /tmp/mnts
7414 var-lib-icinga-spool-checkresults
# ls --inode /var/lib/icinga/spool/
6254 checkresults

So it looks like

  1. The mount happened correctly after systemd-tmpfiles created the source directory
  2. systemd-tmpfiles then deleted the source directory
  3. You're allowed to delete the source directory of a bind mount (?!)
  4. systemd-tmpfiles then created the source directory a second time

I guess there's a number of questions. Can we rely on 1) working? Could 1) still work if something other than systemd-tmpfiles created the source directory? What's the reason for 2) and 4) happening? And what's up with 3), has it always been like that?

Best Answer

bind is unreliable when defined in fstab on a system with systemd. Systemd parses the fstab and tries to work out what order to mount and bind things in. From my own experience it gets this wrong 100% of the time. Best option is to move all you binds out of fstab and make you own xxx.mount system files for systemd. That was you gain controo over the order etc.