Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/tools/aien-local-ci/aien-run-exec # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / tools / aien-local-ci / aien-run-exec` 251 lines ยท 14426 bytes Raw Blame History 1 #!/bin/bash 2 # aien-run-exec: the C8 exec contract of the sovereign local pipeline (lane INFRA). 3 # 4 # aien-run-exec --cwd DIR --timeout SECS --profile host|qemu|gb10 [--env K=V]... -- PROGRAM [ARGS...] 5 # 6 # Runs PROGRAM as the unprivileged system user `aien-run` with: 7 # * a cleared environment: only PATH, HOME, CARGO_HOME, RUSTUP_HOME and the explicit --env pairs survive; 8 # * a private mount namespace in which the whole sandbox home /var/lib/aien-run is READ-ONLY except 9 # (a) the one worktree that contains --cwd and (b) the one target dir named by --env CARGO_TARGET_DIR. 10 # So a check can never plant files that a later check (another PR, or a `main` qualification) would 11 # pick up: no ~/.cargo/config.toml, no swapped registry or cargo binary, no edits to another worktree, 12 # no writes into the other trust class's target dir; 13 # * private, empty /var/tmp, /var/crash, /var/lib/BrlAPI for every profile, and private /tmp, /dev/shm, /run/lock, 14 # /run/screen plus fresh IPC and PID namespaces and a private /proc for host|qemu (gb10 keeps host PID/proc, 15 # /tmp, /dev/shm and IPC for the chip reservation's ancestor-flock proof); 16 # * a fresh network namespace for host|qemu: only its own loopback `lo`, brought up so tests can talk to a 17 # server they start on 127.0.0.1 (the namespace has no other interface, so nothing outside it is reachable, 18 # not even the host's own 127.0.0.1 services; no DNS); 19 # * a root-held shared execution lock for host|qemu, exclusive for gb10; gb10 cannot overlap a check 20 # whose host PID/proc view it would expose. The lock remains held until the scope cleanup finishes; 21 # * a transient systemd scope under aien-run.slice with cgroup caps (memory, pids, cpu) and a 22 # device policy (host|qemu: standard pseudo devices only, qemu adds /dev/kvm; gb10: all devices); 23 # * the operator's model directory (~/models of the sudo caller) mounted READ-ONLY at /var/lib/aien-run/models 24 # when both exist (install.sh makes the empty root-owned mount point), so model gates such as PREFILL-E2E-2 25 # find their checkpoint at $HOME/models without any write access to it; without it the gate reports 26 # NOT_RUN model_missing; 27 # * rlimits (no core dumps, bounded open files); 28 # * `timeout --signal=TERM --kill-after=10 SECS` for host|qemu (exit 124 on expiry); 29 # * NO timeout for gb10: chip runs are never killed; the engine's advisory timeout only marks the receipt. 30 # stdout/stderr pass through. The exit code of PROGRAM passes through (124 = timeout, 137 = killed). 31 # Exit 2 = refused by this script (bad arguments or unsafe host layout); PROGRAM did not run. 32 # When the command finishes the whole scope is stopped, so a crashed check leaves no stray processes. 33 # 34 # Argument rules (all checked before anything runs): 35 # --cwd absolute, existing and canonical (no symlinks, no trailing slash). Inside /var/lib/aien-run it 36 # must be /var/lib/aien-run itself or inside wt//<40-hex sha>; that worktree is the only 37 # writable one. 38 # --env K=V with a plain name; PATH, HOME, CARGO_HOME, RUSTUP_HOME, LD_*, BASH_ENV, ENV refused; 39 # each name at most once. CARGO_TARGET_DIR, if given, must be exactly 40 # /var/lib/aien-run/target//pr/ or .../main/: the head's own dir inside its 41 # trust class (A2), created if missing. With --cwd in a worktree, and must be that 42 # worktree's. One dir per head: a shared dir let cargo link one head's build into another (sc #605). 43 # PROGRAM may not contain '=' or start with '-' (so it can never be read as an extra environment 44 # assignment by `env`). A bare name resolves only through the sandbox PATH 45 # /var/lib/aien-run/cargo/bin:/usr/local/bin:/usr/bin:/bin (all root-owned, checked at start); 46 # a file of the same name inside the worktree is never picked up. 47 # 48 # Optional extensions (not required by C8, used by tests and the engine): 49 # --memory-max BYTES|NUM[KMG] override the per-profile MemoryMax (tests use a small cap) 50 # --tasks-max N override TasksMax 51 # --cpu-quota PCT override CPUQuota (percent, 100 = one core) 52 # 53 # Installed root-owned 0755 at /usr/local/sbin/aien-run-exec by tools/aien-local-ci/install.sh. 54 # Invoked by the trusted side as: sudo -n /usr/local/sbin/aien-run-exec ... (see /etc/sudoers.d/aien-local-ci). 55 set -euo pipefail 56 PATH=/usr/sbin:/usr/bin:/sbin:/bin 57 export PATH 58 umask 022 59 60 RUN_USER=aien-run 61 RUN_HOME=/var/lib/aien-run 62 RUN_PATH=/var/lib/aien-run/cargo/bin:/usr/local/bin:/usr/bin:/bin 63 SLICE=aien-run.slice 64 KILL_AFTER=10 65 LOCK_DIR=/run/aien-local-ci 66 LOCK_FILE=$LOCK_DIR/execution.lock 67 NAME_RE='^[A-Za-z0-9._-]+$' 68 69 die() { printf 'aien-run-exec: %s\n' "$*" >&2; exit 2; } 70 usage() { 71 printf 'usage: aien-run-exec --cwd DIR --timeout SECS --profile host|qemu|gb10 [--env K=V]... -- PROGRAM [ARGS...]\n' >&2 72 exit 2 73 } 74 plain_name() { [[ $1 =~ $NAME_RE && $1 != . && $1 != .. ]]; } 75 76 [[ $(id -u) -eq 0 ]] || die "must run as root (via sudo -n)" 77 78 cwd=""; secs=""; profile=""; envs=(); mem_max=""; tasks_max=""; cpu_quota="" 79 while [[ $# -gt 0 ]]; do 80 case "$1" in 81 --cwd) [[ $# -ge 2 ]] || usage; cwd=$2; shift 2 ;; 82 --timeout) [[ $# -ge 2 ]] || usage; secs=$2; shift 2 ;; 83 --profile) [[ $# -ge 2 ]] || usage; profile=$2; shift 2 ;; 84 --env) [[ $# -ge 2 ]] || usage; envs+=("$2"); shift 2 ;; 85 --memory-max) [[ $# -ge 2 ]] || usage; mem_max=$2; shift 2 ;; 86 --tasks-max) [[ $# -ge 2 ]] || usage; tasks_max=$2; shift 2 ;; 87 --cpu-quota) [[ $# -ge 2 ]] || usage; cpu_quota=$2; shift 2 ;; 88 --) shift; break ;; 89 -h|--help) usage ;; 90 *) die "unknown argument: $1" ;; 91 esac 92 done 93 [[ $# -ge 1 ]] || usage 94 [[ -n $cwd && -n $secs && -n $profile ]] || usage 95 [[ $secs =~ ^[0-9]+$ && $secs -gt 0 ]] || die "--timeout must be a positive integer of seconds: $secs" 96 case "$profile" in host|qemu|gb10) ;; *) die "--profile must be host|qemu|gb10: $profile" ;; esac 97 [[ -z $mem_max || $mem_max =~ ^[0-9]+[KMGT]?$ ]] || die "--memory-max must be NUM[KMGT]: $mem_max" 98 [[ -z $tasks_max || $tasks_max =~ ^[0-9]+$ ]] || die "--tasks-max must be an integer: $tasks_max" 99 [[ -z $cpu_quota || $cpu_quota =~ ^[0-9]+$ ]] || die "--cpu-quota must be an integer percent: $cpu_quota" 100 [[ -n $1 && $1 != *=* && $1 != -* ]] || die "PROGRAM may not be empty, contain '=' or start with '-': $1" 101 102 # --cwd: canonical, and inside the sandbox home only the home itself or a worktree. 103 [[ $cwd == /* && -d $cwd ]] || die "--cwd must be an existing absolute directory: $cwd" 104 [[ $(realpath -e -- "$cwd") == "$cwd" ]] || die "--cwd must be canonical (no symlinks, no trailing slash): $cwd" 105 wtroot=""; repo=""; sha="" 106 case "$cwd" in 107 "$RUN_HOME") ;; 108 "$RUN_HOME"/wt/*/*) 109 rel=${cwd#"$RUN_HOME"/wt/}; repo=${rel%%/*}; rest=${rel#*/}; sha=${rest%%/*} 110 plain_name "$repo" && [[ $sha =~ ^[0-9a-f]{40}$ ]] || die "--cwd must be inside $RUN_HOME/wt//<40-hex sha>: $cwd" 111 wtroot=$RUN_HOME/wt/$repo/$sha ;; 112 "$RUN_HOME"/*) die "--cwd inside $RUN_HOME must be the home itself or inside wt//: $cwd" ;; 113 esac 114 115 # --env: plain names, refused names, no duplicates, CARGO_TARGET_DIR only as target//pr|main/ 116 # (the worktree's own repo and sha when --cwd is in one). 117 tdir=""; seen=" " 118 for kv in "${envs[@]}"; do 119 [[ $kv =~ ^[A-Za-z_][A-Za-z0-9_]*= ]] || die "--env needs K=V with a valid name: $kv" 120 k=${kv%%=*} 121 case "$k" in 122 LD_*|BASH_ENV|ENV|HOME|CARGO_HOME|RUSTUP_HOME|PATH) die "--env may not set $k" ;; 123 esac 124 [[ $seen != *" $k "* ]] || die "--env sets $k twice" 125 seen+="$k " 126 if [[ $k == CARGO_TARGET_DIR ]]; then 127 tdir=${kv#*=} 128 trel=${tdir#"$RUN_HOME"/target/}; IFS=/ read -r trepo tclass tsha textra <<<"$trel" 129 [[ $tdir == "$RUN_HOME/target/$trepo/$tclass/$tsha" && -z $textra && ( $tclass == pr || $tclass == main ) \ 130 && $tsha =~ ^[0-9a-f]{40}$ ]] && plain_name "$trepo" \ 131 || die "CARGO_TARGET_DIR must be $RUN_HOME/target//pr/ or .../main/: $tdir" 132 [[ -z $sha || ( $trepo == "$repo" && $tsha == "$sha" ) ]] \ 133 || die "CARGO_TARGET_DIR must be this worktree's own ($repo $sha): $tdir" 134 fi 135 done 136 getent passwd "$RUN_USER" >/dev/null || die "user $RUN_USER does not exist (run tools/aien-local-ci/install.sh)" 137 138 # Host layout self-check: everything the sandbox could use to influence a later run must be root-owned and not 139 # writable by group/other (the mount namespace below is the main guard; this catches a broken install). 140 for p in "$RUN_HOME" "$RUN_HOME/cargo" "$RUN_HOME/cargo/bin" "$RUN_HOME/rustup" "$RUN_HOME/target" /usr/local/bin /usr/bin; do 141 read -r u m < <(stat -L -c '%u %a' "$p" 2>/dev/null || echo "x x") 142 [[ $u == 0 ]] || die "unsafe host layout: $p is not root-owned (re-run tools/aien-local-ci/install.sh)" 143 (( (8#$m & 8#022) == 0 )) || die "unsafe host layout: $p is group/other-writable ($m)" 144 done 145 146 # /run is root controlled; only root can create or traverse the lock directory. Never follow a 147 # pre-planted symlink or repair unsafe ownership/modes while opening a security boundary. 148 [[ ! -L /run && -d /run ]] || die "unsafe execution lock parent: /run" 149 read -r u m < <(stat -c '%u %a' /run) 150 [[ $u == 0 ]] && (( (8#$m & 8#022) == 0 )) || die "unsafe execution lock parent: /run" 151 if [[ ! -e $LOCK_DIR && ! -L $LOCK_DIR ]]; then 152 mkdir -m 0700 -- "$LOCK_DIR" || [[ -d $LOCK_DIR ]] || die "cannot create execution lock directory" 153 fi 154 [[ ! -L $LOCK_DIR && -d $LOCK_DIR && $(stat -c '%u %a %F' "$LOCK_DIR") == '0 700 directory' ]] \ 155 || die "unsafe execution lock directory: $LOCK_DIR" 156 if [[ ! -e $LOCK_FILE && ! -L $LOCK_FILE ]]; then 157 ( umask 077; set -C; : > "$LOCK_FILE" ) || [[ -f $LOCK_FILE ]] || die "cannot create execution lock" 158 fi 159 [[ ! -L $LOCK_FILE && -f $LOCK_FILE && $(stat -c '%u %a' "$LOCK_FILE") == '0 600' ]] \ 160 || die "unsafe execution lock file: $LOCK_FILE" 161 162 # The head's target dir: created on demand (missing parents root-owned, the head's dir aien-run). Only that one 163 # dir is mounted writable below, so a run can never touch another head's build or the other trust class. 164 if [[ -n $tdir ]]; then 165 if [[ ! -d $tdir ]]; then 166 [[ -d ${tdir%/*} ]] || install -d -o root -g root -m 0755 "${tdir%/*}" 167 install -d -o "$RUN_USER" -g "$RUN_USER" -m 0755 "$tdir" 168 fi 169 [[ $(realpath -e -- "$tdir") == "$tdir" ]] || die "CARGO_TARGET_DIR must be canonical: $tdir" 170 fi 171 172 # Per-profile caps. Machine: 20 cores, 121 GB RAM. host/qemu leave headroom for the trusted side; 173 # gb10 may use most of the unified memory but still cannot take the whole machine. 174 case "$profile" in 175 host) : "${mem_max:=48G}"; : "${tasks_max:=4096}"; : "${cpu_quota:=1600}" ;; 176 qemu) : "${mem_max:=48G}"; : "${tasks_max:=4096}"; : "${cpu_quota:=1600}" ;; 177 gb10) : "${mem_max:=96G}"; : "${tasks_max:=4096}"; : "${cpu_quota:=1800}" ;; 178 esac 179 180 unit="aien-run-exec-$$-$(date +%s%N | tail -c 7)" 181 props=(-p "MemoryMax=$mem_max" -p "MemorySwapMax=0" -p "TasksMax=$tasks_max" -p "CPUQuota=${cpu_quota}%") 182 case "$profile" in 183 host) props+=(-p DevicePolicy=closed) ;; 184 qemu) props+=(-p DevicePolicy=closed -p "DeviceAllow=/dev/kvm rw") ;; 185 gb10) props+=(-p DevicePolicy=auto) ;; 186 esac 187 188 # Mount setup, run as root inside the new mount namespace. Parameters are positional, never interpolated. 189 # Order matters: the writable pieces are bound onto themselves first, then the home is bound recursively onto 190 # itself and only that top mount is made read-only, so the self-binds underneath stay writable. 191 # shellcheck disable=SC2016 192 NS_SETUP=' 193 set -euo pipefail 194 home=$1 profile=$2 wtroot=$3 tdir=$4 models=$5; shift 5 195 for p in "$wtroot" "$tdir"; do [[ -z $p ]] || mount --bind "$p" "$p"; done 196 mount --rbind "$home" "$home" 197 mount -o remount,bind,ro "$home" 198 if [[ -n $models ]]; then mount --bind "$models" "$home/models"; mount -o remount,bind,ro "$home/models"; fi 199 priv=(/var/tmp /var/crash /var/lib/BrlAPI) 200 case $profile in host|qemu) priv+=(/tmp /dev/shm /run/lock /run/screen) ;; esac 201 for d in "${priv[@]}"; do 202 if [[ -d $d ]]; then mount -t tmpfs -o mode=1777,nosuid,nodev aien-run-tmp "$d"; fi 203 done 204 case $profile in host|qemu) mount -t mqueue -o nosuid,nodev,noexec aien-run-mqueue /dev/mqueue ;; esac 205 # host|qemu run in their own network namespace (unshare -n above): raise its loopback, nothing else exists in it. 206 case $profile in host|qemu) ip link set lo up ;; esac 207 exec "$@" 208 ' 209 210 # Model directory of the operator who called sudo, if any (read-only bind, see the header). 211 models_src="" 212 if [[ -n ${SUDO_USER:-} && $SUDO_USER != root ]]; then 213 op_home=$(getent passwd "$SUDO_USER" | cut -d: -f6) 214 if [[ -n $op_home && -d $op_home/models && -d $RUN_HOME/models ]]; then models_src=$(realpath -e -- "$op_home/models"); fi 215 fi 216 217 inner=() 218 case "$profile" in 219 host|qemu) inner+=(unshare -m -n -i -p --fork --mount-proc --kill-child --propagation private --) ;; 220 gb10) inner+=(unshare -m --propagation private --) ;; 221 esac 222 inner+=(bash -c "$NS_SETUP" aien-run-ns "$RUN_HOME" "$profile" "$wtroot" "$tdir" "$models_src") 223 inner+=(prlimit --core=0 --nofile=65536 --) 224 case "$profile" in 225 host|qemu) inner+=(timeout --signal=TERM --kill-after="$KILL_AFTER" "$secs") ;; 226 esac 227 inner+=(setpriv --reuid "$RUN_USER" --regid "$RUN_USER" --init-groups --reset-env --no-new-privs --) 228 inner+=(env -i -C "$cwd" "PATH=$RUN_PATH" "HOME=$RUN_HOME" "CARGO_HOME=$RUN_HOME/cargo" "RUSTUP_HOME=$RUN_HOME/rustup") 229 for kv in "${envs[@]}"; do inner+=("$kv"); done 230 231 # flock keeps its own root-owned descriptor and --close prevents the scope's jobs from 232 # inheriting it. Its child waits for scope cleanup, not just PROGRAM's exit. 233 case "$profile" in host|qemu) lock_mode=-s ;; gb10) lock_mode=-x ;; esac 234 # shellcheck disable=SC2016 235 SCOPE_RUN=' 236 set -euo pipefail 237 unit=$1 slice=$2; shift 2 238 count=$1; shift 239 props=("${@:1:count}"); shift "$count" 240 count=$1; shift 241 inner=("${@:1:count}"); shift "$count" 242 cleanup() { systemctl kill --signal=KILL "$unit.scope" >/dev/null 2>&1 || true; systemctl stop "$unit.scope" >/dev/null 2>&1 || true; } 243 trap '\''cleanup; exit 143'\'' TERM INT HUP 244 rc=0 245 cd / 246 systemd-run --scope --quiet --collect --unit "$unit" --slice "$slice" "${props[@]}" -- "${inner[@]}" "$@" || rc=$? 247 cleanup 248 exit "$rc" 249 ' 250 flock "$lock_mode" --close -- "$LOCK_FILE" bash -c "$SCOPE_RUN" aien-run-scope \ 251 "$unit" "$SLICE" "${#props[@]}" "${props[@]}" "${#inner[@]}" "${inner[@]}" "$@"