#!/usr/bin/env bash # Systemzeitzone setzen (wird von setup-lxc.sh gesourced). Setzt common.sh voraus. ZONEINFO_DIR=/usr/share/zoneinfo TZ_RE='^[A-Za-z0-9_+-]+(/[A-Za-z0-9_+-]+)*$' LOCALTIME_FILE=/etc/localtime TIMEZONE_FILE=/etc/timezone validate_timezone_name() { [[ $1 =~ $TZ_RE ]] || die "Ungültiger Zeitzonenname: '$1' (Beispiel: Europe/Berlin)" } current_timezone() { local tz="" target="" if command -v timedatectl >/dev/null 2>&1; then tz=$(timedatectl show -p Timezone --value 2>/dev/null) || tz="" fi if [[ -z $tz || $tz == n/a ]]; then target=$(readlink -f "$LOCALTIME_FILE" 2>/dev/null) || target="" tz=${target#"$ZONEINFO_DIR"/} [[ $tz != "$target" ]] || tz="" fi printf '%s' "$tz" } set_timezone_files() { ln -sfn "$ZONEINFO_DIR/$1" "$LOCALTIME_FILE" && printf '%s\n' "$1" > "$TIMEZONE_FILE" } # In Containern fehlt oft ein nutzbares timedatectl (kein D-Bus); dann Dateien direkt schreiben, sonst nur warnen. ensure_timezone() { local tz=$1 [[ -f $ZONEINFO_DIR/$tz ]] || die "Zeitzone '$tz' fehlt in $ZONEINFO_DIR (Paket tzdata?)." if [[ $(current_timezone) == "$tz" ]]; then log "Zeitzone ist bereits $tz." return 0 fi if command -v timedatectl >/dev/null 2>&1 && timedatectl set-timezone "$tz" >/dev/null 2>&1; then log "Zeitzone auf $tz gesetzt." elif set_timezone_files "$tz" 2>/dev/null; then log "Zeitzone auf $tz gesetzt (über $LOCALTIME_FILE)." else warn "Zeitzone konnte nicht auf $tz gesetzt werden (Container ohne Schreibrecht?). Zeitzone manuell einstellen (Host- bzw. Container-Konfiguration)." fi return 0 }