wrolpi/debian-live-config/config/includes.chroot/usr/local/sbin/wrolpi-bootstrap.sh
lrnselfreliance 2ac4a3a294
A WROLPi serves its library over rsync, and makes a database snapshot on request (#988)
* A WROLPi serves its library over rsync, and makes a database snapshot on request

The first part of replacing the sync: a secondary will copy its primary's whole media directory with rsync and
replace its database with the primary's, instead of downloading the files of FileGroups over HTTP (which never
copies an ignored directory, such as the map's).

- An rsync daemon serves the media directory read-only on port 8873, on every WROLPi.  It never serves the TLS keys,
  the cookies, or the database in use.  On a Raspberry Pi and Debian it is the wrolpi-rsyncd unit; in Docker the
  Controller runs it in a helper container made from the api container (its image, user and volumes), so the media
  directory is still configured in one place.  All three read controller/rsyncd.conf.
- POST /api/sync/snapshot asks for a snapshot of the database, which the daemon serves from config/.sync.  An
  unchanged database is not written again.
- /api/sync/info also says the database's migration and whether the media directory is mounted.

Nothing uses these yet; the existing sync is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* The rsync daemon follows no links, and no snapshot is made in WROL Mode

- A client that asked the daemon to follow symbolic links could read what a link in the library points at.  A
  relative link to config/ssl was read this way although that directory is left out of the module.  The daemon now
  refuses the three options that follow links; a link is sent as a link.
- A database snapshot is a new file on the primary's drive, so WROL Mode refuses it, as it does a backup.  A snapshot
  made earlier is still served.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* No rsync helper from an image without rsync, and the helper follows the api container

Review found:

- A helper made from an api image built before rsync was added exits at once, and Docker restarted it forever while
  the Controller reported it created.  The image is now looked at first; no helper is made from one without rsync,
  an older helper is left serving, and the reason is logged once.
- The helper was only checked when the Controller started, so a recreated or rebuilt api container left it stale.
  It is now checked every minute.
- A snapshot cut off by its process being killed stayed marked as running and held back every later request.
- The snapshot's half-written temporary file was served by the daemon.
- The comment on the daemon's refused options claimed more than they give: a link that is part of the requested
  path is still followed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-18 16:47:02 -07:00

315 lines
14 KiB
Bash

#!/usr/bin/env bash
# WROLPi bootstrap — runs at every boot via wrolpi-bootstrap.service.
# Idempotent throughout. Works in all four deployment shapes:
#
# 1. Live USB (direct), first boot. No persistence partition exists.
# This script creates one in the free space at the end of the boot
# drive, formats it as ext4 (label `persistence`), writes a
# persistence.conf so future boots pick it up automatically via
# live-boot, bind-mounts the persistence root over /media/wrolpi,
# and enables the WROLPi runtime services. The SQLite database is
# created by the API itself on first start (in /media/wrolpi/config/).
#
# 2. Live USB (direct), subsequent boot. live-boot's initramfs has
# already applied the /media/wrolpi bind mount from persistence.conf.
# The database already exists. Most steps no-op; we always
# regenerate the leaf TLS cert and ensure services are enabled.
#
# 3. Live USB (loop / Ventoy / multiboot). The ISO is loop-mounted by a
# multiboot tool, so we cannot safely write a new partition to the
# underlying disk (it's the user's Ventoy stick, not a blank target).
# /media/wrolpi is backed by tmpfs and everything runs from RAM. All
# user data is lost on reboot. Surfaced to the user via the
# first-boot zenity dialog.
#
# 4. Installed. No live medium present; partition-creation branch is
# skipped. /media/wrolpi is mounted via /etc/fstab on an external
# drive, or is a plain directory on the root filesystem if the user
# installed without one. The SQLite database lives inside
# /media/wrolpi/config/ in every shape, so it travels with the drive.
# Until the Controller onboarding wizard writes controller.yaml, this
# script does nothing; the wizard's repair.sh finishes setup.
set -euo pipefail
log() { echo "[wrolpi-bootstrap] $*"; }
# Show progress on the Plymouth splash so the user sees what's happening
# during the 30-60s first-boot work. Plymouth is up between initramfs and
# lightdm starting; we block lightdm via systemd ordering, so the splash
# owns the screen for our entire run. Falls back to log-only if Plymouth
# isn't active (e.g. running this script manually over SSH).
progress() {
plymouth display-message --text="$1" 2>/dev/null || true
log "$1"
}
# Accumulate human-readable notes about anything non-trivial we did this
# boot. If anything ends up in $SUMMARY by end-of-run we write it to a
# file under /tmp/ that the wrolpi user's XFCE autostart picks up to show
# a zenity dialog (see /usr/local/bin/wrolpi-firstboot-notify). /tmp is
# tmpfs so the file is naturally per-boot; subsequent uneventful boots
# show no dialog.
SUMMARY=""
SUMMARY_FILE=/tmp/wrolpi-firstboot-summary.txt
note_action() { SUMMARY="${SUMMARY}- $1
"; }
progress "Preparing WROLPi..."
# --- Detect deployment mode ---------------------------------------------
# Three mutually-exclusive shapes:
#
# direct Live USB flashed with dd/Raspberry Pi Imager: the ISO partition is a
# real partition on a real disk. Create + manage a
# persistence partition on that disk.
#
# loop Live medium is a loop or device-mapper node — typical
# of Ventoy, YUMI, Easy2Boot, GLIM, AIO Boot, etc. The
# underlying block device is the user's data drive; we
# must NOT try to sfdisk-append a partition onto it.
# Run ephemerally: /media/wrolpi backed by tmpfs, lost
# on reboot.
#
# installed No live medium at all. Skip partition logic entirely;
# /media/wrolpi is either on /etc/fstab or a plain dir
# on the root filesystem.
MODE=installed
BOOT_DRIVE=""
LIVE_MEDIUM=""
for mp in /usr/lib/live/mount/medium /run/live/medium /lib/live/mount/medium; do
if part=$(findmnt -nr -o SOURCE "$mp" 2>/dev/null) && [ -n "$part" ]; then
LIVE_MEDIUM=$part
case "$part" in
/dev/sd*|/dev/nvme*|/dev/mmcblk*|/dev/vd*|/dev/xvd*|/dev/hd*)
# Real partitions on real (or virtual) disks: SCSI/USB (sd*),
# NVMe, SD/eMMC (mmcblk*), VirtIO (vd*), Xen (xvd*), legacy
# IDE (hd*). A blank VM disk imaged with the ISO can safely
# hold a persistence partition, so treat virtual disks as direct
# rather than letting them fall through to the ephemeral default.
MODE=direct
BOOT_DRIVE="/dev/$(lsblk -no PKNAME "$part")"
log "live medium $part on $BOOT_DRIVE (mode: direct)"
;;
/dev/loop*|/dev/dm-*|/dev/mapper/*)
MODE=loop
log "live medium $part is a loop/dm device (mode: loop — ephemeral)"
;;
*)
MODE=loop
log "live medium $part has unrecognised shape (mode: loop — safe default)"
;;
esac
break
fi
done
if [ "$MODE" = "installed" ]; then
log "no live medium detected — running in Installed mode"
fi
# --- Find or create persistence partition (direct mode only) ------------
if [ "$MODE" = "direct" ]; then
PERSIST_PART=$(blkid -L persistence 2>/dev/null || true)
if [ -z "$PERSIST_PART" ]; then
progress "Creating persistence partition on $BOOT_DRIVE..."
# Compute next 1-MiB-aligned start sector after the highest-ending
# existing partition. We need MAX(start+size), not LAST: iso-hybrid
# layouts list a small ESP (sdb2) AFTER the main data partition
# (sdb1) in sfdisk -d output even though sdb1 ends much later on
# disk. Parse via sed because sfdisk -d pads values with spaces
# ("start= 64,") which breaks naive awk.
LAST_END=$(sfdisk -d "$BOOT_DRIVE" \
| sed -n 's|^/dev/[^ ]* *: *start= *\([0-9]\+\), *size= *\([0-9]\+\).*|\1 \2|p' \
| awk '{e=$1+$2; if(e>m) m=e} END{print m+0}')
if [ "${LAST_END:-0}" -lt 1 ]; then
log "ERROR: failed to compute LAST_END from $BOOT_DRIVE partition table"
exit 1
fi
# Reserve 8 GiB of unused space between the ISO partition and the new
# persistence partition. The `wrolpi-usb.sh upgrade` tool overwrites
# the ISO partition with a (potentially larger) new ISO; without
# headroom, growth past the current ISO size would clobber the
# persistence partition's data. 8 GiB covers ~2x the current ISO
# size — enough for several future releases. Cost: ~8 GiB of
# persistence space the user doesn't see.
HEADROOM_SECTORS=$(( 8 * 1024 * 1024 * 1024 / 512 ))
START=$(( (((LAST_END + HEADROOM_SECTORS) + 2047) / 2048) * 2048 ))
# --no-reread --force: required because the live medium is mounted from
# this same drive. Adding a new partition entry past the mounted one
# is safe; we use partx -a below to inform the running kernel without
# forcing a re-read of the whole partition table.
echo "start=$START, type=83" | sfdisk --append --no-reread --force "$BOOT_DRIVE"
partx -a "$BOOT_DRIVE" 2>/dev/null || true
# Identify the new partition by the start sector we just assigned to
# it. We deliberately avoid `lsblk | tail -1`: if partx -a or udev
# haven't registered the new node, that fallback returns the existing
# last partition (typically the ESP or the live data partition) — and
# the mkfs.ext4 -F below would wipe it. Matching on start sector is
# unambiguous: no existing partition can share it. Retry up to 5s
# for udev to settle and the partition node to appear.
PERSIST_PART=""
for _i in 1 2 3 4 5; do
udevadm settle || true
PERSIST_PART=$(lsblk -lnpo NAME,START "$BOOT_DRIVE" 2>/dev/null \
| awk -v s="$START" '$2 == s {print $1; exit}')
[ -n "$PERSIST_PART" ] && [ -b "$PERSIST_PART" ] && break
PERSIST_PART=""
sleep 1
done
if [ -z "$PERSIST_PART" ] || ! [ -b "$PERSIST_PART" ]; then
log "ERROR: failed to locate the newly created persistence partition"
exit 1
fi
progress "Formatting $PERSIST_PART as ext4..."
mkfs.ext4 -L persistence -F "$PERSIST_PART"
PERSIST_SIZE=$(lsblk -bno SIZE "$PERSIST_PART" 2>/dev/null | numfmt --to=iec --suffix=B || echo "?")
note_action "Created persistence partition ${PERSIST_PART} (${PERSIST_SIZE}) on ${BOOT_DRIVE}"
fi
# Mount the persistence partition + lay out the on-disk structure.
# /media/wrolpi is bound to /mnt/persistence/wrolpi by either this
# script (first boot) or live-boot's initramfs (subsequent boots).
mkdir -p /mnt/persistence
mountpoint -q /mnt/persistence || mount "$PERSIST_PART" /mnt/persistence
# The library must be owned by this image's wrolpi. ISOs before wrolpi
# became a system account wrote it as UID 1000, which a USB upgraded with a
# newer ISO would leave unwritable; re-own an existing library once.
if [ -d /mnt/persistence/wrolpi ] \
&& [ "$(stat -c %u /mnt/persistence/wrolpi)" != "$(id -u wrolpi)" ]; then
progress "Updating library ownership for this WROLPi version..."
chown -R wrolpi:wrolpi /mnt/persistence/wrolpi
note_action "Updated the library's ownership for this WROLPi version"
fi
mkdir -p /mnt/persistence/wrolpi
chown wrolpi:wrolpi /mnt/persistence/wrolpi
# persistence.conf: live-boot reads this on boot and applies the bind
# mount before systemd starts on subsequent boots. Only /media/wrolpi
# needs to persist — the SQLite database lives inside it.
cat > /mnt/persistence/persistence.conf <<'EOF'
/media/wrolpi bind,source=wrolpi
EOF
# Bind /media/wrolpi on first boot. live-boot will do this from the
# initramfs on subsequent boots and the call below is then a no-op.
if ! mountpoint -q /media/wrolpi; then
mkdir -p /media/wrolpi
mount --bind /mnt/persistence/wrolpi /media/wrolpi
fi
fi
# --- Ephemeral (loop mode) ----------------------------------------------
# Booted via Ventoy / multiboot tool / loop-mounted ISO. We cannot tell
# which underlying disk is the user's data drive — appending a partition
# would risk corrupting their Ventoy stick or whatever else they had on
# that device. Run from RAM instead. Anything written to /media/wrolpi
# vanishes on reboot, but the rest of WROLPi (config layout, database,
# services) works identically.
if [ "$MODE" = "loop" ]; then
if ! mountpoint -q /media/wrolpi; then
mkdir -p /media/wrolpi
# No explicit size= — defaults to half-RAM, plenty for an
# exploratory session and capped so we can't OOM the box.
mount -t tmpfs -o mode=0755 tmpfs /media/wrolpi
log "ephemeral mode: /media/wrolpi is tmpfs (data lost on reboot)"
# Inside the guard so a manual service restart mid-session (tmpfs
# already mounted) doesn't re-trigger the "NOT writing to disk"
# first-boot dialog.
note_action "Running ephemerally from RAM (Ventoy / multiboot USB detected). Your data WILL NOT persist across reboots."
fi
fi
# --- /media/wrolpi/config layout -----------------------------------------
# Works regardless of whether /media/wrolpi is a bind (Live), a regular
# mount from /etc/fstab (Installed + external drive), or a plain
# directory on root (Installed without external drive). The SQLite
# database (wrolpi.db) is created inside config/ by the API on first
# start — no server or cluster setup is needed here.
if [ ! -d /media/wrolpi ]; then
mkdir -p /media/wrolpi
fi
chown wrolpi:wrolpi /media/wrolpi 2>/dev/null || true
# --- Installed: wait for the Controller onboarding wizard ---------------
# Until the user picks a drive (or the internal disk) in the wizard, there
# is no controller.yaml. Leave Caddy on the onboarding Caddyfile from
# wrolpi-first-startup.service so every page leads to the wizard, and leave
# certificates, the database, and services to the wizard's repair.sh.
if [ "$MODE" = "installed" ] && [ ! -e /media/wrolpi/config/controller.yaml ]; then
log "primary drive is not set up; the Controller onboarding wizard will finish setup"
exit 0
fi
mkdir -p /media/wrolpi/config
chown wrolpi:wrolpi /media/wrolpi/config
# --- Certificates -------------------------------------------------------
# CA persists in /media/wrolpi/config/ssl/ (created on first run). Leaf
# cert is regenerated every boot so SANs reflect the current hostname/IP.
progress "Generating TLS certificate..."
/opt/wrolpi/scripts/generate_certificates.sh
# --- Caddy: swap onboarding stub for the full Caddyfile if needed -------
if ! diff -q /opt/wrolpi/etc/raspberrypios/Caddyfile /etc/caddy/Caddyfile >/dev/null 2>&1; then
progress "Configuring Caddy..."
cp /opt/wrolpi/etc/raspberrypios/Caddyfile /etc/caddy/Caddyfile
systemctl restart caddy || true
fi
# Signal to the Controller that the primary drive is set up. Without this
# the Controller renders its onboarding wizard and offers the persistence
# partition as an unconfigured candidate. Empty YAML yields defaults.
if [ ! -e /media/wrolpi/config/controller.yaml ]; then
install -o wrolpi -g wrolpi -m 0644 /dev/null /media/wrolpi/config/controller.yaml
fi
# --- Enable and start the WROLPi runtime services -----------------------
# These units have implicit `After=wrolpi-bootstrap.service` (via our
# `Before=` in the unit file), so a blocking `systemctl --now` would
# deadlock: the runtime units won't start until bootstrap finishes, and
# bootstrap won't finish until systemctl returns. Enable, then start
# with --no-block so systemd queues the starts after we exit.
progress "Starting WROLPi services..."
systemctl enable wrolpi-api.service wrolpi-app.service wrolpi-kiwix.service wrolpi-rsyncd.service
systemctl start --no-block wrolpi-api.service wrolpi-app.service wrolpi-kiwix.service wrolpi-rsyncd.service
# If we did anything notable, leave a summary for the XFCE autostart to
# show as a zenity dialog once the user lands on the desktop.
if [ -n "$SUMMARY" ]; then
if [ "$MODE" = "loop" ]; then
cat > "$SUMMARY_FILE" <<EOF
WROLPi is running, but NOT writing to disk.
$SUMMARY
You appear to have booted from Ventoy or a similar multiboot tool.
WROLPi can't safely write a persistence partition in that case, so
everything lives in RAM and will be lost when you reboot or power off.
To get a persistent install, flash the ISO directly to a USB drive
with dd or Raspberry Pi Imager and boot from that drive instead.
EOF
else
cat > "$SUMMARY_FILE" <<EOF
WROLPi finished setting itself up:
$SUMMARY
These persist between reboots:
/media/wrolpi — your library (videos, archives, zims, ...)
/media/wrolpi/config — configuration + database
EOF
fi
chmod 0644 "$SUMMARY_FILE"
fi
progress "WROLPi is ready."
log "bootstrap complete"