interplane.Get Omega
Read in your language

omega-gpu-driver

aien-dev/omega-gpu-driver

Brought by drakestapleton

branch main Branches Tags History

omega-gpu-driver / window / d2-pre.sh

201 lines · 10422 bytes Raw Blame History
1#!/bin/sh
2# window/d2-pre.sh -- W0 and W1 of the OwnDriver window. Status: W0 EXERCISED read-only on the host; W1 NOT_RUN.
3# no argument: W0, read-only checks, safe while the 35B runs. Changes nothing except its own log.
4# --go W0: identity only, previously running service restored, two reboots at most.
5# --go [D2|D3]: W1, needs $APPROVAL. Stops the 35B on 18091, writes the one-boot nvidia hold-back file and the
6# boot unit, sets state "run D2" (default) or "run D3", reboots. The unit then runs W2.. by itself
7# (window/d2-run.sh), and after the second reboot R (window/d2-restore.sh).
8# Abort before the reboot: the window files are removed and window/d2-restore.sh restarts the 35B.
9set -eu
10R=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)
11. "$R/window/common.sh"
12LOG=$LOGDIR/d2-$(date -u +%Y%m%dT%H%M%SZ)-pre.log
13GO=${1:-}
14STAGE=${2:-D2}
15case $STAGE in W0 | D2 | D3) ;; *) die "stage must be W0, D2 or D3" ;; esac
16if [ "$STAGE" = W0 ]; then
17 [ ! -e "$BUILD/ogd" ] && [ ! -L "$BUILD/ogd" ] || die "identity-only checkout contains build/ogd"
18 [ "${RESTORE_PATH:-reboot}" = reboot ] || die "identity-only window requires reboot restore"
19fi
20
21log "W0 checks (repo $(git -C "$R" rev-parse HEAD 2>/dev/null || echo no-git), dirty files: $(git -C "$R" status --porcelain 2>/dev/null | wc -l))"
22echo "$FW_SHA $FW" | sha256sum -c --quiet || die "firmware sha256 mismatch"
23log "firmware $FW sha256 ok"
24for f in $HASHED; do
25 if [ -e "$R/$f" ]; then log "sha256 $(sha256sum "$R/$f" | cut -c1-64) $f"; else log "missing $f"; fi
26done
27omc_pin=$(sed -n 's/^OMC_SHA256=//p' "$R/tools/omc.pin")
28[ "$(sha256sum "$R/build/omc" | cut -c1-64)" = "$omc_pin" ] || die "build/omc does not match tools/omc.pin"
29[ -x "$BUILD/w0" ] || die "build/w0 missing: run sh build.sh"
30if [ -e "$BUILD/fw/fw.manifest" ]; then
31 # fwprep writes fw.manifest last, with one 'out NAME bytes=N sha256=H' line per output file.
32 fwsha=$(sha256sum "$FW" | cut -c1-64)
33 grep -q "^fw_file bytes=[0-9]* sha256=$fwsha\$" "$BUILD/fw/fw.manifest" ||
34 die "build/fw/fw.manifest was made from a different firmware file: rerun sh build.sh"
35 sed -n 's/^out \([a-z.]*\) bytes=[0-9]* sha256=\([0-9a-f]\{64\}\)$/\2 \1/p' "$BUILD/fw/fw.manifest" > "$LOGDIR/fw.check"
36 [ "$(wc -l < "$LOGDIR/fw.check")" = 5 ] && grep -qx end "$BUILD/fw/fw.manifest" ||
37 die "build/fw/fw.manifest is incomplete: rerun sh build.sh"
38 (cd "$BUILD/fw" && sha256sum -c --quiet "$LOGDIR/fw.check") ||
39 die "build/fw files do not match build/fw/fw.manifest: rerun sh build.sh"
40 rm -f "$LOGDIR/fw.check"
41 log "build/fw matches fw.manifest"
42fi
43if [ -x "$BUILD/ogd" ]; then
44 log "ogd modes: $("$BUILD/ogd" modes | tr '\n' ' ')"
45fi
46[ "$(cat "$DEV/vendor"):$(cat "$DEV/device")" = 0x10de:0x2e12 ] || die "$BDF is not 10de:2e12"
47[ "$(basename "$(readlink "$DEV/iommu_group")")" = "$GRP" ] || die "iommu group is not $GRP"
48[ "$(ls "$IOMMU/$GRP/devices")" = "$BDF" ] || die "group $GRP is not $BDF alone"
49log "device $(cat "$DEV/vendor"):$(cat "$DEV/device") driver=$(drv) reset_method=$(cat "$DEV/reset_method") power=$(cat "$DEV/power/control")/$(cat "$DEV/power/runtime_status")"
50log "modules: $(lsmod | awk '/^nvidia/{printf "%s(%s) ", $1, $3}')"
51log "cmdline: $(cat /proc/cmdline)"
52log "sysctl: kernel.panic=$(sysctl -n kernel.panic) kernel.panic_on_oops=$(sysctl -n kernel.panic_on_oops)"
53for f in "$HOLD" "$UNIT" "$STATE" "$IDENTITY" "$W0SUM"; do
54 [ ! -e "$f" ] || die "$f already exists (an earlier window did not finish): run window/d2-restore.sh --go"
55done
56# No window is open (checked above), so the current privilege state is the baseline R4 must return to.
57if [ -e "$PRIVBASE" ]; then
58 privstate | diff "$PRIVBASE" - | sed 's/^/baseline change since last W0: /' | tee -a "$LOG" || true
59fi
60privstate > "$PRIVBASE"
61log "privilege baseline: $(wc -l < "$PRIVBASE") entries in $PRIVBASE (sha256 $(sha256sum "$PRIVBASE" | cut -c1-64))"
62privcheck || die "privilege state not clean before the window (see PRIV lines)"
63log "privilege state clean: /dev/vfio groups root 0600, no vfio udev/memlock file, user not in group vfio, no driver_override, no window files"
64# nvgrace-gpu-vfio-pci (the upstream variant for Grace C2C GPUs) must not claim this device: plain vfio-pci.
65if modinfo nvgrace-gpu-vfio-pci 2>/dev/null | grep -qi 'd00002E12'; then
66 die "nvgrace-gpu-vfio-pci now lists 2E12: re-decide variant vs plain vfio-pci before binding"
67fi
68log "nvgrace-gpu-vfio-pci aliases ($(modinfo nvgrace-gpu-vfio-pci 2>/dev/null | grep -c '^alias')) do not list 10de:2e12: plain vfio-pci"
69listing=$(mktemp "$LOGDIR/initramfs.XXXXXX")
70if ! lsinitramfs "/boot/initrd.img-$(uname -r)" > "$listing" || [ ! -s "$listing" ]; then
71 rm -f "$listing"
72 die "could not inspect initramfs"
73fi
74if grep -Eq '/nvidia(_uvm|_modeset|_drm)?\.ko' "$listing"; then
75 rm -f "$listing"
76 die "initramfs carries nvidia.ko: a modprobe.d hold-back would not be enough"
77fi
78rm -f "$listing"
79log "initramfs has no nvidia/nvidia_uvm/nvidia_modeset/nvidia_drm"
80modinfo -n vfio-pci >/dev/null 2>&1 || die "vfio-pci module not available"
81p=$(live_pid)
82if [ -n "$p" ] && [ "$STAGE" = W0 ]; then
83 # An unrelated service can have sensitive command-line options. Compare in memory.
84 [ "$(snap35b "$p")" = "$(snap_expected)" ] || die "port 18091 belongs to another launch recipe. Wait for its owner"
85 snap_expected > "$SNAP"
86 log "35B pid $p matches the restore recipe"
87elif [ -n "$p" ]; then
88 snap35b "$p" > "$SNAP.now"
89 snap_expected > "$SNAP.want"
90 if cmp -s "$SNAP.now" "$SNAP.want"; then
91 mv "$SNAP.now" "$SNAP"; rm -f "$SNAP.want"
92 log "35B pid $p: launch record equals the restore recipe ($SNAP)"
93 else
94 diff "$SNAP.want" "$SNAP.now" | tee -a "$LOG" || true
95 die "the live 35B was not started the way d2-restore.sh restarts it: update snap_expected and R3 first"
96 fi
97else
98 log "35B not running"
99fi
100log "GPU device users: $(sudo -n fuser "$ROOT"/dev/nvidia* 2>/dev/null | tr -s ' ' | tr ' ' '\n' | sort -u | while read -r q; do [ -n "$q" ] && printf '%s(%s) ' "$q" "$(cat "$PROC/$q/comm" 2>/dev/null)"; done)"
101log "chip lock (read from the kernel lock table, not taken): $(spark-window locks gb10 2>&1 | head -2 | tr '\n' ' ')"
102
103if [ "$GO" != --go ]; then
104 log "W0 PASS (checks only; nothing changed)"
105 exit 0
106fi
107
108# W1 --------------------------------------------------------------------------
109# Only substitute checkout paths that are literal in both sed and systemd.
110case $R in *[!a-zA-Z0-9/._-]* | /) die "unsupported checkout path in temporary boot unit" ;; esac
111[ -r "$R/window/d2-boot.sh" ] || die "boot helper is missing"
112rendered=$(sed "s|^WorkingDirectory=@CHECKOUT@$|WorkingDirectory=$R|" "$R/window/aien-owndriver-window.service")
113printf '%s\n' "$rendered" | grep -Fxq "WorkingDirectory=$R" || die "could not render boot unit"
114need_approval
115# build/ogd is optional: without it the window runs W2, W3 (build/w0), W7 and R, and logs the driver steps NOT_RUN.
116if [ "$STAGE" != W0 ]; then
117[ "$STAGE" = D2 ] || has_mode exec || die "D3 needs build/ogd with a successful exact 'exec' mode"
118fi
119case ${RESTORE_PATH:-reboot} in
120reboot) rm -f "$RPATH" ;;
121rebind) echo rebind > "$RPATH" ;;
122*) die "RESTORE_PATH must be reboot or rebind" ;;
123esac
124log "restore path after W7: ${RESTORE_PATH:-reboot}"
125if [ "$STAGE" = D3 ]; then
126 [ -n "${D3_CMD:-}" ] || die "D3 needs D3_CMD: the CUDApp gate command line (window/README.md, D3)"
127 printf '%s\n' "$D3_CMD" > "$LOGDIR/d3-cmd"
128 log "D3 command: $D3_CMD"
129fi
130# The chip lock: no other GPU job may be running; it is held until the reboot ends this process.
131exec 9>>"$GB10_LOCK"
132flock -n 9 || die "$GB10_LOCK is held by another GPU job: wait for it"
133if [ "$STAGE" = W0 ]; then
134 p=$(live_pid)
135 if [ -n "$p" ]; then
136 [ "$(snap35b "$p")" = "$(snap_expected)" ] || die "service changed before the identity window"
137 before=up
138 else
139 listeners=$(ss -Hltn 'sport = :18091') || die "could not inspect port 18091"
140 [ -z "$listeners" ] || die "unrecognized listener on port 18091"
141 before=down
142 fi
143 apps=$(nvidia-smi --query-compute-apps=pid --format=csv,noheader) || die "could not inspect GPU clients"
144 [ -z "$apps" ] || [ "$apps" = "$p" ] || die "another GPU client is active"
145 sha256sum "$BUILD/w0" > "$W0SUM" || die "could not seal identity executable"
146 echo "$before" > "$IDENTITY"
147fi
148abort_w1() {
149 rc=$?
150 trap - EXIT
151 [ "$rc" -eq 0 ] && return
152 sudo -n systemctl disable aien-owndriver-window.service >/dev/null 2>&1 || true
153 sudo -n rm -f "$HOLD" "$UNIT"
154 sudo -n systemctl daemon-reload || true
155 if [ "$STAGE" = W0 ]; then echo "restore W0" > "$STATE"; else echo restore > "$STATE"; fi
156 log "W1 aborted before reboot (rc=$rc); window files removed; restoring the 35B"
157 sh "$R/window/d2-restore.sh" --go || true
158}
159trap 'abort_w1' EXIT
160
161touch "$LIVE_MARKER"
162board "WINDOW OPEN $STAGE (own driver): stopping 18091, reboot with nvidia held back; every agent session on the Spark ends at the reboot. Log $LOG"
163if [ "$STAGE" = W0 ]; then
164 [ "$(live_pid)" = "$p" ] || die "service changed while arming the identity window"
165else
166 p=$(live_pid)
167fi
168if [ -n "$p" ]; then
169 log "TERM 35B pid $p"
170 kill -TERM "$p"
171fi
172i=0
173while [ -n "$(live_pid)" ] || [ -n "$(nvidia-smi --query-compute-apps=pid --format=csv,noheader)" ]; do
174 i=$((i + 1)); [ "$i" -le 60 ] || die "the 35B or another compute app is still on the GPU after 60 s"
175 sleep 1
176done
177log "no compute apps on the GPU"
178printf 'kernel.panic=%s\nkernel.panic_on_oops=%s\n' "$(sysctl -n kernel.panic)" "$(sysctl -n kernel.panic_on_oops)" > "$SAVED"
179sudo -n tee "$HOLD" >/dev/null <<'EOF'
180# aien OwnDriver window: hold the nvidia modules back for ONE boot.
181# window/d2-run.sh deletes this file first thing after the reboot; if it is still here, delete it and reboot.
182blacklist nvidia
183blacklist nvidia_uvm
184blacklist nvidia_modeset
185blacklist nvidia_drm
186install nvidia /bin/false
187install nvidia_uvm /bin/false
188install nvidia_modeset /bin/false
189install nvidia_drm /bin/false
190EOF
191printf '%s\n' "$rendered" | sudo -n tee "$UNIT" >/dev/null
192sudo -n chmod 0644 "$UNIT"
193sudo -n systemctl daemon-reload
194sudo -n systemctl enable aien-owndriver-window.service
195echo "run $STAGE" > "$STATE"
196log "wrote $HOLD, installed and enabled $UNIT, state 'run $STAGE'"
197board "$STAGE W1: rebooting with nvidia held back; the boot unit runs window/d2-run.sh, then R after the next reboot"
198sync
199log "reboot"
200sudo -n systemctl reboot
201trap - EXIT