netbirdio / netbird

Connect your devices into a secure WireGuard®-based overlay network with SSO, MFA and granular access controls.
https://netbird.io
BSD 3-Clause "New" or "Revised" License
10.88k stars 493 forks source link

systemd unit file improvements #1079

Open sebastianschauenburg opened 1 year ago

sebastianschauenburg commented 1 year ago

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
hg commented 10 months ago

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:

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.

nazarewk commented 8 months ago

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