Add basic support for dinit in our system service handling code. (#16836)
* Add basic dinit detection to install-service script. * Add dinit service file for Netdata. * Add service install support for dinit.
Austin S. Hemmelgarn committed
Apr 4, 2024 at 08:25 UTC
0401ac56f711302140542d2989ac849fab8ad0f1
3 files changed
+76
-1
CMakeLists.txt
+5
@@ -2333,6 +2333,11 @@ install(PROGRAMS
2333
${CMAKE_BINARY_DIR}/system/runit/run
2334
DESTINATION usr/lib/netdata/system/runit)
2335
2336
+configure_file(system/dinit/netdata.in system/dinit/netdata @ONLY)
2337
+install(FILES
2338
+ ${CMAKE_BINARY_DIR}/system/dinit/netdata
2339
+ DESTINATION usr/lib/netdata/system/dinit)
2340
+
2341
configure_file(system/systemd/netdata.service.in system/systemd/netdata.service @ONLY)
2342
install(FILES
2343
${CMAKE_BINARY_DIR}/system/systemd/netdata.service
system/dinit/netdata.in
new
+11
@@ -0,0 +1,11 @@
1
+# dinit service description for Netdata
2
+#
3
+# Currently only properly tested on Chimera Linux.
4
+
5
+type = bgprocess
6
+command = @sbindir_POST@/netdata -P @localstatedir_POST@/run/netdata.pid -D
7
+pid-file = @localstatedir_POST@/run/netdata.pid
8
+options = signal-process-only
9
+load-options = export-passwd-vars
10
+depends-on = early-fs-local.target
11
+after = network.target
system/install-service.sh.in
+60
-1
@@ -29,7 +29,7 @@ DUMP_CMDS=0
29
ENABLE="auto"
30
EXPORT_CMDS=0
31
INSTALL=1
32
-LINUX_INIT_TYPES="systemd openrc lsb initd runit"
32
+LINUX_INIT_TYPES="systemd openrc lsb initd runit dinit"
33
PLATFORM="$(uname -s)"
34
SHOW_SVC_TYPE=0
35
SVC_SOURCE="@libsysdir_POST@"
@@ -562,6 +562,65 @@ runit_cmds() {
562
NETDATA_STOP_CMD="sv stop netdata"
563
}
564
565
+# =====================================================================
566
+# dinit support functions
567
+
568
+_check_dinit() {
569
+ # if /etc/dinit.d does not exist, it’s not dinit
570
+ [ ! -d /etc/dinit.d ] && echo "NO" && return 0
571
+
572
+ # if PID 1 is dinit, it’s dinit
573
+ [ "$(basename "$(readlink /proc/1/exe)" 2> /dev/null)" = "dinit" ] && echo "YES" && return 0
574
+
575
+ # if /run/dinitctl exists and is a socket, it’s dinit
576
+ [ -S /run/dinitctl ] && echo "YES" && return 0
577
+
578
+ # if the dinitctl command exists despite getting to this point, it’s dinit, but not booted as such
579
+ [ -n "$(command -v dinitctl 2>/dev/null || true)" ] && echo "OFFLINE" && return 0
580
+
581
+ echo "NO" && return 0
582
+}
583
+
584
+check_dinit() {
585
+ if [ -z "${IS_DINIT}" ]; then
586
+ IS_DINIT="$(_check_dinit)"
587
+ fi
588
+
589
+ echo "${IS_DINIT}"
590
+}
591
+
592
+_run_dinitctl() {
593
+ opts=''
594
+
595
+ if [ "$(check_dinit)" = "OFFLINE" ]; then
596
+ opts="-o"
597
+ fi
598
+
599
+ # shellcheck disable=SC2086
600
+ dinitctl ${opts} "${@}"
601
+}
602
+
603
+enable_dinit() {
604
+ _run_dinitctl enable netdata
605
+}
606
+
607
+enable_dinit() {
608
+ _run_dinitctl disable netdata
609
+}
610
+
611
+install_dinit_service() {
612
+ install_generic_service dinit/netdata "dinit" /etc/dinit.d enable_dinit disable_dinit
613
+}
614
+
615
+dinit_cmds() {
616
+ if [ "$(check_dinit)" = "YES" ]; then
617
+ NETDATA_START_CMD='dinitctl start netdata'
618
+ NETDATA_STOP_CMD='dinitct stop netdata'
619
+ else # Not booted using dinit, use external defaults by not providing commands.
620
+ warning "Detected dinit, but the system is not booted using dinit. Unable to provide commands to start or stop Netdata using the service manager."
621
+ fi
622
+}
623
+
624
# =====================================================================
625
# WSL support functions
626