1 #!/bin/sh2 # prove rootless CDI GPU access, the private socket boundary, and the probe3 # under the same Docker sandbox that an engine receives.4 #5 # Run on a GPU host with rootless Docker, the NVIDIA container toolkit, and6 # the proof image. The script uses only local images and local files; it never7 # pulls and never starts engine code on the host.8 #9 # ./ops/gpu-proof.sh [gpu-index ...]10 #11 # The default is every GPU that nvidia-smi lists. The control/probe image12 # defaults to `inf3r:local`; override it with INF3R_PROOF_IMAGE. The engine13 # image defaults to the mock's local alias; override it with14 # INF3R_ENGINE_IMAGE. The CUDA allocation image15 # defaults to the local devel image and can be changed with INF3R_ALLOC_IMAGE.16 # The allocation is INF3R_ALLOC_MB (default 256) and the settle tolerance is17 # INF3R_SETTLE_MB (default 64).18 set -eu19 20 root_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)21 image=${INF3R_PROOF_IMAGE:-inf3r:local}22 engine_image=${INF3R_ENGINE_IMAGE:-inf3r/mock/x64-cpu:local}23 alloc_image=${INF3R_ALLOC_IMAGE:-nvidia/cuda:13.0.0-devel-ubuntu24.04}24 alloc_mb=${INF3R_ALLOC_MB:-256}25 settle_mb=${INF3R_SETTLE_MB:-64}26 27 die() {28 printf 'gpu-proof: %s\n' "$*" >&229 exit 130 }31 32 command -v docker >/dev/null 2>&1 || die 'docker is not on PATH'33 command -v nvidia-smi >/dev/null 2>&1 || die 'nvidia-smi is not on PATH'34 35 # Rootless Docker normally has no /var/run/docker.sock. Use the daemon socket36 # that belongs to this user unless the caller already named one.37 if [ -z "${DOCKER_HOST:-}" ] && [ -S "/run/user/$(id -u)/docker.sock" ]; then38 DOCKER_HOST="unix:///run/user/$(id -u)/docker.sock"39 export DOCKER_HOST40 fi41 case ${DOCKER_HOST:-} in42 unix://?*) docker_socket=${DOCKER_HOST#unix://} ;;43 *) die 'DOCKER_HOST must name a Unix socket' ;;44 esac45 [ -S "$docker_socket" ] || die "Docker socket $docker_socket is absent"46 [ -x /usr/bin/docker ] || die '/usr/bin/docker is needed for the control container'47 docker info --format '{{.SecurityOptions}}' 2>/dev/null | grep -q rootless ||48 die 'Docker is not rootless'49 docker image inspect "$image" >/dev/null 2>&1 || die "image $image is absent locally"50 docker image inspect "$engine_image" >/dev/null 2>&1 ||51 die "engine image $engine_image is absent locally"52 docker image inspect "$alloc_image" >/dev/null 2>&1 ||53 die "allocation image $alloc_image is absent locally"54 55 nvidia-smi >/dev/null 2>&1 ||56 die 'nvidia-smi cannot talk to the driver; reboot after a driver upgrade'57 58 if [ "$#" -gt 0 ]; then59 gpus=$*60 else61 gpus=$(nvidia-smi --query-gpu=index --format=csv,noheader | tr -d ' ')62 fi63 [ -n "$gpus" ] || die 'nvidia-smi lists no GPU'64 for gpu in $gpus; do65 case $gpu in66 ''|*[!0-9]*) die "bad GPU index: $gpu" ;;67 esac68 done69 70 work=$(mktemp -d "${TMPDIR:-/tmp}/ifg-XXXXXX")71 chmod 700 "$work"72 container=73 cleanup() {74 if [ -n "${container:-}" ]; then75 docker rm -f "$container" >/dev/null 2>&1 || true76 fi77 if [ -n "${allocator:-}" ]; then78 docker rm -f "$allocator" >/dev/null 2>&1 || true79 fi80 rm -rf "$work"81 }82 trap cleanup EXIT INT TERM HUP83 84 # One probe invocation, in the same trusted helper shape that the server uses.85 # The helper receives every CDI GPU and host PID visibility. `all` matters:86 # CDI renumbers a subset, so a probe that received only cuda:1 would call it87 # cuda:0 and would measure the wrong card.88 probe() {89 docker run --rm --pull=never --pid=host --user 0:0 --network none --read-only \90 --tmpfs /tmp --cap-drop ALL --security-opt no-new-privileges \91 --device nvidia.com/gpu=all \92 --entrypoint /usr/local/bin/inf3r-probe "$image" "$@"93 }94 95 # A container shaped like an engine by the D28 flags, but with no socket and no96 # engine. It is used only to prove that the assigned card is usable.97 sandbox_gpu() {98 gpu=$199 entry=$2100 shift 2101 docker run --rm --pull=never --user 0:0 --network none --read-only --tmpfs /tmp \102 --cap-drop ALL --security-opt no-new-privileges \103 --device "nvidia.com/gpu=$gpu" \104 --entrypoint "$entry" "$image" "$@"105 }106 107 # A host path owner after a container writes into a bind mount. This is the108 # measurement behind the 0:0 decision: rootless Docker maps container root to109 # the daemon's host uid, while a non-root container uid gets a subordinate uid.110 mapped_owner() {111 user=$1112 directory=$(mktemp -d "${TMPDIR:-/tmp}/inf3r-gpu-map.XXXXXX")113 chmod 777 "$directory"114 docker run --rm --pull=never --user "$user" --network none --read-only --tmpfs /tmp \115 --cap-drop ALL --security-opt no-new-privileges \116 -v "$directory:/work:rw" --entrypoint sh "$image" -c 'touch /work/owner' \117 >/dev/null118 owner=$(stat -c '%u:%g' "$directory/owner")119 rm -rf "$directory"120 printf '%s' "$owner"121 }122 123 host_uid=$(id -u)124 host_gid=$(id -g)125 root_owner=$(mapped_owner 0:0)126 [ "$root_owner" = "$host_uid:$host_gid" ] ||127 die 'container root did not map to the host uid'128 printf 'uid\t0:0\t%s\n' "$root_owner"129 130 for user in 1000:1000 65534:65534; do131 owner=$(mapped_owner "$user")132 [ "$owner" = "$root_owner" ] &&133 die "container user $user mapped to the control uid"134 printf 'uid\t%s\t%s\n' "$user" "$owner"135 done136 137 # One control run. It starts the mock as a sibling container on the named card,138 # waits for the plugin to connect over the private socket, reads /models and139 # /stats, measures the engine's host pid through the probe, and stops it.140 control_on() {141 gpu=$1142 root="$work/c$gpu"143 mkdir -p "$root/plugins/mock/cuda/models" "$root/bin" "$root/blobs" \144 "$root/s"145 cp "$root_dir/plugins/mock/x64-cpu/models/mock.toml" \146 "$root/plugins/mock/cuda/models/"147 cat >"$root/plugins/mock/cuda/bundle.toml" <<EOF148 id = "mock/cuda"149 model = "mock"150 target = "cuda"151 summary = "rootless GPU proof"152 engine = "authored"153 binary = "inf3r-mock-x64-cpu"154 files = "target.toml,models/mock.toml,BUNDLE.md"155 sha256 = ""156 EOF157 cat >"$root/plugins/mock/cuda/target.toml" <<EOF158 [target]159 name = "cuda"160 backend = "cuda"161 arch = ["x86_64"]162 compute = "sm00"163 devices = 1164 vram_mb = [1024]165 EOF166 printf '# Rootless GPU proof bundle\n' >"$root/plugins/mock/cuda/BUNDLE.md"167 token=$(od -An -N32 -tx1 /dev/urandom | tr -d ' \n')168 printf '%s\n' "$token" >"$root/token"169 cat >"$root/deploy.toml" <<EOF170 [[node]]171 host = "proof"172 bin_dir = "$root/bin"173 bundle_dirs = ["$root/plugins"]174 blob_dir = "$root/blobs"175 socket_dir = "$root/s"176 probe_image = "$image"177 web = { unix = "$root/web.sock" }178 auth_file = "$root/token"179 180 [[node.bind]]181 bundle = "mock/cuda"182 runtime = "container"183 image = "$engine_image"184 command = ["/opt/inf3r/bin/inf3r-mock-x64-cpu"]185 models = ["mock"]186 devices = ["cuda:$gpu"]187 warm = true188 EOF189 chmod 700 "$root"190 lock_env=191 target=${CARGO_TARGET_DIR:-$root_dir/target}192 case "$target" in193 /*) ;;194 *) target=$root_dir/$target ;;195 esac196 if [ -f "$target/images.lock" ]; then197 cp "$target/images.lock" "$root/images.lock"198 lock_env="INF3R_IMAGES_LOCK=$root/images.lock"199 fi200 container="inf3r-gpu-proof-control-$$-$gpu"201 docker rm -f "$container" >/dev/null 2>&1 || true202 docker run -d --pull=never --name "$container" --user 0:0 --network none --read-only \203 --tmpfs /tmp --cap-drop ALL --security-opt no-new-privileges \204 -v /usr/bin/docker:/usr/local/bin/docker:ro \205 -v "$docker_socket:$docker_socket" \206 -v "$root:$root:rw" \207 -e "DOCKER_HOST=$DOCKER_HOST" \208 ${lock_env:+-e "$lock_env"} \209 --entrypoint /usr/local/bin/inf3r-web "$image" \210 --deploy "$root/deploy.toml" >/dev/null211 for _ in $(seq 1 300); do212 [ -S "$root/web.sock" ] && break213 docker inspect -f '{{.State.Running}}' "$container" 2>/dev/null |214 grep -q true || break215 sleep 0.1216 done217 if [ ! -S "$root/web.sock" ]; then218 docker logs "$container" 2>&1 | tail -20 >&2 || true219 die "control on cuda:$gpu did not listen"220 fi221 models=$(curl -fsS --max-time 10 --unix-socket "$root/web.sock" \222 -H "Authorization: Bearer $token" http://localhost/models) ||223 die "control on cuda:$gpu did not answer /models"224 case $models in225 *'"plugin":"mock/cuda"'*) ;;226 *)227 docker logs "$container" 2>&1 | tail -40 >&2 || true228 die "control on cuda:$gpu did not serve mock/cuda: $models"229 ;;230 esac231 stats=$(curl -fsS --max-time 10 --unix-socket "$root/web.sock" \232 -H "Authorization: Bearer $token" http://localhost/stats) ||233 die "control on cuda:$gpu did not answer /stats"234 case $stats in235 *"\"device\":\"cuda:$gpu\""*) ;;236 *) die "stats do not show cuda:$gpu: $stats" ;;237 esac238 engine=$(docker ps -q --filter "label=inf3r.control=$root/s" \239 --filter label=inf3r.plugin=mock/cuda | head -1)240 [ -n "$engine" ] || die "control on cuda:$gpu started no engine"241 host_pid=$(docker inspect -f '{{.State.Pid}}' "$engine")242 [ -n "$host_pid" ] || die "engine on cuda:$gpu has no host pid"243 rss=$(probe --rss "$host_pid")244 case $rss in ''|*[!0-9]*) die "probe --rss returned $rss" ;; esac245 [ "$rss" -gt 0 ] || die "probe --rss measured no resident bytes"246 private=$(find "$root/s" -mindepth 1 -maxdepth 1 -type d | head -1)247 [ -n "$private" ] || die "control on cuda:$gpu created no private directory"248 [ "$(stat -c '%a' "$private")" = 700 ] ||249 die "private directory $private is not 0700"250 [ "$(stat -c '%u' "$private")" = "$host_uid" ] ||251 die "private directory $private is not owned by the control uid"252 socket=$(find "$private" -type s | head -1)253 [ -n "$socket" ] || die "private directory $private has no socket"254 [ "$(stat -c '%a' "$socket")" = 666 ] ||255 die "socket $socket is not 0666"256 [ "$(stat -c '%u' "$socket")" = "$host_uid" ] ||257 die "socket $socket is not owned by the control uid"258 printf 'control\tcuda:%s\tpid %s\trss %s\tsocket %s\n' \259 "$gpu" "$host_pid" "$rss" "${socket#"$root/"}"260 docker stop "$container" >/dev/null 2>&1 || true261 docker rm -f "$container" >/dev/null 2>&1 || true262 container=263 for _ in $(seq 1 100); do264 left=$(docker ps -aq --filter "label=inf3r.control=$root/s" \265 --filter label=inf3r.managed=1 | tr -d ' \n')266 [ -z "$left" ] && break267 sleep 0.1268 done269 [ -z "$(docker ps -aq --filter "label=inf3r.control=$root/s" \270 --filter label=inf3r.managed=1)" ] ||271 die "control on cuda:$gpu left an engine container"272 [ -z "$(find "$root/s" -mindepth 1 2>/dev/null)" ] ||273 die "control on cuda:$gpu left a socket directory"274 }275 276 # A real device allocation in the same sandbox, so "the engine sees the card"277 # is not only an nvidia-smi query. The allocator is a local CUDA image; a278 # deliberately small allocation keeps the two cards free for the next case.279 vram_on() {280 gpu=$1281 baseline=$(probe --free "cuda:$gpu")282 case $baseline in ''|*[!0-9]*) die "probe --free returned $baseline" ;; esac283 source="$work/alloc.cu"284 bytes=$((alloc_mb * 1024 * 1024))285 cat >"$source" <<EOF286 #include <cuda_runtime.h>287 #include <stdio.h>288 #include <unistd.h>289 int main(void) {290 void *p = NULL;291 cudaError_t e = cudaMalloc(&p, ${bytes}ull);292 if (e != cudaSuccess) {293 fprintf(stderr, "cudaMalloc: %s\n", cudaGetErrorString(e));294 return 1;295 }296 printf("allocated\n");297 fflush(stdout);298 sleep(120);299 return 0;300 }301 EOF302 allocator="inf3r-gpu-proof-alloc-$$-$gpu"303 docker rm -f "$allocator" >/dev/null 2>&1 || true304 # /tmp must be executable because nvcc writes the test binary there.305 docker run -d --pull=never --name "$allocator" --user 0:0 --network none --read-only \306 --tmpfs /tmp:exec --cap-drop ALL --security-opt no-new-privileges \307 --device "nvidia.com/gpu=$gpu" -v "$source:/alloc.cu:ro" \308 --entrypoint sh "$alloc_image" \309 -c 'nvcc -o /tmp/alloc /alloc.cu && /tmp/alloc' >/dev/null310 for _ in $(seq 1 240); do311 docker logs "$allocator" 2>&1 | grep -q '^allocated$' && break312 docker inspect -f '{{.State.Running}}' "$allocator" 2>/dev/null |313 grep -q true || break314 sleep 0.25315 done316 if ! docker logs "$allocator" 2>&1 | grep -q '^allocated$'; then317 docker logs "$allocator" 2>&1 | tail -20 >&2 || true318 die "CUDA allocation on cuda:$gpu did not start"319 fi320 host_pid=$(docker inspect -f '{{.State.Pid}}' "$allocator")321 used=322 for _ in $(seq 1 80); do323 used=$(probe --vram "$host_pid" "cuda:$gpu")324 case $used in ''|*[!0-9]*) used=0 ;; esac325 [ "$used" -gt 0 ] && break326 sleep 0.25327 done328 [ "${used:-0}" -gt 0 ] || die "probe --vram saw no bytes on cuda:$gpu"329 free=$(probe --free "cuda:$gpu")330 case $free in ''|*[!0-9]*) die "probe --free returned $free" ;; esac331 [ "$free" -lt "$baseline" ] ||332 die "cuda:$gpu did not report less free memory after the allocation"333 start=$(date +%s%N)334 docker stop "$allocator" >/dev/null 2>&1 || true335 docker rm -f "$allocator" >/dev/null 2>&1 || true336 allocator=337 after=0338 for _ in $(seq 1 400); do339 after=$(probe --free "cuda:$gpu")340 case $after in ''|*[!0-9]*) after=0 ;; esac341 [ "$after" -ge $((baseline - settle_mb * 1024 * 1024)) ] && break342 sleep 0.05343 done344 elapsed=$((($(date +%s%N) - start) / 1000000))345 [ "$after" -ge $((baseline - settle_mb * 1024 * 1024)) ] ||346 die "cuda:$gpu did not settle within ${settle_mb}MiB of its baseline"347 printf 'vram\tcuda:%s\tused %s\tfree %s -> %s\tsettle %sms\n' \348 "$gpu" "$used" "$baseline" "$after" "$elapsed"349 }350 351 for gpu in $gpus; do352 uuid=$(nvidia-smi --id="$gpu" --query-gpu=uuid --format=csv,noheader |353 head -1 | tr -d ' ')354 [ -n "$uuid" ] || die "nvidia-smi has no UUID for cuda:$gpu"355 seen=$(sandbox_gpu "$gpu" nvidia-smi --query-gpu=uuid,memory.total \356 --format=csv,noheader)357 case $seen in358 *"$uuid"*) ;;359 *) die "container on cuda:$gpu sees $seen, not $uuid" ;;360 esac361 host=$(probe --host)362 printf '%s\n' "$host" | grep -q "^gpu[[:space:]]cuda:$gpu[[:space:]]" ||363 die "probe does not see cuda:$gpu"364 free=$(probe --free "cuda:$gpu")365 case $free in ''|*[!0-9]*) die "probe --free returned $free" ;; esac366 printf 'gpu\tcuda:%s\t%s\tfree %s\n' "$gpu" "$uuid" "$free"367 control_on "$gpu"368 vram_on "$gpu"369 done370 371 printf 'gpu-proof: ok\n'