#!/usr/bin/env bash set -uo pipefail CONFIG="/etc/pve-guest-update.conf" if [[ ! -r "$CONFIG" ]]; then echo "FEHLER: $CONFIG nicht gefunden." exit 1 fi source "$CONFIG" UPDATE_HOST="${UPDATE_HOST:-yes}" UPDATE_LXC="${UPDATE_LXC:-yes}" UPDATE_VMS="${UPDATE_VMS:-yes}" UPDATE_STOPPED="${UPDATE_STOPPED:-no}" AUTO_REBOOT_HOST="${AUTO_REBOOT_HOST:-yes}" EXCLUDE_GUESTS="${EXCLUDE_GUESTS:-}" APT_LOCK_TIMEOUT="${APT_LOCK_TIMEOUT:-600}" LOG_FILE="${LOG_FILE:-/var/log/pve-guest-update.log}" # Proxmox Notification System NOTIFY_TARGET="${NOTIFY_TARGET:-}" NOTIFY_MATCHER="${NOTIFY_MATCHER:-proxmox-updater}" NOTIFY_TEMPLATE="${NOTIFY_TEMPLATE:-proxmox-updater}" NOTIFY_ENABLED="${NOTIFY_ENABLED:-no}" MODE="update" ONLY_CT="" ONLY_VM="" NO_REBOOT="no" ERRORS=0 HOST_OK="not-run" HOST_REBOOT_REQUIRED="no" HOST_REBOOT_SCHEDULED="no" MAIL_STATUS_DETAIL="Keine automatischen Neustarts erforderlich." # Laufende Gäste, die nach erfolgreichem Update einen Neustart benötigen. # Die Neustarts werden erst am Ende des gesamten Laufs entschieden. LXC_REBOOT_PENDING=() VM_REBOOT_PENDING=() LXC_OK=0 LXC_CURRENT=0 LXC_FAILED=0 LXC_SKIPPED=0 LXC_CHECKED=0 VM_OK=0 VM_CURRENT=0 VM_FAILED=0 VM_SKIPPED=0 VM_CHECKED=0 if [[ $EUID -ne 0 ]]; then echo "FEHLER: Als root ausführen." exit 1 fi if ! command -v pveversion >/dev/null 2>&1; then echo "FEHLER: Kein Proxmox VE erkannt." exit 1 fi touch "$LOG_FILE" chmod 600 "$LOG_FILE" exec > >(tee -a "$LOG_FILE") 2>&1 log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" } ensure_notification_setup() { [[ "$NOTIFY_ENABLED" == "yes" ]] || return 0 if [[ -z "$NOTIFY_TARGET" ]]; then log "WARNUNG: Benachrichtigungen aktiviert, aber kein Proxmox-Target konfiguriert; keine Mail." return 1 fi # Das konfigurierte Proxmox-Target muss existieren. if ! pvesh get /cluster/notifications/targets --output-format json 2>/dev/null | grep -Fq "\"name\":\"$NOTIFY_TARGET\"" then log "WARNUNG: Notification-Target '$NOTIFY_TARGET' nicht gefunden; keine Mail." return 1 fi # Eigene Templates für Betreff und Nachricht. local template_dir="/etc/pve/notification-templates/default" mkdir -p "$template_dir" cat > "$template_dir/${NOTIFY_TEMPLATE}-subject.txt.hbs" <<'EOF' {{ title }} EOF cat > "$template_dir/${NOTIFY_TEMPLATE}-body.txt.hbs" <<'EOF' {{ message }} EOF cat > "$template_dir/${NOTIFY_TEMPLATE}-body.html.hbs" <<'EOF'
{{ message }}
EOF # Eigener Matcher: nur Meldungen dieses Updaters an das vorhandene # Proxmox-Notification-Target senden. if ! pvesh get "/cluster/notifications/matchers/$NOTIFY_MATCHER" \ >/dev/null 2>&1 then if ! pvesh create /cluster/notifications/matchers \ --name "$NOTIFY_MATCHER" \ --mode all \ --match-field 'exact:type=proxmox-updater' \ --target "$NOTIFY_TARGET" \ --comment 'Proxmox Guest Updater' \ >/dev/null then log "WARNUNG: Notification-Matcher '$NOTIFY_MATCHER' konnte nicht angelegt werden." return 1 fi fi return 0 } send_update_notification() { [[ "$NOTIFY_ENABLED" == "yes" ]] || return 0 if ! ensure_notification_setup; then return 0 fi local severity="info" local status="Erfolgreich" local title local message if (( ERRORS > 0 )); then severity="error" status="FEHLER" fi if [[ "$MODE" == "check" ]]; then title="Proxmox Update-Check - $status" else title="Proxmox Updater - $status" fi message="Proxmox Guest Updater Host: $(hostname) Zeit: $(date '+%d.%m.%Y %H:%M:%S') Host: Update: $HOST_OK Reboot erforderlich: $HOST_REBOOT_REQUIRED Reboot geplant: $HOST_REBOOT_SCHEDULED LXC: Aktualisiert: $LXC_OK Bereits aktuell: $LXC_CURRENT Fehlgeschlagen: $LXC_FAILED Übersprungen: $LXC_SKIPPED VM: Aktualisiert: $VM_OK Bereits aktuell: $VM_CURRENT Fehlgeschlagen: $VM_FAILED Übersprungen: $VM_SKIPPED Fehler gesamt: $ERRORS Neustarts: $MAIL_STATUS_DETAIL Log: $LOG_FILE" export PVE_UPDATER_SEVERITY="$severity" export PVE_UPDATER_TITLE="$title" export PVE_UPDATER_MESSAGE="$message" export PVE_UPDATER_TEMPLATE="$NOTIFY_TEMPLATE" if perl -MPVE::Notify -e ' my $severity = $ENV{PVE_UPDATER_SEVERITY}; my $template = $ENV{PVE_UPDATER_TEMPLATE}; my $data = { %{PVE::Notify::common_template_data()}, title => ($ENV{PVE_UPDATER_TITLE} // ""), message => ($ENV{PVE_UPDATER_MESSAGE} // ""), }; my $fields = { type => "proxmox-updater", }; PVE::Notify::notify($severity, $template, $data, $fields); ' then log "E-Mail-Benachrichtigung über '$NOTIFY_TARGET' versendet: $title" else # Ein Mailproblem macht einen ansonsten erfolgreichen Update-Lauf # nicht nachträglich zu einem Updatefehler. log "WARNUNG: E-Mail-Benachrichtigung konnte nicht versendet werden." fi unset PVE_UPDATER_SEVERITY PVE_UPDATER_TITLE PVE_UPDATER_MESSAGE PVE_UPDATER_TEMPLATE } usage() { cat <<'EOF' Proxmox Guest Updater Verwendung: pve-guest-update Proxmox + konfigurierte Gäste aktualisieren pve-guest-update --check Nur prüfen, nichts installieren pve-guest-update --ct ID Nur einen LXC aktualisieren pve-guest-update --vm ID Nur eine VM aktualisieren pve-guest-update --no-reboot Updates durchführen, aber Host nicht automatisch neu starten pve-guest-update --help Diese Hilfe anzeigen EOF } while [[ $# -gt 0 ]]; do case "$1" in --check) MODE="check" shift ;; --ct) [[ $# -ge 2 ]] || { echo "--ct benötigt eine ID."; exit 2; } ONLY_CT="$2" shift 2 ;; --vm) [[ $# -ge 2 ]] || { echo "--vm benötigt eine ID."; exit 2; } ONLY_VM="$2" shift 2 ;; --no-reboot) NO_REBOOT="yes" shift ;; --help|-h) usage exit 0 ;; *) echo "Unbekannte Option: $1" usage exit 2 ;; esac done is_excluded() { local id="$1" local x for x in $EXCLUDE_GUESTS; do [[ "$x" == "$id" ]] && return 0 done return 1 } wait_for_apt() { local waited=0 while fuser \ /var/lib/dpkg/lock-frontend \ /var/lib/dpkg/lock \ /var/lib/apt/lists/lock \ /var/cache/apt/archives/lock \ >/dev/null 2>&1 do if (( waited >= APT_LOCK_TIMEOUT )); then return 1 fi sleep 5 waited=$((waited + 5)) done return 0 } host_reboot_required() { if [[ -e /var/run/reboot-required || -e /run/reboot-required ]]; then return 0 fi local running local latest running="$(uname -r)" # Paketnamen sauber in Kernel-Versionen umwandeln. # Wichtig: bei "proxmox-kernel-...-signed" muss "-signed" entfernt werden. # Die alte Regex konnte "-signed" in die Versionsnummer aufnehmen und # dadurch fälschlich einen Host-Neustart verlangen. latest="$( dpkg-query -W -f='${Package}\n' \ 'proxmox-kernel-*-signed' \ 'pve-kernel-*' 2>/dev/null | sed -nE \ -e 's/^proxmox-kernel-([0-9].*)-signed$/\1/p' \ -e 's/^pve-kernel-([0-9].*)$/\1/p' | sort -V | tail -n1 )" if [[ -n "$latest" && "$running" != "$latest" ]]; then return 0 fi return 1 } check_host() { log "============================================================" log "Proxmox-Host prüfen" log "============================================================" if ! wait_for_apt; then log "FEHLER: apt/dpkg-Sperre wurde nicht frei." HOST_OK="no" ERRORS=$((ERRORS + 1)) return fi if ! apt-get update; then log "FEHLER: apt-get update fehlgeschlagen." HOST_OK="no" ERRORS=$((ERRORS + 1)) return fi local count count="$( apt list --upgradable 2>/dev/null | tail -n +2 | sed '/^[[:space:]]*$/d' | wc -l )" log "Proxmox-Host: $count Update(s) verfügbar." HOST_OK="yes" } update_host() { log "============================================================" log "Proxmox-Host aktualisieren" log "============================================================" if ! wait_for_apt; then log "FEHLER: apt/dpkg-Sperre wurde nicht frei." HOST_OK="no" ERRORS=$((ERRORS + 1)) return fi if ! apt-get update; then log "FEHLER: apt-get update fehlgeschlagen." HOST_OK="no" ERRORS=$((ERRORS + 1)) return fi # Reboot-Status VOR dem Upgrade merken. So kann ein bereits wirklich # notwendiger Neustart nicht verloren gehen. local reboot_before="no" if host_reboot_required; then reboot_before="yes" fi # Vorab prüfen, ob full-upgrade überhaupt etwas ändern würde. # Wichtig: Bei 0 Änderungen wird full-upgrade nicht ausgeführt. # Damit kann ein APT/DPKG-Hook bei einem Leerlauf nicht nachträglich # fälschlich /run/reboot-required erzeugen. local simulation local upgraded=0 local installed=0 local removed=0 if ! simulation="$(LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get -s full-upgrade 2>&1)"; then printf '%s\n' "$simulation" HOST_OK="no" ERRORS=$((ERRORS + 1)) log "FEHLER: Host full-upgrade Simulation fehlgeschlagen." return fi upgraded="$(printf '%s\n' "$simulation" | sed -nE 's/^([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\1/p' | tail -n1)" installed="$(printf '%s\n' "$simulation" | sed -nE 's/^([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\2/p' | tail -n1)" removed="$(printf '%s\n' "$simulation" | sed -nE 's/^([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\3/p' | tail -n1)" if [[ -z "$upgraded" || -z "$installed" || -z "$removed" ]]; then HOST_OK="no" ERRORS=$((ERRORS + 1)) log "FEHLER: Ergebnis der Host full-upgrade Simulation konnte nicht ausgewertet werden." return fi if (( upgraded == 0 && installed == 0 && removed == 0 )); then HOST_OK="yes" log "Proxmox-Host: keine Paketänderungen erforderlich." if [[ "$reboot_before" == "yes" ]]; then HOST_REBOOT_REQUIRED="yes" log "Host-Neustart war bereits vor dem Update-Lauf erforderlich." else HOST_REBOOT_REQUIRED="no" log "Kein Host-Neustart erforderlich." fi return fi log "Proxmox-Host: $upgraded Upgrade(s), $installed Neuinstallation(en), $removed Entfernung(en) vorgesehen." if DEBIAN_FRONTEND=noninteractive apt-get -y \ -o Dpkg::Options::="--force-confdef" \ -o Dpkg::Options::="--force-confold" \ full-upgrade then HOST_OK="yes" log "Proxmox-Host erfolgreich aktualisiert." else HOST_OK="no" ERRORS=$((ERRORS + 1)) log "FEHLER: Host full-upgrade fehlgeschlagen." return fi if host_reboot_required; then HOST_REBOOT_REQUIRED="yes" log "Host-Neustart erforderlich." else HOST_REBOOT_REQUIRED="no" log "Kein Host-Neustart erforderlich." fi } lxc_name() { pct config "$1" 2>/dev/null | awk -F': ' '$1=="hostname"{print $2; exit}' } lxc_supported() { pct exec "$1" -- sh -c ' [ -r /etc/os-release ] || exit 1 . /etc/os-release case "${ID:-}" in debian|ubuntu) exit 0 ;; *) exit 1 ;; esac ' >/dev/null 2>&1 } check_lxc() { local id="$1" local name local status local count name="$(lxc_name "$id")" name="${name:-unbekannt}" status="$(pct status "$id" 2>/dev/null | awk '{print $2}')" if is_excluded "$id"; then log "CT $id ($name): ausgeschlossen." LXC_SKIPPED=$((LXC_SKIPPED + 1)) return fi if [[ "$status" != "running" ]]; then log "CT $id ($name): gestoppt -> übersprungen." LXC_SKIPPED=$((LXC_SKIPPED + 1)) return fi if ! lxc_supported "$id"; then log "CT $id ($name): kein Debian/Ubuntu -> übersprungen." LXC_SKIPPED=$((LXC_SKIPPED + 1)) return fi log "CT $id ($name): prüfe Updates ..." if ! pct exec "$id" -- bash -lc 'apt-get update'; then log "CT $id ($name): apt-get update FEHLER." LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi count="$( pct exec "$id" -- bash -lc \ "apt list --upgradable 2>/dev/null | tail -n +2 | sed '/^[[:space:]]*$/d' | wc -l" 2>/dev/null | tail -n1 | tr -d '\r[:space:]' )" log "CT $id ($name): ${count:-?} Update(s) verfügbar." LXC_CHECKED=$((LXC_CHECKED + 1)) } update_lxc() { local id="$1" local name local status local started_by_us="no" local reboot_required="no" name="$(lxc_name "$id")" name="${name:-unbekannt}" status="$(pct status "$id" 2>/dev/null | awk '{print $2}')" if is_excluded "$id"; then log "CT $id ($name): ausgeschlossen." LXC_SKIPPED=$((LXC_SKIPPED + 1)) return fi if [[ "$status" != "running" ]]; then if [[ "$UPDATE_STOPPED" != "yes" ]]; then log "CT $id ($name): gestoppt -> übersprungen." LXC_SKIPPED=$((LXC_SKIPPED + 1)) return fi log "CT $id ($name): wird für Update gestartet ..." if ! pct start "$id"; then log "CT $id ($name): Start fehlgeschlagen." LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi started_by_us="yes" sleep 10 fi if ! lxc_supported "$id"; then log "CT $id ($name): kein Debian/Ubuntu -> übersprungen." if [[ "$started_by_us" == "yes" ]]; then pct shutdown "$id" --timeout 60 || pct stop "$id" fi LXC_SKIPPED=$((LXC_SKIPPED + 1)) return fi log "------------------------------------------------------------" log "CT $id ($name) aktualisieren" log "------------------------------------------------------------" if ! pct exec "$id" -- bash -lc 'apt-get update'; then log "CT $id ($name): apt-get update FEHLER." LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then pct shutdown "$id" --timeout 60 || pct stop "$id" fi return fi local simulation local upgraded=0 local installed=0 local removed=0 if ! simulation="$(pct exec "$id" -- bash -lc 'LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get -s full-upgrade' 2>&1)"; then printf '%s\n' "$simulation" log "CT $id ($name): full-upgrade Simulation FEHLER." LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then pct shutdown "$id" --timeout 60 || pct stop "$id" fi return fi upgraded="$(printf '%s\n' "$simulation" | sed -nE 's/^([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\1/p' | tail -n1)" installed="$(printf '%s\n' "$simulation" | sed -nE 's/^([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\2/p' | tail -n1)" removed="$(printf '%s\n' "$simulation" | sed -nE 's/^([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\3/p' | tail -n1)" if [[ -z "$upgraded" || -z "$installed" || -z "$removed" ]]; then log "CT $id ($name): Simulation konnte nicht ausgewertet werden." LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then pct shutdown "$id" --timeout 60 || pct stop "$id" fi return fi if (( upgraded == 0 && installed == 0 && removed == 0 )); then log "CT $id ($name): bereits aktuell." LXC_CURRENT=$((LXC_CURRENT + 1)) else log "CT $id ($name): $upgraded Upgrade(s), $installed Neuinstallation(en), $removed Entfernung(en)." if ! pct exec "$id" -- bash -lc \ 'DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" full-upgrade' then log "CT $id ($name): full-upgrade FEHLER." LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then pct shutdown "$id" --timeout 60 || pct stop "$id" fi return fi if pct exec "$id" -- test -e /var/run/reboot-required >/dev/null 2>&1; then reboot_required="yes" log "CT $id ($name): Update erfolgreich - Neustart erforderlich." else log "CT $id ($name): Update erfolgreich." fi LXC_OK=$((LXC_OK + 1)) fi if [[ "$started_by_us" == "yes" ]]; then log "CT $id ($name): war vorher gestoppt -> wird wieder heruntergefahren." if ! pct shutdown "$id" --timeout 60; then log "CT $id ($name): sauberer Shutdown fehlgeschlagen -> Stop." if ! pct stop "$id"; then log "FEHLER: CT $id ($name) konnte nicht gestoppt werden." LXC_OK=$((LXC_OK - 1)) LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) fi fi return fi if [[ "$reboot_required" == "yes" ]]; then # Nicht sofort neu starten. Erst nach allen Updates entscheiden wir, # ob ohnehin der Proxmox-Host neu gestartet wird. LXC_REBOOT_PENDING+=("$id") log "CT $id ($name): Neustart vorgemerkt." fi } vm_name() { qm config "$1" 2>/dev/null | awk -F': ' '$1=="name"{print $2; exit}' } vm_agent_enabled() { qm config "$1" 2>/dev/null | grep -Eq '^agent:[[:space:]]*1' } vm_agent_alive() { qm agent "$1" ping >/dev/null 2>&1 } vm_is_linux() { local id="$1" local os os="$( qm guest cmd "$id" get-osinfo 2>/dev/null | grep -o '"id"[[:space:]]*:[[:space:]]*"[^"]*"' | head -n1 | cut -d'"' -f4 )" case "$os" in debian|ubuntu) return 0 ;; *) return 1 ;; esac } vm_exec() { local id="$1" shift local cmd="$*" local output local qm_rc local guest_rc # "qm guest exec" liefert das Ergebnis als JSON. Für die Entscheidung, # ob apt/test innerhalb der VM erfolgreich war, werten wir deshalb # ausdrücklich "exitcode" des Gastprozesses aus. output="$(qm guest exec "$id" -- bash -lc "$cmd" 2>&1)" qm_rc=$? printf '%s\n' "$output" if (( qm_rc != 0 )); then return "$qm_rc" fi guest_rc="$( printf '%s\n' "$output" | sed -nE 's/^[[:space:]]*"exitcode"[[:space:]]*:[[:space:]]*([0-9]+),?[[:space:]]*$/\1/p' | head -n1 )" if [[ -z "$guest_rc" ]]; then return 1 fi if (( guest_rc == 0 )); then return 0 fi return 1 } check_vm() { local id="$1" local name local status name="$(vm_name "$id")" name="${name:-unbekannt}" status="$(qm status "$id" 2>/dev/null | awk '{print $2}')" if is_excluded "$id"; then log "VM $id ($name): ausgeschlossen." VM_SKIPPED=$((VM_SKIPPED + 1)) return fi if [[ "$status" != "running" ]]; then log "VM $id ($name): gestoppt -> übersprungen." VM_SKIPPED=$((VM_SKIPPED + 1)) return fi if ! vm_agent_enabled "$id"; then log "FEHLER: VM $id ($name): QEMU Guest Agent nicht aktiviert." VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi if ! vm_agent_alive "$id"; then log "FEHLER: VM $id ($name): QEMU Guest Agent nicht erreichbar." VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi if ! vm_is_linux "$id"; then log "VM $id ($name): keine unterstützte Debian/Ubuntu-VM -> übersprungen." VM_SKIPPED=$((VM_SKIPPED + 1)) return fi log "VM $id ($name): Debian/Ubuntu erkannt." log "VM $id ($name): QEMU Guest Agent erreichbar; automatische Updates unterstützt." VM_CHECKED=$((VM_CHECKED + 1)) } update_vm() { local id="$1" local name local status local started_by_us="no" local reboot_required="no" name="$(vm_name "$id")" name="${name:-unbekannt}" status="$(qm status "$id" 2>/dev/null | awk '{print $2}')" if is_excluded "$id"; then log "VM $id ($name): ausgeschlossen." VM_SKIPPED=$((VM_SKIPPED + 1)) return fi # Windows-VMs bereits anhand der Proxmox-Konfiguration erkennen. # Sie dürfen insbesondere nicht nur für den Linux-Updater gestartet werden. local ostype ostype="$(qm config "$id" 2>/dev/null | sed -nE 's/^ostype:[[:space:]]*//p' | head -n1)" case "$ostype" in win10|win11|win7|win8|wvista|wxp|w2k|w2k3|w2k8) log "VM $id ($name): Windows-VM -> übersprungen." VM_SKIPPED=$((VM_SKIPPED + 1)) return ;; esac if [[ "$status" != "running" ]]; then if [[ "$UPDATE_STOPPED" != "yes" ]]; then log "VM $id ($name): gestoppt -> übersprungen." VM_SKIPPED=$((VM_SKIPPED + 1)) return fi log "VM $id ($name): wird für Update gestartet ..." if ! qm start "$id"; then log "VM $id ($name): Start fehlgeschlagen." VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi started_by_us="yes" # Auf den QEMU Guest Agent warten. local waited=0 while ! vm_agent_alive "$id"; do if (( waited >= 120 )); then log "VM $id ($name): QEMU Guest Agent nach 120 Sekunden nicht erreichbar." if ! qm shutdown "$id" --timeout 60; then qm stop "$id" || true fi VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi sleep 5 waited=$((waited + 5)) done fi if ! vm_agent_enabled "$id"; then log "FEHLER: VM $id ($name): QEMU Guest Agent nicht aktiviert." if [[ "$started_by_us" == "yes" ]]; then qm shutdown "$id" --timeout 60 || qm stop "$id" fi VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi if ! vm_agent_alive "$id"; then log "FEHLER: VM $id ($name): QEMU Guest Agent nicht erreichbar." if [[ "$started_by_us" == "yes" ]]; then qm shutdown "$id" --timeout 60 || qm stop "$id" fi VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) return fi if ! vm_is_linux "$id"; then log "VM $id ($name): keine unterstützte Debian/Ubuntu-VM -> übersprungen." if [[ "$started_by_us" == "yes" ]]; then qm shutdown "$id" --timeout 60 || qm stop "$id" fi VM_SKIPPED=$((VM_SKIPPED + 1)) return fi log "------------------------------------------------------------" log "VM $id ($name) aktualisieren" log "------------------------------------------------------------" if ! vm_exec "$id" 'apt-get update'; then log "VM $id ($name): apt-get update FEHLER." VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then qm shutdown "$id" --timeout 60 || qm stop "$id" fi return fi local simulation local sim_rc local upgraded=0 local installed=0 local removed=0 simulation="$(qm guest exec "$id" -- bash -lc \ 'LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get -s full-upgrade | tail -n1' 2>&1)" sim_rc=$? if (( sim_rc != 0 )) || ! printf '%s\n' "$simulation" | grep -Eq '"exitcode"[[:space:]]*:[[:space:]]*0'; then printf '%s\n' "$simulation" log "VM $id ($name): full-upgrade Simulation FEHLER." VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then qm shutdown "$id" --timeout 60 || qm stop "$id" fi return fi upgraded="$(printf '%s\n' "$simulation" | sed -nE 's/.*out-data"[[:space:]]*:[[:space:]]*"([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\1/p' | head -n1)" installed="$(printf '%s\n' "$simulation" | sed -nE 's/.*out-data"[[:space:]]*:[[:space:]]*"([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\2/p' | head -n1)" removed="$(printf '%s\n' "$simulation" | sed -nE 's/.*out-data"[[:space:]]*:[[:space:]]*"([0-9]+) upgraded, ([0-9]+) newly installed, ([0-9]+) to remove.*/\3/p' | head -n1)" if [[ -z "$upgraded" || -z "$installed" || -z "$removed" ]]; then printf '%s\n' "$simulation" log "VM $id ($name): Simulation konnte nicht ausgewertet werden." VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then qm shutdown "$id" --timeout 60 || qm stop "$id" fi return fi if (( upgraded == 0 && installed == 0 && removed == 0 )); then log "VM $id ($name): bereits aktuell." VM_CURRENT=$((VM_CURRENT + 1)) else log "VM $id ($name): $upgraded Upgrade(s), $installed Neuinstallation(en), $removed Entfernung(en)." if ! vm_exec "$id" \ 'DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" full-upgrade' then log "VM $id ($name): full-upgrade FEHLER." VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) if [[ "$started_by_us" == "yes" ]]; then qm shutdown "$id" --timeout 60 || qm stop "$id" fi return fi if vm_exec "$id" 'test -e /var/run/reboot-required' >/dev/null 2>&1; then reboot_required="yes" log "VM $id ($name): Update erfolgreich - Neustart erforderlich." else log "VM $id ($name): Update erfolgreich." fi VM_OK=$((VM_OK + 1)) fi if [[ "$started_by_us" == "yes" ]]; then log "VM $id ($name): war vorher gestoppt -> wird wieder heruntergefahren." if ! qm shutdown "$id" --timeout 60; then log "VM $id ($name): sauberer Shutdown fehlgeschlagen -> Stop." if ! qm stop "$id"; then log "FEHLER: VM $id ($name) konnte nicht gestoppt werden." VM_OK=$((VM_OK - 1)) VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) fi fi return fi if [[ "$reboot_required" == "yes" ]]; then VM_REBOOT_PENDING+=("$id") log "VM $id ($name): Neustart vorgemerkt." fi } reboot_pending_guests() { local id local name if (( ${#LXC_REBOOT_PENDING[@]} == 0 && ${#VM_REBOOT_PENDING[@]} == 0 )); then return fi log "" log "============================================================" log " Erforderliche Gast-Neustarts" log "============================================================" for id in "${LXC_REBOOT_PENDING[@]}"; do name="$(lxc_name "$id")" name="${name:-unbekannt}" if [[ "$(pct status "$id" 2>/dev/null | awk '{print $2}')" != "running" ]]; then log "CT $id ($name): nicht mehr gestartet -> kein Neustart nötig." continue fi log "CT $id ($name): automatischer Neustart ..." if pct reboot "$id" --timeout 60; then log "CT $id ($name): Neustart erfolgreich ausgelöst." else log "FEHLER: CT $id ($name) konnte nicht neu gestartet werden." LXC_OK=$((LXC_OK - 1)) LXC_FAILED=$((LXC_FAILED + 1)) ERRORS=$((ERRORS + 1)) fi done for id in "${VM_REBOOT_PENDING[@]}"; do name="$(vm_name "$id")" name="${name:-unbekannt}" if [[ "$(qm status "$id" 2>/dev/null | awk '{print $2}')" != "running" ]]; then log "VM $id ($name): nicht mehr gestartet -> kein Neustart nötig." continue fi log "VM $id ($name): automatischer Neustart ..." if qm reboot "$id" >/dev/null 2>&1; then log "VM $id ($name): Neustart erfolgreich ausgelöst." else log "FEHLER: VM $id ($name) konnte nicht neu gestartet werden." VM_OK=$((VM_OK - 1)) VM_FAILED=$((VM_FAILED + 1)) ERRORS=$((ERRORS + 1)) fi done } list_lxc() { pct list 2>/dev/null | awk 'NR>1 && $1 ~ /^[0-9]+$/ {print $1}' } list_vms() { qm list 2>/dev/null | awk 'NR>1 && $1 ~ /^[0-9]+$/ {print $1}' } log "" log "============================================================" log " Proxmox Guest Updater" log "============================================================" log "Start: $(date '+%d.%m.%Y %H:%M:%S')" log "Host: $(hostname)" log "Proxmox: $(pveversion)" log "Modus: $MODE" log "============================================================" if [[ -n "$ONLY_CT" ]]; then if ! pct config "$ONLY_CT" >/dev/null 2>&1; then log "FEHLER: CT $ONLY_CT existiert nicht." exit 2 fi if [[ "$MODE" == "check" ]]; then check_lxc "$ONLY_CT" else update_lxc "$ONLY_CT" fi elif [[ -n "$ONLY_VM" ]]; then if ! qm config "$ONLY_VM" >/dev/null 2>&1; then log "FEHLER: VM $ONLY_VM existiert nicht." exit 2 fi if [[ "$MODE" == "check" ]]; then check_vm "$ONLY_VM" else update_vm "$ONLY_VM" fi else if [[ "$UPDATE_HOST" == "yes" ]]; then if [[ "$MODE" == "check" ]]; then check_host else update_host fi else log "Proxmox-Host laut Konfiguration übersprungen." fi if [[ "$UPDATE_LXC" == "yes" ]]; then while read -r id; do [[ -n "$id" ]] || continue if [[ "$MODE" == "check" ]]; then check_lxc "$id" else update_lxc "$id" fi done < <(list_lxc) else log "LXC-Updates laut Konfiguration deaktiviert." fi if [[ "$UPDATE_VMS" == "yes" ]]; then while read -r id; do [[ -n "$id" ]] || continue if [[ "$MODE" == "check" ]]; then check_vm "$id" else update_vm "$id" fi done < <(list_vms) else log "VM-Updates laut Konfiguration deaktiviert." fi fi log "" log "============================================================" log " Ergebnis" log "============================================================" if [[ "$MODE" == "check" ]]; then log "LXC geprüft: $LXC_CHECKED" log "LXC fehlgeschlagen: $LXC_FAILED" log "LXC übersprungen: $LXC_SKIPPED" log "" log "VM geprüft: $VM_CHECKED" log "VM fehlgeschlagen: $VM_FAILED" log "VM übersprungen: $VM_SKIPPED" else log "LXC aktualisiert: $LXC_OK" log "LXC bereits aktuell: $LXC_CURRENT" log "LXC fehlgeschlagen: $LXC_FAILED" log "LXC übersprungen: $LXC_SKIPPED" log "" log "VM aktualisiert: $VM_OK" log "VM bereits aktuell: $VM_CURRENT" log "VM fehlgeschlagen: $VM_FAILED" log "VM übersprungen: $VM_SKIPPED" fi log "" log "Fehler gesamt: $ERRORS" log "Host Update: $HOST_OK" log "Host Reboot nötig: $HOST_REBOOT_REQUIRED" log "============================================================" if [[ "$MODE" == "update" ]]; then if (( ERRORS > 0 )); then MAIL_STATUS_DETAIL="Keine automatischen Neustarts ausgeführt, weil $ERRORS Fehler im Update-Lauf aufgetreten sind." if [[ -z "$ONLY_CT" && -z "$ONLY_VM" && "$HOST_REBOOT_REQUIRED" == "yes" ]]; then log "" log "Host-Neustart erforderlich, aber automatischer Neustart wird NICHT ausgeführt." log "Grund: $ERRORS Fehler im Update-Lauf." fi if (( ${#LXC_REBOOT_PENDING[@]} > 0 || ${#VM_REBOOT_PENDING[@]} > 0 )); then log "" log "Gast-Neustart erforderlich, aber automatische Gast-Neustarts werden NICHT ausgeführt." log "Grund: $ERRORS Fehler im Update-Lauf." fi elif [[ -n "$ONLY_CT" || -n "$ONLY_VM" ]]; then # Gezielter Gast-Lauf: Der Proxmox-Host wurde nicht aktualisiert. # Deshalb notwendige Gast-Neustarts direkt am Ende ausführen. local_pending=$(( ${#LXC_REBOOT_PENDING[@]} + ${#VM_REBOOT_PENDING[@]} )) reboot_pending_guests if (( local_pending > 0 )); then MAIL_STATUS_DETAIL="$local_pending notwendige(r) Gast-Neustart(s) wurde(n) verarbeitet." else MAIL_STATUS_DETAIL="Kein Neustart erforderlich." fi elif [[ "$HOST_OK" == "yes" \ && "$HOST_REBOOT_REQUIRED" == "yes" \ && "$AUTO_REBOOT_HOST" == "yes" \ && "$NO_REBOOT" != "yes" ]] then log "" log "Host-Neustart erforderlich." log "Keine einzelnen CT/VM-Neustarts: der Host-Neustart übernimmt diese." log "Gesamter Update-Lauf fehlerfrei." log "Automatischer Neustart in 1 Minute." log "Proxmox fährt die laufenden Gäste geordnet herunter." HOST_REBOOT_SCHEDULED="yes" MAIL_STATUS_DETAIL="Host-Neustart in 1 Minute geplant; notwendige Gast-Neustarts werden dadurch mit erledigt." sync shutdown -r +1 \ "Proxmox Guest Updater: Neustart nach erfolgreichem Systemupdate" else # Kein automatischer Host-Neustart: notwendige Gast-Neustarts jetzt # einzeln ausführen. Das gilt auch bei --no-reboot oder wenn der # automatische Host-Neustart in der Konfiguration deaktiviert ist. local_pending=$(( ${#LXC_REBOOT_PENDING[@]} + ${#VM_REBOOT_PENDING[@]} )) reboot_pending_guests if (( local_pending > 0 )); then MAIL_STATUS_DETAIL="$local_pending notwendige(r) Gast-Neustart(s) wurde(n) verarbeitet." elif [[ "$HOST_REBOOT_REQUIRED" == "yes" ]]; then MAIL_STATUS_DETAIL="Host-Neustart erforderlich, aber automatischer Host-Reboot ist deaktiviert/unterdrückt." else MAIL_STATUS_DETAIL="Kein Neustart erforderlich." fi fi # Erst nach allen Update- und Reboot-Entscheidungen versenden, damit # der Bericht den tatsächlichen Endzustand des Laufs enthält. send_update_notification fi if [[ "$MODE" == "check" ]]; then send_update_notification fi if (( ERRORS > 0 )); then exit 1 fi exit 0