From 92488dbe824217fb246828cf928e8b8704b1e858 Mon Sep 17 00:00:00 2001 From: bernd Date: Sat, 31 Oct 2026 13:41:44 +0100 Subject: [PATCH] 01-system-einrichten/prepare-dietpi-sd.sh aktualisiert --- 01-system-einrichten/prepare-dietpi-sd.sh | 204 ++++------------------ 1 file changed, 37 insertions(+), 167 deletions(-) diff --git a/01-system-einrichten/prepare-dietpi-sd.sh b/01-system-einrichten/prepare-dietpi-sd.sh index 7037bd3..3789bad 100644 --- a/01-system-einrichten/prepare-dietpi-sd.sh +++ b/01-system-einrichten/prepare-dietpi-sd.sh @@ -1,196 +1,66 @@ -#!/usr/bin/env bash -set -Eeuo pipefail -echo -echo "==========================================" -echo " DietPi SD-Karten-Vorbereitung" -echo " Raspberry Pi 3B" -echo "==========================================" -echo - -# Boot-Partition ermitteln oder abfragen -BOOT="${1:-}" - -if [[ -z "$BOOT" ]]; then - echo "Gesucht werden automatisch eingehängte DietPi-Partitionen ..." - - candidates=() - - for dir in /media/"$USER"/* /run/media/"$USER"/*; do - [[ -d "$dir" ]] || continue - [[ -f "$dir/dietpi.txt" && -f "$dir/dietpi-wifi.txt" ]] || continue - candidates+=("$dir") - done - - if [[ ${#candidates[@]} -eq 1 ]]; then - BOOT="${candidates[0]}" - elif [[ ${#candidates[@]} -gt 1 ]]; then - echo "Mehrere mögliche Partitionen gefunden:" - for dir in "${candidates[@]}"; do - echo " $dir" - done - read -rp "Bitte Boot-Partition eingeben: " BOOT - else - read -rp "Pfad zur eingehängten Boot-Partition: " BOOT - fi -fi - -BOOT="$(realpath "$BOOT")" - -if [[ ! -f "$BOOT/dietpi.txt" || ! -f "$BOOT/dietpi-wifi.txt" ]]; then - echo "FEHLER: Keine gültige DietPi-Boot-Partition gefunden." - exit 1 -fi - -echo -echo "Verwendete Partition: $BOOT" -echo - -# Eingaben -read -rp "Hostname (z.B. raspi-kind01): " HOSTNAME - -if [[ ! "$HOSTNAME" =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{0,62}$ ]]; then - echo "Ungültiger Hostname." - exit 1 -fi - -read -rp "WLAN-SSID: " WIFI_SSID - -if [[ -z "$WIFI_SSID" ]]; then - echo "Die SSID darf nicht leer sein." - exit 1 -fi - -read -rsp "WLAN-Passwort: " WIFI_KEY -echo - -if [[ ${#WIFI_KEY} -lt 8 ]]; then - echo "Das WLAN-Passwort ist zu kurz." - exit 1 -fi - -echo -echo "Initiales Passwort für root UND dietpi:" -read -rsp "Passwort: " SYSTEM_PASSWORD -echo -read -rsp "Passwort wiederholen: " SYSTEM_PASSWORD2 -echo - -if [[ "$SYSTEM_PASSWORD" != "$SYSTEM_PASSWORD2" ]]; then - echo "Passwörter stimmen nicht überein." - exit 1 -fi - -if [[ ${#SYSTEM_PASSWORD} -lt 12 ]]; then - echo "Bitte ein Passwort mit mindestens 12 Zeichen verwenden." - exit 1 -fi - -if [[ "$SYSTEM_PASSWORD" == *'$'* || "$SYSTEM_PASSWORD" == *'"'* ]]; then - echo 'DietPi unterstützt diese Zeichen im Erstinstallationspasswort nicht: $ und "' - exit 1 -fi - -echo -echo "Konfiguration wird geschrieben ..." - -# Sicherung der Originaldateien -cp -a "$BOOT/dietpi.txt" "$BOOT/dietpi.txt.original" -cp -a "$BOOT/dietpi-wifi.txt" "$BOOT/dietpi-wifi.txt.original" - -export HOSTNAME WIFI_SSID WIFI_KEY SYSTEM_PASSWORD - -python3 - "$BOOT" <<'PY' -import os + + import re import sys from pathlib import Path boot = Path(sys.argv[1]) -def set_value(path, key, value): - text = path.read_text() - - pattern = re.compile( - r"^#?\s*" + re.escape(key) + r"=.*$", - re.MULTILINE - ) - - line = f"{key}={value}" - - if pattern.search(text): - text = pattern.sub(line, text, count=1) +def set_assignment(path: Path, key: str, value: str) -> None: + """Replace the first active or commented assignment, or append it.""" + lines = path.read_text(encoding="utf-8").splitlines() + pattern = re.compile(r"^\s*#?\s*" + re.escape(key) + r"\s*=") + replacement = f"{key}={value}" + for i, line in enumerate(lines): + if pattern.match(line): + lines[i] = replacement + break else: - text += "\n" + line + "\n" + lines.append(replacement) + path.write_text("\n".join(lines) + "\n", encoding="utf-8") - path.write_text(text) - -def shell_quote(value): +def bash_single_quote(value: str) -> str: + # Safe representation for a value in a shell single-quoted assignment. return "'" + value.replace("'", "'\\''") + "'" -# dietpi.txt -config = boot / "dietpi.txt" +dietpi = boot / "dietpi.txt" +wifi = boot / "dietpi-wifi.txt" settings = { "AUTO_SETUP_ACCEPT_LICENSE": "1", - "AUTO_SETUP_AUTOMATED": "1", - "AUTO_SETUP_LOCALE": "de_DE.UTF-8", "AUTO_SETUP_KEYBOARD_LAYOUT": "de", "AUTO_SETUP_TIMEZONE": "Europe/Berlin", - - "AUTO_SETUP_NET_HOSTNAME": os.environ["HOSTNAME"], "AUTO_SETUP_NET_ETHERNET_ENABLED": "0", "AUTO_SETUP_NET_WIFI_ENABLED": "1", "AUTO_SETUP_NET_WIFI_COUNTRY_CODE": "DE", - + "AUTO_SETUP_NET_HOSTNAME": os.environ["HOSTNAME"], + "AUTO_SETUP_AUTOMATED": "1", "AUTO_SETUP_GLOBAL_PASSWORD": os.environ["SYSTEM_PASSWORD"], - - "AUTO_SETUP_APT_INSTALLS": - "git curl ca-certificates python3 python3-venv " - "python3-pip python3-gpiozero python3-lgpio", - - "CONFIG_WIFI_COUNTRY_CODE": "DE", + "AUTO_SETUP_APT_INSTALLS": ( + "git curl ca-certificates python3-venv python3-pip " + "python3-gpiozero python3-lgpio" + ), + # Wait for network before continuing with first-run setup. "CONFIG_BOOT_WAIT_FOR_NETWORK": "2", } for key, value in settings.items(): - set_value(config, key, value) - -# dietpi-wifi.txt -wifi = boot / "dietpi-wifi.txt" - -wifi_settings = { - "aWIFI_SSID[0]": shell_quote(os.environ["WIFI_SSID"]), - "aWIFI_KEY[0]": shell_quote(os.environ["WIFI_KEY"]), -} - -for key, value in wifi_settings.items(): - set_value(wifi, key, value) + set_assignment(dietpi, key, value) +set_assignment(wifi, "aWIFI_SSID[0]", bash_single_quote(os.environ["WIFI_SSID"])) +set_assignment(wifi, "aWIFI_KEY[0]", bash_single_quote(os.environ["WIFI_KEY"])) PY -# WLAN-Zugangsdaten schützen -chmod 600 "$BOOT/dietpi-wifi.txt" +# FAT filesystems do not provide normal Unix permissions, but set them where supported. +chmod 600 "$BOOT/dietpi-wifi.txt" 2>/dev/null || true + +printf '\nVorbereitung abgeschlossen.\n' +printf 'Hostname: %s\n' "$HOSTNAME" +printf 'Locale: de_DE.UTF-8\nTastatur: de\nZeitzone: Europe/Berlin\nWLAN-Ländercode: DE\n' +printf 'Automatische Erstinstallation: aktiviert\n' +printf '\nInitiales Passwort gilt zunächst für root und dietpi.\n' +printf 'Die WLAN-Zugangsdaten und das Systempasswort liegen auf der SD-Karte.\n' +printf 'Karte sicher auswerfen und erst danach in den Raspberry Pi einsetzen.\n' -echo -echo "==========================================" -echo " Vorbereitung abgeschlossen!" -echo "==========================================" -echo -echo "Hostname: $HOSTNAME" -echo "Sprache: Deutsch" -echo "Tastatur: Deutsch" -echo "Zeitzone: Europe/Berlin" -echo "WLAN-Ländercode: DE" -echo "Automatische Erstinstallation: aktiviert" -echo -echo "Installierte Pakete beim ersten Start:" -echo " Git, Python, gpiozero, lgpio, curl" -echo -echo "Bitte SD-Karte sicher auswerfen." -echo "Danach in den Raspberry Pi einsetzen." -echo -echo "HINWEIS: root und dietpi haben zunächst" -echo "dasselbe Passwort." -echo \ No newline at end of file