add script 'fix-ifupdown-resolved.sh'.

The ifupdown hooks /etc/network/if-up.d/resolved and if-down.d/resolved
(Debian 12 and 13) use 'return' outside of a function. With /bin/sh ->
dash this ends the script with 0, with /bin/sh -> bash (set by our
Ansible role common/tasks/basic.yml) it fails with exit code 2, so
'ifup lo' fails and networking.service ends up as 'failed'.

The script replaces top-level 'return' by 'exit 0' (same behaviour as
under dash), keeps a backup in /root/ifupdown-backup/ and resets the
failed state of networking.service. Dry-run by default, --apply to
change the files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrisandClaude Opus 5.5 committed 2026-11-22 01:36:24 +01:00
1 parent 67e34c4d13
commit bdc4445961
1 file changed
+94
+94
View File
@@ -0,0 +1,94 @@
#!/usr/bin/env bash
# =============================================================================
# fix-ifupdown-resolved.sh - networking.service scheitert an "ifup lo"
#
# Hintergrund (2026-11-22):
# Die ifupdown-Hooks /etc/network/if-up.d/resolved und if-down.d/resolved
# (Paket ifupdown, Debian 12 und 13) verlassen sich darauf, dass /bin/sh
# dash ist: sie benutzen "return" ausserhalb einer Funktion. dash beendet
# das Script dann mit 0, bash bricht mit Fehler 2 ab.
# Unsere Ansible-Rolle (common/tasks/basic.yml) setzt /bin/sh -> bash,
# deshalb scheitert "ifup lo" und networking.service steht auf "failed".
#
# Korrektur: "return" auf oberster Ebene -> "exit 0" (Verhalten wie unter
# dash). Die Hooks sind conffiles; dpkg fragt bei einem ifupdown-Update
# nach, ob die lokale Aenderung behalten werden soll.
#
# Aufruf (als root):
# fix-ifupdown-resolved.sh Pruefen (Standard, aendert nichts)
# fix-ifupdown-resolved.sh --apply Hooks korrigieren, networking-Status
# zuruecksetzen (kein Neustart des Netzes)
# =============================================================================
set -u
APPLY=0
for arg in "$@"; do
case "$arg" in
--apply) APPLY=1 ;;
-h|--help) sed -n '2,21p' "$0"; exit 0 ;;
*) echo "Unbekannte Option: $arg" >&2; exit 2 ;;
esac
done
HOST=$(hostname -f 2>/dev/null || hostname)
TS=$(date +%Y-%m-%d-%H%M%S)
BACKUP_DIR=/root/ifupdown-backup
info() { echo "[$HOST] $*"; }
die() { echo "[$HOST] FEHLER: $*" >&2; exit 1; }
[ "$(id -u)" = 0 ] || die "muss als root laufen (sudo $0 $*)"
# Zeilen, die nur aus "return" bestehen (oberste Ebene des Scripts)
PATTERN='^([[:space:]]*)return[[:space:]]*$'
changed=0
for f in /etc/network/if-up.d/resolved /etc/network/if-down.d/resolved; do
[ -f "$f" ] || { info "nicht vorhanden: $f"; continue; }
# Sicherheitscheck: im Script darf es keine Funktionen geben, sonst
# koennte ein "return" legitim sein
if grep -qE '^[[:space:]]*[A-Za-z_][A-Za-z0-9_]*[[:space:]]*\(\)' "$f"; then
info "uebersprungen (enthaelt Funktionen, bitte manuell pruefen): $f"
continue
fi
n=$(grep -cE "$PATTERN" "$f")
if [ "$n" = 0 ]; then
info "ok: $f"
continue
fi
changed=1
info "$f: $n x 'return' auf oberster Ebene -> 'exit 0'"
if [ "$APPLY" = 1 ]; then
mkdir -p "$BACKUP_DIR"
cp -p "$f" "$BACKUP_DIR/$(echo "$f" | tr '/' '_').$TS"
tmp=$(mktemp "$f.XXXXXX") || die "mktemp fehlgeschlagen"
sed -E "s/$PATTERN/\\1exit 0/" "$f" > "$tmp"
bash -n "$tmp" || { rm -f "$tmp"; die "Syntaxfehler nach Aenderung: $f"; }
chmod --reference="$f" "$tmp"; chown --reference="$f" "$tmp"
mv -f "$tmp" "$f"
fi
done
# Test: Hook fuer lo so aufrufen, wie ifupdown es tut
if [ "$APPLY" = 1 ] || [ "$changed" = 0 ]; then
if [ -f /etc/network/if-up.d/resolved ]; then
IFACE=lo ADDRFAM=inet MODE=start PHASE=post-up /etc/network/if-up.d/resolved
rc=$?
if [ "$rc" = 0 ]; then
info "Test if-up.d/resolved fuer lo: ok"
else
info "Test if-up.d/resolved fuer lo: FEHLER rc=$rc"
fi
fi
fi
if [ "$APPLY" = 1 ] && [ "$changed" = 1 ]; then
if systemctl -q is-failed networking 2>/dev/null; then
systemctl reset-failed networking
info "networking.service: Fehlerstatus zurueckgesetzt (wird beim naechsten Boot sauber gestartet)"
fi
elif [ "$changed" = 1 ]; then
info "Dry-run - zum Ausfuehren: $0 --apply"
fi
exit 0