Files
raspi-labor/01-system-einrichten/prepare-dietpi-sd.sh
T

67 lines
2.3 KiB
Bash

import re
import sys
from pathlib import Path
boot = Path(sys.argv[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:
lines.append(replacement)
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
def bash_single_quote(value: str) -> str:
# Safe representation for a value in a shell single-quoted assignment.
return "'" + value.replace("'", "'\\''") + "'"
dietpi = boot / "dietpi.txt"
wifi = boot / "dietpi-wifi.txt"
settings = {
"AUTO_SETUP_ACCEPT_LICENSE": "1",
"AUTO_SETUP_LOCALE": "de_DE.UTF-8",
"AUTO_SETUP_KEYBOARD_LAYOUT": "de",
"AUTO_SETUP_TIMEZONE": "Europe/Berlin",
"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-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_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
# 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'