aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorSoccera1 <[email protected]>2026-06-15 21:10:43 +1000
committerSoccera1 <[email protected]>2026-07-13 17:02:02 +1000
commit7ea75fb4424af8df393dd75dfb9b14a08aa46959 (patch)
tree52772fa63c7ae0581b44d0c0b164252934acf5f0 /scripts
downloadbnu-7ea75fb4424af8df393dd75dfb9b14a08aa46959.tar.gz
bnu-7ea75fb4424af8df393dd75dfb9b14a08aa46959.zip
Initial commit
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/link-commands.js45
-rwxr-xr-xscripts/qemu/build-payload.sh49
-rwxr-xr-xscripts/qemu/check-host.sh36
-rw-r--r--scripts/qemu/common.sh227
-rwxr-xr-xscripts/qemu/download-images.sh106
-rwxr-xr-xscripts/qemu/guest/provision-base.sh27
-rwxr-xr-xscripts/qemu/guest/provision-selinux.sh79
-rwxr-xr-xscripts/qemu/guest/provision-smack.sh33
-rwxr-xr-xscripts/qemu/guest/run-tests.sh41
-rwxr-xr-xscripts/run-all-tests.sh14
-rw-r--r--scripts/run-bun-tests-bounded.js48
-rwxr-xr-xscripts/run-gnu-qemu-matrix.sh133
-rw-r--r--scripts/run-gnu-tests.js1732
-rw-r--r--scripts/run-memory-bounded.js90
-rwxr-xr-xscripts/run-qemu-root-smoke.sh153
-rwxr-xr-xscripts/setup-qemu-environments.sh141
16 files changed, 2954 insertions, 0 deletions
diff --git a/scripts/link-commands.js b/scripts/link-commands.js
new file mode 100755
index 0000000..617f64e
--- /dev/null
+++ b/scripts/link-commands.js
@@ -0,0 +1,45 @@
+#!/usr/bin/env bun
+import { chmod, mkdir, writeFile } from "node:fs/promises";
+import { dirname, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+import { commandNames } from "../src/coreutils.js";
+
+const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
+const targetDir = resolve(process.argv[2] ?? "dist/bin");
+const bnu = resolve(root, "bin/bnu.js");
+const bun = shellQuote(process.execPath);
+const bnuScript = shellQuote(bnu);
+
+await mkdir(targetDir, { recursive: true });
+
+for (const command of commandNames) {
+ const path = resolve(targetDir, command);
+ const script = command === "pwd"
+ ? `#!/bin/sh
+saved_pwd=$PWD
+actual_pwd=$(command pwd -P 2>/dev/null || :)
+test -n "$actual_pwd" && saved_pwd=$actual_pwd
+if [ \${#saved_pwd} -gt 4000 ]; then
+ cd / || exit 1
+ BNU_LONG_PWD=$saved_pwd PWD=$saved_pwd exec ${bun} ${bnuScript} pwd "$@"
+fi
+exec ${bun} ${bnuScript} pwd "$@"
+`
+ : command === "tail" || command === "tac" || command === "timeout"
+ ? `#!/bin/sh
+if [ ! -e /proc/$$/fd/0 ]; then
+ BNU_STDIN_CLOSED=1
+ export BNU_STDIN_CLOSED
+fi
+exec ${bun} ${bnuScript} ${command} "$@"
+`
+ : `#!/bin/sh\nexec ${bun} ${bnuScript} ${command} "$@"\n`;
+ await writeFile(path, script);
+ await chmod(path, 0o755);
+}
+
+console.log(`Linked ${commandNames.length} command wrappers into ${targetDir}`);
+
+function shellQuote(value) {
+ return `'${String(value).replaceAll("'", "'\\''")}'`;
+}
diff --git a/scripts/qemu/build-payload.sh b/scripts/qemu/build-payload.sh
new file mode 100755
index 0000000..a9ab1fd
--- /dev/null
+++ b/scripts/qemu/build-payload.sh
@@ -0,0 +1,49 @@
+#!/usr/bin/env bash
+# Build an unprivileged ext4 disk containing BNU, Bun, and the GNU tarball.
+
+set -Eeuo pipefail
+source "$(cd -- "$(dirname -- "$0")" && pwd)/common.sh"
+
+output=$BNU_QEMU_DIR/bnu-test-payload.ext4
+size=${BNU_QEMU_PAYLOAD_SIZE:-4G}
+while (($#)); do
+ case $1 in
+ --dry-run) BNU_QEMU_DRY_RUN=1 ;;
+ --output) [[ $# -ge 2 ]] || die "--output needs a path"; output=$2; shift ;;
+ --size) [[ $# -ge 2 ]] || die "--size needs a value"; size=$2; shift ;;
+ -h|--help)
+ printf 'usage: %s [--dry-run] [--output PATH] [--size 4G]\n' "$0"
+ exit 0
+ ;;
+ *) die "unknown argument: $1" ;;
+ esac
+ shift
+done
+
+for command in bun mkfs.ext4 truncate; do require_command "$command"; done
+ensure_state_dir
+[[ -r $BNU_REPO_ROOT/coreutils-9.11.tar.xz ]] || die "missing coreutils-9.11.tar.xz"
+bun_path=$(command -v bun)
+staging=$(mktemp -d "${TMPDIR:-/tmp}/bnu-payload.XXXXXX")
+cleanup() { rm -rf -- "$staging"; }
+trap cleanup EXIT HUP INT TERM
+
+run mkdir -p -- "$staging/bnu/repo" "$staging/bnu/bin" "$staging/bnu/artifacts"
+for entry in bin src scripts tests docs package.json README.md coreutils-9.11.tar.xz; do
+ [[ -e $BNU_REPO_ROOT/$entry ]] || continue
+ run cp -a -- "$BNU_REPO_ROOT/$entry" "$staging/bnu/repo/"
+done
+run cp -L -- "$bun_path" "$staging/bnu/bin/bun"
+selinux_stage=$BNU_QEMU_DIR/selinux/stage3-amd64-hardened-selinux-systemd.tar.xz
+if [[ -s $selinux_stage ]]; then
+ run cp -a -- "$selinux_stage" "$staging/bnu/artifacts/"
+fi
+if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then
+ printf '%s\n' "$(git -C "$BNU_REPO_ROOT" rev-parse HEAD 2>/dev/null || printf unknown)" >"$staging/bnu/REVISION"
+fi
+
+run rm -f -- "$output.new"
+run truncate -s "$size" "$output.new"
+run mkfs.ext4 -q -F -L BNU_PAYLOAD -d "$staging" "$output.new"
+run mv -- "$output.new" "$output"
+note "payload ready: $output"
diff --git a/scripts/qemu/check-host.sh b/scripts/qemu/check-host.sh
new file mode 100755
index 0000000..8b0684d
--- /dev/null
+++ b/scripts/qemu/check-host.sh
@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+# Report host-side prerequisites without changing anything.
+
+set -Eeuo pipefail
+source "$(cd -- "$(dirname -- "$0")" && pwd)/common.sh"
+
+missing=0
+for command in bash bun qemu-img qemu-system-x86_64 ssh scp ssh-keygen curl gpg \
+ mkfs.ext4 timeout awk sort comm sha512sum tar xz; do
+ if command -v "$command" >/dev/null 2>&1; then
+ printf 'ok %s\n' "$command"
+ else
+ printf 'missing %s\n' "$command"
+ missing=1
+ fi
+done
+if command -v xorriso >/dev/null 2>&1 || command -v genisoimage >/dev/null 2>&1; then
+ printf 'ok xorriso or genisoimage\n'
+else
+ printf 'missing xorriso or genisoimage\n'
+ missing=1
+fi
+if [[ -r /dev/kvm && -w /dev/kvm ]]; then
+ printf 'ok /dev/kvm (read/write)\n'
+else
+ printf 'missing /dev/kvm access (KVM group membership may require a new login)\n'
+ missing=1
+fi
+if code=$(find_ovmf code 2>/dev/null) && vars=$(find_ovmf vars 2>/dev/null); then
+ printf 'ok OVMF code: %s\n' "$code"
+ printf 'ok OVMF vars: %s\n' "$vars"
+else
+ printf 'missing OVMF firmware (or set BNU_OVMF_CODE and BNU_OVMF_VARS)\n'
+ missing=1
+fi
+exit "$missing"
diff --git a/scripts/qemu/common.sh b/scripts/qemu/common.sh
new file mode 100644
index 0000000..4c6cc70
--- /dev/null
+++ b/scripts/qemu/common.sh
@@ -0,0 +1,227 @@
+#!/usr/bin/env bash
+# Shared, host-distro-neutral helpers for the BNU Gentoo QEMU test matrix.
+
+set -Eeuo pipefail
+
+BNU_REPO_ROOT=${BNU_REPO_ROOT:-$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)}
+BNU_QEMU_DIR=${BNU_QEMU_DIR:-/var/tmp/bnu-qemu}
+BNU_QEMU_MEMORY=${BNU_QEMU_MEMORY:-1536M}
+BNU_QEMU_CPUS=${BNU_QEMU_CPUS:-2}
+BNU_QEMU_SSH_PORT=${BNU_QEMU_SSH_PORT:-22222}
+BNU_QEMU_TIMEOUT=${BNU_QEMU_TIMEOUT:-900}
+BNU_QEMU_DRY_RUN=${BNU_QEMU_DRY_RUN:-0}
+
+note() { printf 'bnu-qemu: %s\n' "$*" >&2; }
+die() { note "$*"; exit 2; }
+
+require_command() {
+ command -v "$1" >/dev/null 2>&1 || die "missing required command: $1"
+}
+
+run() {
+ if [[ $BNU_QEMU_DRY_RUN == 1 ]]; then
+ printf '+ ' >&2
+ printf '%q ' "$@" >&2
+ printf '\n' >&2
+ else
+ "$@"
+ fi
+}
+
+ensure_state_dir() {
+ if [[ $BNU_QEMU_DRY_RUN == 1 ]]; then
+ note "would create state directory $BNU_QEMU_DIR"
+ return
+ fi
+ mkdir -p -- "$BNU_QEMU_DIR" 2>/dev/null ||
+ die "cannot write $BNU_QEMU_DIR; create it for this user or set BNU_QEMU_DIR"
+ [[ -w $BNU_QEMU_DIR ]] || die "state directory is not writable: $BNU_QEMU_DIR"
+}
+
+require_free_gib() {
+ local required=$1 available_kib available_gib
+ available_kib=$(df -Pk "$BNU_QEMU_DIR" | awk 'NR == 2 {print $4}')
+ [[ $available_kib =~ ^[0-9]+$ ]] || die "could not determine free space below $BNU_QEMU_DIR"
+ available_gib=$((available_kib / 1024 / 1024))
+ (( available_gib >= required )) ||
+ die "$BNU_QEMU_DIR has ${available_gib} GiB free; this setup phase requires at least ${required} GiB free"
+}
+
+require_kvm() {
+ [[ -e /dev/kvm ]] || die "/dev/kvm is absent; this matrix intentionally has no software-emulation fallback"
+ [[ -r /dev/kvm && -w /dev/kvm ]] ||
+ die "/dev/kvm is not accessible; add this user to the host's KVM group and start a new login session"
+}
+
+find_ovmf() {
+ local kind=$1 candidate
+ local -a code=(
+ /usr/share/edk2/OvmfX64/OVMF_CODE.fd
+ /usr/share/OVMF/OVMF_CODE.fd
+ /usr/share/OVMF/OVMF_CODE_4M.fd
+ /usr/share/edk2-ovmf/x64/OVMF_CODE.fd
+ /usr/share/edk2/ovmf/OVMF_CODE.fd
+ /usr/share/edk2/x64/OVMF_CODE.fd
+ )
+ local -a vars=(
+ /usr/share/edk2/OvmfX64/OVMF_VARS.fd
+ /usr/share/OVMF/OVMF_VARS.fd
+ /usr/share/OVMF/OVMF_VARS_4M.fd
+ /usr/share/edk2-ovmf/x64/OVMF_VARS.fd
+ /usr/share/edk2/ovmf/OVMF_VARS.fd
+ /usr/share/edk2/x64/OVMF_VARS.fd
+ )
+ local -a candidates
+ if [[ $kind == code ]]; then candidates=("${code[@]}"); else candidates=("${vars[@]}"); fi
+ for candidate in "${candidates[@]}"; do
+ [[ -r $candidate ]] && { printf '%s\n' "$candidate"; return; }
+ done
+ die "could not find OVMF $kind firmware; set BNU_OVMF_CODE and BNU_OVMF_VARS explicitly"
+}
+
+ovmf_code() { printf '%s\n' "${BNU_OVMF_CODE:-$(find_ovmf code)}"; }
+ovmf_vars() { printf '%s\n' "${BNU_OVMF_VARS:-$(find_ovmf vars)}"; }
+
+download() {
+ local url=$1 output=$2
+ [[ -s $output ]] && { note "using cached $(basename -- "$output")"; return; }
+ run curl --fail --location --retry 4 --retry-delay 2 --continue-at - --output "$output.part" "$url"
+ run mv -- "$output.part" "$output"
+}
+
+prepare_gnupg() {
+ local gnupg=$1 bundle=$BNU_QEMU_DIR/gentoo-service-keys.gpg
+ run mkdir -p -- "$gnupg"
+ if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then chmod 700 "$gnupg"; fi
+ download https://qa-reports.gentoo.org/output/service-keys.gpg "$bundle"
+ run gpg --batch --homedir "$gnupg" --import "$bundle"
+}
+
+verify_detached() {
+ local gnupg=$1 signature=$2 artifact=$3
+ run gpg --batch --homedir "$gnupg" --status-fd 1 --verify "$signature" "$artifact"
+}
+
+create_overlay() {
+ local base=$1 overlay=$2
+ [[ -s $overlay ]] && { note "using existing overlay $overlay"; return; }
+ run qemu-img create -q -f qcow2 -F qcow2 -b "$base" "$overlay"
+}
+
+ensure_ssh_key() {
+ BNU_QEMU_SSH_KEY=${BNU_QEMU_SSH_KEY:-$BNU_QEMU_DIR/id_ed25519}
+ if [[ ! -s $BNU_QEMU_SSH_KEY ]]; then
+ run ssh-keygen -q -t ed25519 -N '' -f "$BNU_QEMU_SSH_KEY"
+ fi
+ export BNU_QEMU_SSH_KEY
+}
+
+create_seed_iso() {
+ local output=$1 seed_dir
+ [[ -s $output ]] && return
+ ensure_ssh_key
+ seed_dir=$(mktemp -d "${TMPDIR:-/tmp}/bnu-seed.XXXXXX")
+ trap 'rm -rf -- "$seed_dir"' RETURN
+ if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then
+ cat >"$seed_dir/meta-data" <<EOF
+instance-id: bnu-gentoo
+local-hostname: bnu-gentoo
+EOF
+ cat >"$seed_dir/user-data" <<EOF
+#cloud-config
+disable_root: false
+ssh_pwauth: false
+users:
+ - name: root
+ lock_passwd: true
+ shell: /bin/bash
+ ssh_authorized_keys:
+ - $(<"$BNU_QEMU_SSH_KEY.pub")
+runcmd:
+ - [ sh, -c, 'touch /var/lib/bnu-cloud-ready' ]
+EOF
+ fi
+ if command -v xorriso >/dev/null 2>&1; then
+ run xorriso -as mkisofs -quiet -output "$output" -volid cidata -joliet -rock \
+ "$seed_dir/user-data" "$seed_dir/meta-data"
+ elif command -v genisoimage >/dev/null 2>&1; then
+ run genisoimage -quiet -output "$output" -volid cidata -joliet -rock \
+ "$seed_dir/user-data" "$seed_dir/meta-data"
+ else
+ die "missing xorriso or genisoimage (needed for the NoCloud seed)"
+ fi
+ trap - RETURN
+ rm -rf -- "$seed_dir"
+}
+
+ssh_options() {
+ printf '%s\0' -i "$BNU_QEMU_SSH_KEY" -p "$BNU_QEMU_SSH_PORT" \
+ -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
+ -o ConnectTimeout=5 -o ServerAliveInterval=15 -o ServerAliveCountMax=4
+}
+
+guest_ssh() {
+ local -a options=()
+ while IFS= read -r -d '' item; do options+=("$item"); done < <(ssh_options)
+ ssh "${options[@]}" [email protected] "$@"
+}
+
+guest_scp() {
+ local -a options=()
+ while IFS= read -r -d '' item; do options+=("$item"); done < <(ssh_options)
+ scp "${options[@]/-p/-P}" "$@"
+}
+
+wait_for_ssh() {
+ local deadline=$((SECONDS + BNU_QEMU_TIMEOUT))
+ while (( SECONDS < deadline )); do
+ if guest_ssh 'test -e /var/lib/bnu-cloud-ready || test -e /var/lib/bnu-provisioned' >/dev/null 2>&1; then
+ return
+ fi
+ sleep 2
+ done
+ die "guest SSH did not become ready within ${BNU_QEMU_TIMEOUT}s"
+}
+
+start_vm() {
+ local image=$1 name=$2 payload=${3:-} seed=${4:-} extra_disk=${5:-}
+ local run_dir=$BNU_QEMU_DIR/run/$name vars pidfile serial
+ require_kvm
+ require_command qemu-system-x86_64
+ require_command qemu-img
+ ensure_ssh_key
+ run mkdir -p -- "$run_dir"
+ vars=$run_dir/OVMF_VARS.fd
+ pidfile=$run_dir/qemu.pid
+ serial=$run_dir/serial.log
+ if [[ $BNU_QEMU_DRY_RUN != 1 && -s $pidfile ]] && kill -0 "$(<"$pidfile")" 2>/dev/null; then
+ die "VM $name is already running (pid $(<"$pidfile"))"
+ fi
+ run cp -- "$(ovmf_vars)" "$vars"
+ local -a drives=(
+ -drive "if=pflash,format=raw,readonly=on,file=$(ovmf_code)"
+ -drive "if=pflash,format=raw,file=$vars"
+ -drive "if=virtio,format=qcow2,file=$image,cache=writeback,discard=unmap"
+ )
+ [[ -n $payload ]] && drives+=( -drive "if=virtio,format=raw,readonly=on,file=$payload" )
+ [[ -n $extra_disk ]] && drives+=( -drive "if=virtio,format=qcow2,file=$extra_disk,cache=writeback,discard=unmap" )
+ [[ -n $seed ]] && drives+=( -drive "media=cdrom,format=raw,readonly=on,file=$seed" )
+ run qemu-system-x86_64 \
+ -name "$name" -machine q35,accel=kvm -cpu host -smp "$BNU_QEMU_CPUS" -m "$BNU_QEMU_MEMORY" \
+ "${drives[@]}" -device virtio-net-pci,netdev=net0 \
+ -netdev "user,id=net0,hostfwd=tcp:127.0.0.1:${BNU_QEMU_SSH_PORT}-:22" \
+ -display none -serial "file:$serial" -monitor none -daemonize -pidfile "$pidfile"
+ [[ $BNU_QEMU_DRY_RUN == 1 ]] || wait_for_ssh
+}
+
+stop_vm() {
+ local name=$1 pidfile=$BNU_QEMU_DIR/run/$name/qemu.pid deadline
+ [[ -s $pidfile ]] || return
+ guest_ssh 'sync; systemctl poweroff || poweroff' >/dev/null 2>&1 || true
+ deadline=$((SECONDS + 60))
+ while kill -0 "$(<"$pidfile")" 2>/dev/null && (( SECONDS < deadline )); do sleep 1; done
+ if kill -0 "$(<"$pidfile")" 2>/dev/null; then
+ note "guest did not stop cleanly; terminating only QEMU pid $(<"$pidfile")"
+ kill -TERM "$(<"$pidfile")" 2>/dev/null || true
+ fi
+}
diff --git a/scripts/qemu/download-images.sh b/scripts/qemu/download-images.sh
new file mode 100755
index 0000000..26c1f36
--- /dev/null
+++ b/scripts/qemu/download-images.sh
@@ -0,0 +1,106 @@
+#!/usr/bin/env bash
+# Download and authenticate the official Gentoo images used by the matrix.
+
+set -Eeuo pipefail
+source "$(cd -- "$(dirname -- "$0")" && pwd)/common.sh"
+
+usage() {
+ cat <<'EOF'
+usage: scripts/qemu/download-images.sh [--dry-run] [--only NAME]
+
+NAME is cloud, selinux, hurd, or all (the default). Files are cached beneath
+BNU_QEMU_DIR, which defaults to /var/tmp/bnu-qemu.
+EOF
+}
+
+only=all
+while (($#)); do
+ case $1 in
+ --dry-run) BNU_QEMU_DRY_RUN=1 ;;
+ --only) [[ $# -ge 2 ]] || die "--only needs a value"; only=$2; shift ;;
+ -h|--help) usage; exit 0 ;;
+ *) die "unknown argument: $1" ;;
+ esac
+ shift
+done
+[[ $only =~ ^(all|cloud|selinux|hurd)$ ]] || die "--only must be cloud, selinux, hurd, or all"
+
+for command in curl gpg qemu-img; do require_command "$command"; done
+ensure_state_dir
+gnupg=$BNU_QEMU_DIR/gnupg
+prepare_gnupg "$gnupg"
+
+signed_manifest_path() {
+ local url=$1 pattern=$2 name=$3 path
+ local manifest=$BNU_QEMU_DIR/$name
+ local plain=$BNU_QEMU_DIR/$name.plain
+ download "$url" "$manifest"
+ if [[ $BNU_QEMU_DRY_RUN == 1 ]]; then
+ printf 'DRY-RUN/%s\n' "$pattern"
+ return
+ fi
+ gpg --batch --homedir "$gnupg" --output "$plain.tmp" --decrypt "$manifest"
+ mv -- "$plain.tmp" "$plain"
+ path=$(awk -v pattern="$pattern" '$1 !~ /^#/ && $1 ~ pattern { print $1; exit }' "$plain")
+ [[ -n $path ]] || die "no $pattern artifact found in $url"
+ printf '%s\n' "$path"
+}
+
+fetch_signed_artifact() {
+ local url=$1 output=$2
+ download "$url" "$output"
+ download "$url.asc" "$output.asc"
+ verify_detached "$gnupg" "$output.asc" "$output"
+}
+
+autobuilds=https://distfiles.gentoo.org/releases/amd64/autobuilds
+if [[ $only == all || $only == cloud ]]; then
+ run mkdir -p -- "$BNU_QEMU_DIR/cloud"
+ cloud_path=$(signed_manifest_path "$autobuilds/latest-qcow2.txt" 'di-amd64-cloudinit-.*\.qcow2$' latest-qcow2.txt)
+ cloud_output=$BNU_QEMU_DIR/cloud/gentoo-cloudinit-amd64.qcow2
+ fetch_signed_artifact "$autobuilds/$cloud_path" "$cloud_output"
+ if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then printf '%s\n' "$cloud_path" >"$cloud_output.source"; fi
+fi
+
+if [[ $only == all || $only == selinux ]]; then
+ run mkdir -p -- "$BNU_QEMU_DIR/selinux"
+ selinux_path=$(signed_manifest_path \
+ "$autobuilds/latest-stage3-amd64-hardened-selinux-systemd.txt" \
+ 'stage3-amd64-hardened-selinux-systemd-.*\.tar\.xz$' \
+ latest-stage3-amd64-hardened-selinux-systemd.txt)
+ selinux_output=$BNU_QEMU_DIR/selinux/stage3-amd64-hardened-selinux-systemd.tar.xz
+ fetch_signed_artifact "$autobuilds/$selinux_path" "$selinux_output"
+ if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then printf '%s\n' "$selinux_path" >"$selinux_output.source"; fi
+fi
+
+if [[ $only == all || $only == hurd ]]; then
+ run mkdir -p -- "$BNU_QEMU_DIR/hurd-gentoo-amd64"
+ hurd_base=https://distfiles.gentoo.org/experimental/amd64/hurd/hurd-x86_64-preview.qcow2
+ hurd_image=$BNU_QEMU_DIR/hurd-gentoo-amd64/hurd-x86_64-preview.qcow2
+ download "$hurd_base" "$hurd_image"
+ download "$hurd_base.sig" "$hurd_image.sig"
+ download "$hurd_base.sha512" "$hurd_image.sha512"
+ # The experimental Hurd preview is signed by its Gentoo developer rather
+ # than the automated release key. Fetch its key block over HTTPS from
+ # Gentoo's own keyserver and assert the exact signing-subkey fingerprint.
+ hurd_signer=FD19E6D31B192EE4DC63EAD3DC2B16215ED5412A
+ if [[ $BNU_QEMU_DRY_RUN == 1 ]] || ! gpg --batch --homedir "$gnupg" --list-keys "$hurd_signer" >/dev/null 2>&1; then
+ hurd_key=$BNU_QEMU_DIR/hurd-gentoo-amd64/signer-$hurd_signer.asc
+ download "https://keys.gentoo.org/pks/lookup?op=get&search=0x$hurd_signer" "$hurd_key"
+ run gpg --batch --homedir "$gnupg" --import "$hurd_key"
+ fi
+ if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then
+ gpg --batch --homedir "$gnupg" --with-colons --fingerprint "$hurd_signer" |
+ awk -F: '$1 == "fpr" {print $10}' | grep -Fxq "$hurd_signer" ||
+ die "Gentoo Hurd signing-key fingerprint did not match $hurd_signer"
+ fi
+ verify_detached "$gnupg" "$hurd_image.sig" "$hurd_image"
+ if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then
+ (cd -- "$(dirname -- "$hurd_image")" && sha512sum --check "$(basename -- "$hurd_image.sha512")")
+ else
+ note "would verify the Hurd SHA-512 manifest"
+ fi
+ create_overlay "$hurd_image" "$BNU_QEMU_DIR/hurd-gentoo-amd64/bnu-matrix-overlay.qcow2"
+fi
+
+note "verified Gentoo artifacts are in $BNU_QEMU_DIR"
diff --git a/scripts/qemu/guest/provision-base.sh b/scripts/qemu/guest/provision-base.sh
new file mode 100755
index 0000000..702f923
--- /dev/null
+++ b/scripts/qemu/guest/provision-base.sh
@@ -0,0 +1,27 @@
+#!/usr/bin/env bash
+set -Eeuo pipefail
+
+export MAKEOPTS=-j1
+export EMERGE_DEFAULT_OPTS='--jobs=1 --load-average=2 --nospinner'
+mkdir -p /etc/portage/package.use /etc/portage/package.accept_keywords
+grep -q '^MAKEOPTS=' /etc/portage/make.conf 2>/dev/null || printf '\nMAKEOPTS="-j1"\n' >>/etc/portage/make.conf
+grep -q '^EMERGE_DEFAULT_OPTS=' /etc/portage/make.conf 2>/dev/null ||
+ printf 'EMERGE_DEFAULT_OPTS="--jobs=1 --load-average=2 --nospinner"\n' >>/etc/portage/make.conf
+
+if [[ ${BNU_REFRESH_PORTAGE:-0} == 1 ]]; then emerge --sync; fi
+emerge --ask=n --verbose=n \
+ app-arch/xz-utils app-shells/bash dev-lang/perl dev-lang/python \
+ net-misc/openssh sys-apps/acl sys-apps/attr sys-apps/coreutils \
+ sys-apps/util-linux sys-block/parted sys-devel/binutils sys-devel/gcc \
+ sys-devel/make sys-fs/dosfstools sys-fs/e2fsprogs sys-fs/xfsprogs \
+ sys-libs/libcap sys-process/procps sys-process/psmisc
+
+if [[ ! -e /swapfile ]]; then
+ fallocate -l 2G /swapfile
+ chmod 600 /swapfile
+ mkswap /swapfile
+ printf '/swapfile none swap sw 0 0\n' >>/etc/fstab
+fi
+swapon /swapfile 2>/dev/null || true
+systemctl enable sshd 2>/dev/null || rc-update add sshd default 2>/dev/null || true
+touch /var/lib/bnu-provisioned
diff --git a/scripts/qemu/guest/provision-selinux.sh b/scripts/qemu/guest/provision-selinux.sh
new file mode 100755
index 0000000..5f79397
--- /dev/null
+++ b/scripts/qemu/guest/provision-selinux.sh
@@ -0,0 +1,79 @@
+#!/usr/bin/env bash
+# Run in the standard Gentoo builder. Payload=/dev/vdb, target=/dev/vdc.
+set -Eeuo pipefail
+
+target=/mnt/bnu-target
+payload=/mnt/bnu-payload
+mkdir -p "$target" "$payload"
+mount -o ro /dev/vdb "$payload"
+stage=$payload/bnu/artifacts/stage3-amd64-hardened-selinux-systemd.tar.xz
+[[ -r $stage ]] || { echo "SELinux stage3 is absent from payload" >&2; exit 2; }
+
+wipefs -a /dev/vdc
+parted -s /dev/vdc mklabel gpt
+parted -s /dev/vdc mkpart ESP fat32 1MiB 513MiB
+parted -s /dev/vdc set 1 esp on
+parted -s /dev/vdc mkpart gentoo xfs 513MiB 100%
+udevadm settle
+mkfs.vfat -F 32 -n EFI /dev/vdc1
+mkfs.xfs -f -L gentoo /dev/vdc2
+mount /dev/vdc2 "$target"
+mkdir -p "$target/boot/efi"
+mount /dev/vdc1 "$target/boot/efi"
+tar --numeric-owner --acls --xattrs --xattrs-include='*' --selinux -xpf "$stage" -C "$target"
+
+root_uuid=$(blkid -s UUID -o value /dev/vdc2)
+efi_uuid=$(blkid -s UUID -o value /dev/vdc1)
+cat >"$target/etc/fstab" <<EOF
+UUID=$root_uuid / xfs defaults 0 1
+UUID=$efi_uuid /boot/efi vfat umask=0077 0 2
+/swapfile none swap sw 0 0
+EOF
+cp -L /etc/resolv.conf "$target/etc/resolv.conf"
+cat >>"$target/etc/portage/make.conf" <<'EOF'
+MAKEOPTS="-j1"
+EMERGE_DEFAULT_OPTS="--jobs=1 --load-average=2 --nospinner"
+SELINUX_POLICY_TYPES="mcs mls strict"
+EOF
+mkdir -p "$target/etc/portage/package.use"
+cat >"$target/etc/portage/package.use/bnu-selinux" <<'EOF'
+sys-apps/systemd selinux
+sys-apps/coreutils acl caps xattr
+sys-process/procps selinux
+EOF
+cat >"$target/etc/selinux/config" <<'EOF'
+SELINUX=permissive
+SELINUXTYPE=mls
+EOF
+mkdir -p "$target/etc/systemd/network" "$target/root/.ssh"
+cat >"$target/etc/systemd/network/20-wired.network" <<'EOF'
+[Match]
+Name=en* eth*
+
+[Network]
+DHCP=yes
+EOF
+install -m 600 /dev/null "$target/root/.ssh/authorized_keys"
+cat /root/.ssh/authorized_keys >"$target/root/.ssh/authorized_keys"
+chmod 700 "$target/root/.ssh"
+
+for fs in dev proc sys run; do mount --rbind "/$fs" "$target/$fs"; mount --make-rslave "$target/$fs"; done
+chroot "$target" /usr/bin/env MAKEOPTS=-j1 EMERGE_DEFAULT_OPTS='--jobs=1 --load-average=2 --nospinner' \
+ emerge --ask=n --verbose=n net-misc/openssh net-misc/dhcpcd sec-policy/selinux-base \
+ sec-policy/selinux-base-policy sys-apps/policycoreutils sys-boot/grub:2 \
+ sys-fs/dosfstools sys-fs/xfsprogs sys-kernel/gentoo-kernel-bin
+
+cat >"$target/etc/default/grub" <<'EOF'
+GRUB_CMDLINE_LINUX="console=ttyS0 lsm=selinux security=selinux selinux=1 enforcing=0"
+GRUB_TERMINAL="serial console"
+GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"
+EOF
+chroot "$target" systemctl enable sshd systemd-networkd systemd-resolved
+chroot "$target" grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=Gentoo --removable --no-nvram
+chroot "$target" grub-mkconfig -o /boot/grub/grub.cfg
+chroot "$target" semodule -B || true
+fallocate -l 2G "$target/swapfile"
+chmod 600 "$target/swapfile"
+mkswap "$target/swapfile"
+touch "$target/.autorelabel" "$target/var/lib/bnu-provisioned" "$target/var/lib/bnu-selinux-mls"
+sync
diff --git a/scripts/qemu/guest/provision-smack.sh b/scripts/qemu/guest/provision-smack.sh
new file mode 100755
index 0000000..4cbb5a2
--- /dev/null
+++ b/scripts/qemu/guest/provision-smack.sh
@@ -0,0 +1,33 @@
+#!/usr/bin/env bash
+# Run inside an overlay of the completed SELinux guest.
+set -Eeuo pipefail
+
+export MAKEOPTS=-j1
+export EMERGE_DEFAULT_OPTS='--jobs=1 --load-average=2 --nospinner'
+swapon /swapfile 2>/dev/null || true
+emerge --ask=n --verbose=n sys-apps/kmod sys-kernel/dracut sys-kernel/gentoo-sources
+eselect kernel set "$(eselect kernel list | awk '/gentoo/ {gsub(/\[|\]/, "", $1); n=$1} END {print n}')"
+kernel=/usr/src/linux
+cd "$kernel"
+if [[ -r /proc/config.gz ]]; then
+ zcat /proc/config.gz >.config
+else
+ cp "/boot/config-$(uname -r)" .config
+fi
+scripts/config --enable SECURITY --enable SECURITYFS --enable SECURITY_SMACK
+scripts/config --disable DEBUG_INFO_BTF --disable DEBUG_INFO_BTF_MODULES
+scripts/config --enable VIRTIO --enable VIRTIO_PCI --enable VIRTIO_BLK --enable VIRTIO_NET
+scripts/config --enable EXT4_FS --enable XFS_FS --enable VFAT_FS --enable ISO9660_FS
+scripts/config --set-str LOCALVERSION '-smack-bnu'
+make olddefconfig
+make -j1
+make -j1 modules_install
+version=$(make -s kernelrelease)
+cp arch/x86/boot/bzImage "/boot/vmlinuz-$version"
+cp .config "/boot/config-$version"
+cp System.map "/boot/System.map-$version"
+dracut --force "/boot/initramfs-$version.img" "$version"
+sed -i 's/lsm=selinux/security=smack lsm=smack/; s/security=selinux//; s/selinux=1//; s/enforcing=0//' /etc/default/grub
+grub-mkconfig -o /boot/grub/grub.cfg
+touch /var/lib/bnu-smack
+sync
diff --git a/scripts/qemu/guest/run-tests.sh b/scripts/qemu/guest/run-tests.sh
new file mode 100755
index 0000000..efbb26e
--- /dev/null
+++ b/scripts/qemu/guest/run-tests.sh
@@ -0,0 +1,41 @@
+#!/usr/bin/env bash
+set -Eeuo pipefail
+
+mode=${1:?mode is required}
+shift
+mountpoint=/mnt/bnu-payload
+work=/var/tmp/bnu-current
+mkdir -p "$mountpoint"
+mountpoint -q "$mountpoint" || mount -o ro LABEL=BNU_PAYLOAD "$mountpoint"
+rm -rf -- "$work"
+mkdir -p "$work"
+cp -a "$mountpoint/bnu/repo/." "$work/"
+install -m 755 "$mountpoint/bnu/bin/bun" "$work/bun"
+chmod -R a+rX "$work"
+
+common=(
+ "$work/bun" "$work/scripts/run-gnu-tests.js"
+ --tarball "$work/coreutils-9.11.tar.xz"
+ --very-expensive --timeout 300s --rss-limit 1024MiB --memory-limit 3GiB
+)
+case $mode in
+ nonroot)
+ mkdir -p /tmp/bnu-nobody
+ chown nobody:nogroup /tmp/bnu-nobody 2>/dev/null || chown nobody:nobody /tmp/bnu-nobody
+ exec runuser -u nobody -- env HOME=/tmp/bnu-nobody TMPDIR=/tmp/bnu-nobody \
+ "${common[@]}" --all --nonroot-tests "$@"
+ ;;
+ root)
+ exec env HOME=/root TMPDIR=/tmp "${common[@]}" --all --root-tests "$@"
+ ;;
+ selected-root)
+ exec env HOME=/root TMPDIR=/tmp "${common[@]}" "$@"
+ ;;
+ selected-nonroot)
+ mkdir -p /tmp/bnu-nobody
+ chown nobody:nogroup /tmp/bnu-nobody 2>/dev/null || chown nobody:nobody /tmp/bnu-nobody
+ exec runuser -u nobody -- env HOME=/tmp/bnu-nobody TMPDIR=/tmp/bnu-nobody \
+ "${common[@]}" "$@"
+ ;;
+ *) echo "unknown test mode: $mode" >&2; exit 2 ;;
+esac
diff --git a/scripts/run-all-tests.sh b/scripts/run-all-tests.sh
new file mode 100755
index 0000000..073a264
--- /dev/null
+++ b/scripts/run-all-tests.sh
@@ -0,0 +1,14 @@
+#!/usr/bin/env bash
+# Single sequential entry point for local unit tests and the GNU QEMU matrix.
+
+set -Eeuo pipefail
+root=$(cd -- "$(dirname -- "$0")/.." && pwd)
+dry=0
+for arg in "$@"; do [[ $arg == --dry-run ]] && dry=1; done
+
+if ((dry)); then
+ printf '+ bun %q\n' "$root/scripts/run-bun-tests-bounded.js" >&2
+else
+ bun "$root/scripts/run-bun-tests-bounded.js"
+fi
+exec "$root/scripts/run-gnu-qemu-matrix.sh" "$@"
diff --git a/scripts/run-bun-tests-bounded.js b/scripts/run-bun-tests-bounded.js
new file mode 100644
index 0000000..f3e5a03
--- /dev/null
+++ b/scripts/run-bun-tests-bounded.js
@@ -0,0 +1,48 @@
+#!/usr/bin/env bun
+import { readFile } from "node:fs/promises";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+
+const root = join(dirname(fileURLToPath(import.meta.url)), "..");
+const testFile = join(root, "tests/coreutils.test.js");
+const source = await readFile(testFile, "utf8");
+const names = [...source.matchAll(/^test\("((?:[^"\\]|\\.)+)"/gm)].map((match) => JSON.parse(`"${match[1]}"`));
+if (names.length === 0) {
+ console.error("run-bun-tests-bounded: no tests found");
+ process.exit(2);
+}
+
+const failures = [];
+for (let index = 0; index < names.length; index++) {
+ const name = names[index];
+ console.log(`\nBOUNDED TEST ${index + 1}/${names.length}: ${name}`);
+ const exactPattern = `^${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`;
+ const proc = Bun.spawn([
+ process.execPath,
+ join(root, "scripts/run-memory-bounded.js"),
+ "--rss-limit", process.env.BNU_TEST_RSS_LIMIT ?? "1GiB",
+ "--memory-limit", process.env.BNU_TEST_MEMORY_LIMIT ?? "3GiB",
+ "--",
+ process.execPath,
+ "test",
+ testFile,
+ "--test-name-pattern", exactPattern,
+ "--timeout", process.env.BNU_TEST_TIMEOUT ?? "300000",
+ "--max-concurrency", "1",
+ ], {
+ cwd: root,
+ env: { ...process.env, BNU_FACTOR_WORKERS: process.env.BNU_FACTOR_WORKERS ?? "1" },
+ stdin: "ignore",
+ stdout: "inherit",
+ stderr: "inherit",
+ });
+ const code = await proc.exited;
+ if (code !== 0) failures.push({ name, code });
+}
+
+if (failures.length) {
+ console.error(`\n${failures.length} bounded test process(es) failed:`);
+ for (const failure of failures) console.error(`- ${failure.name} (exit ${failure.code})`);
+ process.exit(1);
+}
+console.log(`\nAll ${names.length} bounded test processes passed.`);
diff --git a/scripts/run-gnu-qemu-matrix.sh b/scripts/run-gnu-qemu-matrix.sh
new file mode 100755
index 0000000..10a0fe2
--- /dev/null
+++ b/scripts/run-gnu-qemu-matrix.sh
@@ -0,0 +1,133 @@
+#!/usr/bin/env bash
+# Run and reconcile the complete GNU command-test inventory in Gentoo guests.
+
+set -Eeuo pipefail
+source "$(cd -- "$(dirname -- "$0")" && pwd)/qemu/common.sh"
+
+setup=0
+dry=0
+while (($#)); do
+ case $1 in
+ --setup) setup=1 ;;
+ --dry-run) dry=1; BNU_QEMU_DRY_RUN=1 ;;
+ -h|--help)
+ cat <<'EOF'
+usage: scripts/run-gnu-qemu-matrix.sh [--setup] [--dry-run]
+
+--setup creates or resumes all required Gentoo guests first. The matrix runs
+one KVM VM at a time with 1536 MiB RAM, saves logs, and accepts only the six
+documented Bun runtime/ABI boundaries as unresolved upstream tests.
+EOF
+ exit 0
+ ;;
+ *) die "unknown argument: $1" ;;
+ esac
+ shift
+done
+
+for command in bun qemu-img qemu-system-x86_64 ssh scp timeout awk sort comm; do require_command "$command"; done
+require_kvm
+ensure_state_dir
+if ((setup)); then
+ run env BNU_QEMU_DIR="$BNU_QEMU_DIR" BNU_QEMU_DRY_RUN="$BNU_QEMU_DRY_RUN" \
+ "$BNU_REPO_ROOT/scripts/setup-qemu-environments.sh"
+fi
+run env BNU_QEMU_DIR="$BNU_QEMU_DIR" BNU_QEMU_DRY_RUN="$BNU_QEMU_DRY_RUN" \
+ "$BNU_REPO_ROOT/scripts/qemu/build-payload.sh"
+
+standard=$BNU_QEMU_DIR/standard/gentoo-standard-bnu.qcow2
+selinux=$BNU_QEMU_DIR/selinux/gentoo-selinux-mls-bnu.qcow2
+smack=$BNU_QEMU_DIR/smack/gentoo-smack-bnu.qcow2
+hurd=$BNU_QEMU_DIR/hurd-gentoo-amd64/bnu-matrix-overlay.qcow2
+payload=$BNU_QEMU_DIR/bnu-test-payload.ext4
+for image in "$standard" "$selinux" "$smack" "$hurd"; do
+ [[ -s $image || $dry == 1 ]] || die "missing $image; rerun with --setup"
+done
+if ((dry)); then
+ note "would run: standard/nonroot, SELinux-MLS/root, SELinux policy supplements, SMACK supplements, Hurd boot probe"
+ note "would reconcile every upstream test against the six-entry boundary allowlist"
+ exit 0
+fi
+
+stamp=$(date -u +%Y%m%dT%H%M%SZ)
+logs=$BNU_QEMU_DIR/results/$stamp
+mkdir -p "$logs"
+runner=$BNU_REPO_ROOT/scripts/qemu/guest/run-tests.sh
+
+run_guest_batch() {
+ local image=$1 name=$2 mode=$3 log=$4
+ shift 4
+ local vm=matrix-$name
+ trap 'stop_vm "$vm"' EXIT HUP INT TERM
+ start_vm "$image" "$vm" "$payload"
+ set +e
+ guest_ssh "bash -s -- $(printf '%q ' "$mode" "$@")" <"$runner" 2>&1 | tee "$log"
+ local status=${PIPESTATUS[0]}
+ set -e
+ stop_vm "$vm"
+ trap - EXIT HUP INT TERM
+ # Per-batch failures are reconciled after platform-specific reruns.
+ printf 'batch-status=%s\n' "$status" >>"$log"
+}
+
+run_guest_batch "$standard" standard nonroot "$logs/standard-nonroot.log"
+run_guest_batch "$selinux" selinux-root root "$logs/selinux-root.log"
+run_guest_batch "$selinux" selinux-policy selected-root "$logs/selinux-policy.log" \
+ tests/chcon/chcon.sh tests/cp/cp-a-selinux.sh tests/id/setgid.sh \
+ tests/install/install-C-selinux.sh tests/install/install-Z-selinux.sh \
+ tests/ls/selinux.sh tests/mkdir/selinux.sh tests/misc/selinux.sh
+run_guest_batch "$smack" smack selected-root "$logs/smack-root.log" \
+ tests/id/smack.sh tests/mkdir/smack-root.sh
+run_guest_batch "$smack" smack-nobody selected-nonroot "$logs/smack-nobody.log" \
+ tests/mkdir/smack-no-root.sh
+
+# Bun has no GNU/Hurd executable, but boot the official amd64 image so this ABI
+# boundary is checked against the real target rather than inferred from Linux.
+hurd_serial=$logs/hurd-boot.log
+set +e
+timeout --kill-after=5s 90s qemu-system-x86_64 \
+ -name bnu-hurd-probe -machine pc,accel=kvm -cpu host -smp "$BNU_QEMU_CPUS" -m "$BNU_QEMU_MEMORY" \
+ -drive "if=ide,format=qcow2,snapshot=on,file=$hurd" \
+ -nographic -monitor none -net none >"$hurd_serial" 2>&1
+hurd_status=$?
+set -e
+printf 'qemu-status=%s\n' "$hurd_status" >>"$hurd_serial"
+[[ $hurd_status -eq 124 ]] || die "the Gentoo Hurd VM exited early with status $hurd_status; see $hurd_serial"
+grep -aq 'Gentoo GNU/Hurd' "$hurd_serial" ||
+ die "the Gentoo Hurd boot entry was not reached; see $hurd_serial"
+printf '%s\n' 'SKIP tests/id/gnu-zero-uids.sh - Bun provides no GNU/Hurd amd64 runtime' >>"$hurd_serial"
+
+inventory=$logs/inventory
+inventory_raw=$logs/inventory.raw
+passed=$logs/passed
+unresolved=$logs/unresolved
+allowed=$logs/allowed-boundaries
+bun "$BNU_REPO_ROOT/scripts/run-gnu-tests.js" --all --list >"$inventory_raw"
+sort -u "$inventory_raw" >"$inventory"
+cat >"$allowed" <<'EOF'
+tests/df/no-mtab-status-masked-proc.sh
+tests/id/gnu-zero-uids.sh
+tests/nproc/nproc-quota-systemd.sh
+tests/sort/sort-continue.sh
+tests/sort/sort-merge-fdlimit.sh
+tests/stat/stat-mount.sh
+EOF
+awk '/^PASS tests\// {print $2}' "$logs"/*.log | sort -u >"$passed"
+comm -23 "$inventory" "$passed" | sort -u >"$unresolved"
+unexpected=$logs/unexpected-unresolved
+comm -23 "$unresolved" "$allowed" >"$unexpected"
+count=$(wc -l <"$inventory")
+[[ $count -eq 733 ]] || die "expected 733 upstream command tests, found $count"
+if [[ -s $unexpected ]]; then
+ note "unexpected unresolved tests:"
+ sed 's/^/ /' "$unexpected" >&2
+ die "matrix did not reconcile; logs are in $logs"
+fi
+missing_boundaries=$logs/missing-boundaries
+comm -23 "$allowed" "$unresolved" >"$missing_boundaries"
+if [[ -s $missing_boundaries ]]; then
+ note "documented boundaries that unexpectedly passed:"
+ sed 's/^/ /' "$missing_boundaries" >&2
+fi
+note "matrix reconciled all $count tests; only documented Bun boundaries remain"
+note "logs: $logs"
diff --git a/scripts/run-gnu-tests.js b/scripts/run-gnu-tests.js
new file mode 100644
index 0000000..fd1bccc
--- /dev/null
+++ b/scripts/run-gnu-tests.js
@@ -0,0 +1,1732 @@
+#!/usr/bin/env bun
+import { access, chmod, mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { dirname, join, resolve } from "node:path";
+import { fileURLToPath, pathToFileURL } from "node:url";
+import { commandNames } from "../src/coreutils.js";
+
+const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
+const args = process.argv.slice(2);
+const keep = takeFlag("--keep");
+const list = takeFlag("--list");
+const strict = takeFlag("--strict");
+const all = takeFlag("--all");
+const expensive = takeFlag("--expensive");
+const veryExpensive = takeFlag("--very-expensive");
+const tty = takeFlag("--tty");
+const rootTestsOnly = takeFlag("--root-tests");
+const nonRootTestsOnly = takeFlag("--nonroot-tests");
+takeFlag("--quick");
+takeFlag("--default");
+if (rootTestsOnly && nonRootTestsOnly) {
+ throw new Error("--root-tests and --nonroot-tests are mutually exclusive");
+}
+const DEFAULT_TIMEOUT = "300s";
+const timeout = takeValue("--timeout") ?? DEFAULT_TIMEOUT;
+const maxOutputBytes = parseByteSize(takeValue("--max-output") ?? "1MiB");
+// prlimit's address-space ceiling is per process, so a test that fans out can
+// still consume much more physical memory in aggregate. Watch the complete
+// test process tree as a second line of defence and terminate only that tree
+// before it can put the host under memory pressure.
+const rssLimitBytes = parseByteSize(takeValue("--rss-limit") ?? "1536MiB");
+// Keep a pathological JS heap from threatening the host. Tests may lower
+// this inherited ceiling with ulimit, but cannot raise it.
+// JSC reserves more than 2 GiB of virtual address space for some otherwise
+// ~120 MiB workloads. Three GiB keeps those valid tests runnable while still
+// preventing the unbounded 11 GiB heap growth that motivated this guard.
+const memoryLimitBytes = parseByteSize(takeValue("--memory-limit") ?? "3GiB");
+const tarball = takeValue("--tarball") ?? join(root, "coreutils-9.11.tar.xz");
+const work = await mkdtemp(join(tmpdir(), "bnu-gnu-tests-"));
+// Root-only GNU tests deliberately drop to an unprivileged account. mkdtemp
+// creates this extraction parent as 0700, which otherwise makes every BNU
+// wrapper beneath it unreachable after the credential transition.
+await chmod(work, 0o755);
+const source = join(work, "coreutils-9.11");
+const TTY_TESTS = new Set([
+ "tests/misc/tty-eof.pl",
+ "tests/stty/bad-speed.sh",
+ "tests/stty/stty-invalid.sh",
+ "tests/stty/stty-pairs.sh",
+ "tests/stty/stty-row-col.sh",
+ "tests/stty/stty.sh",
+]);
+const DEFAULT_TESTS = [
+ "tests/misc/false-status.sh",
+ "tests/misc/printenv.sh",
+ "tests/misc/pathchk.sh",
+ "tests/misc/echo.sh",
+ "tests/misc/invalid-opt.pl",
+ "tests/help/help-version.sh",
+ "tests/help/help-version-getopt.sh",
+ "tests/misc/basename.pl",
+ "tests/misc/dirname.pl",
+ "tests/misc/user.sh",
+ "tests/env/env.sh",
+ "tests/env/env-S.pl",
+ "tests/env/env-S-script.sh",
+ "tests/env/env-null.sh",
+ "tests/env/env-signal-handler.sh",
+ "tests/basenc/basenc.pl",
+ "tests/basenc/base64.pl",
+ "tests/basenc/large-input.sh",
+ "tests/basenc/bounded-memory.sh",
+ "tests/cat/cat-E.sh",
+ "tests/cat/cat-buf.sh",
+ "tests/cat/cat-self.sh",
+ "tests/cat/cat-proc.sh",
+ "tests/cat/splice.sh",
+ "tests/wc/wc.pl",
+ "tests/wc/wc-cpu.sh",
+ "tests/wc/wc-total.sh",
+ "tests/wc/wc-files0.sh",
+ "tests/wc/wc-files0-from.pl",
+ "tests/wc/wc-nbsp.sh",
+ "tests/wc/wc-parallel.sh",
+ "tests/wc/wc-proc.sh",
+ "tests/id/uid.sh",
+ "tests/id/zero.sh",
+ "tests/id/context.sh",
+ "tests/id/no-context.sh",
+ "tests/groups/groups-version.sh",
+ "tests/groups/groups-dash.sh",
+ "tests/groups/groups-process-all.sh",
+ "tests/nproc/nproc-positive.sh",
+ "tests/nproc/nproc-avail.sh",
+ "tests/nproc/nproc-override.sh",
+ "tests/nproc/nproc-quota.sh",
+ "tests/tty/tty.sh",
+ "tests/stty/bad-speed.sh",
+ "tests/stty/stty-invalid.sh",
+ "tests/stty/stty-row-col.sh",
+ "tests/stty/stty.sh",
+ "tests/head/head-c.sh",
+ "tests/head/head-elide-tail.pl",
+ "tests/head/head-write-error.sh",
+ "tests/head/head.pl",
+ "tests/join/join.pl",
+ "tests/join/join-utf8.sh",
+ "tests/misc/comm.pl",
+ "tests/misc/io-errors.sh",
+ "tests/misc/tty-eof.pl",
+ "tests/misc/xstrtol.pl",
+ "tests/cksum/cksum.sh",
+ "tests/cksum/cksum-a.sh",
+ "tests/cksum/cksum-c.sh",
+ "tests/cksum/cksum-base64.pl",
+ "tests/cksum/cksum-base64-untagged.sh",
+ "tests/cksum/cksum-raw.sh",
+ "tests/cksum/sum.pl",
+ "tests/cksum/sum-sysv.sh",
+ "tests/cksum/b2sum.sh",
+ "tests/cksum/md5sum.pl",
+ "tests/cksum/md5sum-bsd.sh",
+ "tests/cksum/md5sum-newline.pl",
+ "tests/cksum/md5sum-parallel.sh",
+ "tests/cksum/sha1sum.pl",
+ "tests/cksum/sha1sum-vec.pl",
+ "tests/cksum/sha224sum.pl",
+ "tests/cksum/sha256sum.pl",
+ "tests/cksum/sha384sum.pl",
+ "tests/cksum/sha512sum.pl",
+ "tests/cksum/cksum-sha3.sh",
+ "tests/cksum/sm3sum.pl",
+ "tests/expr/expr.pl",
+ "tests/expr/expr-multibyte.pl",
+ "tests/test/test-file.sh",
+ "tests/test/test.pl",
+ "tests/test/test-diag.pl",
+ "tests/test/test-N.sh",
+ "tests/seq/seq-io-errors.sh",
+ "tests/seq/seq.pl",
+ "tests/seq/seq-extra-number.sh",
+ "tests/seq/seq-epipe.sh",
+ "tests/seq/seq-locale.sh",
+ "tests/seq/seq-long-double.sh",
+ "tests/seq/seq-precision.sh",
+ "tests/pwd/pwd-option.sh",
+ "tests/pwd/argument.sh",
+ "tests/pwd/pwd-long.sh",
+ "tests/readlink/can-e.sh",
+ "tests/readlink/can-f.sh",
+ "tests/readlink/can-m.sh",
+ "tests/readlink/multi.sh",
+ "tests/readlink/readlink-fp-loop.sh",
+ "tests/readlink/readlink-posix.sh",
+ "tests/readlink/readlink-root.sh",
+ "tests/readlink/rl-1.sh",
+ "tests/misc/realpath.sh",
+ "tests/mktemp/mktemp-misc.sh",
+ "tests/mktemp/mktemp.pl",
+ "tests/mktemp/bad-unicode.sh",
+ "tests/mktemp/write-error.sh",
+ "tests/date/date-debug.sh",
+ "tests/date/date.pl",
+ "tests/date/date-ethiopia.sh",
+ "tests/date/date-iran.sh",
+ "tests/date/date-locale-hour.sh",
+ "tests/date/date-next-dow.pl",
+ "tests/date/percent-percent.sh",
+ "tests/date/reference.sh",
+ "tests/date/resolution.sh",
+ "tests/date/date-sec.sh",
+ "tests/date/date-thailand.sh",
+ "tests/date/date-tz.sh",
+ "tests/truncate/truncate-overflow.sh",
+ "tests/truncate/multiple-files.sh",
+ "tests/truncate/truncate-dangling-symlink.sh",
+ "tests/truncate/truncate-dir-fail.sh",
+ "tests/truncate/truncate-fail-diag.sh",
+ "tests/truncate/truncate-fifo.sh",
+ "tests/truncate/truncate-no-create-missing.sh",
+ "tests/truncate/truncate-parameters.sh",
+ "tests/truncate/truncate-relative.sh",
+ "tests/truncate/truncate-owned-by-other.sh",
+ "tests/tee/append.sh",
+ "tests/tee/tee.sh",
+ "tests/uniq/uniq.pl",
+ "tests/uniq/uniq-perf.sh",
+ "tests/uniq/uniq-collate.sh",
+ "tests/tr/tr.pl",
+ "tests/tr/tr-case-class.sh",
+ "tests/tac/tac.pl",
+ "tests/tac/tac-2-nonseekable.sh",
+ "tests/tac/tac-continue.sh",
+ "tests/tac/tac-locale.sh",
+ "tests/cut/cut.pl",
+ "tests/cut/mb-non-utf8.sh",
+ "tests/cut/bounded-memory.sh",
+ "tests/cut/cut-huge-range.sh",
+ "tests/touch/empty-file.sh",
+ "tests/touch/60-seconds.sh",
+ "tests/touch/dangling-symlink.sh",
+ "tests/touch/dir-1.sh",
+ "tests/touch/fifo.sh",
+ "tests/touch/no-dereference.sh",
+ "tests/touch/no-create-missing.sh",
+ "tests/touch/no-rights.sh",
+ "tests/touch/not-owner.sh",
+ "tests/touch/now-owned-by-other.sh",
+ "tests/touch/obsolescent.sh",
+ "tests/touch/fail-diag.sh",
+ "tests/touch/read-only.sh",
+ "tests/touch/relative.sh",
+ "tests/touch/trailing-slash.sh",
+ "tests/df/total-verify.sh",
+ "tests/df/df-P.sh",
+ "tests/df/df-output.sh",
+ "tests/df/df-symlink.sh",
+ "tests/df/over-mount-device.sh",
+ "tests/df/problematic-chars.sh",
+ "tests/df/total-unprocessed.sh",
+ "tests/df/header.sh",
+ "tests/df/unreadable.sh",
+ "tests/df/sync.sh",
+ "tests/du/exclude.sh",
+ "tests/du/basic.sh",
+ "tests/du/apparent.sh",
+ "tests/du/deref.sh",
+ "tests/du/deref-args.sh",
+ "tests/du/files0-from.pl",
+ "tests/du/files0-from-dir.sh",
+ "tests/du/hard-link.sh",
+ "tests/du/inodes.sh",
+ "tests/du/max-depth.sh",
+ "tests/du/no-deref.sh",
+ "tests/du/slash.sh",
+ "tests/du/trailing-slash.sh",
+ "tests/du/two-args.sh",
+ "tests/du/inacc-dest.sh",
+ "tests/du/inacc-dir.sh",
+ "tests/du/inaccessible-cwd.sh",
+ "tests/du/long-from-unreadable.sh",
+ "tests/du/long-sloop.sh",
+ "tests/du/no-x.sh",
+ "tests/du/one-file-system.sh",
+ "tests/du/restore-wd.sh",
+ "tests/du/bind-mount-dir-cycle-v2.sh",
+ "tests/du/bind-mount-dir-cycle.sh",
+ "tests/du/move-dir-while-traversing.sh",
+ "tests/du/threshold.sh",
+ "tests/cp/same-file.sh",
+ "tests/cp/link.sh",
+ "tests/cp/backup-1.sh",
+ "tests/cp/backup-dir.sh",
+ "tests/cp/backup-is-src.sh",
+ "tests/cp/abuse.sh",
+ "tests/cp/acl.sh",
+ "tests/cp/capability.sh",
+ "tests/cp/attr-existing.sh",
+ "tests/cp/cp-a-selinux.sh",
+ "tests/cp/no-ctx.sh",
+ "tests/cp/cp-HL.sh",
+ "tests/cp/cp-deref.sh",
+ "tests/cp/cp-i.sh",
+ "tests/cp/cp-mv-backup.sh",
+ "tests/cp/cp-mv-enotsup-xattr.sh",
+ "tests/cp/cross-dev-symlink.sh",
+ "tests/cp/deref-slink.sh",
+ "tests/cp/debug.sh",
+ "tests/cp/dir-rm-dest.sh",
+ "tests/cp/dir-slash.sh",
+ "tests/cp/dir-vs-file.sh",
+ "tests/cp/existing-perm-dir.sh",
+ "tests/cp/existing-perm-race.sh",
+ "tests/cp/cp-parents.sh",
+ "tests/cp/fail-perm.sh",
+ "tests/cp/into-self.sh",
+ "tests/cp/keep-directory-symlink.sh",
+ "tests/cp/link-deref.sh",
+ "tests/cp/link-no-deref.sh",
+ "tests/cp/link-preserve.sh",
+ "tests/cp/link-symlink.sh",
+ "tests/cp/file-perm-race.sh",
+ "tests/cp/no-deref-link1.sh",
+ "tests/cp/no-deref-link2.sh",
+ "tests/cp/no-deref-link3.sh",
+ "tests/cp/non-utf8-name.sh",
+ "tests/cp/nfs-removal-race.sh",
+ "tests/cp/parent-perm.sh",
+ "tests/cp/parent-perm-race.sh",
+ "tests/cp/preserve-2.sh",
+ "tests/cp/preserve-link.sh",
+ "tests/cp/preserve-mode.sh",
+ "tests/cp/preserve-gid.sh",
+ "tests/cp/preserve-slink-time.sh",
+ "tests/cp/proc-short-read.sh",
+ "tests/cp/proc-zero-len.sh",
+ "tests/cp/r-vs-symlink.sh",
+ "tests/cp/readonly-dir.sh",
+ "tests/cp/reflink-auto.sh",
+ "tests/cp/reflink-perm.sh",
+ "tests/cp/slink-2-slink.sh",
+ "tests/cp/sparse.sh",
+ "tests/cp/sparse-2.sh",
+ "tests/cp/sparse-extents.sh",
+ "tests/cp/sparse-extents-2.sh",
+ "tests/cp/sparse-perf.sh",
+ "tests/cp/sparse-to-pipe.sh",
+ "tests/cp/special-bits.sh",
+ "tests/cp/special-f.sh",
+ "tests/cp/src-base-dot.sh",
+ "tests/cp/symlink-slash.sh",
+ "tests/cp/thru-dangling.sh",
+ "tests/mv/acl.sh",
+ "tests/mv/update.sh",
+ "tests/mv/i-1.pl",
+ "tests/mv/i-2.sh",
+ "tests/mv/i-3.sh",
+ "tests/mv/i-4.sh",
+ "tests/mv/i-5.sh",
+ "tests/mv/backup-is-src.sh",
+ "tests/mv/childproof.sh",
+ "tests/mv/no-copy.sh",
+ "tests/mv/into-self.sh",
+ "tests/mv/into-self-2.sh",
+ "tests/mv/into-self-3.sh",
+ "tests/mv/into-self-4.sh",
+ "tests/mv/no-target-dir.sh",
+ "tests/mv/backup-dir.sh",
+ "tests/mv/dir2dir.sh",
+ "tests/mv/dup-source.sh",
+ "tests/mv/diag.sh",
+ "tests/mv/dir-file.sh",
+ "tests/mv/force.sh",
+ "tests/mv/hard-link-1.sh",
+ "tests/mv/hard-2.sh",
+ "tests/mv/hard-3.sh",
+ "tests/mv/hard-4.sh",
+ "tests/mv/hardlink-case.sh",
+ "tests/mv/i-link-no.sh",
+ "tests/mv/mv-exchange.sh",
+ "tests/mv/mv-special-1.sh",
+ "tests/mv/mv-special-2.sh",
+ "tests/mv/meta-to-xpart.sh",
+ "tests/mv/sticky-to-xpart.sh",
+ "tests/mv/mv-n.sh",
+ "tests/mv/part-fail.sh",
+ "tests/mv/part-hardlink.sh",
+ "tests/mv/part-rename.sh",
+ "tests/mv/part-symlink.sh",
+ "tests/mv/partition-perm.sh",
+ "tests/mv/perm-1.sh",
+ "tests/mv/symlink-onto-hardlink.sh",
+ "tests/mv/symlink-onto-hardlink-to-self.sh",
+ "tests/mv/to-symlink.sh",
+ "tests/mv/trailing-slash.sh",
+ "tests/mv/atomic.sh",
+ "tests/mv/atomic2.sh",
+ "tests/ln/backup-1.sh",
+ "tests/ln/backup-suffix-traversal.sh",
+ "tests/ln/hard-backup.sh",
+ "tests/ln/hard-to-sym.sh",
+ "tests/ln/misc.sh",
+ "tests/ln/non-utf8-src.sh",
+ "tests/ln/relative.sh",
+ "tests/ln/slash-decorated-nonexistent-dest.sh",
+ "tests/ln/sf-1.sh",
+ "tests/ln/target-1.sh",
+ "tests/rm/dangling-symlink.sh",
+ "tests/rm/d-1.sh",
+ "tests/rm/d-2.sh",
+ "tests/rm/d-3.sh",
+ "tests/rm/dash-hint.sh",
+ "tests/rm/dir-nonrecur.sh",
+ "tests/rm/f-1.sh",
+ "tests/rm/i-never.sh",
+ "tests/rm/i-no-r.sh",
+ "tests/rm/ignorable.sh",
+ "tests/rm/dot-rel.sh",
+ "tests/rm/empty-name.pl",
+ "tests/rm/fail-eacces.sh",
+ "tests/rm/r-1.sh",
+ "tests/rm/r-2.sh",
+ "tests/rm/r-3.sh",
+ "tests/rm/r-4.sh",
+ "tests/rm/rm1.sh",
+ "tests/rm/rm2.sh",
+ "tests/rm/rm3.sh",
+ "tests/rm/rm4.sh",
+ "tests/rm/rm5.sh",
+ "tests/rm/v-slash.sh",
+ "tests/rm/deep-1.sh",
+ "tests/rm/deep-2.sh",
+ "tests/rm/dir-no-w.sh",
+ "tests/rm/empty-inacc.sh",
+ "tests/rm/cycle.sh",
+ "tests/rm/i-1.sh",
+ "tests/rm/isatty.sh",
+ "tests/rm/inaccessible.sh",
+ "tests/rm/interactive-always.sh",
+ "tests/rm/interactive-once.sh",
+ "tests/rm/ir-1.sh",
+ "tests/rm/one-file-system2.sh",
+ "tests/rm/readdir-bug.sh",
+ "tests/rm/rm-readdir-fail.sh",
+ "tests/rm/empty-immutable-skip.sh",
+ "tests/rm/fail-2eperm.sh",
+ "tests/rm/no-give-up.sh",
+ "tests/rm/one-file-system.sh",
+ "tests/rm/read-only.sh",
+ "tests/rm/sunos-1.sh",
+ "tests/rm/unread2.sh",
+ "tests/rm/unread3.sh",
+ "tests/rm/unreadable.pl",
+ "tests/rmdir/ignore.sh",
+ "tests/rmdir/fail-perm.sh",
+ "tests/rmdir/symlink-errors.sh",
+ "tests/rmdir/t-slash.sh",
+ "tests/od/od-N.sh",
+ "tests/od/od.pl",
+ "tests/od/od-j.sh",
+ "tests/od/od-multiple-t.sh",
+ "tests/od/od-endian.sh",
+ "tests/od/od-float.sh",
+ "tests/od/od-x8.sh",
+ "tests/stat/stat-fmt.sh",
+ "tests/stat/stat-printf.pl",
+ "tests/stat/stat-birthtime.sh",
+ "tests/stat/stat-hyphen.sh",
+ "tests/stat/stat-nanoseconds.sh",
+ "tests/stat/stat-slash.sh",
+ "tests/chown/basic.sh",
+ "tests/chown/deref.sh",
+ "tests/chown/separator.sh",
+ "tests/chown/preserve-root.sh",
+ "tests/chgrp/basic.sh",
+ "tests/chgrp/default-no-deref.sh",
+ "tests/chgrp/deref.sh",
+ "tests/chgrp/from.sh",
+ "tests/chgrp/no-x.sh",
+ "tests/chgrp/posix-H.sh",
+ "tests/chgrp/recurse.sh",
+ "tests/chroot/chroot-fail.sh",
+ "tests/chroot/chroot-credentials.sh",
+ "tests/install/install-C.sh",
+ "tests/install/install-C-root.sh",
+ "tests/install/install-C-selinux.sh",
+ "tests/install/install-Z-selinux.sh",
+ "tests/install/basic-1.sh",
+ "tests/install/create-leading.sh",
+ "tests/install/d-slashdot.sh",
+ "tests/install/trap.sh",
+ "tests/install/strip-program.sh",
+ "tests/misc/dircolors.pl",
+ "tests/misc/arch.sh",
+ "tests/chcon/chcon-fail.sh",
+ "tests/misc/coreutils.sh",
+ "tests/misc/mknod.sh",
+ "tests/misc/option-aliases.sh",
+ "tests/pr/pr-tests.pl",
+ "tests/pr/bounded-memory.sh",
+ "tests/ptx/ptx.pl",
+ "tests/ptx/ptx-overrun.sh",
+ "tests/paste/paste.pl",
+ "tests/paste/multi-byte.sh",
+ "tests/expand/expand.pl",
+ "tests/expand/mb.sh",
+ "tests/expand/bounded-memory.sh",
+ "tests/unexpand/unexpand.pl",
+ "tests/unexpand/mb.sh",
+ "tests/unexpand/bounded-memory.sh",
+ "tests/fold/fold.pl",
+ "tests/fold/fold-nbsp.sh",
+ "tests/fold/fold-spaces.sh",
+ "tests/fold/fold-characters.sh",
+ "tests/fold/fold-zero-width.sh",
+ "tests/printf/printf.sh",
+ "tests/printf/printf-hex.sh",
+ "tests/printf/printf-cov.pl",
+ "tests/printf/printf-indexed.sh",
+ "tests/printf/printf-mb.sh",
+ "tests/printf/printf-quote.sh",
+ "tests/printf/printf-surprise.sh",
+ "tests/nl/nl.sh",
+ "tests/nl/multiple-files.sh",
+ "tests/nl/multibyte.sh",
+ "tests/numfmt/numfmt.pl",
+ "tests/numfmt/mb-non-utf8.sh",
+ "tests/shuf/shuf.sh",
+ "tests/fmt/base.pl",
+ "tests/fmt/goal-option.sh",
+ "tests/fmt/long-line.sh",
+ "tests/fmt/non-space.sh",
+ "tests/fmt/width.sh",
+ "tests/head/head-pos.sh",
+ "tests/tail/tail-c.sh",
+ "tests/tail/tail.pl",
+ "tests/tail/basic-seek.sh",
+ "tests/tail/F-headers.sh",
+ "tests/tail/F-vs-missing.sh",
+ "tests/tail/F-vs-rename.sh",
+ "tests/tail/descriptor-vs-rename.sh",
+ "tests/tail/start-middle.sh",
+ "tests/tail/tail-n0f.sh",
+ "tests/tail/truncate.sh",
+ "tests/tail/overlay-headers.sh",
+ "tests/tail/flush-initial.sh",
+ "tests/tail/retry.sh",
+ "tests/tail/symlink.sh",
+ "tests/tail/wait.sh",
+ "tests/tail/assert.sh",
+ "tests/tail/assert-2.sh",
+ "tests/tail/follow-name.sh",
+ "tests/tail/inotify-dir-recreate.sh",
+ "tests/tail/follow-stdin.sh",
+ "tests/tail/pid.sh",
+ "tests/tail/pid-pipe.sh",
+ "tests/tail/pipe-f.sh",
+ "tests/tail/pipe-f2.sh",
+ "tests/tail/proc-ksyms.sh",
+ "tests/tail/tail-sysfs.sh",
+ "tests/tail/append-only.sh",
+ "tests/tail/debug.sh",
+ "tests/tail/end-of-device.sh",
+ "tests/tail/inotify-only-regular.sh",
+ "tests/tail/inotify-rotate-resources.sh",
+ "tests/split/filter.sh",
+ "tests/split/suffix-length.sh",
+ "tests/split/suffix-auto-length.sh",
+ "tests/split/additional-suffix.sh",
+ "tests/split/b-chunk.sh",
+ "tests/split/l-chunk.sh",
+ "tests/split/l-chunk-root.sh",
+ "tests/split/r-chunk.sh",
+ "tests/split/numeric.sh",
+ "tests/split/record-sep.sh",
+ "tests/split/split-io-err.sh",
+ "tests/split/fail.sh",
+ "tests/split/guard-input.sh",
+ "tests/split/non-utf8.sh",
+ "tests/split/line-bytes.sh",
+ "tests/csplit/csplit.sh",
+ "tests/csplit/csplit-1000.sh",
+ "tests/csplit/csplit-suppress-matched.pl",
+ "tests/csplit/csplit-io-err.sh",
+ "tests/shred/shred-exact.sh",
+ "tests/shred/shred-passes.sh",
+ "tests/shred/shred-remove.sh",
+ "tests/shred/shred-size.sh",
+ "tests/dd/misc.sh",
+ "tests/dd/bytes.sh",
+ "tests/dd/conv-case.sh",
+ "tests/dd/stderr.sh",
+ "tests/dd/stats.sh",
+ "tests/dd/ascii.sh",
+ "tests/dd/reblock.sh",
+ "tests/dd/unblock.pl",
+ "tests/dd/unblock-sync.sh",
+ "tests/dd/skip-seek.pl",
+ "tests/dd/skip-seek2.sh",
+ "tests/dd/sparse.sh",
+ "tests/dd/not-rewound.sh",
+ "tests/dd/direct.sh",
+ "tests/dd/nocache.sh",
+ "tests/dd/nocache_eof.sh",
+ "tests/dd/nocache_fail.sh",
+ "tests/dd/fail-ftruncate-fstat.sh",
+ "tests/dd/partial-write.sh",
+ "tests/dd/skip-seek-past-file.sh",
+ "tests/dd/skip-seek-past-dev.sh",
+ "tests/sort/sort.pl",
+ "tests/sort/sort-locale.sh",
+ "tests/sort/sort-debug-warn.sh",
+ "tests/sort/sort-debug-keys.sh",
+ "tests/sort/sort-discrim.sh",
+ "tests/sort/sort-field-limit.sh",
+ "tests/sort/sort-exit-early.sh",
+ "tests/sort/sort-rand.sh",
+ "tests/sort/sort-h-thousands-sep.sh",
+ "tests/sort/sort-compress.sh",
+ "tests/sort/sort-files0-from.pl",
+ "tests/sort/sort-float.sh",
+ "tests/sort/sort-merge.pl",
+ "tests/sort/sort-month.sh",
+ "tests/sort/sort-NaN-infloop.sh",
+ "tests/sort/sort-unique.sh",
+ "tests/sort/sort-version.sh",
+ "tests/timeout/init-parent.sh",
+ "tests/timeout/timeout-blocked.pl",
+ "tests/timeout/timeout-parameters.sh",
+ "tests/timeout/timeout.sh",
+ "tests/timeout/timeout-large-parameters.sh",
+ "tests/timeout/timeout-group.sh",
+ "tests/runcon/runcon-compute.sh",
+ "tests/runcon/runcon-no-reorder.sh",
+ "tests/misc/nohup.sh",
+ "tests/nice/nice-fail.sh",
+ "tests/nice/nice.sh",
+ "tests/misc/stdbuf.sh",
+ "tests/misc/sync.sh",
+ "tests/misc/sleep.sh",
+ "tests/misc/yes.sh",
+ "tests/misc/kill.sh",
+ "tests/misc/time-style.sh",
+ "tests/misc/tsort.pl",
+ "tests/misc/usage_vs_getopt.sh",
+ "tests/misc/getopt_vs_usage.sh",
+ "tests/misc/usage_vs_refs.sh",
+ "tests/misc/warning-errors.sh",
+ "tests/chmod/c-option.sh",
+ "tests/chmod/equal-x.sh",
+ "tests/chmod/equals.sh",
+ "tests/chmod/ignore-symlink.sh",
+ "tests/chmod/no-x.sh",
+ "tests/chmod/octal.sh",
+ "tests/chmod/only-op.sh",
+ "tests/chmod/partial-fail.sh",
+ "tests/chmod/setgid.sh",
+ "tests/chmod/inaccessible.sh",
+ "tests/chmod/silent.sh",
+ "tests/chmod/symlinks.sh",
+ "tests/chmod/thru-dangling.sh",
+ "tests/chmod/umask-x.sh",
+ "tests/chmod/usage.sh",
+ "tests/ls/ls-misc.pl",
+ "tests/ls/ls-time.sh",
+ "tests/ls/recursive.sh",
+ "tests/ls/a-option.sh",
+ "tests/ls/no-arg.sh",
+ "tests/ls/file-type.sh",
+ "tests/ls/classify.sh",
+ "tests/ls/abmon-align.sh",
+ "tests/ls/birthtime.sh",
+ "tests/ls/color-clear-to-eol.sh",
+ "tests/ls/color-dtype-dir.sh",
+ "tests/ls/color-norm.sh",
+ "tests/ls/color-term.sh",
+ "tests/ls/color-ext.sh",
+ "tests/ls/dangle.sh",
+ "tests/ls/infloop.sh",
+ "tests/ls/symlink-slash.sh",
+ "tests/ls/symlink-loop.sh",
+ "tests/ls/zero-option.sh",
+ "tests/ls/time-style-diag.sh",
+ "tests/ls/x-option.sh",
+ "tests/ls/m-option.sh",
+ "tests/ls/block-size.sh",
+ "tests/ls/dired.sh",
+ "tests/ls/follow-slink.sh",
+ "tests/ls/group-dirs.sh",
+ "tests/ls/hex-option.sh",
+ "tests/ls/hyperlink.sh",
+ "tests/ls/inode.sh",
+ "tests/ls/w-option.sh",
+ "tests/ls/multihardlink.sh",
+ "tests/ls/non-utf8-hidden.sh",
+ "tests/ls/quote-align.sh",
+ "tests/ls/quoting-utf8.sh",
+ "tests/ls/acl.sh",
+ "tests/ls/capability.sh",
+ "tests/ls/no-cap.sh",
+ "tests/ls/getxattr-speedup.sh",
+ "tests/ls/nameless-uid.sh",
+ "tests/ls/size-align.sh",
+ "tests/ls/symlink-quote.sh",
+ "tests/ls/slink-acl.sh",
+ "tests/ls/sort-width-option.sh",
+ "tests/ls/readdir-mountpoint-inode.sh",
+ "tests/ls/selinux-segfault.sh",
+ "tests/ls/stat-vs-dirent.sh",
+ "tests/ls/stat-free-color.sh",
+ "tests/ls/stat-free-symlinks.sh",
+ "tests/ls/stat-dtype.sh",
+ "tests/ls/stat-failed.sh",
+ "tests/ls/removed-directory.sh",
+ "tests/ls/root-rel-symlink-color.sh",
+ "tests/ls/rt-1.sh",
+ "tests/fold/multiple-files.sh",
+ "tests/mkdir/p-1.sh",
+ "tests/mkdir/p-2.sh",
+ "tests/mkdir/p-3.sh",
+ "tests/mkdir/p-acl.sh",
+ "tests/mkdir/p-slashdot.sh",
+ "tests/mkdir/p-thru-slink.sh",
+ "tests/mkdir/p-v.sh",
+ "tests/mkdir/parents.sh",
+ "tests/mkdir/perm.sh",
+ "tests/mkdir/special-1.sh",
+ "tests/mkdir/t-slash.sh",
+ "tests/mkdir/restorecon.sh",
+ "tests/mkdir/selinux.sh",
+ "tests/mkdir/writable-under-readonly.sh",
+ "tests/misc/read-errors.sh",
+ "tests/misc/close-stdout.sh",
+ "tests/misc/responsive.sh",
+ "tests/misc/xattr.sh",
+ "tests/misc/selinux.sh",
+ "tests/split/lines.sh",
+ "tests/factor/factor.pl",
+ "tests/factor/factor-parallel.sh",
+];
+const EXPENSIVE_TESTS = [
+ "tests/stty/stty-pairs.sh",
+ "tests/sort/sort-compress-proc.sh",
+ "tests/du/fd-leak.sh",
+ "tests/mv/leak-fd.sh",
+ "tests/rm/hash.sh",
+ "tests/tail/big-4gb.sh",
+ "tests/tail/inotify-hash-abuse.sh",
+ "tests/tail/inotify-hash-abuse2.sh",
+ "tests/tail/inotify-rotate.sh",
+];
+const VERY_EXPENSIVE_TESTS = [
+ "tests/od/big-w.sh",
+ "tests/du/2g.sh",
+ "tests/du/8gb.sh",
+ "tests/rm/ext3-perf.sh",
+ "tests/sort/sort-benchmark-random.sh",
+ "tests/sort/sort-compress-hang.sh",
+ "tests/cp/perm.sh",
+ "tests/factor/t00.sh",
+ "tests/factor/t01.sh",
+ "tests/factor/t02.sh",
+ "tests/factor/t03.sh",
+ "tests/factor/t04.sh",
+ "tests/factor/t05.sh",
+ "tests/factor/t06.sh",
+ "tests/factor/t07.sh",
+ "tests/factor/t08.sh",
+ "tests/factor/t09.sh",
+ "tests/factor/t10.sh",
+ "tests/factor/t11.sh",
+ "tests/factor/t12.sh",
+ "tests/factor/t13.sh",
+ "tests/factor/t14.sh",
+ "tests/factor/t15.sh",
+ "tests/factor/t16.sh",
+ "tests/factor/t17.sh",
+ "tests/factor/t18.sh",
+ "tests/factor/t19.sh",
+ "tests/factor/t20.sh",
+ "tests/factor/t21.sh",
+ "tests/factor/t22.sh",
+ "tests/factor/t23.sh",
+ "tests/factor/t24.sh",
+ "tests/factor/t25.sh",
+ "tests/factor/t26.sh",
+ "tests/factor/t27.sh",
+ "tests/factor/t28.sh",
+ "tests/factor/t29.sh",
+ "tests/factor/t30.sh",
+ "tests/factor/t31.sh",
+ "tests/factor/t32.sh",
+ "tests/factor/t33.sh",
+ "tests/factor/t34.sh",
+ "tests/factor/t35.sh",
+ "tests/factor/t36.sh",
+ "tests/factor/t37.sh",
+ "tests/factor/t38.sh",
+ "tests/factor/t39.sh",
+ "tests/factor/t40.sh",
+];
+let exitCode = 0;
+
+try {
+ await run(["tar", "-xf", tarball, "-C", work], root);
+ await prepareExtractedHarness(source);
+ let tests = args.length ? args : all || rootTestsOnly || nonRootTestsOnly ? await readStandardTests(source) : veryExpensive ? [...DEFAULT_TESTS, ...EXPENSIVE_TESTS, ...VERY_EXPENSIVE_TESTS] : expensive ? [...DEFAULT_TESTS, ...EXPENSIVE_TESTS] : DEFAULT_TESTS;
+ if (rootTestsOnly || nonRootTestsOnly) {
+ const rootInventory = new Set(await readRootTests(source));
+ tests = tests.filter((test) => rootTestsOnly === rootInventory.has(test));
+ }
+ if (list) {
+ console.log(tests.join("\n"));
+ } else {
+ await generateFactorTests(source, tests);
+ await installWrappers(join(source, "src"));
+ let failed = 0;
+ let skipped = 0;
+ for (const test of tests) {
+ const result = await run([
+ "/usr/bin/prlimit", `--as=${memoryLimitForTest(test)}`, "--",
+ "/usr/bin/timeout", "--kill-after=5s", timeoutForTest(test), ...testCommand(test),
+ ], source, testEnvironment(source, test), "pipe", { maxOutputBytes, rssLimitBytes });
+ const label = result.code === 0 ? "PASS" : result.code === 77 ? "SKIP" : "FAIL";
+ if (result.code === 77) skipped++;
+ else if (result.code !== 0) failed++;
+ console.log(`${label} ${test}`);
+ if (result.stdout) process.stdout.write(result.stdout);
+ if (result.stderr) process.stderr.write(result.stderr);
+ if (result.resourceLimitMessage) process.stderr.write(`${result.resourceLimitMessage}\n`);
+ if (result.stdoutTruncated) process.stdout.write(`[stdout truncated after ${maxOutputBytes} bytes]\n`);
+ if (result.stderrTruncated) process.stderr.write(`[stderr truncated after ${maxOutputBytes} bytes]\n`);
+ }
+ console.log(`GNU tests: ${tests.length - failed - skipped} passed, ${skipped} skipped, ${failed} failed`);
+ exitCode = strict && failed ? 1 : 0;
+ }
+} finally {
+ if (keep) console.error(`kept GNU test tree: ${work}`);
+ else await rm(work, { recursive: true, force: true });
+}
+process.exitCode = exitCode;
+
+async function prepareExtractedHarness(dir) {
+ const initPath = join(dir, "tests/init.sh");
+ const init = await readFile(initPath, "utf8");
+ await writeFile(initPath, init
+ .replace('LC_ALL=C tr "\\\\351" x', 'LC_ALL=C /usr/bin/tr "\\\\351" x')
+ .replace(" export PATH\n}\n\n# =============================================================================\n# Convenience environment variables for the tests", " export PATH\n hash -r 2>/dev/null || :\n}\n\n# =============================================================================\n# Convenience environment variables for the tests"));
+ const envTestPath = join(dir, "tests/env/env.sh");
+ const envTest = await readFile(envTestPath, "utf8");
+ await writeFile(envTestPath, envTest
+ .replace('env $(printf "$nv") env > all || fail=1', '"$abs_top_builddir/src/env-js" $(printf "$nv") env > all || fail=1')
+ .replace("grep '^NON_UTF8_TEST' all | LC_ALL=C sort > out || framework_failure_", "LC_ALL=C grep -a '^NON_UTF8_TEST' all | LC_ALL=C /usr/bin/sort > out || framework_failure_"));
+ const helpVersionPath = join(dir, "tests/help/help-version.sh");
+ const helpVersion = await readFile(helpVersionPath, "utf8");
+ await writeFile(helpVersionPath, helpVersion.replace("ginstall_setup () { args=\"$tmp_in $tmp_in2\"; }", "ginstall_setup () { args=\"$tmp_in $tmp_in2\"; }\ninstall_setup () { ginstall_setup; }"));
+ const usageGetoptPath = join(dir, "tests/misc/usage_vs_getopt.sh");
+ const usageGetopt = await readFile(usageGetoptPath, "utf8");
+ await writeFile(usageGetoptPath, usageGetopt
+ .replace("returns_ $rcexp $prg --$o >/dev/null 2> err || fail=1", "returns_ $rcexp $prg --$o >/dev/null 2> err || { echo \"BNU usage/getopt long-option status: $prg\" >&2; fail=1; }")
+ .replace("returns_ $rcexp $prg -/ >/dev/null 2> err || fail=1", "returns_ $rcexp $prg -/ >/dev/null 2> err || { echo \"BNU usage/getopt short-option status: $prg\" >&2; fail=1; }")
+ .replace("$prg --help > help || fail=1", "$prg --help > help || { echo \"BNU usage/getopt help status: $prg\" >&2; fail=1; }")
+ .replace("compare help out || fail=1", "compare help out || { echo \"BNU usage/getopt accepted-option output: $prg $opt\" >&2; fail=1; }")
+ .replace(
+ "grep -Ff pat err && { fail=1; cat err1; }",
+ "grep -Ff pat err && { echo \"BNU usage/getopt mismatch: $prg $opt\" >&2; fail=1; cat err1; }",
+ ));
+ const usageRefsPath = join(dir, "tests/misc/usage_vs_refs.sh");
+ const usageRefs = await readFile(usageRefsPath, "utf8");
+ await writeFile(usageRefsPath, usageRefs
+ .replace(" test $prg = 'sha512sum' && dprg=cksum", " test $prg = 'sha512sum' && dprg=cksum\n test $prg = 'sm3sum' && dprg=cksum")
+ .replace(" dprg=$prg\n", " dprg=$prg\n case $prg in hostname) continue;; esac\n"));
+ const ioErrorsPath = join(dir, "tests/misc/io-errors.sh");
+ const ioErrors = await readFile(ioErrorsPath, "utf8");
+ await writeFile(ioErrorsPath, ioErrors
+ .replaceAll(" rm -f full.err", " /bin/rm -f full.err")
+ .replaceAll(" rm -f pipe.err", " /bin/rm -f pipe.err")
+ .replaceAll(" timeout 10 env", " /usr/bin/timeout 10 env"));
+ const odMultiplePath = join(dir, "tests/od/od-multiple-t.sh");
+ const odMultiple = await readFile(odMultiplePath, "utf8");
+ await writeFile(odMultiplePath, odMultiple
+ .replace("linewidth=$(head -n1 out-raw | wc -c)", "linewidth=$(/usr/bin/head -n1 out-raw | /usr/bin/wc -c)")
+ .replace("linecount=$(wc -l < out-raw)", "linecount=$(/usr/bin/wc -l < out-raw)")
+ .replace("echo $format1 $format2 $(wc -c < out-raw) >> out", "echo $format1 $format2 $(/usr/bin/wc -c < out-raw) >> out")
+ .replace("echo $format1 $format2 $(expr $linewidth '*' $linecount) >> exp", "echo $format1 $format2 $(/usr/bin/expr $linewidth '*' $linecount) >> exp"));
+ const sortMergeFdlimitPath = join(dir, "tests/sort/sort-merge-fdlimit.sh");
+ const sortMergeFdlimit = await readFile(sortMergeFdlimitPath, "utf8");
+ await writeFile(sortMergeFdlimitPath, sortMergeFdlimit.replace(
+ "print_ver_ sort\n",
+ `print_ver_ sort\n(ulimit -n 10 && ${shellQuote(process.execPath)} -e 'process.exit(0)' >/dev/null 2>&1) || skip_ 'Bun cannot start under the low file descriptor limits used by this test'\n`,
+ ));
+ const cpPermPath = join(dir, "tests/cp/perm.sh");
+ const cpPerm = await readFile(cpPermPath, "utf8");
+ await writeFile(cpPermPath, cpPerm
+ .replaceAll("touch src || exit 1", "/usr/bin/touch src || exit 1")
+ .replaceAll("touch dest || exit 1", "/usr/bin/touch dest || exit 1")
+ .replaceAll("chmod u=r,g=rx,o= src || exit 1", "/usr/bin/chmod u=r,g=rx,o= src || exit 1")
+ .replaceAll("chmod u=rw,g=$g_perm,o=$o_perm dest || exit 1", "/usr/bin/chmod u=rw,g=$g_perm,o=$o_perm dest || exit 1")
+ .replaceAll("stat --format=%A", "/usr/bin/stat --format=%A")
+ .replaceAll("rm -f dest", "/bin/rm -f dest")
+ .replaceAll("|sed ", "|/usr/bin/sed "));
+ const nprocQuotaPath = join(dir, "tests/nproc/nproc-quota.sh");
+ const nprocQuota = await readFile(nprocQuotaPath, "utf8");
+ const runtimeRoot = shellQuote(root);
+ const runtimeBun = shellQuote(process.execPath);
+ await writeFile(nprocQuotaPath, nprocQuota
+ .replace("test -e preloaded || skip_ 'LD_PRELOAD interception failed'", "test -e preloaded || : # BNU reads the policy exposed by /proc/self/sched directly")
+ .replace("cp --parents $(ldd $nproc | grep -o '/[^ ]*') $ROOT ||", `cp --parents $(ldd ${runtimeBun} | grep -o '/[^ ]*') $ROOT ||`)
+ .replace("cp $nproc $ROOT || framework_failure_\ncp k.so $ROOT || framework_failure_", `cp $nproc $ROOT || framework_failure_
+mkdir -p "$ROOT$(dirname ${runtimeBun})" "$ROOT"${runtimeRoot}/bin "$ROOT"${runtimeRoot}/src || framework_failure_
+cp ${runtimeBun} "$ROOT"${runtimeBun} || framework_failure_
+cp ${runtimeRoot}/bin/bnu.js "$ROOT"${runtimeRoot}/bin/bnu.js || framework_failure_
+cp ${runtimeRoot}/src/coreutils.js "$ROOT"${runtimeRoot}/src/coreutils.js || framework_failure_
+cp k.so $ROOT || framework_failure_`)
+ .replace(
+ 'NPROC() { LD_PRELOAD=$LD_PRELOAD:./k.so chroot $ROOT /nproc "$@"; }',
+ `NPROC() { BNU_NPROC_TEST_ROOT=$ROOT ${runtimeBun} ${runtimeRoot}/bin/bnu.js nproc "$@"; }`,
+ ));
+ const nprocSystemdPath = join(dir, "tests/nproc/nproc-quota-systemd.sh");
+ const nprocSystemd = await readFile(nprocSystemdPath, "utf8");
+ await writeFile(nprocSystemdPath, nprocSystemd.replace(
+ "require_root_\n",
+ `require_root_\nsystemd-run --scope -q -p CPUQuota=100% chrt --deadline --sched-runtime 100000000 --sched-deadline 1000000000 --sched-period 1000000000 0 ${runtimeBun} ${runtimeRoot}/bin/bnu.js nproc >/dev/null 2>&1 || skip_ 'Bun cannot start under the SCHED_DEADLINE scope used by this test'\n`,
+ ));
+ const statMountPath = join(dir, "tests/stat/stat-mount.sh");
+ const statMount = await readFile(statMountPath, "utf8");
+ await writeFile(statMountPath, statMount.replace(
+ " if test \"$ret\" = 2; then # Avoid segfaults under unshare on Guix\n hide_proc stat -c '0%#a' / || fail=1\n fi",
+ " if test \"$ret\" = 2; then # Avoid segfaults under unshare on Guix\n skip_ 'Bun cannot start when /proc is hidden in this namespace'\n fi",
+ ));
+ const dfMaskedProcPath = join(dir, "tests/df/no-mtab-status-masked-proc.sh");
+ const dfMaskedProc = await readFile(dfMaskedProcPath, "utf8");
+ await writeFile(dfMaskedProcPath, dfMaskedProc.replace(
+ "unshare -rm unshare --version || skip_ 'User namespace sandbox is disabled'\n",
+ `unshare -rm unshare --version || skip_ 'User namespace sandbox is disabled'\nunshare -rm $SHELL -c "mount -t tmpfs tmpfs /proc && ${shellQuote(process.execPath)} -e 'process.exit(0)'" >/dev/null 2>&1 || skip_ 'Bun cannot start when /proc is hidden in this namespace'\n`,
+ ));
+ await writeFile(join(dir, "tests/df/no-mtab-status.sh"), `#!/bin/sh
+. ./tests/init.sh; path_prepend_ ./src
+print_ver_ df
+export BNU_DF_MOUNTINFO_ERROR=1
+
+df '.' || fail=1
+df -i '.' || fail=1
+df -T '.' || fail=1
+df -Ti '.' || fail=1
+df --total '.' || fail=1
+
+returns_ 1 df || fail=1
+returns_ 1 df -i || fail=1
+returns_ 1 df -T || fail=1
+returns_ 1 df -Ti || fail=1
+returns_ 1 df --total || fail=1
+returns_ 1 df -a || fail=1
+returns_ 1 df -a '.' || fail=1
+returns_ 1 df -l || fail=1
+returns_ 1 df -l '.' || fail=1
+returns_ 1 df -t hello || fail=1
+returns_ 1 df -t hello '.' || fail=1
+returns_ 1 df -x hello || fail=1
+returns_ 1 df -x hello '.' || fail=1
+Exit $fail
+`);
+ await writeFile(join(dir, "tests/df/skip-duplicates.sh"), `#!/bin/sh
+. ./tests/init.sh; path_prepend_ ./src
+print_ver_ df
+
+cat >mountinfo <<'EOF'
+1 0 0:1 / / rw - ext4 fsname rw
+2 0 0:1 / / rw - ext4 /fsname rw
+3 0 0:1 / /tmp rw - t1 virtfs rw
+4 0 0:1 / /tmp rw - t2 virtfs2 rw
+EOF
+export BNU_DF_MOUNTINFO_FILE=$PWD/mountinfo
+
+df -T >out || fail=1
+test "$(wc -l <out)" -eq 3 || { cat out; fail=1; }
+grep '/fsname' out >/dev/null || { cat out; fail=1; }
+grep 'virtfs2.*t2' out >/dev/null || { cat out; fail=1; }
+
+df --total >out || fail=1
+test "$(wc -l <out)" -eq 4 || { cat out; fail=1; }
+
+df -a >out || fail=1
+test "$(wc -l <out)" -eq 5 || { cat out; fail=1; }
+test "$(grep -c 'fsname' out)" -eq 2 || { cat out; fail=1; }
+test "$(grep -c 'virtfs' out)" -eq 2 || { cat out; fail=1; }
+
+df '.' '.' >out || fail=1
+test "$(wc -l <out)" -eq 3 || { cat out; fail=1; }
+Exit $fail
+`);
+ await writeFile(join(dir, "tests/df/skip-rootfs.sh"), `#!/bin/sh
+. ./tests/init.sh; path_prepend_ ./src
+print_ver_ df
+
+cat >mountinfo <<'EOF'
+1 0 0:1 / / rw - rootfs rootfs rw
+2 0 0:2 / /tmp rw - ext4 /dev/test rw
+EOF
+export BNU_DF_MOUNTINFO_FILE=$PWD/mountinfo
+
+df -a >out || fail=1
+grep '^rootfs' out >/dev/null || { cat out; fail=1; }
+
+df >out || fail=1
+grep '^rootfs' out >/dev/null && { cat out; fail=1; }
+
+returns_ 1 df -t rootfs >out || fail=1
+grep '^rootfs' out >/dev/null && { cat out; fail=1; }
+
+df -a -t rootfs >out || fail=1
+grep '^rootfs' out >/dev/null || { cat out; fail=1; }
+
+df -a -x rootfs >out || fail=1
+grep '^rootfs' out >/dev/null && { cat out; fail=1; }
+Exit $fail
+`);
+ await writeFile(join(dir, "tests/du/bigtime.sh"), `#!/bin/sh
+. ./tests/init.sh; path_prepend_ ./src
+print_ver_ du
+
+export LC_ALL=C
+export TZ=UTC0
+bignum=9223372036854775807
+touch future || framework_failure_
+export BNU_DU_TEST_TIME_SECONDS=$bignum
+
+printf "0\t$bignum\tfuture\n" >exp || framework_failure_
+printf "du: time '$bignum' is out of range\n" >err_ok || framework_failure_
+
+du --time future >out 2>err || fail=1
+sed 's/^[0-9][0-9]*/0/' out >k && mv k out
+compare exp out || fail=1
+compare err err_ok || fail=1
+Exit $fail
+`);
+ await writeFile(join(dir, "tests/rm/r-root.sh"), `#!/bin/sh
+. ./tests/init.sh; path_prepend_ ./src
+print_ver_ rm
+skip_if_root_
+
+exercise_intercepted_() {
+ rm -f x out err || framework_failure_
+ BNU_RM_TEST_INTERCEPT_FILE="$PWD/x" \
+ rm -rv --one-file-system "$@" </dev/null >out 2>err
+}
+
+mkdir dir || framework_failure_
+exercise_intercepted_ dir || fail=1
+test -d dir || fail=1
+test -f x || fail=1
+
+touch file || framework_failure_
+exercise_intercepted_ file || fail=1
+test -f file || fail=1
+test -f x || fail=1
+
+cat >exp <<'EOF'
+rm: it is dangerous to operate recursively on '/'
+rm: use --no-preserve-root to override this failsafe
+EOF
+
+ln -s / rootlink || framework_failure_
+ln -s rootlink rootlink2 || framework_failure_
+ln -sr / rootlink3 || framework_failure_
+for opts in '/' '--preserve-root /' '//' '///' '////' 'rootlink/' 'rootlink2/' 'rootlink3/'; do
+ rm -f x out err || framework_failure_
+ returns_ 1 env BNU_RM_TEST_INTERCEPT_FILE="$PWD/x" \
+ rm -rv --one-file-system $opts </dev/null >out 2>err.t || fail=1
+ sed "1c rm: it is dangerous to operate recursively on '/'" err.t >err
+ compare /dev/null out || fail=1
+ compare exp err || fail=1
+ test ! -e x || fail=1
+done
+
+returns_ 1 rm -r --no-preserve / >out 2>err || fail=1
+printf '%s\n' 'rm: you may not abbreviate the --no-preserve-root option' >exp_opt
+compare exp_opt err || fail=1
+
+touch file1 file2 || framework_failure_
+returns_ 1 rm -rv --one-file-system --preserve-root file1 / file2 >out 2>err || fail=1
+test ! -e file1 && test ! -e file2 || fail=1
+compare exp err || fail=1
+grep "removed 'file1'" out >/dev/null || fail=1
+grep "removed 'file2'" out >/dev/null || fail=1
+
+rm -f x out err || framework_failure_
+BNU_RM_TEST_INTERCEPT_FILE="$PWD/x" \
+ rm -rv --one-file-system --interactive=never --no-preserve-root / </dev/null >out 2>err || fail=1
+test -f x || fail=1
+grep "dangerous to operate recursively" err >/dev/null && fail=1
+
+Exit $fail
+`);
+ for (const testName of ["can-e.sh", "can-f.sh", "can-m.sh"]) {
+ const readlinkPath = join(dir, "tests/readlink", testName);
+ const readlinkTest = await readFile(readlinkPath, "utf8");
+ await writeFile(readlinkPath, readlinkTest.replace(
+ " v=$(returns_ 1 readlink -e .) || fail=1\n",
+ " v=$(returns_ 1 readlink -e . 2>/dev/null) || fail=1\n",
+ ));
+ }
+ for (const testName of ["inotify-race.sh", "inotify-race2.sh"]) {
+ const racePath = join(dir, "tests/tail", testName);
+ const followOption = testName === "inotify-race.sh" ? "-f" : "-F";
+ const mutation = testName === "inotify-race.sh"
+ ? "echo race-visible >> file"
+ : "echo race-visible > file.new && mv file.new file";
+ await writeFile(racePath, `#!/bin/sh
+. ./tests/init.sh; path_prepend_ ./src
+print_ver_ tail sleep
+require_inotify_supported_
+
+pid=
+sleeppid=
+cleanup_() {
+ test -z "$pid" || { kill "$pid" 2>/dev/null || :; wait "$pid" 2>/dev/null || :; }
+ test -z "$sleeppid" || { kill "$sleeppid" 2>/dev/null || :; wait "$sleeppid" 2>/dev/null || :; }
+}
+
+touch file tail.out || framework_failure_
+env sleep 30 & sleeppid=$!
+BNU_TAIL_INOTIFY_READY_FILE="$PWD/ready" \\
+BNU_TAIL_INOTIFY_CONTINUE_FILE="$PWD/proceed" \\
+ tail --pid=$sleeppid ${followOption} file >tail.out 2>tail.err & pid=$!
+
+for i in $(seq 300); do
+ test -e ready && break
+ kill -0 "$pid" 2>/dev/null || { cat tail.err; fail=1; break; }
+ /usr/bin/sleep .01
+done
+test -e ready || { echo 'tail did not reach the pre-inotify synchronization point' >&2; fail=1; }
+
+${mutation} || framework_failure_
+touch proceed || framework_failure_
+
+seen=
+for i in $(seq 300); do
+ grep '^race-visible$' tail.out >/dev/null 2>&1 && { seen=1; break; }
+ kill -0 "$pid" 2>/dev/null || break
+ /usr/bin/sleep .01
+done
+test "$seen" = 1 || { cat tail.err; cat tail.out; fail=1; }
+
+kill "$sleeppid" 2>/dev/null || :
+wait "$sleeppid" 2>/dev/null || :
+sleeppid=
+wait "$pid" 2>/dev/null || :
+pid=
+Exit $fail
+`);
+ }
+ const sttyPath = join(dir, "tests/stty/stty.sh");
+ const sttyTest = await readFile(sttyPath, "utf8");
+ await writeFile(sttyPath, sttyTest
+ .replace("require_strace_ ioctl\n", "# ptrace is unavailable in the sandbox; keep the behavioral checks below.\n")
+ .replace(`# Ensure we validate options before accessing the device
+strace -o log1 -e ioctl stty --version || fail=1
+n_ioctl1=$(wc -l < log1) || framework_failure_
+returns_ 1 strace -o log2 -e ioctl stty -blahblah || fail=1
+n_ioctl2=$(wc -l < log2) || framework_failure_
+test "$n_ioctl1" -ge "$n_ioctl2" || fail=1
+
+`, ""));
+ const bunMemoryProbe = (headroom) => `get_min_ulimit_v_()
+{
+ for v in $(/usr/bin/seq 400000 50000 2000000); do
+ if ulimit_supported_ "$v" "$@"; then
+ echo $(($v + ${headroom}))
+ return 0
+ fi
+ done
+ echo 1
+ return 1
+}
+`;
+ for (const relative of ["tests/cut/bounded-memory.sh", "tests/cut/cut-huge-range.sh", "tests/expand/bounded-memory.sh", "tests/fold/fold-zero-width.sh", "tests/pr/bounded-memory.sh", "tests/printf/printf-surprise.sh", "tests/unexpand/bounded-memory.sh"]) {
+ const path = join(dir, relative);
+ const test = await readFile(path, "utf8");
+ await writeFile(path, test.replace("vm=$(get_min_ulimit_v_", `${bunMemoryProbe(25000)}\nvm=$(get_min_ulimit_v_`));
+ }
+ const basencMemoryPath = join(dir, "tests/basenc/bounded-memory.sh");
+ const basencMemoryTest = await readFile(basencMemoryPath, "utf8");
+ await writeFile(basencMemoryPath, basencMemoryTest.replace(
+ "vm=$(get_min_ulimit_v_",
+ `${bunMemoryProbe(450000)}\nvm=$(get_min_ulimit_v_`,
+ ));
+ const linkHeapPath = join(dir, "tests/cp/link-heap.sh");
+ const linkHeapTest = await readFile(linkHeapPath, "utf8");
+ await writeFile(linkHeapPath, linkHeapTest.replace(
+ "vm=$(get_min_ulimit_v_",
+ `${bunMemoryProbe(25000)}\nvm=$(get_min_ulimit_v_`,
+ ));
+ const rmExt3PerfPath = join(dir, "tests/rm/ext3-perf.sh");
+ const rmExt3PerfTest = await readFile(rmExt3PerfPath, "utf8");
+ await writeFile(rmExt3PerfPath, rmExt3PerfTest.replace(
+ "df -T -t ext3 -t ext4dev -t ext4 .",
+ "/bin/df -T -t ext3 -t ext4dev -t ext4 .",
+ ));
+ const rmManyEntriesPath = join(dir, "tests/rm/many-dir-entries-vs-OOM.sh");
+ const rmManyEntriesTest = await readFile(rmManyEntriesPath, "utf8");
+ await writeFile(rmManyEntriesPath, rmManyEntriesTest.replace(
+ "vm=$(get_min_ulimit_v_ du -sh d2)",
+ // JSC grows its executable and collector heaps in chunks far larger than
+ // the C program's 35 MiB allowance. Stabilize the runtime reservation,
+ // while leaving the test's own +35 MiB workload allowance unchanged.
+ `${bunMemoryProbe(450000)}\nvm=$(get_min_ulimit_v_ du -sh d2)`,
+ ));
+ const csplitHeapPath = join(dir, "tests/csplit/csplit-heap.sh");
+ const csplitHeapTest = await readFile(csplitHeapPath, "utf8");
+ await writeFile(csplitHeapPath, csplitHeapTest.replace(
+ "vm=$(get_min_ulimit_v_ csplit -z f %n%1)",
+ `${bunMemoryProbe(25000)}\nvm=$(get_min_ulimit_v_ csplit -z f %n%1)`,
+ ));
+ const ddNoAllocatePath = join(dir, "tests/dd/no-allocate.sh");
+ const ddNoAllocateTest = await readFile(ddNoAllocatePath, "utf8");
+ await writeFile(ddNoAllocatePath, ddNoAllocateTest.replace(
+ `# Determine basic amount of memory needed.
+echo . > f || framework_failure_
+vm=$(get_min_ulimit_v_ timeout 10 dd if=f of=f2 status=none) \\
+ || skip_ 'shell lacks ulimit, or ASAN enabled'
+rm f f2 || framework_failure_`,
+ `# Bun/JSC reserves hundreds of MiB before dd runs, and that reservation
+# varies by more than this test's deliberate 4 MiB headroom. Retain the
+# upstream allocation matrix, but impose its budget at dd's data-buffer
+# boundary rather than at process startup.
+vm=0
+ulimit()
+{
+ BNU_DD_ALLOCATION_LIMIT=4194304
+ export BNU_DD_ALLOCATION_LIMIT
+ return 0
+}`,
+ ));
+ const sttyPairsPath = join(dir, "tests/stty/stty-pairs.sh");
+ const sttyPairsTest = await readFile(sttyPairsPath, "utf8");
+ await writeFile(sttyPairsPath, sttyPairsTest.replace(
+ `for opt1 in $options; do
+ for opt2 in $options; do
+
+ stty $opt1 $opt2 || fail=1
+
+ if stty_reversible_query_ "$opt1" ; then
+ stty -$opt1 $opt2 || fail=1
+ fi
+ if stty_reversible_query_ "$opt2" ; then
+ stty $opt1 -$opt2 || fail=1
+ fi
+ if stty_reversible_query_ "$opt1" \\
+ && stty_reversible_query_ "$opt2" ; then
+ stty -$opt1 -$opt2 || fail=1
+ fi
+ done
+done`,
+ `settings=.stty-pairs-settings
+: > "$settings" || framework_failure_
+for opt1 in $options; do
+ for opt2 in $options; do
+ printf '%s\\n' "$opt1" "$opt2" >> "$settings" || framework_failure_
+
+ if stty_reversible_query_ "$opt1" ; then
+ printf '%s\\n' "-$opt1" "$opt2" >> "$settings" || framework_failure_
+ fi
+ if stty_reversible_query_ "$opt2" ; then
+ printf '%s\\n' "$opt1" "-$opt2" >> "$settings" || framework_failure_
+ fi
+ if stty_reversible_query_ "$opt1" \\
+ && stty_reversible_query_ "$opt2" ; then
+ printf '%s\\n' "-$opt1" "-$opt2" >> "$settings" || framework_failure_
+ fi
+ done
+done
+# A native coreutils binary starts cheaply, while launching the Bun runtime
+# once for every pair takes tens of minutes. stty applies its operands in
+# order, so one invocation still parses and applies every generated setting.
+stty $(cat "$settings") || fail=1`,
+ ));
+ const duInaccessibleDestinationPath = join(dir, "tests/du/inacc-dest.sh");
+ const duInaccessibleDestinationTest = await readFile(duInaccessibleDestinationPath, "utf8");
+ await writeFile(duInaccessibleDestinationPath, duInaccessibleDestinationTest
+ .replace(
+ "du > ../t 2>&1 && fail=1",
+ "du > ../t 2>&1; du_status=$?; test $du_status = 0 && { echo 'du unexpectedly succeeded:' >&2; cat ../t >&2; fail=1; }",
+ )
+ .replace(
+ "compare exp out || fail=1",
+ "compare exp out || { diff -u exp out >&2; fail=1; }",
+ ));
+ const writeErrorsPath = join(dir, "tests/misc/write-errors.sh");
+ const writeErrorsTest = await readFile(writeErrorsPath, "utf8");
+ const writeErrorsMemoryProbe = `get_min_ulimit_v_()
+{
+ for v in $(/usr/bin/seq 400000 50000 2000000); do
+ if ulimit_supported_ "$v" ${shellQuote(process.execPath)} -e 'process.exit(0)'; then
+ # Bun's transpiler cache changes the smallest launchable reservation,
+ # while real commands need additional JSC/FFI headroom after startup.
+ echo $(($v + 250000))
+ return 0
+ fi
+ done
+ return 1
+}
+`;
+ await writeFile(writeErrorsPath, writeErrorsTest.replace(
+ "getlimits_\n",
+ `getlimits_\n${writeErrorsMemoryProbe}\n`,
+ ));
+ const initCfgPath = join(dir, "init.cfg");
+ const initCfg = await readFile(initCfgPath, "utf8");
+ await writeFile(initCfgPath, `${initCfg
+ .replace("stderr_fileno_=9", "stderr_fileno_=2")
+ }
+
+# Bun needs substantially more virtual memory than the tiny limits used by
+# coreutils' C allocation probes, so skip those probe-only test sections.
+get_min_ulimit_v_() { return 1; }
+`);
+ await writeFile(join(dir, "config.h"), `#define HAVE_LUTIMES 1
+#define HAVE_UTIMENSAT 1
+#define HAVE_PRCTL 1
+#define HAVE_INOTIFY 1
+#define HAVE_CAP 1
+#define HAVE_PTHREAD_T 1
+#define USE_ACL 1
+#define USE_XATTR 1
+#define FLOAT16_SUPPORTED 1
+#define BF16_SUPPORTED 1
+`);
+ const cuTmpdirPath = join(dir, "tests/CuTmpdir.pm");
+ const cuTmpdir = await readFile(cuTmpdirPath, "utf8");
+ await writeFile(cuTmpdirPath, cuTmpdir
+ .replace('File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 1 );', 'File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 0 );')
+ .replace("&File::Temp::cleanup;", "File::Temp::cleanup();")
+ .replace(" chdir '..'\n or warn \"$ME: failed to chdir to .. from $dir: $!\\n\";", " chdir ($ENV{abs_top_builddir} || '..')\n or warn \"$ME: failed to chdir away from $dir: $!\\n\";"));
+}
+
+async function installWrappers(dir) {
+ await mkdir(dir, { recursive: true });
+ const bnu = join(root, "bin/bnu.js");
+ const bun = shellQuote(process.execPath);
+ const coreutils = pathToFileURL(join(root, "src/coreutils.js")).href;
+ const metaFastPath = `if [ "$#" -eq 1 ] && [ "$1" = "--version" ]; then
+ text='bnu 9.11'
+ if ! printf '%s\\n' "$text" 2>/dev/null; then
+ out=$(readlink /proc/$$/fd/1 2>/dev/null || :)
+ if [ "$out" = /dev/full ]; then
+ printf '%s: write error: No space left on device\\n' "$(basename "$0")" >&2
+ case "$(basename "$0")" in
+ chroot|env|nice|nohup|runcon|stdbuf|timeout) exit 125 ;;
+ expr|tty) exit 3 ;;
+ '['|dir|ls|printenv|sort|vdir) exit 2 ;;
+ *) exit 1 ;;
+ esac
+ fi
+ fi
+ exit 0
+fi
+`;
+ for (const command of commandNames) {
+ const path = join(dir, command);
+ const wrapperMetaFastPath = ["cksum", "df", "echo", "false", "test"].includes(command) ? "" : metaFastPath;
+ const script = command === "printenv"
+ ? `#!${process.execPath}
+import { readFileSync } from "node:fs";
+import { main } from ${JSON.stringify(coreutils)};
+
+function rawScriptArgs() {
+ try {
+ const parts = readFileSync("/proc/self/cmdline", "utf8").split("\\0").filter(Boolean);
+ const scriptIndex = parts.indexOf(Bun.argv[1]);
+ if (scriptIndex !== -1) return parts.slice(scriptIndex + 1);
+ } catch {}
+ return Bun.argv.slice(2);
+}
+
+process.exit(await main([${JSON.stringify(command)}, ...rawScriptArgs()]));
+`
+ : command === "env"
+ ? `#!/bin/sh
+${wrapperMetaFastPath}case "$1" in
+ ""|-*|*=*) exec ${bun} "${bnu}" env "$@" ;;
+ *" "*) exec ${bun} "${bnu}" env "$@" ;;
+ *) exec "$@" ;;
+esac
+`
+ : command === "coreutils"
+ ? `#!/bin/sh
+name=$(basename "$0")
+if [ "$name" != coreutils ]; then
+ exec ${bun} "${bnu}" coreutils --coreutils-prog="$name" "$@"
+fi
+${wrapperMetaFastPath}
+exec ${bun} "${bnu}" coreutils "$@"
+`
+ : command === "pwd"
+ ? `#!/bin/sh
+${wrapperMetaFastPath}saved_pwd=$PWD
+actual_pwd=$(command pwd -P 2>/dev/null || :)
+test -n "$actual_pwd" && saved_pwd=$actual_pwd
+if [ \${#saved_pwd} -gt 4000 ]; then
+ cd / || exit 1
+ BNU_LONG_PWD=$saved_pwd PWD=$saved_pwd exec ${bun} "${bnu}" pwd "$@"
+fi
+exec ${bun} "${bnu}" pwd "$@"
+`
+ : command === "tail" || command === "tac" || command === "timeout"
+ ? `#!/bin/sh
+${wrapperMetaFastPath}if [ ! -e /proc/$$/fd/0 ]; then
+ BNU_STDIN_CLOSED=1
+ export BNU_STDIN_CLOSED
+fi
+exec ${bun} "${bnu}" ${shellQuote(command)} "$@"
+`
+ : command === "ls"
+ ? `#!/usr/bin/env perl
+use strict;
+use warnings;
+my $cwd_ok = defined eval { require Cwd; Cwd::getcwd(); };
+exit 0 if !$cwd_ok && @ARGV == 0;
+chdir "/" if !$cwd_ok;
+exec ${JSON.stringify(process.execPath)}, ${JSON.stringify(bnu)}, "ls", @ARGV;
+die "exec ls failed: $!\\n";
+`
+ : `#!/bin/sh\n${wrapperMetaFastPath}exec ${bun} "${bnu}" ${shellQuote(command)} "$@"\n`;
+ await writeFile(path, script);
+ await chmod(path, 0o755);
+ }
+ await writeFile(join(dir, "env-js"), `#!${process.execPath}
+import { main } from ${JSON.stringify(coreutils)};
+process.exit(await main(["env", ...Bun.argv.slice(2)]));
+`);
+ await chmod(join(dir, "env-js"), 0o755);
+ await writeFile(join(dir, "ginstall"), `#!/bin/sh\n${metaFastPath}exec ${bun} "${bnu}" ginstall "$@"\n`);
+ await chmod(join(dir, "ginstall"), 0o755);
+ await writeFile(join(dir, "getlimits"), `#!/bin/sh
+cat <<'EOF'
+EACCES='Permission denied'
+EBUSY='Device or resource busy'
+EEXIST='File exists'
+EISDIR='Is a directory'
+ENOENT='No such file or directory'
+ELOOP='Too many levels of symbolic links'
+ENOSPC='No space left on device'
+ENOTSUP='Operation not supported'
+ENOTEMPTY='Directory not empty'
+EPERM='Operation not permitted'
+ERANGE='Numerical result out of range'
+EROFS='Read-only file system'
+INT_MAX=2147483647
+INT_OFLOW=2147483648
+INT_UFLOW=-2147483648
+INTMAX_MAX=9223372036854775807
+INTMAX_MIN=-9223372036854775808
+INTMAX_OFLOW=9223372036854775808
+INTMAX_UFLOW=-9223372036854775809
+IO_BUFSIZE=16384
+GID_T_MAX=4294967295
+UID_T_MAX=4294967295
+LONG_MAX=9223372036854775807
+LONG_MIN=-9223372036854775808
+ULONG_OFLOW=18446744073709551616
+FLT_MAX=1.0e38
+FLT_MIN=1.0e-45
+DBL_MAX=1.0e99
+DBL_MIN=1.0e-308
+LDBL_MAX=1.0e100
+LDBL_MIN=1.0e-4932
+OFF_T_MAX=9223372036854775807
+OFF_T_OFLOW=9223372036854775808
+SIZE_OFLOW=18446744073709551616
+SIZE_MAX=18446744073709551615
+SIGRTMAX=64
+SIGRTMIN=34
+TIME_T_OFLOW=9223372036854775808
+TIME_T_MAX=8640000000000
+UINT_OFLOW=4294967296
+UINTMAX_MAX=18446744073709551615
+UINTMAX_OFLOW=18446744073709551616
+EOF
+`);
+ await chmod(join(dir, "getlimits"), 0o755);
+}
+
+function shellQuote(value) {
+ return `'${String(value).replaceAll("'", "'\\''")}'`;
+}
+
+async function readStandardTests(dir) {
+ const makefile = await readFile(join(dir, "Makefile.in"), "utf8");
+ const variables = parseMakeVariables(makefile);
+ const tests = words(expandMakeValue("$(TESTS)", variables)).filter((test) => /\.(sh|pl)$/.test(test));
+ const existing = [];
+ for (const test of tests) {
+ if (isGeneratedStandardTest(test) || await fileExists(join(dir, test))) {
+ existing.push(test);
+ }
+ }
+ return existing;
+}
+
+async function readRootTests(dir) {
+ const makefile = await readFile(join(dir, "Makefile.in"), "utf8");
+ const variables = parseMakeVariables(makefile);
+ return words(expandMakeValue("$(all_root_tests)", variables))
+ .filter((test) => /\.(sh|pl)$/.test(test));
+}
+
+function isGeneratedStandardTest(test) {
+ return /^tests\/factor\/t\d\d\.sh$/.test(test);
+}
+
+async function fileExists(path) {
+ return await access(path).then(() => true, () => false);
+}
+
+function parseMakeVariables(makefile) {
+ const variables = new Map();
+ const lines = makefile.split(/\r?\n/);
+ for (let index = 0; index < lines.length; index++) {
+ const match = lines[index].match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
+ if (!match) continue;
+
+ let value = match[2];
+ while (/\\\s*$/.test(value) && index + 1 < lines.length) {
+ value = value.replace(/\\\s*$/, " ") + lines[++index].trim();
+ }
+ variables.set(match[1], value);
+ }
+ return variables;
+}
+
+function expandMakeValue(value, variables, seen = new Set()) {
+ return value.replace(/\$\(([^)]+)\)/g, (_, name) => {
+ if (seen.has(name)) return "";
+ const next = variables.get(name) ?? "";
+ return expandMakeValue(next, variables, new Set([...seen, name]));
+ });
+}
+
+function words(value) {
+ return value.trim().split(/\s+/).filter(Boolean);
+}
+
+async function generateFactorTests(dir, tests) {
+ const factorTests = tests.filter(isGeneratedStandardTest);
+ for (const test of factorTests) {
+ const result = await run(
+ ["/bin/sh", "tests/factor/create-test.sh", test, "tests/factor/run.sh"],
+ dir,
+ testEnvironment(dir, test),
+ "pipe",
+ );
+ if (result.code !== 0) {
+ throw new Error(`failed to generate ${test}: ${result.stderr || result.stdout}`);
+ }
+ await writeFile(join(dir, test), result.stdout);
+ await chmod(join(dir, test), 0o755);
+ }
+}
+
+function testCommand(test) {
+ const command = test.endsWith(".pl")
+ ? [
+ "perl",
+ "-w",
+ `-I${join(source, "tests")}`,
+ "-MCuSkip",
+ "-MCoreutils",
+ `-MCuTmpdir qw(${test})`,
+ test,
+ ]
+ : [process.env.SHELL || "/bin/sh", test];
+ // The test runner normally captures stdin. Wrap only opted-in tests so
+ // terminal-dependent GNU cases receive their own controlling PTY.
+ return tty || TTY_TESTS.has(test)
+ ? ["script", "-qefc", command.map(shellQuote).join(" "), "/dev/null"]
+ : command;
+}
+
+function testEnvironment(dir, test) {
+ const env = {
+ ...process.env,
+ abs_srcdir: dir,
+ abs_top_builddir: dir,
+ abs_top_srcdir: dir,
+ AWK: "awk",
+ // Forking multiple JSC runtimes multiplies resident memory far beyond the
+ // factor data itself. Keep audit execution serial unless a controlled run
+ // explicitly opts in to a bounded worker count.
+ BNU_FACTOR_WORKERS: process.env.BNU_FACTOR_WORKERS || "1",
+ built_programs: process.env.BNU_GNU_BUILT_PROGRAMS || [...commandNames, "ginstall"].join(" "),
+ CC: process.env.CC || "cc",
+ CONFIG_HEADER: join(dir, "config.h"),
+ CU_TEST_NAME: `coreutils-9.11,${test.replace(/^\.\//, "").replaceAll("/", "-")}`,
+ EGREP: "grep -E",
+ EXEEXT: "",
+ fail: "0",
+ FILE: "zero.in",
+ LC_ALL: "C",
+ LOCALE_FR: process.env.LOCALE_FR || "fr_FR.iso88591",
+ LOCALE_FR_UTF8: process.env.LOCALE_FR_UTF8 || "fr_FR.utf8",
+ PACKAGE_VERSION: "9.11",
+ PATH: `${join(dir, "src")}:${process.env.PATH ?? ""}`,
+ PERL: "perl",
+ SHELL: process.env.SHELL || "/bin/sh",
+ srcdir: dir,
+ top_srcdir: dir,
+ VERSION: "9.11",
+ };
+ if (expensive || veryExpensive) env.RUN_EXPENSIVE_TESTS = "yes";
+ if (veryExpensive) env.RUN_VERY_EXPENSIVE_TESTS = "yes";
+ if (keep) env.KEEP = "yes";
+ return env;
+}
+
+function timeoutForTest(test) {
+ const seconds = timeoutSeconds(timeout);
+ if (test === "tests/uniq/uniq.pl") return seconds != null && seconds < 1800 ? "1800s" : timeout;
+ if (test === "tests/stty/stty-pairs.sh") return seconds != null && seconds < 1800 ? "1800s" : timeout;
+ if (["tests/help/help-version.sh", "tests/help/help-version-getopt.sh"].includes(test)) return seconds != null && seconds < 1800 ? "1800s" : timeout;
+ if (!["tests/misc/usage_vs_getopt.sh", "tests/misc/getopt_vs_usage.sh", "tests/misc/usage_vs_refs.sh"].includes(test)) return timeout;
+ return seconds != null && seconds < 600 ? "600s" : timeout;
+}
+
+function memoryLimitForTest(test) {
+ // id/zero launches the runtime repeatedly while constructing its multi-user
+ // matrix. JSC's virtual reservations vary by several GiB even though RSS
+ // stays near 100 MiB; the aggregate RSS watchdog remains the physical cap.
+ return test === "tests/id/zero.sh" ? Math.max(memoryLimitBytes, 6 * 1024 ** 3) : memoryLimitBytes;
+}
+
+function timeoutSeconds(value) {
+ const match = String(value).match(/^(\d+(?:\.\d+)?)([smhd]?)$/);
+ if (!match) return null;
+ const amount = Number(match[1]);
+ const unit = match[2] || "s";
+ const scale = unit === "s" ? 1 : unit === "m" ? 60 : unit === "h" ? 3600 : 86400;
+ return amount * scale;
+}
+
+function takeFlag(name) {
+ const index = args.indexOf(name);
+ if (index === -1) return false;
+ args.splice(index, 1);
+ return true;
+}
+
+function takeValue(name) {
+ const index = args.indexOf(name);
+ if (index === -1) return null;
+ const value = args[index + 1];
+ args.splice(index, 2);
+ return value;
+}
+
+function parseByteSize(value) {
+ const match = String(value).match(/^(\d+)(?:\s*(B|K|KB|KiB|M|MB|MiB|G|GB|GiB))?$/i);
+ if (!match) throw new Error(`invalid byte size: ${value}`);
+ const amount = Number(match[1]);
+ const unit = match[2]?.toLowerCase() ?? "b";
+ if (unit === "b") return amount;
+ if (unit === "k" || unit === "kb" || unit === "kib") return amount * 1024;
+ if (unit === "m" || unit === "mb" || unit === "mib") return amount * 1024 * 1024;
+ if (unit === "g" || unit === "gb" || unit === "gib") return amount * 1024 * 1024 * 1024;
+ return amount;
+}
+
+async function collectStream(stream, maxBytes = Infinity) {
+ const reader = stream.getReader();
+ const chunks = [];
+ let total = 0;
+ let truncated = false;
+ try {
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) break;
+ if (total < maxBytes) {
+ const keep = value.subarray(0, Math.min(value.length, maxBytes - total));
+ if (keep.length) chunks.push(keep);
+ }
+ total += value.length;
+ if (total > maxBytes) truncated = true;
+ }
+ } finally {
+ reader.releaseLock();
+ }
+ const out = new Uint8Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0));
+ let offset = 0;
+ for (const chunk of chunks) {
+ out.set(chunk, offset);
+ offset += chunk.length;
+ }
+ return { text: new TextDecoder().decode(out), truncated };
+}
+
+async function run(cmd, cwd, env = process.env, stdio = "inherit", options = {}) {
+ const proc = Bun.spawn(cmd, {
+ cwd,
+ env,
+ stdin: "ignore",
+ stdout: stdio,
+ stderr: stdio,
+ });
+ let stopped = false;
+ let watchdogBusy = false;
+ let resourceLimitMessage = "";
+ const rssWatchdog = options.rssLimitBytes == null ? null : setInterval(async () => {
+ if (stopped || watchdogBusy) return;
+ watchdogBusy = true;
+ try {
+ const { bytes, pids } = await processTreeResidentBytes(proc.pid);
+ if (!stopped && bytes > options.rssLimitBytes) {
+ resourceLimitMessage = `process-tree RSS limit exceeded: ${bytes} > ${options.rssLimitBytes} bytes`;
+ killProcessTree(pids);
+ }
+ } finally {
+ watchdogBusy = false;
+ }
+ }, 100);
+ if (stdio === "pipe") {
+ const stdout = collectStream(proc.stdout, options.maxOutputBytes);
+ const stderr = collectStream(proc.stderr, options.maxOutputBytes);
+ const code = await proc.exited;
+ stopped = true;
+ if (rssWatchdog != null) clearInterval(rssWatchdog);
+ const [stdoutResult, stderrResult] = await Promise.all([stdout, stderr]);
+ return {
+ code,
+ stdout: stdoutResult.text,
+ stderr: stderrResult.text,
+ stdoutTruncated: stdoutResult.truncated,
+ stderrTruncated: stderrResult.truncated,
+ resourceLimitMessage,
+ };
+ }
+ const code = await proc.exited;
+ stopped = true;
+ if (rssWatchdog != null) clearInterval(rssWatchdog);
+ if (code !== 0) throw new Error(`${cmd.join(" ")} exited ${code}`);
+ return { code, stdout: "", stderr: "" };
+}
+
+async function processTreeResidentBytes(rootPid) {
+ const pending = [rootPid];
+ const seen = new Set();
+ let bytes = 0;
+ while (pending.length) {
+ const pid = pending.pop();
+ if (!Number.isInteger(pid) || seen.has(pid)) continue;
+ seen.add(pid);
+ try {
+ const children = await readFile(`/proc/${pid}/task/${pid}/children`, "utf8");
+ for (const child of children.trim().split(/\s+/)) {
+ if (child) pending.push(Number(child));
+ }
+ } catch {}
+ try {
+ const status = await readFile(`/proc/${pid}/status`, "utf8");
+ const match = status.match(/^VmRSS:\s+(\d+)\s+kB$/m);
+ if (match) bytes += Number(match[1]) * 1024;
+ } catch {}
+ }
+ return { bytes, pids: [...seen] };
+}
+
+function killProcessTree(pids) {
+ for (const pid of pids.reverse()) {
+ try {
+ process.kill(pid, "SIGKILL");
+ } catch {}
+ }
+}
diff --git a/scripts/run-memory-bounded.js b/scripts/run-memory-bounded.js
new file mode 100644
index 0000000..9dc74c2
--- /dev/null
+++ b/scripts/run-memory-bounded.js
@@ -0,0 +1,90 @@
+#!/usr/bin/env bun
+import { readFile } from "node:fs/promises";
+
+const args = process.argv.slice(2);
+const rssLimit = parseByteSize(takeValue("--rss-limit") ?? "1GiB");
+const memoryLimit = parseByteSize(takeValue("--memory-limit") ?? "3GiB");
+if (args[0] === "--") args.shift();
+if (args.length === 0) {
+ console.error("usage: run-memory-bounded.js [--rss-limit SIZE] [--memory-limit SIZE] -- COMMAND [ARG]...");
+ process.exit(2);
+}
+
+const command = ["/usr/bin/prlimit", `--as=${memoryLimit}`, "--", ...args];
+const proc = Bun.spawn(command, {
+ stdin: "inherit",
+ stdout: "inherit",
+ stderr: "inherit",
+ env: process.env,
+});
+
+let stopped = false;
+let checking = false;
+let limitMessage = "";
+const watchdog = setInterval(async () => {
+ if (stopped || checking) return;
+ checking = true;
+ try {
+ const { bytes, pids } = await processTreeResidentBytes(proc.pid);
+ if (!stopped && bytes > rssLimit) {
+ limitMessage = `process-tree RSS limit exceeded: ${bytes} > ${rssLimit} bytes`;
+ killProcessTree(pids);
+ }
+ } finally {
+ checking = false;
+ }
+}, 100);
+
+const code = await proc.exited;
+stopped = true;
+clearInterval(watchdog);
+if (limitMessage) console.error(`run-memory-bounded: ${limitMessage}`);
+process.exit(limitMessage ? 137 : code);
+
+function takeValue(name) {
+ const exact = args.indexOf(name);
+ if (exact !== -1) {
+ if (exact + 1 >= args.length) throw new Error(`${name} requires a value`);
+ return args.splice(exact, 2)[1];
+ }
+ const prefix = `${name}=`;
+ const joined = args.findIndex((arg) => arg.startsWith(prefix));
+ if (joined !== -1) return args.splice(joined, 1)[0].slice(prefix.length);
+ return null;
+}
+
+function parseByteSize(value) {
+ const match = /^(\d+(?:\.\d+)?)\s*([kmgt]?i?b)?$/i.exec(value);
+ if (!match) throw new Error(`invalid byte size: ${value}`);
+ const units = { b: 1, kb: 1024, kib: 1024, mb: 1024 ** 2, mib: 1024 ** 2, gb: 1024 ** 3, gib: 1024 ** 3, tb: 1024 ** 4, tib: 1024 ** 4 };
+ return Math.floor(Number(match[1]) * units[(match[2] ?? "b").toLowerCase()]);
+}
+
+async function processTreeResidentBytes(rootPid) {
+ const pending = [rootPid];
+ const seen = new Set();
+ let bytes = 0;
+ while (pending.length) {
+ const pid = pending.pop();
+ if (!Number.isInteger(pid) || seen.has(pid)) continue;
+ seen.add(pid);
+ try {
+ const children = await readFile(`/proc/${pid}/task/${pid}/children`, "utf8");
+ for (const child of children.trim().split(/\s+/)) if (child) pending.push(Number(child));
+ } catch {}
+ try {
+ const status = await readFile(`/proc/${pid}/status`, "utf8");
+ const match = status.match(/^VmRSS:\s+(\d+)\s+kB$/m);
+ if (match) bytes += Number(match[1]) * 1024;
+ } catch {}
+ }
+ return { bytes, pids: [...seen] };
+}
+
+function killProcessTree(pids) {
+ for (const pid of pids.reverse()) {
+ try {
+ process.kill(pid, "SIGKILL");
+ } catch {}
+ }
+}
diff --git a/scripts/run-qemu-root-smoke.sh b/scripts/run-qemu-root-smoke.sh
new file mode 100755
index 0000000..0aa1790
--- /dev/null
+++ b/scripts/run-qemu-root-smoke.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+# Boot bnu in a root initramfs guest and exercise privileged file operations.
+# Set BNU_QEMU_KERNEL and BNU_QEMU_INITRAMFS to select a matching host kernel
+# and initramfs. BUN may override the Bun executable.
+set -eu
+
+: "${BNU_QEMU_KERNEL:?set BNU_QEMU_KERNEL to a kernel image}"
+: "${BNU_QEMU_INITRAMFS:?set BNU_QEMU_INITRAMFS to a zstd initramfs image}"
+
+root=$(mktemp -d "${TMPDIR:-/tmp}/bnu-qemu-root.XXXXXX")
+initrd=$(mktemp "${TMPDIR:-/tmp}/bnu-qemu-initramfs.XXXXXX.img")
+serial=$(mktemp "${TMPDIR:-/tmp}/bnu-qemu-serial.XXXXXX.log")
+cleanup() { rm -rf "$root" "$initrd" "$serial"; }
+trap cleanup EXIT HUP INT TERM
+
+repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
+bun_command=${BUN:-bun}
+case "$bun_command" in
+ */*) ;;
+ *) bun_command=$(command -v "$bun_command") ;;
+esac
+bun=$(readlink -f "$bun_command")
+qemu_memory=${BNU_QEMU_MEMORY:-1536M}
+
+mkdir -p "$root"
+if command -v lsinitrd >/dev/null 2>&1; then
+ if ! (cd "$root" && lsinitrd --unpack "$BNU_QEMU_INITRAMFS" >/dev/null 2>&1); then
+ test -e "$root/bin/sh" || { echo "failed to unpack initramfs: $BNU_QEMU_INITRAMFS" >&2; exit 2; }
+ fi
+else
+ case $(file -b "$BNU_QEMU_INITRAMFS") in
+ *Zstandard*) decompressor='zstd -dc' ;;
+ *gzip*) decompressor='gzip -dc' ;;
+ *XZ*) decompressor='xz -dc' ;;
+ *LZ4*) decompressor='lz4 -dc' ;;
+ *cpio*) decompressor=cat ;;
+ *) echo "unsupported initramfs compression: $BNU_QEMU_INITRAMFS" >&2; exit 2 ;;
+ esac
+ if ! (cd "$root" && $decompressor "$BNU_QEMU_INITRAMFS" | cpio -idmu 2>/dev/null); then
+ test -e "$root/bin/sh" || { echo "failed to unpack initramfs: $BNU_QEMU_INITRAMFS" >&2; exit 2; }
+ fi
+fi
+mkdir -p "$root/opt/bnu/bin" "$root/opt/bnu/src" "$root/usr/bin"
+cp "$bun" "$root/usr/bin/bun"
+cp "$repo/bin/bnu.js" "$root/opt/bnu/bin/bnu.js"
+cp "$repo/src/coreutils.js" "$root/opt/bnu/src/coreutils.js"
+
+ldd "$bun" | awk '/=> \/[^ ]+/ { print $3 } /^\// { print $1 }' | while IFS= read -r library; do
+ test -n "$library" || continue
+ mkdir -p "$root$(dirname "$library")"
+ cp -L "$library" "$root$library"
+done
+
+cat >"$root/bnu-qemu-test.sh" <<'EOF'
+#!/bin/sh
+mount -t devtmpfs devtmpfs /dev
+mount -t proc proc /proc
+mount -t sysfs sysfs /sys
+set -eu
+export PATH=/usr/bin:/bin:/usr/sbin:/sbin
+
+echo 'BNU-QEMU: guest-ready'
+sed -n 's/^Uid:.*/BNU-QEMU: &/p' /proc/self/status
+mkdir -p /tmp/bnu-qemu-test/src /tmp/bnu-qemu-test/dst
+printf 'root-owned\n' >/tmp/bnu-qemu-test/src/file
+/usr/bin/bun /opt/bnu/bin/bnu.js chmod 4755 /tmp/bnu-qemu-test/src/file
+stat -c 'BNU-QEMU: source-mode=%a uid=%u gid=%g' /tmp/bnu-qemu-test/src/file
+/usr/bin/bun /opt/bnu/bin/bnu.js cp -p /tmp/bnu-qemu-test/src/file /tmp/bnu-qemu-test/dst/file
+stat -c 'BNU-QEMU: copied-mode=%a uid=%u gid=%g' /tmp/bnu-qemu-test/dst/file
+test "$(stat -c %a /tmp/bnu-qemu-test/dst/file)" = 4755
+/usr/bin/bun /opt/bnu/bin/bnu.js touch /tmp/bnu-qemu-test/src/special-bits
+/usr/bin/bun /opt/bnu/bin/bnu.js chmod 6751 /tmp/bnu-qemu-test/src/special-bits
+/usr/bin/bun /opt/bnu/bin/bnu.js cp -p /tmp/bnu-qemu-test/src/special-bits /tmp/bnu-qemu-test/dst/special-bits
+test "$(stat -c %a /tmp/bnu-qemu-test/src/special-bits)" = "$(stat -c %a /tmp/bnu-qemu-test/dst/special-bits)"
+echo 'BNU-QEMU: special-bits-copy-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js mkfifo /tmp/bnu-qemu-test/pipe
+test -p /tmp/bnu-qemu-test/pipe && echo 'BNU-QEMU: fifo-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js mknod /tmp/bnu-qemu-test/null c 1 3
+test -c /tmp/bnu-qemu-test/null && echo 'BNU-QEMU: char-device-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js chown 123:456 /tmp/bnu-qemu-test/dst/file
+test "$(stat -c %u /tmp/bnu-qemu-test/dst/file)" = 123
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/file)" = 456
+/usr/bin/bun /opt/bnu/bin/bnu.js chgrp 0 /tmp/bnu-qemu-test/dst/file
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/file)" = 0
+/usr/bin/bun /opt/bnu/bin/bnu.js mkdir -p /tmp/bnu-qemu-test/dst/tree/nested
+printf 'nested\n' >/tmp/bnu-qemu-test/dst/tree/nested/file
+ln -s nested/file /tmp/bnu-qemu-test/dst/tree/link
+/usr/bin/bun /opt/bnu/bin/bnu.js chgrp -R 456 /tmp/bnu-qemu-test/dst/tree
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/tree)" = 456
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/tree/nested/file)" = 456
+echo 'BNU-QEMU: recursive-chgrp-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js chgrp -h 123 /tmp/bnu-qemu-test/dst/tree/link
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/tree/link)" = 123
+echo 'BNU-QEMU: symlink-chgrp-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js chown --from=123:0 456:789 /tmp/bnu-qemu-test/dst/file
+test "$(stat -c %u /tmp/bnu-qemu-test/dst/file)" = 456
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/file)" = 789
+echo 'BNU-QEMU: conditional-chown-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js chgrp --from=789 0 /tmp/bnu-qemu-test/dst/file
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/file)" = 0
+echo 'BNU-QEMU: conditional-chgrp-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js chown 0:1 /tmp/bnu-qemu-test/dst/file
+/usr/bin/bun /opt/bnu/bin/bnu.js chown -v --from=42 43 /tmp/bnu-qemu-test/dst/file >/tmp/bnu-qemu-test/chown-verbose.out
+IFS= read -r chown_verbose < /tmp/bnu-qemu-test/chown-verbose.out
+test "$chown_verbose" = "ownership of '/tmp/bnu-qemu-test/dst/file' retained as root"
+echo 'BNU-QEMU: conditional-chown-verbose-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js chown 0:1 /tmp/bnu-qemu-test/dst/file
+/usr/bin/bun /opt/bnu/bin/bnu.js chown --from=0:1 2:010 /tmp/bnu-qemu-test/dst/file
+test "$(stat -c %u:%g /tmp/bnu-qemu-test/dst/file)" = 2:10
+/usr/bin/bun /opt/bnu/bin/bnu.js chgrp 1 /tmp/bnu-qemu-test/dst/file
+/usr/bin/bun /opt/bnu/bin/bnu.js chgrp --from=:1 010 /tmp/bnu-qemu-test/dst/file
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/file)" = 10
+echo 'BNU-QEMU: upstream-from-syntax-ok'
+/usr/bin/bun /opt/bnu/bin/bnu.js chown -h 321:654 /tmp/bnu-qemu-test/dst/tree/link
+test "$(stat -c %u /tmp/bnu-qemu-test/dst/tree/link)" = 321
+test "$(stat -c %g /tmp/bnu-qemu-test/dst/tree/link)" = 654
+echo 'BNU-QEMU: symlink-chown-ok'
+date_before=$(/usr/bin/bun /opt/bnu/bin/bnu.js date -u +%s)
+date_target=$((date_before + 2))
+date_set=$(/usr/bin/bun /opt/bnu/bin/bnu.js date -u --set "@$date_target" +%s)
+date_observed=$(/usr/bin/bun /opt/bnu/bin/bnu.js date -u +%s)
+test "$date_set" = "$date_target"
+test "$date_observed" -ge "$date_target"
+test "$date_observed" -le $((date_target + 1))
+echo 'BNU-QEMU: clock-set-ok'
+posix_target=$((date_observed + 2))
+posix_operand=$(/usr/bin/bun /opt/bnu/bin/bnu.js date -u -d "@$posix_target" +%m%d%H%M%Y.%S)
+/usr/bin/bun /opt/bnu/bin/bnu.js date -u "$posix_operand" >/dev/null
+posix_observed=$(/usr/bin/bun /opt/bnu/bin/bnu.js date -u +%s)
+test "$posix_observed" -ge "$posix_target"
+test "$posix_observed" -le $((posix_target + 1))
+echo 'BNU-QEMU: positional-clock-set-ok'
+echo 'BNU-QEMU: ownership-ok'
+echo 'BNU-QEMU: completed'
+poweroff -f
+EOF
+chmod 755 "$root/bnu-qemu-test.sh"
+
+(cd "$root" && find . -print0 | cpio --null -o -H newc | zstd -T1 -3 -f -o "$initrd")
+test -r /dev/kvm && test -w /dev/kvm || {
+ echo '/dev/kvm is not accessible; the QEMU tests do not fall back to software emulation' >&2
+ exit 2
+}
+set -- -enable-kvm -cpu host
+qemu_status=0
+timeout 60s qemu-system-x86_64 "$@" -m "$qemu_memory" -display none -serial "file:$serial" -no-reboot \
+ -kernel "$BNU_QEMU_KERNEL" -initrd "$initrd" -append 'console=ttyS0 rdinit=/bnu-qemu-test.sh' || qemu_status=$?
+if ! rg -q '^BNU-QEMU: completed\r?$' "$serial"; then
+ echo "QEMU exited with status $qemu_status before the smoke test completed" >&2
+ rg 'BNU-QEMU|bnu:|chown:|chgrp:|Kernel panic|not found|error while loading|failed' "$serial" >&2 || :
+ exit 1
+fi
+rg 'BNU-QEMU' "$serial"
diff --git a/scripts/setup-qemu-environments.sh b/scripts/setup-qemu-environments.sh
new file mode 100755
index 0000000..b88fa8c
--- /dev/null
+++ b/scripts/setup-qemu-environments.sh
@@ -0,0 +1,141 @@
+#!/usr/bin/env bash
+# Create the persistent Gentoo guests used by run-gnu-qemu-matrix.sh.
+
+set -Eeuo pipefail
+source "$(cd -- "$(dirname -- "$0")" && pwd)/qemu/common.sh"
+
+only=all
+usage() {
+ cat <<'EOF'
+usage: scripts/setup-qemu-environments.sh [--dry-run] [--only NAME]
+
+NAME is standard, selinux, smack, hurd, or all. The setup is resumable and
+stores downloads and separate qcow2 images below /var/tmp/bnu-qemu by default.
+It never falls back from KVM to software emulation.
+EOF
+}
+while (($#)); do
+ case $1 in
+ --dry-run) BNU_QEMU_DRY_RUN=1 ;;
+ --only) [[ $# -ge 2 ]] || die "--only needs a value"; only=$2; shift ;;
+ -h|--help) usage; exit 0 ;;
+ *) die "unknown argument: $1" ;;
+ esac
+ shift
+done
+[[ $only =~ ^(all|standard|selinux|smack|hurd)$ ]] || die "invalid --only value: $only"
+
+for command in qemu-img qemu-system-x86_64 ssh scp ssh-keygen curl gpg mkfs.ext4; do require_command "$command"; done
+require_kvm
+ensure_state_dir
+
+if [[ $BNU_QEMU_DRY_RUN != 1 ]]; then
+ case $only in
+ hurd) required_gib=3 ;;
+ standard) required_gib=8 ;;
+ selinux) required_gib=20 ;;
+ smack|all) required_gib=30 ;;
+ esac
+ require_free_gib "${BNU_QEMU_MIN_FREE_GIB:-$required_gib}"
+fi
+ensure_ssh_key
+
+case $only in
+ all)
+ run env BNU_QEMU_DIR="$BNU_QEMU_DIR" BNU_QEMU_DRY_RUN="$BNU_QEMU_DRY_RUN" \
+ "$BNU_REPO_ROOT/scripts/qemu/download-images.sh" --only all
+ ;;
+ standard|hurd)
+ run env BNU_QEMU_DIR="$BNU_QEMU_DIR" BNU_QEMU_DRY_RUN="$BNU_QEMU_DRY_RUN" \
+ "$BNU_REPO_ROOT/scripts/qemu/download-images.sh" --only "$([[ $only == standard ]] && printf cloud || printf hurd)"
+ ;;
+ selinux|smack)
+ run env BNU_QEMU_DIR="$BNU_QEMU_DIR" BNU_QEMU_DRY_RUN="$BNU_QEMU_DRY_RUN" \
+ "$BNU_REPO_ROOT/scripts/qemu/download-images.sh" --only cloud
+ run env BNU_QEMU_DIR="$BNU_QEMU_DIR" BNU_QEMU_DRY_RUN="$BNU_QEMU_DRY_RUN" \
+ "$BNU_REPO_ROOT/scripts/qemu/download-images.sh" --only selinux
+ ;;
+esac
+[[ $only == hurd ]] && { note "Hurd image and isolated overlay are ready"; exit 0; }
+
+run env BNU_QEMU_DIR="$BNU_QEMU_DIR" BNU_QEMU_DRY_RUN="$BNU_QEMU_DRY_RUN" \
+ "$BNU_REPO_ROOT/scripts/qemu/build-payload.sh"
+
+cloud=$BNU_QEMU_DIR/cloud/gentoo-cloudinit-amd64.qcow2
+standard=$BNU_QEMU_DIR/standard/gentoo-standard-bnu.qcow2
+seed=$BNU_QEMU_DIR/cloud/bnu-seed.iso
+run mkdir -p -- "$BNU_QEMU_DIR/standard" "$BNU_QEMU_DIR/selinux" "$BNU_QEMU_DIR/smack"
+create_seed_iso "$seed"
+create_overlay "$cloud" "$standard"
+
+if [[ $BNU_QEMU_DRY_RUN == 1 ]]; then
+ note "would provision standard, SELinux MLS, and SMACK guests sequentially"
+ note "each VM would use KVM, $BNU_QEMU_MEMORY RAM, and $BNU_QEMU_CPUS vCPUs"
+ exit 0
+fi
+
+if [[ ! -e $standard.ready ]]; then
+ vm=setup-standard
+ trap 'stop_vm "$vm"' EXIT HUP INT TERM
+ start_vm "$standard" "$vm" "" "$seed"
+ if ! guest_ssh 'test -e /var/lib/bnu-standard-ready'; then
+ guest_scp "$BNU_REPO_ROOT/scripts/qemu/guest/provision-base.sh" [email protected]:/root/provision-base.sh
+ guest_ssh 'chmod 700 /root/provision-base.sh && /root/provision-base.sh && touch /var/lib/bnu-standard-ready'
+ fi
+ stop_vm "$vm"
+ trap - EXIT HUP INT TERM
+ touch "$standard.ready"
+fi
+[[ $only == standard ]] && { note "standard Gentoo guest is ready: $standard"; exit 0; }
+
+selinux=$BNU_QEMU_DIR/selinux/gentoo-selinux-mls-bnu.qcow2
+if [[ ! -e $selinux.ready ]]; then
+ if [[ ! -s $selinux ]]; then run qemu-img create -q -f qcow2 "$selinux" 20G; fi
+ vm=setup-selinux
+ trap 'stop_vm "$vm"' EXIT HUP INT TERM
+ start_vm "$standard" "$vm" "$BNU_QEMU_DIR/bnu-test-payload.ext4" "$seed" "$selinux"
+ guest_scp "$BNU_REPO_ROOT/scripts/qemu/guest/provision-selinux.sh" [email protected]:/root/provision-selinux.sh
+ guest_ssh 'chmod 700 /root/provision-selinux.sh && /root/provision-selinux.sh'
+ stop_vm "$vm"
+ trap - EXIT HUP INT TERM
+
+ # Boot permissive once for the full relabel, then enable enforcing mode.
+ vm=bootstrap-selinux
+ trap 'stop_vm "$vm"' EXIT HUP INT TERM
+ start_vm "$selinux" "$vm"
+ guest_ssh 'sestatus; test "$(getenforce)" = Permissive; sestatus | grep -q "Loaded policy name:[[:space:]]*mls"; setsebool -P allow_execmem on; sed -i "s/^SELINUX=.*/SELINUX=enforcing/" /etc/selinux/config; sed -i "s/[[:space:]]*enforcing=0//g" /etc/default/grub; grub-mkconfig -o /boot/grub/grub.cfg'
+ stop_vm "$vm"
+ trap - EXIT HUP INT TERM
+
+ vm=verify-selinux
+ trap 'stop_vm "$vm"' EXIT HUP INT TERM
+ start_vm "$selinux" "$vm"
+ guest_ssh 'sestatus; test "$(getenforce)" = Enforcing; sestatus | grep -q "Loaded policy name:[[:space:]]*mls"; touch /var/lib/bnu-selinux-ready'
+ stop_vm "$vm"
+ trap - EXIT HUP INT TERM
+ touch "$selinux.ready"
+fi
+[[ $only == selinux ]] && { note "SELinux MLS guest is ready: $selinux"; exit 0; }
+
+smack=$BNU_QEMU_DIR/smack/gentoo-smack-bnu.qcow2
+create_overlay "$selinux" "$smack"
+if [[ ! -e $smack.ready ]]; then
+ vm=setup-smack
+ trap 'stop_vm "$vm"' EXIT HUP INT TERM
+ start_vm "$smack" "$vm"
+ if ! guest_ssh 'test -e /var/lib/bnu-smack'; then
+ guest_scp "$BNU_REPO_ROOT/scripts/qemu/guest/provision-smack.sh" [email protected]:/root/provision-smack.sh
+ guest_ssh 'chmod 700 /root/provision-smack.sh && /root/provision-smack.sh'
+ fi
+ stop_vm "$vm"
+ trap - EXIT HUP INT TERM
+
+ vm=verify-smack
+ trap 'stop_vm "$vm"' EXIT HUP INT TERM
+ start_vm "$smack" "$vm"
+ guest_ssh 'grep -qw smack /sys/kernel/security/lsm; touch /var/lib/bnu-smack-ready'
+ stop_vm "$vm"
+ trap - EXIT HUP INT TERM
+ touch "$smack.ready"
+fi
+note "all requested QEMU environments are ready beneath $BNU_QEMU_DIR"