So, for everyone who wants to install NUT as a service follow these steps. Modifying the systemd files can make you system unbootable! It should not happen, but there is chance since we are also modifying some "natively shipped files". So an update can break your working configuration again and that is not the fault of XCP-NG!!!
As prerequisite it requires the installation and testing of this step-by-step guide https://xcp-ng.org/forum/post/104253. Make sure everything runs before proceeding to the following steps! Now:
#################################
Remove "nut-driver.target" dependency
#################################
Remove "nut-driver.target" dependency from "nut-server.service" by calling
nano /lib/systemd/system/nut-server.service
and remove nut-driver.target from routine. I prefer to duplicate and comment to keep the modified rows in the original code. It then should look like this. DO NOT COPY AND PASTE, read, compare and modify carefully!
[Unit]
Description=Network UPS Tools - power devices information server
#After=local-fs.target network.target nut-driver.target
After=local-fs.target network.target
# We don't Require drivers to be successfully started! This would be
# a change of behavior compared to init SysV, and could prevent from
# accessing successfully started, at least to audit a system.
#Wants=nut-driver.target
Wants=
# The `upsd` is a networked service (even if bound to a `localhost`)
# so it requires that the OS has some notion of networking already.
# Extending the unit does not require *this* file to be edited, you
# can instead drop in an additional piece of configuration, e.g. add
# a `/etc/systemd/system/nut-server.service.d/network.conf` with:
# [Unit]
# Requires=network-online.target
# After=network-online.target
Requires=network.target
Before=nut-monitor.service
PartOf=nut.target
[Service]
EnvironmentFile=-/etc/ups/nut.conf
SyslogIdentifier=%N
# Note: foreground mode by default skips writing a PID file (and
# needs Type=simple); can use "-FF" here to create one anyway:
ExecStart=/usr/sbin/upsd -F
ExecReload=/usr/sbin/upsd -c reload -P $MAINPID
[Install]
WantedBy=nut.target
#################################
Add Service for ups-driver
#################################
nano /etc/systemd/system/ups-driver.service
[Unit]
Description=NUT UPS Driver
After=network-online.target
Wants=network-online.target
Before=nut-server.service
[Service]
Type=oneshot
ExecStart=/usr/sbin/upsdrvctl start
ExecStop=/usr/sbin/upsdrvctl stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
###################################
Add Requirement for nut-server
#################################
mkdir -p /etc/systemd/system/nut-server.service.d
nano /etc/systemd/system/nut-server.service.d/ups-driver.conf
[Unit]
Requires=ups-driver.service
After=ups-driver.service
###################################
Add Requirement for nut monitor
#################################
mkdir -p /etc/systemd/system/nut-monitor.service.d
nano /etc/systemd/system/nut-monitor.service.d/nut-server.conf
[Unit]
Requires=nut-server.service
After=nut-server.service
###################################
Enable services
###################################
systemctl daemon-reload
systemctl enable ups-driver.service
systemctl enable nut.target
systemctl enable nut-server.service
systemctl enable nut-monitor.service
systemctl start nut-monitor.service
###################################
Analyze & debugging commands
###################################
systemctl list-dependencies nut-monitor.service
systemd-analyze critical-chain nut-monitor.service
systemctl show nut-monitor.service -p Requires -p Wants -p After
systemctl show nut-server.service -p Requires -p Wants -p After