Skip to content

Commit

Permalink
Add basic support for dinit in our system service handling code. (net…
Browse files Browse the repository at this point in the history
…data#16836)

* Add basic dinit detection to install-service script.

* Add dinit service file for Netdata.

* Add service install support for dinit.
  • Loading branch information
Ferroin authored Apr 4, 2024
1 parent e85be29 commit 0401ac5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,11 @@ install(PROGRAMS
${CMAKE_BINARY_DIR}/system/runit/run
DESTINATION usr/lib/netdata/system/runit)

configure_file(system/dinit/netdata.in system/dinit/netdata @ONLY)
install(FILES
${CMAKE_BINARY_DIR}/system/dinit/netdata
DESTINATION usr/lib/netdata/system/dinit)

configure_file(system/systemd/netdata.service.in system/systemd/netdata.service @ONLY)
install(FILES
${CMAKE_BINARY_DIR}/system/systemd/netdata.service
Expand Down
11 changes: 11 additions & 0 deletions system/dinit/netdata.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# dinit service description for Netdata
#
# Currently only properly tested on Chimera Linux.

type = bgprocess
command = @sbindir_POST@/netdata -P @localstatedir_POST@/run/netdata.pid -D
pid-file = @localstatedir_POST@/run/netdata.pid
options = signal-process-only
load-options = export-passwd-vars
depends-on = early-fs-local.target
after = network.target
61 changes: 60 additions & 1 deletion system/install-service.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ DUMP_CMDS=0
ENABLE="auto"
EXPORT_CMDS=0
INSTALL=1
LINUX_INIT_TYPES="systemd openrc lsb initd runit"
LINUX_INIT_TYPES="systemd openrc lsb initd runit dinit"
PLATFORM="$(uname -s)"
SHOW_SVC_TYPE=0
SVC_SOURCE="@libsysdir_POST@"
Expand Down Expand Up @@ -562,6 +562,65 @@ runit_cmds() {
NETDATA_STOP_CMD="sv stop netdata"
}

# =====================================================================
# dinit support functions

_check_dinit() {
# if /etc/dinit.d does not exist, it’s not dinit
[ ! -d /etc/dinit.d ] && echo "NO" && return 0

# if PID 1 is dinit, it’s dinit
[ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "dinit" ] && echo "YES" && return 0

# if /run/dinitctl exists and is a socket, it’s dinit
[ -S /run/dinitctl ] && echo "YES" && return 0

# if the dinitctl command exists despite getting to this point, it’s dinit, but not booted as such
[ -n "$(command -v dinitctl 2>/dev/null || true)" ] && echo "OFFLINE" && return 0

echo "NO" && return 0
}

check_dinit() {
if [ -z "${IS_DINIT}" ]; then
IS_DINIT="$(_check_dinit)"
fi

echo "${IS_DINIT}"
}

_run_dinitctl() {
opts=''

if [ "$(check_dinit)" = "OFFLINE" ]; then
opts="-o"
fi

# shellcheck disable=SC2086
dinitctl ${opts} "${@}"
}

enable_dinit() {
_run_dinitctl enable netdata
}

enable_dinit() {
_run_dinitctl disable netdata
}

install_dinit_service() {
install_generic_service dinit/netdata "dinit" /etc/dinit.d enable_dinit disable_dinit
}

dinit_cmds() {
if [ "$(check_dinit)" = "YES" ]; then
NETDATA_START_CMD='dinitctl start netdata'
NETDATA_STOP_CMD='dinitct stop netdata'
else # Not booted using dinit, use external defaults by not providing commands.
warning "Detected dinit, but the system is not booted using dinit. Unable to provide commands to start or stop Netdata using the service manager."
fi
}

# =====================================================================
# WSL support functions

Expand Down

0 comments on commit 0401ac5

Please sign in to comment.