Add native systemd service

Inspired by: https://github.com/pi-hole/pi-hole/pull/2112

A pre-start and a post-stop script are added to reduce doubled setup and cleanup code.

Since systemd services do not natively support dynamic users, test once whether capabilities are supported during install/update, and remove User=pihole otherwise.

Signed-off-by: MichaIng <micha@dietpi.com>
Co-authored-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
MichaIng 2022-09-18 21:44:06 +02:00
parent 01bf9ca42a
commit 81927334f2
No known key found for this signature in database
GPG key ID: 0442B9ADE65643FE
5 changed files with 123 additions and 34 deletions

View file

@ -1377,8 +1377,29 @@ installConfigs() {
fi
fi
# Install pihole-FTL.service
install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" "/etc/init.d/pihole-FTL"
# Install pihole-FTL systemd or init.d service, based on whether systemd is the init system or not
# Follow debhelper logic, which checks for /run/systemd/system to derive whether systemd is the init system
if [[ -d '/run/systemd/system' ]]; then
install -T -m 0644 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.systemd" '/etc/systemd/system/pihole-FTL.service'
# Set net admin permissions so that FTL can serve DNS, DHCP and IMAP (for DHCPv6). If this does not work, run FTL as root user.
if ! setcap CAP_NET_BIND_SERVICE,CAP_NET_RAW,CAP_NET_ADMIN,CAP_SYS_NICE,CAP_IPC_LOCK,CAP_CHOWN+eip '/usr/bin/pihole-FTL'; then
sed -i '/^User=/d' '/etc/systemd/system/pihole-FTL.service'
fi
# Remove init.d service if present
if [[ -e '/etc/init.d/pihole-FTL' ]]; then
rm '/etc/init.d/pihole-FTL'
update-rc.d pihole-FTL remove
fi
# Load final service
systemctl daemon-reload
else
install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL.service" '/etc/init.d/pihole-FTL'
fi
install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL-prestart.sh" "${PI_HOLE_INSTALL_DIR}/pihole-FTL-prestart.sh"
install -T -m 0755 "${PI_HOLE_LOCAL_REPO}/advanced/Templates/pihole-FTL-poststop.sh" "${PI_HOLE_INSTALL_DIR}/pihole-FTL-poststop.sh"
# If the user chose to install the dashboard,
if [[ "${INSTALL_WEB_SERVER}" == true ]]; then