omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / window / common.sh
| 1 | # window/common.sh -- shared settings for the OwnDriver window scripts. Sourced, never run. |
| 2 | # Status: NOT_RUN on the GB10; window/README.md has the procedure. tests/window_sim.sh runs these exact |
| 3 | # scripts against a simulated machine under /tmp (OWNDRIVER_WINDOW_ROOT, below). |
| 4 | BDF=000f:01:00.0 |
| 5 | GRP=20 |
| 6 | FW=/lib/firmware/nvidia/580.173.02/gsp_ga10x.bin |
| 7 | FW_SHA=32cb7cf3e77f97e92f6e92135939ccb3ba382748144569c0dfccf3338d43d963 |
| 8 | |
| 9 | # ROOT is empty on the Spark. tests/window_sim.sh sets it to a simulated machine: every system path below |
| 10 | # moves under it, and the commands that act on the machine must be that simulator's shims (checked here, |
| 11 | # so a half-set-up simulation can never reach the real sudo, systemctl or the live 35B). |
| 12 | ROOT=${OWNDRIVER_WINDOW_ROOT:-} |
| 13 | if [ -n "$ROOT" ]; then |
| 14 | case $ROOT in /tmp/*) ;; *) echo "OWNDRIVER_WINDOW_ROOT must be under /tmp" >&2; exit 2 ;; esac |
| 15 | [ -e "$ROOT/.owndriver-window-sim" ] || { echo "$ROOT is not a window simulator root" >&2; exit 2; } |
| 16 | for c in sudo systemctl modprobe sysctl lsmod nvidia-smi pgrep curl tee stat fuser modinfo lsinitramfs spark-window ss; do |
| 17 | [ "$(command -v "$c")" = "$ROOT/bin/$c" ] || { echo "simulator: $c is not $ROOT/bin/$c" >&2; exit 2; } |
| 18 | done |
| 19 | HOMEDIR=$ROOT/home |
| 20 | Q36=$ROOT/q36 |
| 21 | Q36_KEY=$ROOT/q36/KEY |
| 22 | BUILD=$ROOT/build |
| 23 | else |
| 24 | HOMEDIR=/home/drakestapleton |
| 25 | Q36=/home/drakestapleton/.local/state/aien-q36 |
| 26 | Q36_KEY=/home/drakestapleton/.local/state/aien-coordinator/KEY |
| 27 | BUILD=$R/build |
| 28 | fi |
| 29 | Q36_BIN=bin-19173f9e |
| 30 | PCI=$ROOT/sys/bus/pci |
| 31 | DEV=$PCI/devices/$BDF |
| 32 | IOMMU=$ROOT/sys/kernel/iommu_groups |
| 33 | VFIODEV=$ROOT/dev/vfio |
| 34 | PROC=$ROOT/proc |
| 35 | ETC=$ROOT/etc |
| 36 | # Window files. Each exists only between W1 and R; privcheck fails if one persists. |
| 37 | HOLD=$ETC/modprobe.d/zz-aien-owndriver-window.conf |
| 38 | UNIT=$ETC/systemd/system/aien-owndriver-window.service |
| 39 | LIVE_MARKER=$ROOT/tmp/aien-q36-live-down-for-tests |
| 40 | GB10_LOCK=$ROOT/tmp/aien-gb10.lock |
| 41 | BOARD=$HOMEDIR/handoffs/BOARD.md |
| 42 | LOGDIR=$HOMEDIR/handoffs/OwnDriver/window |
| 43 | # /tmp is emptied at every boot (tmpfiles "D /tmp"), so the approval and the state live with the logs. |
| 44 | APPROVAL=$LOGDIR/APPROVED |
| 45 | # STATE holds one line between reboots: "run D2|D3" (W2.. pending), "restore" (R pending). SAVED keeps the |
| 46 | # sysctl values W2 changes; SNAP is the live 35B launch record W0 checks the restore recipe against; |
| 47 | # RPATH holds "rebind" if W1 was asked for RESTORE_PATH=rebind; PRIVBASE is the privilege baseline (W0). |
| 48 | STATE=$LOGDIR/state |
| 49 | IDENTITY=$LOGDIR/identity-service |
| 50 | W0SUM=$LOGDIR/identity-executable.sha256 |
| 51 | SAVED=$LOGDIR/saved-sysctl |
| 52 | SNAP=$LOGDIR/live-35b.snapshot |
| 53 | RPATH=$LOGDIR/restore-path |
| 54 | PRIVBASE=$LOGDIR/priv-baseline |
| 55 | mkdir -p "$LOGDIR" |
| 56 | |
| 57 | utc() { date -u +%Y-%m-%dT%H:%M:%SZ; } |
| 58 | log() { echo "$(utc) $*" | tee -a "$LOG"; } |
| 59 | board() { echo "$(utc) OwnDriver: $*" >> "$BOARD"; } |
| 60 | die() { log "ABORT: $*"; exit 1; } |
| 61 | # A character device on the Spark; the simulator cannot create device nodes and uses plain files. |
| 62 | ischr() { if [ -n "$ROOT" ]; then [ -e "$1" ]; else [ -c "$1" ]; fi; } |
| 63 | |
| 64 | # The approval file (Main writes it when Drake opens the window) must carry these lines: |
| 65 | # window: <Drake's words / BOARD line ref> |
| 66 | # reset: approved -- vfio-pci FLRs the GPU on open and bus-resets (SBR) root port 000f:00:00.0 on release |
| 67 | # reboot: approved -- W1 and W7 reboot the Spark; every agent session on it ends |
| 68 | need_approval() { |
| 69 | [ -s "$APPROVAL" ] || die "no $APPROVAL (Main writes it when Drake opens the window)" |
| 70 | grep -q '^window: ..*' "$APPROVAL" || die "$APPROVAL lacks a 'window: <Drake's words>' line" |
| 71 | grep -qx 'reset: approved' "$APPROVAL" || die "$APPROVAL lacks 'reset: approved' (VFIO bind/release resets the GPU)" |
| 72 | grep -qx 'reboot: approved' "$APPROVAL" || die "$APPROVAL lacks 'reboot: approved' (the window reboots the Spark twice)" |
| 73 | if [ "$STAGE" = W0 ] || grep -q '^stage:' "$APPROVAL"; then |
| 74 | [ "$(sed -n 's/^stage: //p' "$APPROVAL")" = "$STAGE" ] || die "approval does not name this stage" |
| 75 | fi |
| 76 | log "approval: $(grep '^window: ' "$APPROVAL" | head -1)" |
| 77 | } |
| 78 | |
| 79 | # Every file that can grant device, root or memlock rights, with its sha256, plus the user's groups. |
| 80 | privstate() { |
| 81 | for d in "$ETC/sudoers.d" "$ETC/udev/rules.d" "$ETC/security/limits.d" "$ETC/modprobe.d"; do |
| 82 | for f in "$d"/*; do |
| 83 | [ -e "$f" ] && echo "$(sudo -n sha256sum "$f" | cut -c1-64) $(stat -c '%U:%G:%a' "$f") $f" |
| 84 | done |
| 85 | done |
| 86 | echo "groups $(id -nG "$(id -un)" | tr ' ' '\n' | sort | tr '\n' ' ')" |
| 87 | } |
| 88 | |
| 89 | # Privilege state outside the window. No udev rule, group, sudoers line, memlock limit or /dev/vfio |
| 90 | # permission change is ever made: the window programs run as root through sudo -n instead. Fails closed |
| 91 | # if the privilege files differ from the W0 baseline, or a window file (HOLD, UNIT) or a driver_override persists. |
| 92 | privcheck() { |
| 93 | bad=0 |
| 94 | for f in "$VFIODEV"/[0-9]*; do |
| 95 | [ -e "$f" ] || continue |
| 96 | m=$(stat -c '%U:%G:%a' "$f") |
| 97 | [ "$m" = root:root:600 ] || { log "PRIV: $f is $m, expected root:root:600"; bad=1; } |
| 98 | done |
| 99 | # Pre-existing files (e.g. /etc/sudoers.d/aien-local-ci, 2026-10-19, not this lane's) are compared against the |
| 100 | # baseline W0 records before the window opens: anything added, removed or changed since then fails. |
| 101 | privstate > "$PRIVBASE.now" |
| 102 | if [ -e "$PRIVBASE" ] && ! cmp -s "$PRIVBASE" "$PRIVBASE.now"; then |
| 103 | diff "$PRIVBASE" "$PRIVBASE.now" | sed 's/^/PRIV: /' | tee -a "${LOG:-/dev/null}" |
| 104 | bad=1 |
| 105 | fi |
| 106 | for f in "$ETC"/udev/rules.d/*vfio* "$ETC"/security/limits.d/*memlock* "$HOLD" "$UNIT"; do |
| 107 | [ -e "$f" ] && { log "PRIV: $f exists"; bad=1; } |
| 108 | done |
| 109 | id -nG | tr ' ' '\n' | grep -qx vfio && { log "PRIV: $(id -un) is in group vfio"; bad=1; } |
| 110 | o=$(cat "$DEV/driver_override" 2>/dev/null || true) |
| 111 | [ -z "$o" ] || [ "$o" = "(null)" ] || { log "PRIV: driver_override=$o"; bad=1; } |
| 112 | return $bad |
| 113 | } |
| 114 | |
| 115 | # The 35B serving process on 18091, if any (empty when down). |
| 116 | live_pid() { pgrep -f -- '--port 18091' | head -1 || true; } |
| 117 | |
| 118 | # The restore recipe (d2-restore.sh R3) must equal how the live server was started. Prints the record: |
| 119 | # argv, cwd and the env names it needs; the coordinator key is compared, never written. |
| 120 | snap35b() { |
| 121 | p=$1 |
| 122 | echo "argv $(tr '\0' ' ' < "$PROC/$p/cmdline")" |
| 123 | echo "cwd $(readlink "$PROC/$p/cwd")" |
| 124 | tr '\0' '\n' < "$PROC/$p/environ" | grep -E '^(Q36_BATCH_BIN|AIEN_Q36_STROKE|AIEN_SERVE_GPU_LOCK)=' | sort | sed 's/^/env /' |
| 125 | k=$(tr '\0' '\n' < "$PROC/$p/environ" | sed -n 's/^AIEN_COORD_KEY=//p') |
| 126 | if [ -n "$k" ] && [ "$k" = "$(cat "$Q36_KEY")" ]; then echo "env AIEN_COORD_KEY=<equals KEY>"; else echo "env AIEN_COORD_KEY=<MISMATCH>"; fi |
| 127 | } |
| 128 | snap_expected() { |
| 129 | echo "argv ./aien-serve-http --engine omega --model-dir /home/drakestapleton/models/qwen3.6-35b-a3b-fp8-95a723d0 --port 18091 --max-context 49152 --max-tokens-cap 16384 --thinking off --batch-slots 8 " |
| 130 | echo "cwd $Q36/$Q36_BIN" |
| 131 | echo "env AIEN_Q36_STROKE=1" |
| 132 | echo "env AIEN_SERVE_GPU_LOCK=/tmp/aien-serve-18091.lock" |
| 133 | echo "env Q36_BATCH_BIN=$Q36_BIN" |
| 134 | echo "env AIEN_COORD_KEY=<equals KEY>" |
| 135 | } |
| 136 | |
| 137 | # Runtime PM on and active before any BAR0 access (arm64: a read of a D3 device is an external abort). |
| 138 | pm_on() { |
| 139 | echo on | sudo -n tee "$DEV/power/control" >/dev/null |
| 140 | s=$(cat "$DEV/power/runtime_status") |
| 141 | [ "$s" = active ] || die "runtime_status=$s, not active" |
| 142 | log "power/control=on runtime_status=active" |
| 143 | } |
| 144 | pm_auto() { echo auto | sudo -n tee "$DEV/power/control" >/dev/null || true; } |
| 145 | |
| 146 | drv() { basename "$(readlink "$DEV/driver" 2>/dev/null || echo none)"; } |
| 147 | nv_modules() { lsmod | grep -cE '^nvidia(_uvm|_modeset|_drm)? ' || true; } |
| 148 | |
| 149 | # Capability declarations must succeed, not just print a matching line before failing. |
| 150 | has_mode() { |
| 151 | [ -x "$BUILD/ogd" ] || return 1 |
| 152 | modes=$("$BUILD/ogd" modes 2>/dev/null) || return 1 |
| 153 | printf '%s\n' "$modes" | grep -Fxq "$1" |
| 154 | } |
| 155 | |
| 156 | # Files whose sha256 W0 records and WINDOW-READY posts. |
| 157 | HASHED="window/common.sh window/d2-pre.sh window/d2-boot.sh window/d2-run.sh window/d2-restore.sh window/aien-owndriver-window.service tools/omc.pin build/omc build/w0 build/ogd build/fw/fw.manifest" |