waouw-activate.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/bin/bash
  2. # /boot/waouw-activate.sh — Boot 1 (via cloud-init runcmd ou systemd.run)
  3. # But : créer l’utilisateur 'waouw' (bypass wizard), activer le service firstboot, puis reboot.
  4. set -eu
  5. BOOTDIR="/boot"
  6. [ -d "/boot/firmware" ] && BOOTDIR="/boot/firmware"
  7. LOGF="$BOOTDIR/waouw-activate.log"
  8. [ -f "$BOOTDIR/waouw-deploy-log.sh" ] && _WAOUW_SCRIPT="waouw-activate" . "$BOOTDIR/waouw-deploy-log.sh"
  9. _log() { waouw_deploy_log "INFO" "$@" 2>/dev/null || echo "[INFO] $*" | tee -a "$LOGF"; }
  10. _step() { waouw_deploy_step "$@" 2>/dev/null || echo "[STEP] $*" | tee -a "$LOGF"; }
  11. # Ne pas relancer si déjà exécuté (cloud-init peut re-exécuter runcmd)
  12. if [ -f "$BOOTDIR/waouw-activate.ok" ]; then
  13. _log "waouw-activate déjà exécuté, skip"
  14. exit 0
  15. fi
  16. _log "waouw-activate démarré"
  17. {
  18. echo "=== $(date -u) ==="
  19. set -x
  20. # DNS fiable (le DNS de la box peut timeout sur les gros téléchargements)
  21. if ! grep -q '8.8.8.8' /etc/resolv.conf 2>/dev/null; then
  22. echo "nameserver 8.8.8.8" >> /etc/resolv.conf
  23. fi
  24. mkdir -p /etc/systemd/resolved.conf.d
  25. echo -e "[Resolve]\nDNS=8.8.8.8 8.8.4.4\nFallbackDNS=1.1.1.1" > /etc/systemd/resolved.conf.d/waouw-dns.conf
  26. systemctl restart systemd-resolved 2>/dev/null || true
  27. # 1) Crée l’utilisateur si absent (bypass wizard)
  28. if ! id -u waouw >/dev/null 2>&1; then
  29. # useradd (Debian)
  30. if command -v useradd >/dev/null 2>&1; then
  31. useradd -m -s /bin/bash waouw || true
  32. elif command -v adduser >/dev/null 2>&1; then
  33. adduser --disabled-password --gecos "" waouw || true
  34. fi
  35. # Mot de passe par défaut (à changer ensuite)
  36. echo 'waouw:waouw' | chpasswd || true
  37. # sudo et docker s’ils existent
  38. if getent group sudo >/dev/null 2>&1; then usermod -aG sudo waouw || true; fi
  39. if getent group docker >/dev/null 2>&1; then usermod -aG docker waouw || true; fi
  40. echo "waouw ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/waouw || true
  41. chmod 440 /etc/sudoers.d/waouw || true
  42. fi
  43. # 2) (Optionnel) expand FS si outil dispo
  44. if command -v raspi-config >/dev/null 2>&1; then
  45. raspi-config nonint do_expand_rootfs || true
  46. fi
  47. # 3) Déploie/active le service firstboot (boot 2)
  48. install -D -m 0644 "$BOOTDIR/waouw-firstboot.service" /etc/systemd/system/waouw-firstboot.service
  49. systemctl daemon-reload || true
  50. systemctl enable waouw-firstboot.service || true
  51. # 4) Fichier témoin
  52. echo "ok" >"$BOOTDIR/waouw-activate.ok"
  53. _step "activate-boot1" "ok" "Utilisateur waouw créé, firstboot activé"
  54. # 5) Nettoyer cmdline.txt si systemd.run= était utilisé (ancien mécanisme)
  55. if [ -f "$BOOTDIR/cmdline.txt" ] && grep -q 'systemd\.run=' "$BOOTDIR/cmdline.txt" 2>/dev/null; then
  56. sed -i \
  57. -e 's| systemd.run=[^ ]*||g' \
  58. -e 's| systemd.run_success_action=[^ ]*||g' \
  59. -e 's| systemd.unit=[^ ]*||g' \
  60. "$BOOTDIR/cmdline.txt" || true
  61. _log "cmdline.txt nettoyé (ancien mécanisme systemd.run)"
  62. fi
  63. set +x
  64. _log "Redémarrage..."
  65. } | tee -a "$LOGF"
  66. # Flush les écritures sur la partition boot (FAT32) avant reboot
  67. sync
  68. sleep 2
  69. reboot