#!/bin/bash # Verify refusal before package/config changes on a running source kernel. set -euo pipefail [[ $EUID == 0 && -f /var/lib/phoebus/postinstall-test-vm ]] case $(cat /sys/class/dmi/id/sys_vendor) in QEMU|KVM) ;; *) exit 2 ;; esac case $(uname -r) in *-gentoo-dist-phoebus-balanced) choice=tuned ;; *-gentoo-dist-phoebus-tuned) choice=balanced ;; *) echo 'This check requires a source-kernel test boot.' >&2; exit 2 ;; esac trap 'echo "CASE source-kernel-running-build-guard FAIL"' ERR state=$(mktemp -d /var/tmp/phoebus-kernel-guard.XXXXXX) trap 'rm -rf "$state"' EXIT capture() { python3 - <<'PY' import hashlib import json from pathlib import Path files = list(Path('/var/db/pkg').glob('*/*/COUNTER')) files += [p for p in Path('/etc/kernel').rglob('*') if p.is_file()] files += [Path('/efi/loader/loader.conf')] print(json.dumps({str(p): hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(files)})) PY } capture > "$state/before" if sys --legacy --yes --action "{\"action\":\"build_${choice}_kernel\",\"make_default\":false}" > "$state/output" 2>&1; then echo 'replacement build was unexpectedly permitted' >&2 exit 1 fi grep -q 'boot the prebuilt generic kernel before building a source kernel' "$state/output" capture > "$state/after" cmp "$state/before" "$state/after" echo 'CASE source-kernel-running-build-guard PASS'