2
0
mirror of https://github.com/xcat2/xcat-core.git synced 2026-08-04 00:16:59 +00:00
Files
xcat-core/xCAT-server/share/xcat/scripts/xcatd-init-compat
T
Vinícius Ferrão 557913693c fix(packaging): expose explicit init target detection
(cherry picked from commit 8489e15969)
2026-07-24 15:25:45 +00:00

188 lines
4.6 KiB
Bash
Executable File

#!/bin/sh
# Keep the xcatd SysV init script only on systems that actually use a
# legacy init implementation. XCAT_COMPAT_ROOT is used by image builders
# and tests to operate on a target root without chrooting into it.
compat_root=${XCAT_COMPAT_ROOT:-}
compat_root=${compat_root%/}
xcat_root=${XCATROOT:-/opt/xcat}
case "$xcat_root" in
/*) ;;
*)
echo "XCATROOT must be an absolute path" >&2
exit 2
;;
esac
legacy_init="$compat_root/etc/init.d/xcatd"
legacy_template="$compat_root$xcat_root/share/xcat/scripts/xcatd"
systemd_os_release_status()
{
os_release="$compat_root/etc/os-release"
[ -r "$os_release" ] || return 2
os_id=
os_version=
while IFS='=' read -r key value; do
value=${value#\"}
value=${value%\"}
value=${value#\'}
value=${value%\'}
case "$key" in
ID) os_id=$value ;;
VERSION_ID) os_version=$value ;;
esac
done < "$os_release"
# Debian testing/sid intentionally omits VERSION_ID.
if [ "$os_id" = "debian" ] && [ -z "$os_version" ]; then
return 0
fi
os_major=${os_version%%.*}
case "$os_major" in
''|*[!0-9]*) return 2 ;;
esac
case "$os_id" in
rhel|centos|rocky|almalinux|ol|scientific)
[ "$os_major" -ge 7 ] && return 0
return 1
;;
fedora|ubuntu)
[ "$os_major" -ge 15 ] && return 0
return 1
;;
debian)
[ "$os_major" -ge 8 ] && return 0
return 1
;;
sles|sled|opensuse|opensuse-leap|opensuse-tumbleweed)
[ "$os_major" -ge 12 ] && return 0
return 1
;;
*)
return 2
;;
esac
}
uses_systemd()
{
detection_mode=${1:-compat}
if [ -d "$compat_root/run/systemd/system" ]; then
return 0
fi
if [ "$detection_mode" = explicit ]; then
if [ -L "$compat_root/sbin/init" ]; then
init_target=$(readlink "$compat_root/sbin/init" 2>/dev/null || true)
case "$init_target" in
*systemd*) return 0 ;;
*) return 1 ;;
esac
fi
if [ -e "$compat_root/sbin/init" ]; then
return 1
fi
if systemd_os_release_status; then
return 0
else
os_release_status=$?
if [ "$os_release_status" -eq 1 ]; then
return 1
fi
fi
fi
# Preserve the historical fallback order unless every package lifecycle
# phase has explicitly opted in to authoritative /sbin/init detection.
if [ -x "$compat_root/usr/lib/systemd/systemd" ] ||
[ -x "$compat_root/lib/systemd/systemd" ]; then
return 0
fi
if systemd_os_release_status; then
return 0
fi
if [ -L "$compat_root/sbin/init" ]; then
init_target=$(readlink "$compat_root/sbin/init" 2>/dev/null || true)
case "$init_target" in
*systemd*) return 0 ;;
esac
fi
return 1
}
configure_legacy_init()
{
replace_existing=${1:-}
if uses_systemd; then
rm -f "$legacy_init"
return
fi
# A broken link cannot provide a usable init script. Replace it while
# preserving links whose administrator-managed target still exists.
if [ -L "$legacy_init" ] && [ ! -e "$legacy_init" ]; then
rm -f "$legacy_init"
fi
if [ -e "$legacy_init" ] || [ -L "$legacy_init" ]; then
if [ "$replace_existing" != "--replace" ]; then
return
fi
rm -f "$legacy_init"
fi
if [ ! -r "$legacy_template" ]; then
echo "Legacy xcatd init template not found: $legacy_template" >&2
exit 1
fi
mkdir -p "$compat_root/etc/init.d"
cp "$legacy_template" "$legacy_init"
chmod 755 "$legacy_init"
}
case "${1:-}" in
uses-systemd)
detection_mode=compat
case "${2:-}" in
'') ;;
--explicit-target) detection_mode=explicit ;;
*)
echo "Usage: $0 uses-systemd [--explicit-target]" >&2
exit 2
;;
esac
uses_systemd "$detection_mode"
;;
configure)
case "${2:-}" in
''|--replace) ;;
*)
echo "Usage: $0 {uses-systemd [--explicit-target]|configure [--replace]|remove}" >&2
exit 2
;;
esac
configure_legacy_init "${2:-}"
;;
remove)
rm -f "$legacy_init"
;;
*)
echo "Usage: $0 {uses-systemd [--explicit-target]|configure [--replace]|remove}" >&2
exit 2
;;
esac