diff options
| author | MoonEyes <ducoinbenjamin@yahoo.fr> | 2026-11-28 20:40:37 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-28 20:40:37 +0100 |
| commit | df3e8a85e0a998e5fc038d4ff71363ec76de88d0 (patch) | |
| tree | 05c6d63910f704afca466fbbcece191fc12e21bf /scripts/linux-boot | |
| parent | da4fd4ea0bb687be9ffda5db7d2bebbe4e461a39 (diff) | |
| parent | bfde05dcb741441379ebc6e0a43ae46eec660467 (diff) | |
Merge pull request #2274 from kirigayakazima/fix/linux-boot-scripts-safety
linux-boot: harden boot-digest/run-boot dedup, empty-stats, and argument checks
Diffstat (limited to 'scripts/linux-boot')
| -rwxr-xr-x | scripts/linux-boot/boot-digest.sh | 31 | ||||
| -rwxr-xr-x | scripts/linux-boot/run-boot.sh | 30 |
2 files changed, 43 insertions, 18 deletions
diff --git a/scripts/linux-boot/boot-digest.sh b/scripts/linux-boot/boot-digest.sh index dd2289e30..9a7c06d68 100755 --- a/scripts/linux-boot/boot-digest.sh +++ b/scripts/linux-boot/boot-digest.sh | |||
| @@ -3,14 +3,23 @@ | |||
| 3 | # Writes /opt/leonida/work/<agent>/boot-<n>.digest.txt and appends a line to /opt/leonida/work/boots.tsv. | 3 | # Writes /opt/leonida/work/<agent>/boot-<n>.digest.txt and appends a line to /opt/leonida/work/boots.tsv. |
| 4 | # Local only: digests quote trace lines, so they never go into commits or PRs. | 4 | # Local only: digests quote trace lines, so they never go into commits or PRs. |
| 5 | set -u | 5 | set -u |
| 6 | AGENT=$1; N=$2; WORK=/opt/leonida/work/$AGENT; LOG=$WORK/boot-$N.log; RES=$WORK/boot-$N.resources.tsv | 6 | AGENT=${1:?agent}; N=${2:?n} |
| 7 | META=$WORK/boot-$N.meta; OUT=$WORK/boot-$N.digest.txt; LEDGER=/opt/leonida/work/boots.tsv | 7 | if [ "$AGENT" = "." ] || [ "$AGENT" = ".." ] || ! [[ "$AGENT" =~ ^[A-Za-z0-9._-]+$ ]]; then |
| 8 | echo "boot-digest: invalid agent: $AGENT" >&2; exit 2 | ||
| 9 | fi | ||
| 10 | if ! [[ "$N" =~ ^[0-9]+$ ]]; then | ||
| 11 | echo "boot-digest: invalid boot number: $N" >&2; exit 2 | ||
| 12 | fi | ||
| 13 | ROOT=${LEONIDA_BOOT_ROOT:-/opt/leonida} | ||
| 14 | WORK=$ROOT/work/$AGENT; LOG=$WORK/boot-$N.log; RES=$WORK/boot-$N.resources.tsv | ||
| 15 | META=$WORK/boot-$N.meta; OUT=$WORK/boot-$N.digest.txt; LEDGER=$ROOT/work/boots.tsv | ||
| 16 | mkdir -p "$WORK" "$(dirname "$LEDGER")" | ||
| 8 | START=$(sed -n 's/^start_epoch=//p' "$META" 2>/dev/null); BOOT=$(sed -n 's/^boot_epoch=//p' "$META" 2>/dev/null) | 17 | START=$(sed -n 's/^start_epoch=//p' "$META" 2>/dev/null); BOOT=$(sed -n 's/^boot_epoch=//p' "$META" 2>/dev/null) |
| 9 | START=${START:-$(date +%s)}; BOOT=${BOOT:-$START}; END=$(date +%s) | 18 | START=${START:-$(date +%s)}; BOOT=${BOOT:-$START}; END=$(date +%s) |
| 10 | RESULT=${SERVICE_RESULT:-?}; ECODE=${EXIT_CODE:-?}; ESTAT=${EXIT_STATUS:-?} | 19 | RESULT=${SERVICE_RESULT:-?}; ECODE=${EXIT_CODE:-?}; ESTAT=${EXIT_STATUS:-?} |
| 11 | D3D=$(grep -o 'd3d12=[^ ]*' "$LOG" 2>/dev/null | tail -1 | cut -d= -f2) | 20 | D3D=$(grep -o 'd3d12=[^ ]*' "$LOG" 2>/dev/null | tail -1 | cut -d= -f2) |
| 12 | STDOUT=${D3D/d3d12-run-/stdout-run-} | 21 | STDOUT=${D3D/d3d12-run-/stdout-run-} |
| 13 | CAP=/opt/leonida/capture/$AGENT-$N | 22 | CAP=$ROOT/capture/$AGENT-$N |
| 14 | 23 | ||
| 15 | verdict=OK | 24 | verdict=OK |
| 16 | # systemd-oomd can kill the unit's processes without the unit ending in "oom-kill" (boot.sh then exits on its own). | 25 | # systemd-oomd can kill the unit's processes without the unit ending in "oom-kill" (boot.sh then exits on its own). |
| @@ -39,15 +48,15 @@ cnt() { local c=0; [ -f "$D3D" ] && c=$(grep -a -c -E "$@" "$D3D"); echo "${c:-0 | |||
| 39 | grep -a 'present-probe: source frame .* via' "$D3D" 2>/dev/null | tail -1 | sed 's/.*via/source via/' | cut -c1-120 | 48 | grep -a 'present-probe: source frame .* via' "$D3D" 2>/dev/null | tail -1 | sed 's/.*via/source via/' | cut -c1-120 |
| 40 | echo | 49 | echo |
| 41 | echo "-- frames (swapchain; then the title's own backbuffer when LEONIDA_CAPTURE_SOURCE=1)" | 50 | echo "-- frames (swapchain; then the title's own backbuffer when LEONIDA_CAPTURE_SOURCE=1)" |
| 42 | [ -f "$CAP/index.txt" ] && { echo "$(wc -l < "$CAP/index.txt") captured; last: $(tail -1 "$CAP/index.txt" | cut -c1-110)"; awk '{m=$(NF-2)+$(NF-1)+$NF; if (m>0) n++} END{print n+0 " of " NR " frames non-black"}' "$CAP/index.txt"; } || echo "no capture index" | 51 | [ -s "$CAP/index.txt" ] && { echo "$(wc -l < "$CAP/index.txt") captured; last: $(tail -1 "$CAP/index.txt" | cut -c1-110)"; awk '{m=$(NF-2)+$(NF-1)+$NF; if (m>0) n++} END{print n+0 " of " NR " frames non-black"}' "$CAP/index.txt"; } || echo "no capture index" |
| 43 | grep -a 'present-probe: source frame .* mean' "$D3D" 2>/dev/null | tail -2 | sed 's/.*source frame/source frame/' | cut -c1-140 | 52 | grep -a 'present-probe: source frame .* mean' "$D3D" 2>/dev/null | tail -2 | sed 's/.*source frame/source frame/' | cut -c1-140 |
| 44 | echo | 53 | echo |
| 45 | echo "-- resources (2 s samples)" | 54 | echo "-- resources (2 s samples)" |
| 46 | [ -s "$RES" ] && awk -F'\t' 'NR>1{ if(min==""||$2<min)min=$2; if($3>sw)sw=$3; if($4>um)um=$4; if($5>us)us=$5; if($6+0>gm)gm=$6 } END{printf "min RAM available %s MB, max swap used %s MB, game peak %s MB RAM + %s MB swap, VRAM peak %s MB\n",min,sw,um,us,gm}' "$RES" || echo "no samples" | 55 | [ -s "$RES" ] && awk -F'\t' 'NR>1{ c++; if(min==""||$2<min)min=$2; if($3>sw)sw=$3; if($4>um)um=$4; if($5>us)us=$5; if($6+0>gm)gm=$6 } END{ if(c>0) printf "min RAM available %s MB, max swap used %s MB, game peak %s MB RAM + %s MB swap, VRAM peak %s MB\n",min,sw,um,us,gm; else print "no samples" }' "$RES" || echo "no samples" |
| 47 | journalctl --user -u "leonida-boot-$AGENT-$N" --since "@$START" -q --no-pager 2>/dev/null | grep -E 'Consumed|oom|Failed' | sed 's/^.*\]: //' | sort -u | cut -c1-200 | 56 | journalctl --user -u "leonida-boot-$AGENT-$N" --since "@$START" -q --no-pager 2>/dev/null | grep -E 'Consumed|oom|Failed' | sed 's/^.*\]: //' | sort -u | cut -c1-200 |
| 48 | # Compare with other boots instead of guessing: on the console (Series S) path the title itself grows to ~20 GB of | 57 | # Compare with other boots instead of guessing: on the console (Series S) path the title itself grows to ~20 GB of |
| 49 | # RAM+swap by ~130 s (fsdraw-6: 9.9 GB + 11.2 GB), mostly zero pages that zram compresses well. | 58 | # RAM+swap by ~130 s (fsdraw-6: 9.9 GB + 11.2 GB), mostly zero pages that zram compresses well. |
| 50 | [ -s "$RES" ] && awk -F'\t' 'NR>1{t=$4+$5; if(t>m)m=t} END{printf "game peak RAM+swap %.1f GB (console-path baseline ~20 GB at 130 s; oomd kills when zram swap passes 98%%)\n", m/1024}' "$RES" | 59 | [ -s "$RES" ] && awk -F'\t' 'NR>1{ c++; t=$4+$5; if(t>m)m=t } END{ if(c>0) printf "game peak RAM+swap %.1f GB (console-path baseline ~20 GB at 130 s; oomd kills when zram swap passes 98%%)\n", m/1024 }' "$RES" |
| 51 | echo | 60 | echo |
| 52 | echo "-- system events during the boot (OOM, GPU Xid, segfaults)" | 61 | echo "-- system events during the boot (OOM, GPU Xid, segfaults)" |
| 53 | { journalctl --since "@$BOOT" -q --no-pager 2>/dev/null | grep -i -E 'systemd-oomd.*(Marked|killed)|oom-kill|out of memory|NVRM: Xid|segfault|gta6rt|wineserver.*crash' ; } | cut -c1-200 | tail -15 | 62 | { journalctl --since "@$BOOT" -q --no-pager 2>/dev/null | grep -i -E 'systemd-oomd.*(Marked|killed)|oom-kill|out of memory|NVRM: Xid|segfault|gta6rt|wineserver.*crash' ; } | cut -c1-200 | tail -15 |
| @@ -63,13 +72,13 @@ cnt() { local c=0; [ -f "$D3D" ] && c=$(grep -a -c -E "$@" "$D3D"); echo "${c:-0 | |||
| 63 | [ -f "$D3D" ] && tail -n 8 "$D3D" | cut -c1-200 | 72 | [ -f "$D3D" ] && tail -n 8 "$D3D" | cut -c1-200 |
| 64 | } > "$OUT" 2>&1 | 73 | } > "$OUT" 2>&1 |
| 65 | 74 | ||
| 66 | frames=$( [ -f "$CAP/index.txt" ] && awk '{m=$(NF-2)+$(NF-1)+$NF; if (m>0) n++} END{print (n+0) "/" NR}' "$CAP/index.txt" || echo "-") | 75 | frames=$( [ -s "$CAP/index.txt" ] && awk '{m=$(NF-2)+$(NF-1)+$NF; if (m>0) n++} END{print (n+0) "/" NR}' "$CAP/index.txt" || echo "-") |
| 67 | peak=$( [ -s "$RES" ] && awk -F'\t' 'NR>1{if($4>u)u=$4} END{print u+0}' "$RES" || echo "-") | 76 | peak=$( [ -s "$RES" ] && awk -F'\t' 'NR>1{c++; if($4>u)u=$4} END{if(c>0) print u+0; else print "-"}' "$RES" || echo "-") |
| 68 | exec 7>>"$LEDGER"; flock 7 | 77 | exec 7>>"$LEDGER"; if command -v flock >/dev/null 2>&1; then flock 7; fi |
| 69 | # Both the unit's ExecStopPost and the outer wrapper call this; the first one to finish records the boot. | 78 | # Both the unit's ExecStopPost and the outer wrapper call this; the first one to finish records the boot. |
| 70 | grep -q -P "^[^\t]*\t$AGENT\t$N\t" "$LEDGER" 2>/dev/null && exit 0 | 79 | awk -F'\t' -v a="$AGENT" -v n="$N" 'NR>1 && $2==a && $3==n {found=1; exit} END{exit !found}' "$LEDGER" 2>/dev/null && exit 0 |
| 71 | [ -s "$LEDGER" ] || printf 'end\tagent\tn\tresult\tran_s\tpresentx_ok\tpso_failed\tfspx_miss\tnonblack_frames\tpeak_mb\tgta6rt\n' >&7 | 80 | [ -s "$LEDGER" ] || printf 'end\tagent\tn\tresult\tran_s\tpresentx_ok\tpso_failed\tfspx_miss\tnonblack_frames\tpeak_mb\tgta6rt\n' >&7 |
| 72 | printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$(date '+%F %T')" "$AGENT" "$N" "$RESULT" "$((END-BOOT))" \ | 81 | printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$(date '+%F %T')" "$AGENT" "$N" "$RESULT" "$((END-BOOT))" \ |
| 73 | "$(cnt 'XboxPresentation::PresentX ok')" "$(cnt 'CreatePipelineState failed')" "$(cnt 'no translated compute shader for FSPX')" \ | 82 | "$(cnt 'XboxPresentation::PresentX ok')" "$(cnt 'CreatePipelineState failed')" "$(cnt 'no translated compute shader for FSPX')" \ |
| 74 | "$frames" "$peak" "$(sed -n 's/^unit=.*gta6rt=\([^ ]*\).*/\1/p' "$META")" >&7 | 83 | "$frames" "$peak" "$(sed -n 's/^unit=.*gta6rt=\([^ ]*\).*/\1/p' "$META" 2>/dev/null)" >&7 |
| 75 | exit 0 | 84 | exit 0 |
diff --git a/scripts/linux-boot/run-boot.sh b/scripts/linux-boot/run-boot.sh index 523d17795..b23036ccb 100755 --- a/scripts/linux-boot/run-boot.sh +++ b/scripts/linux-boot/run-boot.sh | |||
| @@ -16,10 +16,26 @@ | |||
| 16 | # Outputs (in /opt/leonida/work/<agent>/): boot-<n>.log boot-<n>.resources.tsv boot-<n>.digest.txt | 16 | # Outputs (in /opt/leonida/work/<agent>/): boot-<n>.log boot-<n>.resources.tsv boot-<n>.digest.txt |
| 17 | set -u | 17 | set -u |
| 18 | AGENT=${1:?agent}; N=${2:?n}; EXE=${3:?gta6rt.exe}; SECS=${4:-150}; shift $(( $# >= 4 ? 4 : $# )) | 18 | AGENT=${1:?agent}; N=${2:?n}; EXE=${3:?gta6rt.exe}; SECS=${4:-150}; shift $(( $# >= 4 ? 4 : $# )) |
| 19 | if [ "$AGENT" = "." ] || [ "$AGENT" = ".." ] || ! [[ "$AGENT" =~ ^[A-Za-z0-9._-]+$ ]]; then | ||
| 20 | echo "run-boot: invalid agent: $AGENT" >&2; exit 2 | ||
| 21 | fi | ||
| 22 | if ! [[ "$N" =~ ^[0-9]+$ ]]; then | ||
| 23 | echo "run-boot: invalid boot number: $N" >&2; exit 2 | ||
| 24 | fi | ||
| 25 | if ! [[ "$SECS" =~ ^[1-9][0-9]*$ ]]; then | ||
| 26 | echo "run-boot: invalid seconds (expected positive integer): $SECS" >&2; exit 2 | ||
| 27 | fi | ||
| 28 | for kv in "$@"; do | ||
| 29 | if ! [[ "$kv" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then | ||
| 30 | echo "run-boot: invalid environment override (expected KEY=VALUE): $kv" >&2; exit 2 | ||
| 31 | fi | ||
| 32 | done | ||
| 19 | [ -f "$EXE" ] || { echo "run-boot: no such gta6rt: $EXE" >&2; exit 2; } | 33 | [ -f "$EXE" ] || { echo "run-boot: no such gta6rt: $EXE" >&2; exit 2; } |
| 20 | # gta6rt loads gta6rt_tls_host.dll from its own folder; without it the title gets no module TLS index and exits at once. | 34 | # gta6rt loads gta6rt_tls_host.dll from its own folder; without it the title gets no module TLS index and exits at once. |
| 21 | [ -f "$(dirname "$EXE")/gta6rt_tls_host.dll" ] || { echo "run-boot: $(dirname "$EXE")/gta6rt_tls_host.dll is missing: copy it next to the exe (build it with the gta6rt target) or boot the exe from its build folder" >&2; exit 2; } | 35 | [ -f "$(dirname "$EXE")/gta6rt_tls_host.dll" ] || { echo "run-boot: $(dirname "$EXE")/gta6rt_tls_host.dll is missing: copy it next to the exe (build it with the gta6rt target) or boot the exe from its build folder" >&2; exit 2; } |
| 22 | WORK=/opt/leonida/work/$AGENT; mkdir -p "$WORK" | 36 | ROOT=${LEONIDA_BOOT_ROOT:-/opt/leonida} |
| 37 | WIN_ROOT=${ROOT#/}; WIN_ROOT="Z:\\${WIN_ROOT//\//\\}" | ||
| 38 | WORK=$ROOT/work/$AGENT; mkdir -p "$WORK" | ||
| 23 | UNIT=leonida-boot-$AGENT-$N | 39 | UNIT=leonida-boot-$AGENT-$N |
| 24 | if systemctl --user is-active --quiet "$UNIT"; then echo "run-boot: $UNIT is already running" >&2; exit 2; fi | 40 | if systemctl --user is-active --quiet "$UNIT"; then echo "run-boot: $UNIT is already running" >&2; exit 2; fi |
| 25 | 41 | ||
| @@ -28,10 +44,10 @@ declare -A ENV=( | |||
| 28 | [WINEPREFIX]="$HOME/.local/share/leonida-proton/pfx" | 44 | [WINEPREFIX]="$HOME/.local/share/leonida-proton/pfx" |
| 29 | [WINEDEBUG]="-all,err+all" | 45 | [WINEDEBUG]="-all,err+all" |
| 30 | [GTA6RT]="$EXE" | 46 | [GTA6RT]="$EXE" |
| 31 | [LEONIDA_CAPTURE]="Z:\\opt\\leonida\\capture\\$AGENT-$N" | 47 | [LEONIDA_CAPTURE]="$WIN_ROOT\\capture\\$AGENT-$N" |
| 32 | [LEONIDA_CAPTURE_EVERY]=4800 [LEONIDA_CAPTURE_LIMIT]=24 [LEONIDA_CAPTURE_SOURCE]=1 | 48 | [LEONIDA_CAPTURE_EVERY]=4800 [LEONIDA_CAPTURE_LIMIT]=24 [LEONIDA_CAPTURE_SOURCE]=1 |
| 33 | [LEONIDA_DEVICE_TYPE]=6 | 49 | [LEONIDA_DEVICE_TYPE]=6 |
| 34 | [LEONIDA_PIPELINE_CACHE_DIR]="Z:\\opt\\leonida\\cache\\pipelines" | 50 | [LEONIDA_PIPELINE_CACHE_DIR]="$WIN_ROOT\\cache\\pipelines" |
| 35 | ) | 51 | ) |
| 36 | for kv in "$@"; do ENV[${kv%%=*}]="${kv#*=}"; done | 52 | for kv in "$@"; do ENV[${kv%%=*}]="${kv#*=}"; done |
| 37 | args=() | 53 | args=() |
| @@ -39,16 +55,16 @@ for k in "${!ENV[@]}"; do args+=(--setenv="$k=${ENV[$k]}"); done | |||
| 39 | 55 | ||
| 40 | echo "run-boot: $UNIT starting (queues for memory headroom, the GPU lock and the build lock)" | 56 | echo "run-boot: $UNIT starting (queues for memory headroom, the GPU lock and the build lock)" |
| 41 | rm -f "$WORK/boot-$N.digest.txt" | 57 | rm -f "$WORK/boot-$N.digest.txt" |
| 42 | systemd-run --user --collect --quiet --unit="$UNIT" -p WorkingDirectory=/opt/leonida \ | 58 | systemd-run --user --collect --quiet --unit="$UNIT" -p WorkingDirectory="$ROOT" \ |
| 43 | --setenv=BOOT_AGENT="$AGENT" --setenv=BOOT_N="$N" --setenv=BOOT_SECS="$SECS" "${args[@]}" \ | 59 | --setenv=BOOT_AGENT="$AGENT" --setenv=BOOT_N="$N" --setenv=BOOT_SECS="$SECS" "${args[@]}" \ |
| 44 | /opt/leonida/boot-inner.sh || { echo "run-boot: could not start $UNIT" >&2; exit 2; } | 60 | "$ROOT/boot-inner.sh" || { echo "run-boot: could not start $UNIT" >&2; exit 2; } |
| 45 | # The digest is written by a separate, tiny watcher unit: it survives systemd-oomd killing the boot's cgroup and the | 61 | # The digest is written by a separate, tiny watcher unit: it survives systemd-oomd killing the boot's cgroup and the |
| 46 | # caller's shell going away. It waits for the boot unit to end, reads its result from the journal and writes the digest. | 62 | # caller's shell going away. It waits for the boot unit to end, reads its result from the journal and writes the digest. |
| 47 | systemd-run --user --collect --quiet --unit="leonida-digest-$AGENT-$N" -p MemoryMax=256M \ | 63 | systemd-run --user --collect --quiet --unit="leonida-digest-$AGENT-$N" -p MemoryMax=256M \ |
| 48 | /bin/bash -c 'while systemctl --user is-active --quiet "$0" || systemctl --user is-activating --quiet "$0"; do sleep 3; done | 64 | /bin/bash -c 'while systemctl --user is-active --quiet "$0" || systemctl --user is-activating --quiet "$0"; do sleep 3; done |
| 49 | START=$(sed -n "s/^start_epoch=//p" "/opt/leonida/work/$1/boot-$2.meta" 2>/dev/null) | 65 | START=$(sed -n "s/^start_epoch=//p" "$3/work/$1/boot-$2.meta" 2>/dev/null) |
| 50 | RES=$(journalctl --user -u "$0" ${START:+--since "@$START"} -q --no-pager 2>/dev/null | grep -o "Failed with result .[a-z-]*." | tail -1 | tr -d "\x27" | sed "s/Failed with result //") | 66 | RES=$(journalctl --user -u "$0" ${START:+--since "@$START"} -q --no-pager 2>/dev/null | grep -o "Failed with result .[a-z-]*." | tail -1 | tr -d "\x27" | sed "s/Failed with result //") |
| 51 | SERVICE_RESULT=${RES:-success} exec /opt/leonida/boot-digest.sh "$1" "$2"' "$UNIT" "$AGENT" "$N" | 67 | SERVICE_RESULT=${RES:-success} exec "$3/boot-digest.sh" "$1" "$2"' "$UNIT" "$AGENT" "$N" "$ROOT" |
| 52 | # Block until the digest exists (so a caller that waits on this script gets the result), then print it. | 68 | # Block until the digest exists (so a caller that waits on this script gets the result), then print it. |
| 53 | while [ ! -s "$WORK/boot-$N.digest.txt" ] || systemctl --user is-active --quiet "leonida-digest-$AGENT-$N"; do sleep 5; done | 69 | while [ ! -s "$WORK/boot-$N.digest.txt" ] || systemctl --user is-active --quiet "leonida-digest-$AGENT-$N"; do sleep 5; done |
| 54 | echo "run-boot: $UNIT finished. Digest:" | 70 | echo "run-boot: $UNIT finished. Digest:" |