From 9313731e96c29ac2fa41f5ca4f5ccd2a75d17dc9 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Tue, 19 Sep 2023 17:34:55 +1000 Subject: [PATCH] ctdb-scripts: Update detect_init_style to use /etc/os-release /etc/os-release is quite universal. It can be found on most Linux distros and on FreeBSD. Attempt to use /etc/os-release to detect Red Hat, SUSE and Debian based distros. If /etc/os-release exists but distro is unknown then $ID is printed as the detected distro, which will probably result in sub-optimal behaviour, but when tracing it will at least indicate that a new distro needs to be handled. The only way to handle missing /etc/os-release is to set CTDB_INIT_STYLE - see ctdb.sysconfig(5) for details. The event script unit tests are updated to use /etc/os-release so the new logic is exercised. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs Autobuild-User(master): Amitay Isaacs Autobuild-Date(master): Mon Oct 30 09:19:11 UTC 2023 on atb-devel-224 --- ctdb/config/functions | 50 +++++++++++++++---- .../tests/UNIT/eventscripts/etc-ctdb/rc.local | 4 +- ctdb/tests/UNIT/eventscripts/etc/os-release | 2 + 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 ctdb/tests/UNIT/eventscripts/etc/os-release diff --git a/ctdb/config/functions b/ctdb/config/functions index d8f7f57b84c..a40b276e2b8 100755 --- a/ctdb/config/functions +++ b/ctdb/config/functions @@ -160,18 +160,48 @@ ctdb_check_args() # determine on what type of system (init style) we are running detect_init_style() { - # only do detection if not already set: - if [ -n "$CTDB_INIT_STYLE" ]; then - return - fi + _init_style_file="${CTDB_SCRIPT_VARDIR}/init-style" - if [ -x /sbin/startproc ]; then - CTDB_INIT_STYLE="suse" - elif [ -x /sbin/start-stop-daemon ]; then - CTDB_INIT_STYLE="debian" - else - CTDB_INIT_STYLE="redhat" + if [ ! -f "$_init_style_file" ]; then + if [ -n "$CTDB_INIT_STYLE" ]; then + echo "$CTDB_INIT_STYLE" >"$_init_style_file" + return + fi + + # Subshell to contain variables in os-release file + ( + _os_release="${CTDB_SYS_ETCDIR}/os-release" + if [ -f "$_os_release" ]; then + . "$_os_release" + case "$ID" in + centos | fedora | rhel) + echo "redhat" + ;; + debian | ubuntu) + echo "debian" + ;; + sles | suse) + echo "suse" + ;; + *) + case "$ID_LIKE" in + *centos* | *rhel*) + echo "redhat" + ;; + *) + echo "$ID" + ;; + esac + ;; + esac + else + echo "WARNING: unknown distribution ${ID}" >&2 + echo "unknown" + fi + ) >"$_init_style_file" fi + + read -r CTDB_INIT_STYLE <"$_init_style_file" } ###################################################### diff --git a/ctdb/tests/UNIT/eventscripts/etc-ctdb/rc.local b/ctdb/tests/UNIT/eventscripts/etc-ctdb/rc.local index aa9b8b22fec..777aeaff8b3 100755 --- a/ctdb/tests/UNIT/eventscripts/etc-ctdb/rc.local +++ b/ctdb/tests/UNIT/eventscripts/etc-ctdb/rc.local @@ -51,4 +51,6 @@ background_with_logging () "$@" 2>&1