feat(guanghu-os): add cross-root supervisor slice

This commit is contained in:
冰朔 2026-08-15 23:49:28 +08:00
commit 9db3ecd2e7
10 changed files with 317 additions and 5 deletions

View file

@ -0,0 +1,58 @@
#!/usr/bin/env bash
set -Eeuo pipefail
[[ $# == 3 ]] || {
echo 'usage: build-guanghu-cross-root-fixture.sh <output-image> <config> <root-supervisor>' >&2
exit 64
}
output=$(readlink -m "$1")
config=$(readlink -f "$2")
supervisor=$(readlink -f "$3")
root_uuid=$(sed -n 's/^root_uuid=//p' "${config}")
[[ ${root_uuid} =~ ^[0-9a-f-]{36}$ ]]
[[ -x /bin/busybox && -x ${supervisor} ]]
command -v mkfs.ext4 >/dev/null
staging=$(mktemp -d)
trap 'rm -rf "${staging}"' EXIT
mkdir -p "${staging}/bin" "${staging}/sbin" "${staging}/usr/lib/guanghu" \
"${staging}/etc/guanghu" "${staging}/run" "${staging}/dev" \
"${staging}/proc" "${staging}/sys" "${staging}/lib/modules" \
"$(dirname "${output}")"
install -m 0755 /bin/busybox "${staging}/bin/busybox"
for applet in sh sleep cat grep tr stat mkdir mv chmod readlink kill sync poweroff; do
ln -s busybox "${staging}/bin/${applet}"
done
install -m 0755 "${supervisor}" "${staging}/usr/lib/guanghu/guanghu-root-supervisor"
install -m 0600 "${config}" "${staging}/etc/guanghu/first-boot-supervisor.conf"
printf '%s\n' 'guanghu.cross-root-fixture/v1' >"${staging}/etc/guanghu/rootfs.marker"
cat >"${staging}/sbin/init" <<'EOF'
#!/bin/sh
set -eu
export PATH=/bin:/sbin
echo 'GUANGHU_SWITCH_ROOT_INIT_ACTIVE marker=guanghu.cross-root-fixture/v1'
/usr/lib/guanghu/guanghu-root-supervisor run &
supervisor_pid=$!
attempt=0
while [ ! -s /run/guanghu/root-supervisor/state.json ] && [ "${attempt}" -lt 100 ]; do
sleep 0.05
attempt=$((attempt + 1))
done
kill -0 "${supervisor_pid}"
grep -Fq '"stage":"ROOT_SUPERVISOR_ACTIVE"' /run/guanghu/root-supervisor/state.json
echo "GUANGHU_CROSS_ROOT_PERSISTENCE_VERIFIED pid=${supervisor_pid}"
kill "${supervisor_pid}"
wait "${supervisor_pid}"
sync
poweroff -f
while :; do sleep 30; done
EOF
chmod 0755 "${staging}/sbin/init"
truncate -s 128M "${output}"
mkfs.ext4 -q -F -U "${root_uuid}" -L GUANGHU_XROOT -d "${staging}" "${output}"
chmod 0600 "${output}"
sha256sum "${output}" >"${output}.sha256"
printf 'GUANGHU_CROSS_ROOT_FIXTURE_BUILT output=%s root_uuid=%s\n' "${output}" "${root_uuid}"

View file

@ -0,0 +1,109 @@
#!/bin/sh
set -eu
die() {
printf 'GUANGHU_ROOT_SUPERVISOR_FAIL_0: %s\n' "$1" >&2
exit 1
}
test_root=${GUANGHU_ROOT_SUPERVISOR_TEST_ROOT:-}
if [ -n "${test_root}" ]; then
test_root=$(readlink -f "${test_root}")
config_path=${GUANGHU_ROOT_SUPERVISOR_CONFIG:-}
handoff_path=${GUANGHU_ROOT_SUPERVISOR_HANDOFF:-}
state_root=${GUANGHU_ROOT_SUPERVISOR_STATE_ROOT:-}
dmi_path=${GUANGHU_ROOT_SUPERVISOR_DMI_PATH:-}
cmdline_path=${GUANGHU_ROOT_SUPERVISOR_CMDLINE_PATH:-}
boot_id_path=${GUANGHU_ROOT_SUPERVISOR_BOOT_ID_PATH:-}
for override_path in "${config_path}" "${handoff_path}" "${state_root}" \
"${dmi_path}" "${cmdline_path}" "${boot_id_path}"; do
resolved_path=$(readlink -f "${override_path}")
case "${resolved_path}" in "${test_root}"|"${test_root}"/*) ;; *) die "test path escapes isolated root" ;; esac
done
else
[ -z "${GUANGHU_ROOT_SUPERVISOR_CONFIG:-}${GUANGHU_ROOT_SUPERVISOR_HANDOFF:-}${GUANGHU_ROOT_SUPERVISOR_STATE_ROOT:-}${GUANGHU_ROOT_SUPERVISOR_DMI_PATH:-}${GUANGHU_ROOT_SUPERVISOR_CMDLINE_PATH:-}${GUANGHU_ROOT_SUPERVISOR_BOOT_ID_PATH:-}" ] ||
die "production path overrides are forbidden"
config_path=/etc/guanghu/first-boot-supervisor.conf
handoff_path=/run/guanghu/first-boot/handoff.json
state_root=/run/guanghu/root-supervisor
dmi_path=/sys/class/dmi/id/product_uuid
cmdline_path=/proc/cmdline
boot_id_path=/proc/sys/kernel/random/boot_id
fi
[ -f "${config_path}" ] && [ ! -L "${config_path}" ] || die "configuration is missing or unsafe"
config_mode=$(stat -c '%a' "${config_path}" 2>/dev/null || stat -f '%Lp' "${config_path}")
[ "${config_mode}" = 600 ] || die "configuration mode must be 0600"
if [ -z "${test_root}" ]; then
config_owner=$(stat -c '%u' "${config_path}" 2>/dev/null || stat -f '%u' "${config_path}")
[ "${config_owner}" = 0 ] || die "configuration must be root-owned"
fi
schema=
node_id=
instance_id=
root_uuid=
linux_rescue_entry=
linux_code_bridge=
while IFS='=' read -r key value; do
case "${key}" in
schema) schema=${value} ;;
node_id) node_id=${value} ;;
instance_id) instance_id=${value} ;;
root_uuid) root_uuid=${value} ;;
linux_rescue_entry) linux_rescue_entry=${value} ;;
linux_code_bridge) linux_code_bridge=${value} ;;
''|'#'*) ;;
*) die "unknown configuration field ${key}" ;;
esac
done <"${config_path}"
[ "${schema}" = guanghu.first-boot-supervisor/v1 ] || die "configuration schema mismatch"
[ "${node_id}" = JD-FD-PRIMARY ] || die "node identity mismatch"
observed_instance=$(tr 'A-F' 'a-f' <"${dmi_path}" | tr -d '\r\n')
[ "${observed_instance}" = "${instance_id}" ] || die "DMI identity mismatch"
cmdline=$(cat "${cmdline_path}")
case " ${cmdline} " in *' guanghu.first_boot=1 '*) ;; *) die "explicit first-boot marker is missing" ;; esac
case " ${cmdline} " in *" root=UUID=${root_uuid} "*) ;; *) die "root UUID binding is missing" ;; esac
boot_id=$(tr -d '\r\n' <"${boot_id_path}")
verify_handoff() {
[ -f "${handoff_path}" ] && [ ! -L "${handoff_path}" ] || die "pre-root handoff is missing or unsafe"
handoff_mode=$(stat -c '%a' "${handoff_path}" 2>/dev/null || stat -f '%Lp' "${handoff_path}")
[ "${handoff_mode}" = 600 ] || die "pre-root handoff mode must be 0600"
handoff=$(cat "${handoff_path}")
for binding in \
'"schema":"guanghu.first-boot-handoff/v1"' \
"\"node_id\":\"${node_id}\"" \
"\"instance_id\":\"${instance_id}\"" \
"\"boot_id\":\"${boot_id}\"" \
'"control":"GUANGHU_OS"' \
'"stage":"PRE_ROOT_SUPERVISOR_ACTIVE"' \
'"full_linux_userspace":"DORMANT"' \
"\"linux_rescue\":\"${linux_rescue_entry}\""; do
printf '%s' "${handoff}" | grep -Fq "${binding}" || die "pre-root handoff binding mismatch"
done
}
write_state() {
mkdir -p "${state_root}"
temporary_path=${state_root}/state.json.tmp.$$
printf '%s\n' "{\"schema\":\"guanghu.root-supervisor-state/v1\",\"node_id\":\"${node_id}\",\"instance_id\":\"${instance_id}\",\"boot_id\":\"${boot_id}\",\"pid\":$$,\"control\":\"GUANGHU_OS\",\"stage\":\"ROOT_SUPERVISOR_ACTIVE\",\"linux_kernel_role\":\"HARDWARE_COMPATIBILITY_SUBSTRATE\",\"linux_code_bridge\":\"${linux_code_bridge}\",\"linux_rescue\":\"${linux_rescue_entry}\"}" >"${temporary_path}"
chmod 0600 "${temporary_path}"
mv "${temporary_path}" "${state_root}/state.json"
}
case "${1:-}" in
verify)
verify_handoff
printf 'GUANGHU_ROOT_HANDOFF_VERIFIED node=%s boot_id=%s\n' "${node_id}" "${boot_id}"
;;
run)
verify_handoff
write_state
printf 'GUANGHU_ROOT_SUPERVISOR_ACTIVE node=%s boot_id=%s pid=%s\n' "${node_id}" "${boot_id}" "$$"
trap 'exit 0' TERM INT HUP
while :; do sleep 30 & wait $! || true; done
;;
*) die "usage: guanghu-root-supervisor <verify|run>" ;;
esac

View file

@ -0,0 +1,64 @@
#!/usr/bin/env bash
set -Eeuo pipefail
source_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
supervisor=${source_root}/scripts/guanghu-root-supervisor.sh
unit=${source_root}/packaging/systemd/guanghu-root-supervisor.service
fixture=$(mktemp -d)
supervisor_pid=
trap '[[ -z ${supervisor_pid} ]] || kill "${supervisor_pid}" 2>/dev/null || true; rm -rf "${fixture}"' EXIT
mkdir -p "${fixture}/etc" "${fixture}/run/first-boot" "${fixture}/run/root-supervisor" \
"${fixture}/proc/sys/kernel/random" "${fixture}/sys"
cat >"${fixture}/etc/supervisor.conf" <<'EOF'
schema=guanghu.first-boot-supervisor/v1
node_id=JD-FD-PRIMARY
instance_id=f3d4b730-7f02-452f-975b-7091a4800431
root_uuid=9e4550a0-452b-4f28-b5a5-d5364aa450f6
linux_rescue_entry=gnulinux-simple-9e4550a0-452b-4f28-b5a5-d5364aa450f6
linux_code_bridge=hlcc-jd-candidate.service
EOF
chmod 0600 "${fixture}/etc/supervisor.conf"
printf '%s\n' f3d4b730-7f02-452f-975b-7091a4800431 >"${fixture}/sys/product_uuid"
printf '%s\n' 11111111-2222-4333-8444-555555555555 >"${fixture}/proc/sys/kernel/random/boot_id"
printf '%s\n' 'guanghu.first_boot=1 root=UUID=9e4550a0-452b-4f28-b5a5-d5364aa450f6 ro' >"${fixture}/proc/cmdline"
printf '%s\n' '{"schema":"guanghu.first-boot-handoff/v1","node_id":"JD-FD-PRIMARY","instance_id":"f3d4b730-7f02-452f-975b-7091a4800431","boot_id":"11111111-2222-4333-8444-555555555555","control":"GUANGHU_OS","stage":"PRE_ROOT_SUPERVISOR_ACTIVE","full_linux_userspace":"DORMANT","linux_code_bridge":"PENDING_BOUNDED_HANDOFF","linux_rescue":"gnulinux-simple-9e4550a0-452b-4f28-b5a5-d5364aa450f6"}' >"${fixture}/run/first-boot/handoff.json"
chmod 0600 "${fixture}/run/first-boot/handoff.json"
env GUANGHU_ROOT_SUPERVISOR_TEST_ROOT="${fixture}" \
GUANGHU_ROOT_SUPERVISOR_CONFIG="${fixture}/etc/supervisor.conf" \
GUANGHU_ROOT_SUPERVISOR_HANDOFF="${fixture}/run/first-boot/handoff.json" \
GUANGHU_ROOT_SUPERVISOR_STATE_ROOT="${fixture}/run/root-supervisor" \
GUANGHU_ROOT_SUPERVISOR_DMI_PATH="${fixture}/sys/product_uuid" \
GUANGHU_ROOT_SUPERVISOR_CMDLINE_PATH="${fixture}/proc/cmdline" \
GUANGHU_ROOT_SUPERVISOR_BOOT_ID_PATH="${fixture}/proc/sys/kernel/random/boot_id" \
"${supervisor}" run >"${fixture}/stdout" 2>"${fixture}/stderr" &
supervisor_pid=$!
for _ in {1..50}; do [[ -s ${fixture}/run/root-supervisor/state.json ]] && break; sleep 0.02; done
if ! kill -0 "${supervisor_pid}" 2>/dev/null; then
cat "${fixture}/stderr" >&2
exit 1
fi
grep -Fq 'GUANGHU_ROOT_SUPERVISOR_ACTIVE node=JD-FD-PRIMARY' "${fixture}/stdout"
grep -Fq '"stage":"ROOT_SUPERVISOR_ACTIVE"' "${fixture}/run/root-supervisor/state.json"
kill "${supervisor_pid}"
wait "${supervisor_pid}"
supervisor_pid=
printf '%s\n' aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee >"${fixture}/proc/sys/kernel/random/boot_id"
if env GUANGHU_ROOT_SUPERVISOR_TEST_ROOT="${fixture}" \
GUANGHU_ROOT_SUPERVISOR_CONFIG="${fixture}/etc/supervisor.conf" \
GUANGHU_ROOT_SUPERVISOR_HANDOFF="${fixture}/run/first-boot/handoff.json" \
GUANGHU_ROOT_SUPERVISOR_STATE_ROOT="${fixture}/run/root-supervisor" \
GUANGHU_ROOT_SUPERVISOR_DMI_PATH="${fixture}/sys/product_uuid" \
GUANGHU_ROOT_SUPERVISOR_CMDLINE_PATH="${fixture}/proc/cmdline" \
GUANGHU_ROOT_SUPERVISOR_BOOT_ID_PATH="${fixture}/proc/sys/kernel/random/boot_id" \
"${supervisor}" verify >/dev/null 2>&1; then
echo 'root supervisor accepted a handoff from another boot' >&2
exit 1
fi
grep -Fq 'DefaultDependencies=no' "${unit}"
grep -Fq 'Before=sysinit.target basic.target' "${unit}"
grep -Fq 'ExecStart=/usr/lib/guanghu/guanghu-root-supervisor run' "${unit}"
echo GUANGHU_ROOT_SUPERVISOR_CONTRACT_OK