zfs is mounted after mongod starts: how do I set the boot order?

Guy De Pauw asked:

Fairly simple problem, I think, but I can’t seem to get my head around a fix.

I’m on Ubuntu 18.04.3 and mounting zfs volumes with mongo-data at boot correctly with the config below in /etc/systemd/system/multi-user.target.wants.

[Unit]
Description=ZFS startup target

[Install]
WantedBy=multi-user.target

Problem is that this happens after the mongod service is started, making mongod fail.

[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network.target

[Service]
User=root
Group=root
EnvironmentFile=-/etc/default/mongod
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
Restart=always
# Recommended limits for for mongod as specified in
# http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings

[Install]
WantedBy=multi-user.target

I read up about the WantedBy, Requires and After directives, but I’m not confident I understand them well enough to play around with them.

My answer:


I had a very similar issue with another service, which wanted to start before its files, which are on a ZFS dataset, became available.

I solved it with a very simple systemd override. You can do the same:

# cat /etc/systemd/system/mongod.service.d/zfs.conf 
[Unit]
Requires=zfs.target
After=zfs.target

After a sudo systemctl daemon-reload (or of course a reboot) this will take effect. Enjoy!

Remember not to edit the systemd unit shipped with the package, as your changes would not be preserved when you update the package. Use override files (e.g. as above) instead.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.