netbird icon indicating copy to clipboard operation
netbird copied to clipboard

systemd unit file improvements

Open sebastianschauenburg opened this issue 1 year ago • 2 comments

Describe the problem After a reboot, netbird fails to start using the systemd unit file netbird.service. Example of an error which is visible:

aug 11 12:48:14 hostname systemd[7480]: netbird.service: Failed to set up standard output: No such file or directory

I'm using a Ubuntu 23.04 system with netbird 0.22.3 (installed via apt).

To Reproduce

  1. Set up /var/log as RAM log with (for example) log2ram.
  2. Reboot the system
  3. View the error logs

Expected behavior No errors and a working netbird interface

Additional context

/var/log/netbird is created at a certain point, but was not synced to the persistent /var/log (if available). So effectively, after a reboot, the /var/log/netbird directory does not exist. This in turn means it cannot be used for stdout and stderr. I tried creating it with an ExecStartPre using mkdir inside the unit file, but attaching the stdout and stderr apparently happens before that point and as such, errors with:

aug 11 12:49:36 hostname systemd[8997]: netbird.service: Failed at step STDOUT spawning /usr/bin/mkdir: No such file or directory

Proposed solution Add this to the unit file, which ensures client.log can be created / updated:

ExecStartPre=/usr/bin/mkdir /var/log/netbird

Remove the following lines, since that output should be captured inside of systemd (journalctl) anyway:

StandardOutput=file:/var/log/netbird/netbird.out
StandardError=file:/var/log/netbird/netbird.err

sebastianschauenburg avatar Aug 12 '23 11:08 sebastianschauenburg

For future reference: the canonical way to fix this is to add:

CacheDirectory=netbird
ConfigurationDirectory=netbird
LogDirectory=netbird
RuntimeDirectory=netbird
StateDirectory=netbird

under the [Service] section.

systemd will then make sure to create the appropriate directories and give your service full access to them before starting the daemon:

  • /var/cache/netbird
  • /etc/netbird
  • /var/log/netbird
  • /var/run/netbird
  • /var/lib/netbird

It's not immediately obvious how to add this to netbird service install since netbird relies on a third-party (not really) package for its service management, and the Option field I see there does not "accept" these keys.

hg avatar Nov 22 '23 17:11 hg

it's LogsDirectory not LogDirectory, also currently only State and Runtime directories are used?

nazarewk avatar Feb 13 '24 07:02 nazarewk