# ---- Beginn (copy & paste) ----
# 1) Variablen anpassen (ggf. /dev/sda ersetzen)
TARGET_DISK=/dev/sda
TARGET_PART=${TARGET_DISK}1 # Root-Partition auf sda1 vorausgesetzt
MNT=/mnt/newroot
DEBIAN_RELEASE=bookworm
MIRROR="http://deb.debian.org/debian"
# 2) Vorwarnung (manuell bestätigen)
echo "ACHTUNG: Installation überschreibt ${TARGET_DISK} (Root=${TARGET_PART}). Fortfahren? Tippe 'ja' um weiterzumachen."
read ANTWORT
[ "$ANTWORT" != "ja" ] && echo "Abbruch." && exit 1
# 3) Ziel vorbereiten (wenn Partition schon existiert, nur mounten)
mkdir -p ${MNT}
mount ${TARGET_PART} ${MNT} || { echo "Fehler: kann ${TARGET_PART} nicht mounten"; exit 1; }
# 4) Minimal ins Ziel kopieren (falls leer) bzw. debootstrap Fresh install
apt update
apt install -y debootstrap gdisk rsync
# optional: leere Zielpartition vorbenutzen (ACHTUNG: löscht Daten auf PARTITION)
# um die Partition neu zu formatieren, entferne Kommentar:
# mkfs.ext4 -F ${TARGET_PART}
debootstrap --variant=minbase ${DEBIAN_RELEASE} ${MNT} ${MIRROR}
# 5) Wichtige Pseudo-Dateisysteme mounten
mount --bind /dev ${MNT}/dev
mount --bind /dev/pts ${MNT}/dev/pts
mount -t proc /proc ${MNT}/proc
mount --bind /sys ${MNT}/sys
cp /etc/resolv.conf ${MNT}/etc/resolv.conf
# 6) Chroot und Basispakete installieren
chroot ${MNT} /bin/bash -c "
set -e
export DEBIAN_FRONTEND=noninteractive
echo '${HOSTNAME:-debian-host}' > /etc/hostname
# sources
cat > /etc/apt/sources.list <<'EOF'
deb ${MIRROR} ${DEBIAN_RELEASE} main contrib non-free
deb ${MIRROR} ${DEBIAN_RELEASE}-updates main contrib non-free
deb http://security.debian.org/ ${DEBIAN_RELEASE}-security main contrib non-free
EOF
apt update
apt install -y locales systemd-sysv linux-image-amd64 grub-pc openssh-server sudo net-tools iproute2 ifupdown
dpkg-reconfigure locales || true
# set root password interactively? set temporary password
echo root:toor | chpasswd
"
# 7) fstab erstellen (UUID ermitteln)
ROOT_UUID=$(blkid -s UUID -o value ${TARGET_PART})
cat > ${MNT}/etc/fstab <<EOF
UUID=${ROOT_UUID} / ext4 errors=remount-ro 0 1
EOF
# 8) GRUB installieren (Legacy/MBR). Passe /dev/sda an, falls nötig.
chroot ${MNT} /bin/bash -c "
grub-install --target=i386-pc ${TARGET_DISK}
update-grub
"
# 9) Aufräumen, unmount
umount ${MNT}/dev/pts || true
umount ${MNT}/dev || true
umount ${MNT}/proc || true
umount ${MNT}/sys || true
sync
echo "Installation fertig. Bitte kontrolliere /boot und starte neu, wenn alles passt."
echo "Reboot jetzt? Tippe 'nein' zum Abwarten. ('ja' zum reboot)"
read RB
if [ \"\$RB\" = \"ja\" ]; then
reboot
fi
# ---- Ende ----