Update ha_entrypoint.sh

This commit is contained in:
Alexandre
2026-01-16 10:29:37 +01:00
committed by GitHub
parent b2570bd4aa
commit 8ddf0df0ce

View File

@@ -1,152 +1,248 @@
#!/command/with-contenv bashio #!/bin/bash
# shellcheck shell=bash # shellcheck shell=bash
set -euo pipefail
##########################################
# Detect if this is PID1 (main process) #
##########################################
# Detect if this is PID1 (main container process) — do this once at the start
PID1=false PID1=false
if [ "$$" -eq 1 ]; then if [ "$$" -eq 1 ]; then
PID1=true PID1=true
echo "Starting as entrypoint" echo "Starting as entrypoint"
# Allow s6 commands if [ -d /command ]; then
if [ -d /command ]; then ln -sf /command/* /usr/bin/ 2>/dev/null || true
ln -sf /command/* /usr/bin/ fi
fi
else else
echo "Starting custom scripts" echo "Starting custom scripts"
fi
##########################################
# Pick an exec-capable directory #
##########################################
pick_exec_dir() {
# Prefer locations that are commonly exec-capable in containers
# and writable. Avoid /tmp because it may be mounted noexec.
local d
for d in /dev/shm /run /var/run /mnt /root /; do
if [ -d "$d" ] && [ -w "$d" ]; then
# Create a tiny test executable to confirm "exec" works
local t="${d%/}/.exec_test_$$"
printf '#!/bin/sh\necho ok\n' > "$t" 2>/dev/null || { rm -f "$t" 2>/dev/null || true; continue; }
chmod 700 "$t" 2>/dev/null || { rm -f "$t" 2>/dev/null || true; continue; }
if "$t" >/dev/null 2>&1; then
rm -f "$t" 2>/dev/null || true
echo "$d"
return 0
fi
rm -f "$t" 2>/dev/null || true
fi
done
return 1
}
EXEC_DIR="$(pick_exec_dir || true)"
if [ -z "${EXEC_DIR:-}" ]; then
echo "ERROR: Could not find an exec-capable writable directory (e.g., /dev/shm,/run)."
echo "Your environment likely mounts all writable dirs as noexec; shebang validation cannot run safely."
exit 1
fi fi
###################### ######################
# Select the shebang # # Select the shebang #
###################### ######################
# List of candidate shebangs, prioritize with-contenv if PID1 candidate_shebangs=(
candidate_shebangs=() "/command/with-contenv bashio"
if $PID1; then "/usr/bin/with-contenv bashio"
candidate_shebangs+=("/command/with-contenv bashio" "/usr/bin/with-contenv bashio") "/usr/bin/env bashio"
fi "/usr/bin/bashio"
candidate_shebangs+=( "/usr/bin/bash"
"/usr/bin/env bashio" "/bin/bash"
"/usr/bin/bashio" "/usr/bin/sh"
"/usr/bin/bash" "/bin/sh"
"/usr/bin/sh"
"/bin/bash"
"/bin/sh"
) )
# Find the first valid shebang interpreter in candidate list probe_script_content='
set -e
if ! command -v bashio::addon.version >/dev/null 2>&1; then
for f in \
/usr/lib/bashio/bashio.sh \
/usr/lib/bashio/lib.sh \
/usr/src/bashio/bashio.sh \
/usr/local/lib/bashio/bashio.sh
do
if [ -f "$f" ]; then
# shellcheck disable=SC1090
. "$f"
break
fi
done
fi
bashio::addon.version
'
validate_shebang() {
local candidate="$1"
local tmp out rc
# shellcheck disable=SC2206
local cmd=( $candidate )
local exe="${cmd[0]}"
if [ ! -x "$exe" ]; then
echo " - FAIL (not executable): #!$candidate" >&2
return 1
fi
tmp="${EXEC_DIR%/}/shebang_test.$$.$RANDOM"
{
printf '#!%s\n' "$candidate"
printf '%s\n' "$probe_script_content"
} > "$tmp"
chmod 700 "$tmp" 2>/dev/null || true
set +e
out="$("$tmp" 2>"${EXEC_DIR%/}/shebang_probe_err.$$")"
rc=$?
set -e
rm -f "$tmp" 2>/dev/null || true
if [ "$rc" -eq 0 ] && [ -n "${out:-}" ] && [ "$out" != "null" ]; then
rm -f "${EXEC_DIR%/}/shebang_probe_err.$$" 2>/dev/null || true
return 0
fi
{
echo " - FAIL: #!$candidate"
echo " rc=$rc, stdout='${out:-}'"
if [ -s "${EXEC_DIR%/}/shebang_probe_err.$$" ]; then
echo " stderr:"
sed -n '1,8p' "${EXEC_DIR%/}/shebang_probe_err.$$"
else
echo " stderr: <empty>"
fi
} >&2
rm -f "${EXEC_DIR%/}/shebang_probe_err.$$" 2>/dev/null || true
return 1
}
shebang="" shebang=""
for candidate in "${candidate_shebangs[@]}"; do for candidate in "${candidate_shebangs[@]}"; do
command_path="${candidate%% *}" if validate_shebang "$candidate"; then
# Test if command exists and can actually execute a shell command (for shells) shebang="$candidate"
if [ -x "$command_path" ]; then break
# Try as both 'sh -c' and 'bashio echo' style fi
if "$command_path" -c 'echo yes' > /dev/null 2>&1 || "$command_path" echo "yes" > /dev/null 2>&1; then
shebang="$candidate"
break
fi
fi
done done
if [ -z "$shebang" ]; then if [ -z "$shebang" ]; then
echo "ERROR: No valid shebang found!" echo "ERROR: No valid shebang found (unable to execute bashio::addon.version via candidates)." >&2
exit 1 echo "Tried:" >&2
printf ' - %s\n' "${candidate_shebangs[@]}" >&2
exit 1
fi fi
echo "Selected shebang: #!$shebang"
#################### ####################
# Starting scripts # # Starting scripts #
#################### ####################
# Loop through /etc/cont-init.d/* scripts and execute them run_one_script() {
for SCRIPTS in /etc/cont-init.d/*; do local script="$1"
echo "$script: executing"
if [ "$(id -u)" -eq 0 ]; then
chown "$(id -u)":"$(id -g)" "$script" || true
chmod a+x "$script" || true
else
echo -e "\e[38;5;214m$(date) WARNING: Script executed with user $(id -u):$(id -g), things can break and chown won't work\e[0m"
sed -i "s/^[[:space:]]*chown /true # chown /g" "$script"
sed -i "s/^[[:space:]]*chmod /true # chmod /g" "$script"
fi
sed -i "1s|^.*|#!$shebang|" "$script"
chmod +x "$script"
if [ "${ha_entry_source:-null}" = "true" ]; then
sed -i -E 's/^[[:space:]]*exit ([0-9]+)/return \1 \|\| exit \1/g' "$script"
sed -i 's/bashio::exit\.nok/return 1/g' "$script"
sed -i 's/bashio::exit\.ok/return 0/g' "$script"
# shellcheck disable=SC1090
source "$script" || echo -e "\033[0;31mError\033[0m : $script exiting $?"
else
"$script" || echo -e "\033[0;31mError\033[0m : $script exiting $?"
fi
sed -i '1a exit 0' "$script"
}
if [ -d /etc/cont-init.d ]; then
for SCRIPTS in /etc/cont-init.d/*; do
[ -e "$SCRIPTS" ] || continue [ -e "$SCRIPTS" ] || continue
echo "$SCRIPTS: executing" run_one_script "$SCRIPTS"
done
fi
# Check if run as root (UID 0)
if [ "$(id -u)" -eq 0 ]; then
# Fix permissions for root user
chown "$(id -u)":"$(id -g)" "$SCRIPTS"
chmod a+x "$SCRIPTS"
else
echo -e "\e[38;5;214m$(date) WARNING: Script executed with user $(id -u):$(id -g), things can break and chown won't work\e[0m"
# Disable chown and chmod commands inside the script for non-root users
sed -i "s/^\s*chown /true # chown /g" "$SCRIPTS"
sed -i "s/^\s*chmod /true # chmod /g" "$SCRIPTS"
fi
# Prepare to run
sed -i "1s|^.*|#!$shebang|" "$SCRIPTS"
chmod +x "$SCRIPTS"
# Optionally use 'source' to share env variables, when requested
if [ "${ha_entry_source:-null}" = true ]; then
# Replace exit with return, so sourced scripts can return errors
sed -i -E 's/^\s*exit ([0-9]+)/return \1 \|\| exit \1/g' "$SCRIPTS"
sed -i 's/bashio::exit\.nok/return 1/g' "$SCRIPTS"
sed -i 's/bashio::exit\.ok/return 0/g' "$SCRIPTS"
# shellcheck disable=SC1090
source "$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
else
"$SCRIPTS" || echo -e "\033[0;31mError\033[0m : $SCRIPTS exiting $?"
fi
# Cleanup after execution
sed -i '1a exit 0' "$SCRIPTS"
done
# Start run scripts in services.d and s6-overlay/s6-rc.d if PID1
if $PID1; then if $PID1; then
# Run services shopt -s nullglob
shopt -s nullglob for runfile in /etc/services.d/*/run /etc/s6-overlay/s6-rc.d/*/run; do
for runfile in /etc/services.d/*/run /etc/s6-overlay/s6-rc.d/*/run; do [ -f "$runfile" ] || continue
[ -f "$runfile" ] || continue echo "Starting: $runfile"
echo "Starting: $runfile" sed -i "1s|^.*|#!$shebang|" "$runfile"
sed -i "1s|^.*|#!$shebang|" "$runfile" chmod +x "$runfile"
chmod +x "$runfile" (exec "$runfile") &
(exec "$runfile") & true
true done
done shopt -u nullglob
shopt -u nullglob
fi fi
###################### ######################
# Starting container # # Starting container #
###################### ######################
# If this is PID 1, keep alive and manage sigterm for clean shutdown
if $PID1; then if $PID1; then
echo " " echo " "
echo -e "\033[0;32mEverything started!\033[0m" echo -e "\033[0;32mEverything started!\033[0m"
terminate() {
echo "Termination signal received, forwarding to subprocesses..." terminate() {
# Terminate all direct child processes echo "Termination signal received, forwarding to subprocesses..."
if command -v pgrep &> /dev/null; then if command -v pgrep >/dev/null 2>&1; then
for pid in $(pgrep -P $$); do while read -r pid; do
echo "Terminating child PID $pid" [ -n "$pid" ] || continue
kill -TERM "$pid" 2> /dev/null || echo "Failed to terminate PID $pid" echo "Terminating child PID $pid"
done kill -TERM "$pid" 2>/dev/null || echo "Failed to terminate PID $pid"
else done < <(pgrep -P "$$" || true)
# Fallback: Scan /proc for children else
for pid in /proc/[0-9]*/; do for p in /proc/[0-9]*/; do
pid=${pid#/proc/} local_pid="${p#/proc/}"
pid=${pid%/} local_pid="${local_pid%/}"
if [[ "$pid" -ne 1 ]] && grep -q "^PPid:\s*$$" "/proc/$pid/status" 2> /dev/null; then if [ "$local_pid" -ne 1 ] && grep -q "^PPid:[[:space:]]*$$" "/proc/$local_pid/status" 2>/dev/null; then
echo "Terminating child PID $pid" echo "Terminating child PID $local_pid"
kill -TERM "$pid" 2> /dev/null || echo "Failed to terminate PID $pid" kill -TERM "$local_pid" 2>/dev/null || echo "Failed to terminate PID $local_pid"
fi
done
fi fi
wait done
echo "All subprocesses terminated. Exiting." fi
exit 0 wait || true
} echo "All subprocesses terminated. Exiting."
trap terminate SIGTERM SIGINT exit 0
# Main keep-alive loop }
while :; do
sleep infinity & trap terminate SIGTERM SIGINT
wait $! while :; do
done sleep infinity &
wait $!
done
else else
echo " " echo " "
echo -e "\033[0;32mStarting the upstream container\033[0m" echo -e "\033[0;32mStarting the upstream container\033[0m"
echo " " echo " "
# Launch optional mods script if present if [ -f /docker-mods ]; then
if [ -f /docker-mods ]; then exec /docker-mods; fi exec /docker-mods
fi
fi fi