From 85b106938d9ea5c87a568ab0176844a4404cca05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B0=E6=9C=94?= <565183519@qq.com> Date: Sat, 15 Aug 2026 22:19:56 +0800 Subject: [PATCH] feat(guanghu-os): bridge JD code plane through supervisor --- .../systemd/guanghu-language-primary.target | 3 +- .../systemd/guanghu-supervisor.service | 18 +++ .../10-guanghu-supervisor.conf | 3 + .../guanghu-supervisor-control-bridge.sh | 120 ++++++++++++++++++ .../test-guanghu-supervisor-control-bridge.sh | 57 +++++++++ 5 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-supervisor.service create mode 100644 product-source/hololake-platform/guanghu-os/packaging/systemd/hlcc-jd-candidate.service.d/10-guanghu-supervisor.conf create mode 100644 product-source/hololake-platform/guanghu-os/scripts/guanghu-supervisor-control-bridge.sh create mode 100644 product-source/hololake-platform/guanghu-os/scripts/test-guanghu-supervisor-control-bridge.sh diff --git a/product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-language-primary.target b/product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-language-primary.target index 306762c..d52800c 100644 --- a/product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-language-primary.target +++ b/product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-language-primary.target @@ -8,6 +8,7 @@ Requires=lake-lamp-authz.service Requires=lake-lamp-architecture-provision.service Requires=lake-lamp-deployment-event-worker.service Requires=guanghu-boot-acceptance.service +Requires=guanghu-supervisor.service After=multi-user.target After=guanghu-world-gate.service After=bingshuo-tcs-living-controller.service @@ -16,5 +17,5 @@ After=lake-lamp-authz.service After=lake-lamp-architecture-provision.service After=lake-lamp-deployment-event-worker.service After=guanghu-boot-acceptance.service +After=guanghu-supervisor.service AllowIsolate=yes - diff --git a/product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-supervisor.service b/product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-supervisor.service new file mode 100644 index 0000000..94d51ee --- /dev/null +++ b/product-source/hololake-platform/guanghu-os/packaging/systemd/guanghu-supervisor.service @@ -0,0 +1,18 @@ +[Unit] +Description=Guanghu OS master control bridge for Linux collaboration services +Before=hlcc-jd-candidate.service +After=local-fs.target docker.service +Wants=docker.service + +[Service] +Type=oneshot +ExecStart=/usr/local/sbin/guanghu-supervisor-control-bridge activate +RemainAfterExit=yes +NoNewPrivileges=true +PrivateTmp=true +ProtectHome=true +ProtectSystem=strict +ReadWritePaths=/run/guanghu/supervisor + +[Install] +WantedBy=guanghu-language-primary.target diff --git a/product-source/hololake-platform/guanghu-os/packaging/systemd/hlcc-jd-candidate.service.d/10-guanghu-supervisor.conf b/product-source/hololake-platform/guanghu-os/packaging/systemd/hlcc-jd-candidate.service.d/10-guanghu-supervisor.conf new file mode 100644 index 0000000..d7abc06 --- /dev/null +++ b/product-source/hololake-platform/guanghu-os/packaging/systemd/hlcc-jd-candidate.service.d/10-guanghu-supervisor.conf @@ -0,0 +1,3 @@ +[Unit] +Requires=guanghu-supervisor.service +After=guanghu-supervisor.service diff --git a/product-source/hololake-platform/guanghu-os/scripts/guanghu-supervisor-control-bridge.sh b/product-source/hololake-platform/guanghu-os/scripts/guanghu-supervisor-control-bridge.sh new file mode 100644 index 0000000..fbd5bf7 --- /dev/null +++ b/product-source/hololake-platform/guanghu-os/scripts/guanghu-supervisor-control-bridge.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +set -Eeuo pipefail +umask 077 + +die() { + echo "GUANGHU_SUPERVISOR_BRIDGE_FAIL_0: $*" >&2 + exit 1 +} + +test_mode=${GH_SUPERVISOR_TEST_MODE:-0} +if [[ "${test_mode}" == 1 ]]; then + config_path=${GH_SUPERVISOR_CONFIG:?test config is required} + dmi_path=${GH_SUPERVISOR_DMI_PATH:?test DMI path is required} + grub_cfg=${GH_SUPERVISOR_GRUB_CFG:?test GRUB path is required} + state_root=${GH_SUPERVISOR_STATE_ROOT:?test state root is required} + systemctl_bin=${GH_SUPERVISOR_SYSTEMCTL:?test systemctl is required} + current_link=${GH_SUPERVISOR_CURRENT_LINK:?test current link is required} +else + [[ "${EUID}" == 0 ]] || die "root execution is required" + for override in GH_SUPERVISOR_CONFIG GH_SUPERVISOR_DMI_PATH GH_SUPERVISOR_GRUB_CFG GH_SUPERVISOR_STATE_ROOT GH_SUPERVISOR_SYSTEMCTL GH_SUPERVISOR_CURRENT_LINK; do + [[ -z "${!override:-}" ]] || die "production path override is forbidden: ${override}" + done + config_path=/etc/guanghu/supervisor-bridge.conf + dmi_path=/sys/class/dmi/id/product_uuid + grub_cfg=/boot/grub/grub.cfg + state_root=/run/guanghu/supervisor + systemctl_bin=/usr/bin/systemctl + current_link=/guanghu/current + [[ -f "${config_path}" && ! -L "${config_path}" ]] || die "root-owned supervisor config is missing" + [[ "$(stat -c '%u:%a' "${config_path}")" =~ ^0:(600|640|644)$ ]] || + die "supervisor config owner or mode is unsafe" +fi + +schema= +node_id= +instance_id= +backend_id= +backend_command= +rescue_slot_id= +current_root= +code_unit= +while IFS='=' read -r key value; do + [[ -n "${key}" ]] || continue + case "${key}" in + schema|node_id|instance_id|backend_id|backend_command|rescue_slot_id|current_root|code_unit) + printf -v "${key}" '%s' "${value}" + ;; + *) die "unknown config field: ${key}" ;; + esac +done <"${config_path}" + +[[ "${schema}" == "guanghu.supervisor-control-bridge/v1" ]] || die "config schema mismatch" +[[ "${node_id}" == "JD-FD-PRIMARY" ]] || die "target node mismatch" +[[ "${instance_id}" =~ ^[0-9a-f-]{36}$ ]] || die "instance id is invalid" +[[ "${backend_id}" =~ ^[a-z0-9-]+$ ]] || die "backend id is invalid" +[[ "${rescue_slot_id}" =~ ^[A-Za-z0-9._-]+$ ]] || die "rescue slot id is invalid" +if [[ "${test_mode}" == 1 ]]; then + [[ "${current_root}" == /* ]] || die "test current root is invalid" +else + [[ "${current_root}" =~ ^/guanghu/versions/[A-Za-z0-9._-]+$ ]] || die "current root is invalid" +fi +[[ "${code_unit}" == "hlcc-jd-candidate.service" ]] || die "code unit binding mismatch" +[[ -x "${backend_command}" && ! -L "${backend_command}" ]] || die "subcontrol backend is unavailable" +[[ -x "${systemctl_bin}" ]] || die "systemctl is unavailable" + +verify_machine() { + local observed + observed=$(tr '[:upper:]' '[:lower:]' <"${dmi_path}") + [[ "${observed}" == "${instance_id}" ]] || die "machine identity mismatch" +} + +verify_rescue() { + grep -F -- 'menuentry ' "${grub_cfg}" | grep -Fq -- "'${rescue_slot_id}'" || + die "independent Linux rescue slot is unavailable" +} + +verify_world() { + [[ "$(readlink -f "${current_link}")" == "$(readlink -f "${current_root}")" ]] || + die "active Guanghu world mismatch" +} + +verify_code_unit() { + [[ "$("${systemctl_bin}" show -p LoadState --value "${code_unit}")" == loaded ]] || + die "Linux Forgejo bridge unit is unavailable" +} + +preflight() { + verify_machine + verify_rescue + verify_world + verify_code_unit + "${backend_command}" preflight "${backend_id}" >/dev/null + [[ "$("${backend_command}" observe "${backend_id}")" == dormant ]] || + die "on-demand Linux subcontrol is not dormant" +} + +action=${1:-} +case "${action}" in + preflight) + preflight + echo "GUANGHU_SUPERVISOR_BRIDGE_PREFLIGHT_OK" + ;; + activate) + preflight + mkdir -p "${state_root}" + temporary="${state_root}/control.json.tmp" + printf '{"schema":"guanghu.supervisor-control-state/v1","node_id":"%s","control":"GUANGHU_OS","linux_code_bridge":"%s","linux_subcontrol":"dormant","linux_rescue":"%s"}\n' \ + "${node_id}" "${code_unit}" "${rescue_slot_id}" >"${temporary}" + chmod 600 "${temporary}" + mv "${temporary}" "${state_root}/control.json" + echo "GUANGHU_SUPERVISOR_BRIDGE_ACTIVE" + ;; + observe) + [[ -f "${state_root}/control.json" ]] || die "control state is unavailable" + cat "${state_root}/control.json" + ;; + *) + die "usage: $0 " + ;; +esac diff --git a/product-source/hololake-platform/guanghu-os/scripts/test-guanghu-supervisor-control-bridge.sh b/product-source/hololake-platform/guanghu-os/scripts/test-guanghu-supervisor-control-bridge.sh new file mode 100644 index 0000000..4e300ef --- /dev/null +++ b/product-source/hololake-platform/guanghu-os/scripts/test-guanghu-supervisor-control-bridge.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash +set -Eeuo pipefail + +source_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +bridge="${source_root}/scripts/guanghu-supervisor-control-bridge.sh" +fixture=$(mktemp -d) +trap 'rm -rf "${fixture}"' EXIT + +mkdir -p "${fixture}/bin" "${fixture}/state" "${fixture}/versions/test-supervisor-world" +ln -sfn "${fixture}/versions/test-supervisor-world" "${fixture}/current" +printf '%s\n' 'f3d4b730-7f02-452f-975b-7091a4800431' >"${fixture}/dmi" +printf '%s\n' "menuentry 'Ubuntu' --id 'rescue-slot' {" >"${fixture}/grub.cfg" + +cat >"${fixture}/backend" <<'EOF' +#!/usr/bin/env bash +[[ "${1:-}" == preflight || "${1:-}" == observe ]] || exit 1 +if [[ "${1:-}" == observe ]]; then echo dormant; fi +EOF +chmod +x "${fixture}/backend" + +cat >"${fixture}/systemctl" <<'EOF' +#!/usr/bin/env bash +[[ "$*" == 'show -p LoadState --value hlcc-jd-candidate.service' ]] || exit 1 +echo loaded +EOF +chmod +x "${fixture}/systemctl" + +cat >"${fixture}/bridge.conf" <"${fixture}/dmi" +if "${bridge}" preflight; then + echo "wrong machine identity was accepted" >&2 + exit 1 +fi + +echo "GUANGHU_SUPERVISOR_CONTROL_BRIDGE_OK"