From 1b61567f9a2c3c047dc5f09587edfe099ff2add1 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Sat, 24 Oct 2026 09:02:30 -0600 Subject: [PATCH] feat: add guarded RPi4 image installation --- jeannie | 2 +- lib/jeannie/commands.sh | 7 ++- scripts/rpi4-image-install | 89 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) create mode 100755 scripts/rpi4-image-install diff --git a/jeannie b/jeannie index 3ef61c8..39c7622 100755 --- a/jeannie +++ b/jeannie @@ -64,7 +64,7 @@ Build And Bootstrap deploy-hermes Deploy the Hermes agent (git-install + venv + gateway service). bootstrap-gitea-repo Ensure the Gitea repo and SSH key wiring exist. rpi-services Deploy Pi-hole, Unbound, and Uptime Kuma on RPi4. - host-bootstrap rpi4 [status|plan|nfs-stage|apply --yes] + host-bootstrap rpi4 [status|plan|nfs-stage|install-image|apply --yes] Provision/check the physical RPi4, then bootstrap host services. ollama-setup Install/configure Ollama on the Debian host. artifact-cache {status|up|down|instructions} diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 8b52239..812acc4 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -39,11 +39,11 @@ host_bootstrap_manage() { local rpi_host rpi_user rpi_key rpi_os_image rpi_os_url rpi_install_device if [[ "${host_target}" != "rpi4" ]]; then - echo "Usage: ./jeannie host-bootstrap rpi4 [status|plan|nfs-stage|apply --yes]" >&2 + echo "Usage: ./jeannie host-bootstrap rpi4 [status|plan|nfs-stage|install-image|apply --yes]" >&2 return 2 fi if [[ "${action}" == "--help" || "${action}" == "help" ]]; then - echo "Usage: ./jeannie host-bootstrap rpi4 [status|plan|nfs-stage|apply --yes]" + echo "Usage: ./jeannie host-bootstrap rpi4 [status|plan|nfs-stage|install-image|apply --yes]" return 0 fi rpi_host="${LAB_RPI_HOST:-${LAB_RASPBERRY_HOST:-192.168.100.89}}" @@ -57,6 +57,9 @@ host_bootstrap_manage() { nfs-stage) "${REPO_ROOT}/scripts/rpi4-nfs-stage" "${3:-plan}" "${4:-}" ;; + install-image) + "${REPO_ROOT}/scripts/rpi4-image-install" "${3:-plan}" "${4:-}" + ;; status) echo "== RPi4 host bootstrap status ==" echo "target=${rpi_user}@${rpi_host} key=${rpi_key}" diff --git a/scripts/rpi4-image-install b/scripts/rpi4-image-install new file mode 100755 index 0000000..f0f8d02 --- /dev/null +++ b/scripts/rpi4-image-install @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +set -euo pipefail + +action="${1:-plan}" +rpi_host="${LAB_RPI_HOST:-${LAB_RASPBERRY_HOST:-192.168.100.89}}" +rpi_user="${LAB_RPI_USER:-${LAB_RASPBERRY_USER:-jv}}" +rpi_key="${LAB_RPI_SSH_KEY_PATH:-${LAB_RASPBERRY_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}}" +target="/dev/mmcblk0" +image_url="${LAB_RPI4_OS_IMAGE_URL:-https://downloads.raspberrypi.com/raspios_lite_arm64_latest}" +cache_dir="${LAB_RPI4_IMAGE_CACHE_DIR:-${HOME}/.cache/homelab/rpi4}" +static_ip="${LAB_RPI_HOST:-${LAB_RASPBERRY_HOST:-192.168.100.89}}" +gateway="${LAB_RPI4_GATEWAY:-192.168.100.1}" +dns_servers="${LAB_RPI4_DNS_SERVERS:-192.168.100.73 1.1.1.1}" +public_key="${LAB_RPI4_SSH_PUBLIC_KEY:-}" + +if [[ -z "${public_key}" && -r "${HOME}/.ssh/id_ed25519.pub" ]]; then + public_key="$(<"${HOME}/.ssh/id_ed25519.pub")" +fi + +case "${action}" in + plan) + cat <&2; exit 2; } + [[ -n "${public_key}" ]] || { echo "An SSH public key is required" >&2; exit 2; } + root_source="$(ssh -i "${rpi_key}" -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${rpi_user}@${rpi_host}" 'findmnt -no SOURCE /')" + [[ "${root_source}" == /dev/nfs* || "${root_source}" == *:* ]] || { echo "Refusing image: Pi root is ${root_source}, not NFS" >&2; exit 2; } + mkdir -p "${cache_dir}" + image_file="${cache_dir}/raspios_lite_arm64_latest.img.xz" + curl -fL --retry 3 "${image_url}" -o "${image_file}" + xz -t "${image_file}" + echo "Writing verified Raspberry Pi OS image to ${target}..." + xz -dc "${image_file}" | ssh -i "${rpi_key}" -o BatchMode=yes "${rpi_user}@${rpi_host}" "sudo dd of=${target} bs=16M conv=fsync status=progress" + ssh -i "${rpi_key}" -o BatchMode=yes "${rpi_user}@${rpi_host}" 'sudo partprobe /dev/mmcblk0; for i in $(seq 1 30); do test -b /dev/mmcblk0p1 && test -b /dev/mmcblk0p2 && exit 0; sleep 1; done; exit 1' + password_hash="$(openssl passwd -6 "$(openssl rand -hex 24)")" + key_b64="$(printf '%s' "${public_key}" | base64 | tr -d '\n')" + dns_csv="${dns_servers// /,}" + ssh -i "${rpi_key}" -o BatchMode=yes "${rpi_user}@${rpi_host}" bash -s -- "${password_hash}" "${key_b64}" "${static_ip}" "${gateway}" "${dns_csv}" <<'REMOTE' +set -euo pipefail +password_hash="$1"; key_b64="$2"; static_ip="$3"; gateway="$4"; dns_csv="$5" +boot=$(mktemp -d); root=$(mktemp -d) +cleanup() { sudo umount "$boot" "$root" 2>/dev/null || true; rmdir "$boot" "$root" 2>/dev/null || true; } +trap cleanup EXIT +sudo mount /dev/mmcblk0p1 "$boot" +sudo mount /dev/mmcblk0p2 "$root" +sudo touch "$boot/ssh" +printf 'jv:%s\n' "$password_hash" | sudo tee "$boot/userconf.txt" >/dev/null +sudo mkdir -p "$root/home/jv/.ssh" "$root/etc/NetworkManager/system-connections" +printf '%s\n' "$(printf '%s' "$key_b64" | base64 -d)" | sudo tee "$root/home/jv/.ssh/authorized_keys" >/dev/null +sudo chmod 0700 "$root/home/jv/.ssh"; sudo chmod 0600 "$root/home/jv/.ssh/authorized_keys" +sudo tee "$root/etc/NetworkManager/system-connections/eth0.nmconnection" >/dev/null </dev/null +sudo sed -i 's/^127\.0\.1\.1.*/127.0.1.1\trpi4/' "$root/etc/hosts" || true +sudo mkdir -p "$root/etc/sudoers.d" +printf 'jv ALL=(ALL) NOPASSWD:ALL\n' | sudo tee "$root/etc/sudoers.d/90-homelab-jv" >/dev/null +sudo chmod 0440 "$root/etc/sudoers.d/90-homelab-jv" +REMOTE + echo "RPi4 image installed and headless configuration injected. Rebooting..." + ssh -i "${rpi_key}" -o BatchMode=yes "${rpi_user}@${rpi_host}" 'sudo reboot' || true + ;; + *) + echo "Usage: scripts/rpi4-image-install {plan|apply --yes}" >&2 + exit 2 + ;; +esac