upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts/linux-boot/boot-inner.sh
diff options
context:
space:
mode:
authorkirigayakazima <46666711+kirigayakazima@users.noreply.github.com>2026-11-26 21:19:58 +0800
committerkirigayakazima <46666711+kirigayakazima@users.noreply.github.com>2026-11-26 21:19:58 +0800
commit9a2ac71066ee1cfc23b39c6b4b4b121ed2ab72d0 (patch)
tree2c4739bb7d3558774b579fe0afe321edae38f776 /scripts/linux-boot/boot-inner.sh
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
linux-boot: harden boot-inner.sh and configure-worktree.sh
Diffstat (limited to 'scripts/linux-boot/boot-inner.sh')
-rwxr-xr-xscripts/linux-boot/boot-inner.sh117
1 files changed, 99 insertions, 18 deletions
diff --git a/scripts/linux-boot/boot-inner.sh b/scripts/linux-boot/boot-inner.sh
index e52d3daef..aa5f1147f 100755
--- a/scripts/linux-boot/boot-inner.sh
+++ b/scripts/linux-boot/boot-inner.sh
@@ -1,44 +1,125 @@
1#!/bin/bash 1#!/bin/bash
2# Runs inside the leonida-boot-* unit (see run-boot.sh). Waits for memory, takes the locks, samples resources, boots. 2# Runs inside the leonida-boot-* unit (see run-boot.sh). Waits for memory, takes the locks, samples resources, boots.
3set -u 3set -u
4WORK=/opt/leonida/work/$BOOT_AGENT; N=$BOOT_N; LOG=$WORK/boot-$N.log; RES=$WORK/boot-$N.resources.tsv 4
5die() {
6 echo "ERROR: $*" >&2
7 exit 2
8}
9
10OUT=${LEONIDA_BOOT_ROOT:-${LEONIDA_OUT:-/opt/leonida}}
11BOOT_AGENT=${BOOT_AGENT:-}
12BOOT_N=${BOOT_N:-}
13BOOT_SECS=${BOOT_SECS:-}
14GTA6RT=${GTA6RT:-}
15BOOT_SH=${BOOT_SH:-$OUT/boot.sh}
16NVIDIA_SMI=${NVIDIA_SMI:-nvidia-smi}
17PROC_MEMINFO=${LEONIDA_MEMINFO_PATH:-/proc/meminfo}
18CGROUP_SELF=${LEONIDA_SELF_CGROUP_PATH:-/proc/self/cgroup}
19CGROUP_ROOT=${LEONIDA_CGROUP_ROOT:-/sys/fs/cgroup}
20WAIT_SLEEP=${LEONIDA_WAIT_SLEEP_SECS:-15}
21SAMPLE_SLEEP=${LEONIDA_SAMPLE_SLEEP_SECS:-2}
22WAIT_ITERS=${LEONIDA_WAIT_ITERS:-40}
23
24[ -n "$BOOT_AGENT" ] || die "BOOT_AGENT must be set"
25[ -n "$BOOT_N" ] || die "BOOT_N must be set"
26[ -n "$BOOT_SECS" ] || die "BOOT_SECS must be set"
27[ -n "$GTA6RT" ] || die "GTA6RT must be set"
28case "$BOOT_AGENT" in
29 .|..|*/*|*\\*|*..*) die "BOOT_AGENT must be a single path segment: $BOOT_AGENT" ;;
30esac
31case "$BOOT_N" in
32 .|..|*/*|*\\*|*..*) die "BOOT_N must be a single path segment: $BOOT_N" ;;
33esac
34[[ "$BOOT_SECS" =~ ^[0-9]+$ ]] || die "BOOT_SECS must be a non-negative integer: $BOOT_SECS"
35[[ "$WAIT_ITERS" =~ ^[0-9]+$ ]] || die "WAIT_ITERS must be a non-negative integer: $WAIT_ITERS"
36
37NEED=${LEONIDA_BOOT_MIN_FREE_MB:-6500}
38[[ "$NEED" =~ ^[0-9]+$ ]] || die "LEONIDA_BOOT_MIN_FREE_MB must be a non-negative integer: $NEED"
39
40WORK=${LEONIDA_BOOT_WORK:-$OUT/work/$BOOT_AGENT}; N=$BOOT_N; LOG=$WORK/boot-$N.log; RES=$WORK/boot-$N.resources.tsv
5META=$WORK/boot-$N.meta 41META=$WORK/boot-$N.meta
42mkdir -p "$OUT" "$WORK" || die "cannot create work directory: $WORK"
6: > "$LOG" 43: > "$LOG"
7echo "start_epoch=$(date +%s)" > "$META" 44echo "start_epoch=$(date +%s)" > "$META"
8echo "unit=leonida-boot-$BOOT_AGENT-$N gta6rt=$GTA6RT secs=$BOOT_SECS" >> "$META" 45echo "unit=leonida-boot-$BOOT_AGENT-$N gta6rt=$GTA6RT secs=$BOOT_SECS" >> "$META"
9env | grep -E '^(LEONIDA_|WINEDEBUG=)' | sort >> "$META" 46env | grep -E '^(LEONIDA_|WINEDEBUG=)' | sort >> "$META" || true
10 47
11mem_free_mb() { # RAM available plus a third of free zram swap (zram pages cost RAM, roughly 3:1) 48mem_free_mb() { # RAM available plus a third of free zram swap (zram pages cost RAM, roughly 3:1)
12 awk '/^MemAvailable:/{a=$2} /^SwapFree:/{s=$2} END{printf "%d", (a + s/3)/1024}' /proc/meminfo 49 [ -r "$PROC_MEMINFO" ] || { echo 0; return; }
50 awk 'BEGIN{a=0; s=0} /^MemAvailable:/{a=$2} /^SwapFree:/{s=$2} END{printf "%d", (a + s/3)/1024}' "$PROC_MEMINFO"
13} 51}
14NEED=${LEONIDA_BOOT_MIN_FREE_MB:-6500} 52swap_pct() {
15swap_pct() { awk '/^SwapTotal:/{t=$2} /^SwapFree:/{f=$2} END{printf "%d", t ? (t-f)*100/t : 0}' /proc/meminfo; } 53 [ -r "$PROC_MEMINFO" ] || { echo 0; return; }
54 awk 'BEGIN{t=0; f=0} /^SwapTotal:/{t=$2} /^SwapFree:/{f=$2} END{pct=(t>0 ? (t-f)*100/t : 0); printf "%d", pct}' "$PROC_MEMINFO"
55}
56read_uint() {
57 local v=""
58 if [ -r "$1" ]; then
59 v=$(cat "$1" 2>/dev/null || true)
60 v=${v//$'\r'/}
61 fi
62 [[ "$v" =~ ^[0-9]+$ ]] || v=0
63 printf '%s' "$v"
64}
65
66SAMPLER=""
67cleanup() {
68 if [ -n "${SAMPLER:-}" ]; then
69 kill "$SAMPLER" 2>/dev/null || true
70 wait "$SAMPLER" 2>/dev/null || true
71 SAMPLER=""
72 fi
73}
74trap cleanup EXIT INT TERM
16 75
17exec 8>/opt/leonida/.gpu.lock; flock 8 # one boot at a time 76exec 8>"$OUT/.gpu.lock"
77if command -v flock >/dev/null 2>&1; then flock 8; fi # one boot at a time
18echo "[run-boot] $(date +%T) got the GPU lock" >> "$LOG" 78echo "[run-boot] $(date +%T) got the GPU lock" >> "$LOG"
19for i in $(seq 1 40); do # up to 10 min for memory headroom 79for ((i = 1; i <= WAIT_ITERS; i++)); do # up to 10 min for memory headroom
20 f=$(mem_free_mb); sp=$(swap_pct); [ "$f" -ge "$NEED" ] && [ "$sp" -lt 70 ] && break 80 f=$(mem_free_mb); sp=$(swap_pct); [ "$f" -ge "$NEED" ] && [ "$sp" -lt 70 ] && break
21 echo "[run-boot] $(date +%T) waiting for memory: ${f} MB free (need ${NEED}), swap ${sp}% (need < 70%)" >> "$LOG"; sleep 15 81 echo "[run-boot] $(date +%T) waiting for memory: ${f} MB free (need ${NEED}), swap ${sp}% (need < 70%)" >> "$LOG"; sleep "$WAIT_SLEEP"
22done 82done
23echo "[run-boot] $(date +%T) memory before boot: $(mem_free_mb) MB free, swap $(swap_pct)%" >> "$LOG" 83echo "[run-boot] $(date +%T) memory before boot: $(mem_free_mb) MB free, swap $(swap_pct)%" >> "$LOG"
24exec 9>/opt/leonida/.build.lock; flock 9 # no compiles while the game runs 84exec 9>"$OUT/.build.lock"
85if command -v flock >/dev/null 2>&1; then flock 9; fi # no compiles while the game runs
25echo "[run-boot] $(date +%T) got the build lock; booting" >> "$LOG" 86echo "[run-boot] $(date +%T) got the build lock; booting" >> "$LOG"
26echo "boot_epoch=$(date +%s)" >> "$META" 87echo "boot_epoch=$(date +%s)" >> "$META"
27 88
28CG=/sys/fs/cgroup$(cut -d: -f3 /proc/self/cgroup) 89cg_rel=""
29( printf 'time\tmem_avail_mb\tswap_used_mb\tunit_mem_mb\tunit_swap_mb\tgpu_mem_mb\tgpu_util\n' 90if [ -r "$CGROUP_SELF" ]; then
91 cg_rel=$(awk -F: '$1=="0"{print $3; exit}' "$CGROUP_SELF" 2>/dev/null || true)
92 [ -n "$cg_rel" ] || cg_rel=$(awk -F: 'NF>=3{p=$3} END{print p}' "$CGROUP_SELF" 2>/dev/null || true)
93 cg_rel=${cg_rel//$'\r'/}
94fi
95CG=$CGROUP_ROOT$cg_rel
96( trap 'kill "${sleep_pid:-}" 2>/dev/null || true; exit 0' TERM INT
97 sleep_pid=""
98 printf 'time\tmem_avail_mb\tswap_used_mb\tunit_mem_mb\tunit_swap_mb\tgpu_mem_mb\tgpu_util\n'
30 while :; do 99 while :; do
31 read -r ma su < <(awk '/^MemAvailable:/{a=$2} /^SwapTotal:/{t=$2} /^SwapFree:/{f=$2} END{print int(a/1024), int((t-f)/1024)}' /proc/meminfo) 100 ma=0; su=0
32 um=$(( $(cat "$CG/memory.current" 2>/dev/null || echo 0) / 1048576 )); us=$(( $(cat "$CG/memory.swap.current" 2>/dev/null || echo 0) / 1048576 )) 101 if [ -r "$PROC_MEMINFO" ]; then
33 g=$(nvidia-smi --query-gpu=memory.used,utilization.gpu --format=csv,noheader,nounits 2>/dev/null | tr -d ' ' | tr ',' '\t') 102 read -r ma su < <(awk 'BEGIN{a=0; t=0; f=0} /^MemAvailable:/{a=$2} /^SwapTotal:/{t=$2} /^SwapFree:/{f=$2} END{u=(t>f ? t-f : 0); print int(a/1024), int(u/1024)}' "$PROC_MEMINFO")
103 fi
104 um=$(( $(read_uint "$CG/memory.current") / 1048576 ))
105 us=$(( $(read_uint "$CG/memory.swap.current") / 1048576 ))
106 g=$("$NVIDIA_SMI" --query-gpu=memory.used,utilization.gpu --format=csv,noheader,nounits 2>/dev/null | head -n 1 | tr -d ' \r' | tr ',' '\t')
34 printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$(date +%T)" "$ma" "$su" "$um" "$us" "${g:-? ?}" 107 printf '%s\t%s\t%s\t%s\t%s\t%s\n' "$(date +%T)" "$ma" "$su" "$um" "$us" "${g:-? ?}"
35 sleep 2 108 sleep "$SAMPLE_SLEEP" &
109 sleep_pid=$!
110 wait "$sleep_pid" 2>/dev/null || break
111 sleep_pid=""
36 done ) > "$RES" 2>/dev/null & 112 done ) > "$RES" 2>/dev/null &
37SAMPLER=$! 113SAMPLER=$!
38 114
39/opt/leonida/boot.sh run "$BOOT_SECS" >> "$LOG" 2>&1 115if [ -f "$BOOT_SH" ]; then
40rc=$? 116 "$BOOT_SH" run "$BOOT_SECS" >> "$LOG" 2>&1
41kill $SAMPLER 2>/dev/null 117 rc=$?
118else
119 echo "[run-boot] missing boot script: $BOOT_SH" >> "$LOG"
120 rc=127
121fi
122cleanup
42echo "[run-boot] $(date +%T) boot.sh exit $rc" >> "$LOG" 123echo "[run-boot] $(date +%T) boot.sh exit $rc" >> "$LOG"
43# boot.sh stops the title with timeout(1) at the time limit: 124 means it ran the whole time, which is success here. 124# boot.sh stops the title with timeout(1) at the time limit: 124 means it ran the whole time, which is success here.
44[ "$rc" = 124 ] && { echo "[run-boot] time limit reached" >> "$LOG"; rc=0; } 125[ "$rc" = 124 ] && { echo "[run-boot] time limit reached" >> "$LOG"; rc=0; }