diff --git a/01-system-einrichten/prepare-dietpi-sd.sh b/01-system-einrichten/prepare-dietpi-sd.sh new file mode 100644 index 0000000..7037bd3 --- /dev/null +++ b/01-system-einrichten/prepare-dietpi-sd.sh @@ -0,0 +1,196 @@ +#!/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) + else: + text += "\n" + line + "\n" + + path.write_text(text) + +def shell_quote(value): + return "'" + value.replace("'", "'\\''") + "'" + +# dietpi.txt +config = boot / "dietpi.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_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", + "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) + +PY + +# WLAN-Zugangsdaten schützen +chmod 600 "$BOOT/dietpi-wifi.txt" + +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