gioser

sergio/takana

> Una distribución Linux construida con un laboratorio funcional y hermético en el > sótano, y una terminal mutable, clásica y anárquica en el piso de arriba — diseñada > desde el suelo para que una IA programadora entienda, modifique y co…

TOML 1572Shell 203Rust 109Markdown 993728 Definitions

⎇ 02513b861f6fbb462045ba411c6bec0a2cae7e93 scripts/takana-install.sh Shell · 102 lines · 6.0 KB History raw

renombre: los instaladores pasan a `takana-*` — y el peligro no era el fichero, era el PROTOCOLO 008dd3925 · Sergio ·

1#!/usr/bin/env bash
2# takana-install.sh — Etapa E2: instalador a disco. Vuelca el `product-rootfs` (lean: 4/4 + userland
3# Rust + sshd) sobre un DISCO destino (block device real `/dev/sdX`, o un fichero pre-dimensionado para
4# pruebas) con el mismo layout GPT + GRUB BIOS de la imagen E1, pero IN-PLACE sobre el target. Tras
5# instalar, el disco arranca solo (SeaBIOS → GRUB → kernel → arje-zero PID1 → hammerd+getty+sshd).
6#
7# Es la contraparte "a disco real" de scripts/product-image.sh (que produce un fichero imagen portátil):
8# la lógica de particionado/format/GRUB es la misma (delegada a install-image.sh); sólo cambia el target.
9#
10# Uso:
11#   sudo PRODUCT=<hash> AUTHKEYS=~/.ssh/id_ed25519.pub FORCE=1 ./scripts/takana-install.sh /dev/sdX
12#   # prueba sin hardware (fichero pre-dimensionado, se valida el camino in-place + boot SSH):
13#   PRODUCT=<hash> TESTKEY=1 BOOT=1 ./scripts/takana-install.sh work/target.img
14#
15# Variables:
16#   PRODUCT   product-rootfs sellado (hash/prefijo; default: el más reciente del store)
17#   AUTHKEYS  fichero authorized_keys a instalar para root (recomendado en instalación real)
18#   TESTKEY   1 ⇒ genera un par efímero y usa su pubkey como authorized_keys (para validar por SSH)
19#   FORCE     1 ⇒ requerido para escribir sobre un BLOCK DEVICE (seguridad anti-pisada)
20#   KERNEL    bzImage a embeber (default store/*-linux/boot/bzImage)
21#   ROOT_SIZE/STORE_SIZE/STATE_SIZE   MiB de cada partición (default 1024/512/512)
22#   BOOT      1 ⇒ tras instalar, arranca el target con QEMU (útil sólo para fichero/imagen) + handshake
23#   KVM/MEM/PORT/DEADLINE   passthrough del arranque de prueba
24set -euo pipefail
25ROOT="$(cd "$(dirname "$0")/.." && pwd)"; cd "$ROOT"
26TARGET="${1:-}"
27[ -n "$TARGET" ] || { echo "uso: $0 <device|imagen>   (p.ej. /dev/sdX o work/target.img)"; exit 2; }
28WORK="$ROOT/work/takana-install"; RFS="$WORK/rootfs"
29
30# --- resolver product-rootfs sellado ---
31if [ -n "${PRODUCT:-}" ]; then
32  PDIR=$(ls -d store/"${PRODUCT#b3:}"*-product-rootfs 2>/dev/null | head -1)
33else
34  PDIR=$(ls -dt store/*-product-rootfs 2>/dev/null | head -1)
35fi
36[ -n "$PDIR" ] && [ -d "$PDIR" ] || { echo "no encuentro product-rootfs (corré 'hammer bootstrap product')"; exit 1; }
37echo "==> product-rootfs: $PDIR"
38echo "==> target:         $TARGET"
39
40# --- guardas de seguridad para un disco real ---
41PREALLOC=0
42if [ -b "$TARGET" ]; then
43  [ "${FORCE:-0}" = 1 ] || { echo "!! $TARGET es un BLOCK DEVICE — exijo FORCE=1 para escribirlo (borra datos)"; exit 1; }
44  if mount | grep -q "^$TARGET"; then echo "!! $TARGET (o una partición) está MONTADO — desmontá antes"; exit 1; fi
45  echo "==> escritura sobre block device REAL (FORCE=1)"
46elif [ -e "$TARGET" ]; then
47  PREALLOC=1   # fichero existente ⇒ in-place (no truncar): exactamente el camino de un device
48  echo "==> target fichero pre-dimensionado ⇒ instalación in-place (PREALLOC)"
49else
50  echo "==> target fichero nuevo ⇒ install-image lo crea (truncate)"
51fi
52
53# --- copia escribible del product-rootfs + provisión de authorized_keys ---
54rm -rf "$WORK"; mkdir -p "$WORK"
55cp -a "$PDIR"/. "$RFS"/; chmod -R u+w "$RFS"; rm -rf "$RFS/.hammer"
56install -d -m 0700 "$RFS/root/.ssh"
57if [ "${TESTKEY:-0}" = 1 ]; then
58  "$(ls store/*-openssh/usr/bin/ssh-keygen | head -1)" -t ed25519 -N '' -f "$WORK/clientkey" -q
59  cat "$WORK/clientkey.pub" > "$RFS/root/.ssh/authorized_keys"
60  echo "==> authorized_keys: llave de prueba efímera ($WORK/clientkey)"
61elif [ -n "${AUTHKEYS:-}" ] && [ -f "$AUTHKEYS" ]; then
62  cat "$AUTHKEYS" > "$RFS/root/.ssh/authorized_keys"
63  echo "==> authorized_keys: $AUTHKEYS"
64else
65  echo "==> (sin authorized_keys: nadie podrá entrar por SSH hasta provisionarlas; usá AUTHKEYS= o TESTKEY=1)"
66fi
67chmod 0600 "$RFS/root/.ssh/authorized_keys" 2>/dev/null || true
68
69# --- delegar particionado/format/GRUB a install-image.sh, apuntando al target (IN-PLACE si aplica) ---
70echo "==> instalando sobre $TARGET (GPT + ext4 + GRUB BIOS, in-place=$PREALLOC)"
71ROOTFS="$RFS" IMG="$TARGET" PREALLOC="$PREALLOC" BOOT=0 \
72  ROOT_SIZE="${ROOT_SIZE:-1024}" STORE_SIZE="${STORE_SIZE:-512}" STATE_SIZE="${STATE_SIZE:-512}" \
73  sh ./scripts/install-image.sh
74echo "==> instalado. El disco arranca solo (SeaBIOS → GRUB → arje-zero)."
75
76[ "${BOOT:-0}" = 1 ] || { echo "    (set BOOT=1 para arrancar el target y probar SSH)"; exit 0; }
77[ "${TESTKEY:-0}" = 1 ] || { echo "    BOOT=1 sin TESTKEY=1: no tengo llave para el handshake; omito."; exit 0; }
78
79# --- arranque de prueba del target (sólo fichero/imagen) + handshake SSH ---
80KVM="${KVM:-1}"; MEM="${MEM:-2048}"; PORT="${PORT:-2225}"; DEADLINE="${DEADLINE:-150}"
81accel=(-cpu Broadwell); { [ "$KVM" = 1 ] && [ -w /dev/kvm ]; } && accel=(-enable-kvm -cpu host)
82echo "==> AUTO-BOOT del target (sin -kernel) + hostfwd :$PORT->:22"
83qemu-system-x86_64 -m "$MEM" -no-reboot -nographic "${accel[@]}" \
84  -drive file="$TARGET",format=raw,if=virtio \
85  -netdev "user,id=n0,hostfwd=tcp::$PORT-:22" -device e1000,netdev=n0 > "$WORK/console.log" 2>&1 &
86QPID=$!; trap 'kill $QPID 2>/dev/null || true' EXIT
87SSHOPTS=(-p "$PORT" -i "$WORK/clientkey" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null
88         -o GlobalKnownHostsFile=/dev/null -o PasswordAuthentication=no -o ConnectTimeout=4 -o LogLevel=ERROR)
89echo "==> esperando handshake desde el disco instalado (deadline ${DEADLINE}s)…"
90start=$(date +%s); ok=0
91while [ $(( $(date +%s) - start )) -lt "$DEADLINE" ]; do
92  kill -0 $QPID 2>/dev/null || { echo "!! QEMU murió"; break; }
93  if out=$(ssh "${SSHOPTS[@]}" root@localhost \
94        'echo INSTALLED_DISK_SSH_OK uid=$(id -u); uname -sr; ls --version 2>&1|head -1; df -h /store /var/lib/hammer 2>/dev/null|tail -2' 2>/dev/null); then
95    echo "========= RESPUESTA DEL DISCO INSTALADO ========="; echo "$out"; echo "================================================="
96    echo "$out" | grep -q INSTALLED_DISK_SSH_OK && ok=1; break
97  fi
98  sleep 3
99done
100echo "---- consola (cola) ----"; tail -12 "$WORK/console.log" 2>/dev/null
101[ "$ok" = 1 ] && { echo; echo "*** HAMMER INSTALL OK — disco instalado in-place arranca solo y sirve SSH ***"; exit 0; }
102echo; echo "*** HAMMER INSTALL FALLÓ — ver $WORK/console.log ***"; exit 1