git clone https://garage44.eu/jeroen/midas.git.. /node/install.sh
#!/bin/bash
# Install or upgrade a midas node, as the node's own user. Run by `sudo midas
# setup` (src/setup.rs), which compiles this file in and passes the settings it
# keeps in /etc/midas/node.conf; not meant to be run by hand any more.
#
# install.sh --node gpu-1 --listen 10.44.0.2:2223 \
# --hub http://10.44.0.1:4791 --authorized-key "ssh-ed25519 AAAA… agent" \
# [--operator-key "ssh-ed25519 AAAA… hub"]
#
# The midas binary to put in the image is ./midas next to this script.
#
# Re-running upgrades: the image is rebuilt and the container restarted, with
# the work volume (--volume: checkout, build directory, host key, node log) kept. The
# previous image stays tagged :previous for a one-command rollback.
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
node="" listen="" hub="" repo="" recipe="" auth_key="" midas_bin="$here/midas" operator_keys="" mounts="" volume=""
memory=10g cpus=6 vram=768M render=/dev/dri/renderD128 warn=80 stop=92
while [ $# -gt 0 ]; do
case "$1" in
--node) node=$2; shift ;;
--listen) listen=$2; shift ;;
--hub) hub=$2; shift ;;
--repo) repo=$2; shift ;;
# What `midas setup` set this node up from (setup::recipe), reported by
# the node so `midas check` can tell when its owner must run setup again.
--recipe) recipe=$2; shift ;;
--authorized-key) auth_key=$2; shift ;;
# Repeatable: keys that may log in whether or not the node is leased (the
# hub's reachability check and warmer).
--operator-key) operator_keys+="$2"$'\n'; shift ;;
# Repeatable: HOST_DIR:CONTAINER_DIR, mounted read-only (the Android NDK).
--mount) mounts+="Volume=$2:ro"$'\n'; shift ;;
--midas) midas_bin=$2; shift ;;
# The podman volume with the node's data (/work). `midas setup` names it
# once and keeps it, so a renamed node keeps its data.
--volume) volume=$2; shift ;;
--memory) memory=$2; shift ;;
--cpus) cpus=$2; shift ;;
--vram) vram=$2; shift ;;
--render-node) render=$2; shift ;;
--guard-warn) warn=$2; shift ;;
--guard-stop) stop=$2; shift ;;
*) echo "install.sh: unknown argument $1" >&2; exit 2 ;;
esac
shift
done
[ -n "$node" ] && [ -n "$listen" ] && [ -n "$hub" ] && [ -n "$repo" ] || { sed -n '6,8p' "$0" >&2; exit 2; }
[ -x "$midas_bin" ] || { echo "install.sh: no midas binary at $midas_bin" >&2; exit 1; }
# `--render-node none` for a node with no GPU (the VPS).
if [ "$render" = none ]; then
device_line=""
else
[ -c "$render" ] || { echo "install.sh: no render node at $render (--render-node none for a CPU node)" >&2; exit 1; }
device_line="AddDevice=$render"
fi
listen_ip=${listen%:*}; listen_port=${listen##*:}
ip -o addr show | grep -q " $listen_ip/" || {
echo "install.sh: $listen_ip is not an address on this machine — is its WireGuard up?" >&2; exit 1; }
name="midas-node-$node"
volume=${volume:-$name-work}
cfg=${XDG_CONFIG_HOME:-$HOME/.config}/midas/$node
# 755, not 700: inside the container this directory belongs to root (this user,
# through the user namespace), and sshd's key lookup runs as `build`, which must
# read it. Nothing in it is secret — the node name, the hub's address and public
# keys.
mkdir -p "$cfg"; chmod 755 "$cfg"
# This machine's public IPv4 addresses. The fence (entrypoint.sh) lets the node
# reach them on ports 80 and 443 only — what the internet sees of the VPS, which
# serves the map data a capture fetches — and drops every other port on them.
public_ips=$(ip -4 -o addr show scope global | awk '{split($4, a, "/"); print a[1]}' \
| grep -vE '^(10|127)\.|^169\.254\.|^192\.168\.|^172\.(1[6-9]|2[0-9]|3[01])\.|^100\.(6[4-9]|[7-9][0-9]|1[01][0-9]|12[0-7])\.' \
| paste -sd, - || true)
cat > "$cfg/node.env" <<EOF
MIDAS_NODE=$node
MIDAS_HUB=$hub
MIDAS_REPO=$repo
MIDAS_GUARD_WARN=$warn
MIDAS_GUARD_STOP=$stop
MIDAS_HOST_PUBLIC=$public_ips
MIDAS_RECIPE=$recipe
EOF
printf '%s\n' "$auth_key" > "$cfg/authorized_keys"
printf '%s' "$operator_keys" > "$cfg/operator_keys"
chmod 644 "$cfg/node.env" "$cfg/authorized_keys" "$cfg/operator_keys"
# The container's `build` user must be able to read its configuration, and it
# is this user through the user namespace.
build=$(mktemp -d); trap 'rm -rf "$build"' EXIT
cp "$here"/Containerfile "$here"/sshd_config "$here"/entrypoint.sh "$build"/
cp "$midas_bin" "$build/midas"
if podman image exists "localhost/$name:latest"; then
podman tag "localhost/$name:latest" "localhost/$name:previous"
fi
podman build --tag "localhost/$name:latest" "$build"
# The GPU's dedicated memory, capped through the dmem cgroup controller where
# the machine allows it. Two things have to hold, and the installer checks
# rather than assumes: the device has a dmem region, and systemd delegates dmem
# down to this user's manager — on SteamOS it reaches user@1000.service but not
# into it until root adds `DelegateControllers=… dmem` (DESIGN.md §5). The value
# has a space in it, so it is one quoted PodmanArgs word: quadlet turns that into
# a single \x20-escaped argument (an *escaped* space made it drop the whole
# line, limits included, on the first try). On an APU this caps only VRAM; the
# shared pool is the guard's job (DESIGN.md §5).
dmem_arg=""
if [ -n "$device_line" ] && [ "$vram" != none ]; then
region=$(awk '/vram/ {print $1; exit}' /sys/fs/cgroup/dmem.capacity 2>/dev/null || true)
if [ -n "$region" ] && podman run --rm --entrypoint true \
"--cgroup-conf=dmem.max=$region $vram" "localhost/$name:latest" 2>/dev/null; then
dmem_arg=" \"--cgroup-conf=dmem.max=$region $vram\""
else
echo "note: no GPU memory cap (dmem${region:+ on $region}) — not delegated to this user; see DESIGN.md §5" >&2
fi
fi
unit_dir=${XDG_CONFIG_HOME:-$HOME/.config}/containers/systemd
mkdir -p "$unit_dir"
cat > "$unit_dir/$name.container" <<EOF
# Written by midas node/install.sh -- edit that, not this.
[Unit]
Description=midas node $node (contained build and capture node)
After=network-online.target
# At boot the node usually starts before the tunnel is up, and pasta cannot
# listen on an address that is not there yet. Keep retrying for as long as it
# takes, rather than giving up after systemd's default five tries in ten
# seconds — which left a node down until somebody restarted it by hand.
StartLimitIntervalSec=0
[Container]
Image=localhost/$name:latest
ContainerName=$name
Pull=never
# pasta: no gateway mapped onto this machine's loopback, and exactly one port
# forwarded, from the WireGuard address only, to sshd. The container has an
# address of its own: by default pasta copies the machine's, and the machine's
# public address then means the container itself, and every request the node
# made to its own machine's website was refused (seen 2026-10-12).
Network=pasta:--no-map-gw,-a,10.88.0.2,-n,30,-g,10.88.0.1,-t,$listen_ip/$listen_port:2222,-u,none,-T,none,-U,none
DropCapability=all
# NET_ADMIN for the fence (this container's network namespace only); the rest is
# what sshd needs to hand a session to \`build\`.
AddCapability=NET_ADMIN SETUID SETGID CHOWN DAC_OVERRIDE FOWNER SYS_CHROOT KILL AUDIT_WRITE
NoNewPrivileges=true
$device_line
Volume=$volume:/work
Volume=$cfg:/config:ro
${mounts}PodmanArgs=--memory=$memory --memory-swap=$memory --cpus=$cpus --pids-limit=8192$dmem_arg
[Service]
Restart=always
RestartSec=15
TimeoutStartSec=300
[Install]
WantedBy=default.target
EOF
# Quadlet drops a line it cannot parse without failing the unit: a
# backslash-escaped space once removed every limit (memory, cpus, pids, dmem)
# and the node ran unbounded. So read back what quadlet will actually run.
gen=/usr/lib/systemd/user-generators/podman-user-generator
[ -x "$gen" ] || gen=/usr/libexec/podman/quadlet
if [ -x "$gen" ]; then
run=$("$gen" -user -dryrun 2>/dev/null | grep -A200 "^---$name.service---" | grep -m1 '^ExecStart=' || true)
wants=("--memory=$memory" "--cpus=$cpus" "--pids-limit=8192")
[ -n "$device_line" ] && wants+=("--device $render")
[ -n "$dmem_arg" ] && wants+=("dmem.max=")
for want in "${wants[@]}"; do
case "$run" in *"$want"*) ;; *)
echo "install.sh: quadlet would not pass $want to podman — the limits would not apply. Not starting." >&2
echo "$run" >&2; exit 1 ;;
esac
done
fi
systemctl --user daemon-reload
systemctl --user restart "$name.service"
loginctl enable-linger "$USER" 2>/dev/null || \
echo "note: could not enable lingering; the node starts at login, not at boot" >&2
# Checked, not assumed: the first install printed "listening" over a container
# that had already died five times (sshd could not bind its port).
for _ in $(seq 1 20); do
systemctl --user is-active --quiet "$name.service" && \
podman logs "$name" 2>/dev/null | grep -q 'sshd starting' && break
sleep 1
done
sleep 2
if ! systemctl --user is-active --quiet "$name.service"; then
echo "install.sh: $name did not stay up. Its log:" >&2
journalctl --user -u "$name.service" --no-pager -n 15 >&2
exit 1
fi
echo "midas node $node: up, listening on $listen."
echo "Rollback: podman tag localhost/$name:previous localhost/$name:latest && systemctl --user restart $name"