01-system-einrichten/prepare-dietpi-sd.sh aktualisiert

This commit is contained in:
bernd committed 2026-10-31 13:41:44 +01:00
1 parent 2b9b915884
commit 92488dbe82
1 file changed
+35 -165
+35 -165
View File
@@ -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 re
import sys import sys
from pathlib import Path from pathlib import Path
boot = Path(sys.argv[1]) boot = Path(sys.argv[1])
def set_value(path, key, value): def set_assignment(path: Path, key: str, value: str) -> None:
text = path.read_text() """Replace the first active or commented assignment, or append it."""
lines = path.read_text(encoding="utf-8").splitlines()
pattern = re.compile( pattern = re.compile(r"^\s*#?\s*" + re.escape(key) + r"\s*=")
r"^#?\s*" + re.escape(key) + r"=.*$", replacement = f"{key}={value}"
re.MULTILINE for i, line in enumerate(lines):
) if pattern.match(line):
lines[i] = replacement
line = f"{key}={value}" break
if pattern.search(text):
text = pattern.sub(line, text, count=1)
else: else:
text += "\n" + line + "\n" lines.append(replacement)
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
path.write_text(text) def bash_single_quote(value: str) -> str:
# Safe representation for a value in a shell single-quoted assignment.
def shell_quote(value):
return "'" + value.replace("'", "'\\''") + "'" return "'" + value.replace("'", "'\\''") + "'"
# dietpi.txt dietpi = boot / "dietpi.txt"
config = boot / "dietpi.txt" wifi = boot / "dietpi-wifi.txt"
settings = { settings = {
"AUTO_SETUP_ACCEPT_LICENSE": "1", "AUTO_SETUP_ACCEPT_LICENSE": "1",
"AUTO_SETUP_AUTOMATED": "1",
"AUTO_SETUP_LOCALE": "de_DE.UTF-8", "AUTO_SETUP_LOCALE": "de_DE.UTF-8",
"AUTO_SETUP_KEYBOARD_LAYOUT": "de", "AUTO_SETUP_KEYBOARD_LAYOUT": "de",
"AUTO_SETUP_TIMEZONE": "Europe/Berlin", "AUTO_SETUP_TIMEZONE": "Europe/Berlin",
"AUTO_SETUP_NET_HOSTNAME": os.environ["HOSTNAME"],
"AUTO_SETUP_NET_ETHERNET_ENABLED": "0", "AUTO_SETUP_NET_ETHERNET_ENABLED": "0",
"AUTO_SETUP_NET_WIFI_ENABLED": "1", "AUTO_SETUP_NET_WIFI_ENABLED": "1",
"AUTO_SETUP_NET_WIFI_COUNTRY_CODE": "DE", "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_GLOBAL_PASSWORD": os.environ["SYSTEM_PASSWORD"],
"AUTO_SETUP_APT_INSTALLS": (
"AUTO_SETUP_APT_INSTALLS": "git curl ca-certificates python3-venv python3-pip "
"git curl ca-certificates python3 python3-venv " "python3-gpiozero python3-lgpio"
"python3-pip python3-gpiozero python3-lgpio", ),
# Wait for network before continuing with first-run setup.
"CONFIG_WIFI_COUNTRY_CODE": "DE",
"CONFIG_BOOT_WAIT_FOR_NETWORK": "2", "CONFIG_BOOT_WAIT_FOR_NETWORK": "2",
} }
for key, value in settings.items(): for key, value in settings.items():
set_value(config, key, value) set_assignment(dietpi, 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(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 PY
# WLAN-Zugangsdaten schützen # FAT filesystems do not provide normal Unix permissions, but set them where supported.
chmod 600 "$BOOT/dietpi-wifi.txt" 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