opnsense-deps/release/vm.sh
Jeremy Karst 87893ded3e Build 26.7 for FreeBSD 15 only; port caam and cdx to its kernel
- caam: the bus activate, deactivate and release resource methods take
  no type and rid in FreeBSD 15.
- cdx: sys/cdefs.h defines __maybe_unused now; define it only if missing.
- 26.1 is end of life and no longer built: remove patches/26.1 and
  make-26.1.conf, default to 26.7 and FreeBSD 15.1 throughout, and move
  the build examples in README.md and RELEASE.md to 26.7.9. The 26.1
  tree on the mirror stays, for gateways that still have to upgrade.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 19:26:41 -05:00

207 lines
7.2 KiB
Bash

#!/bin/sh
#
# vm.sh: the FreeBSD arm64 build VM, driven from this workstation. The VM runs on the
# build host GW_VMHOST (an ssh destination; empty: this machine), emulated by QEMU in
# a rootless podman container (release/vmhost.sh, which vm.sh runs there). Its ssh
# port listens on the build host's loopback only; vm.sh reaches it through the build
# host (ProxyJump). The build is native: base, kernel, ports and image are all built
# on arm64. Emulation makes it slow, not unreliable: qemu's user-mode emulation of a
# cross build cannot even run cargo.
#
# vm.sh create OSREL download and verify the official FreeBSD OSREL arm64
# image on the build host, grow it, make the seed
# vm.sh start | stop | status OSREL
# vm.sh ssh OSREL [COMMAND...]
# vm.sh setup OSREL install the build host packages, swap on the swap
# disk, a small ZFS cache (build.sh setup)
# vm.sh sync OSREL copy this repository (with .git) to /usr/deps in the VM
# vm.sh run OSREL BUILD-ARGS... run release/build.sh BUILD-ARGS in the VM, detached;
# output goes to /root/gwbuild.log
# vm.sh log OSREL follow /root/gwbuild.log
# vm.sh sign OSREL SERIES VERSION put the signing key into a memory file system in the
# VM, run "build.sh finish", and remove the key again
# vm.sh pull-out OSREL SERIES copy the signed sets and image to $GW_OUT/SERIES
# vm.sh destroy OSREL stop the VM and delete its disk
#
# OSREL is the FreeBSD release the OPNsense series is built on: 15.1 for 26.7
# (the OS line of tools' config/<series>/build.conf).
#
# Settings come from the environment or from $GW_KEYDIR/vm.conf (sourced): GW_VMHOST,
# GW_VMROOT (the VM directory on the build host; vmhost.sh's default ~/gwbuild-vm),
# GW_VM_CPUS (8), GW_VM_MEM (16G), GW_VM_DISK (100G), GW_VM_SWAP (16G), GW_OUT (default
# /scratch/mono-opnsense/out), GW_KEYDIR (default ~/.config/gwmirror, holding repo.key,
# repo.pub and the VM's ssh key).
#
# Copyright 2026 Karst Tech
# SPDX-License-Identifier: BSD-2-Clause
set -eu
HERE=$(cd "$(dirname "$0")/.." && pwd)
KEYDIR=${GW_KEYDIR:-${HOME}/.config/gwmirror}
# shellcheck disable=SC1091
[ ! -f "${KEYDIR}/vm.conf" ] || . "${KEYDIR}/vm.conf"
VMHOST=${GW_VMHOST:-}
OUT=${GW_OUT:-/scratch/mono-opnsense/out}
SSHKEY=${KEYDIR}/vm_ed25519
die()
{
echo "vm.sh: $*" >&2
exit 1
}
usage()
{
sed -n '3,33p' "${0}" >&2
exit 2
}
[ $# -ge 2 ] || usage
CMD=${1}
OSREL=${2}
shift 2
echo "${OSREL}" | grep -Eqx '1[4-9]\.[0-9]' || die "bad FreeBSD release '${OSREL}'"
PORT=$((2200 + ${OSREL%.*}))
KNOWN=${KEYDIR}/vm_known_hosts-${OSREL}
SSH_OPTS="-o Port=${PORT} -o IdentityFile=${SSHKEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile=${KNOWN} -o ConnectTimeout=20 -o ServerAliveInterval=60"
[ -z "${VMHOST}" ] || SSH_OPTS="${SSH_OPTS} -o ProxyJump=${VMHOST}"
running()
{
vssh true 2> /dev/null
}
vssh()
{
# shellcheck disable=SC2086
ssh ${SSH_OPTS} root@127.0.0.1 "$@"
}
# words passed through ssh are checked, since the remote shell parses them again
safe_words()
{
for w; do
echo "${w}" | grep -Eqx '[A-Za-z0-9][A-Za-z0-9._/,@+=-]*' || die "unsafe argument '${w}'"
done
}
# vmhost COMMAND [ARG...]: release/vmhost.sh on the build host, with the VM settings
vmhost()
{
safe_words "$@"
_env=
for _v in GW_VMROOT GW_VM_CPUS GW_VM_MEM GW_VM_DISK GW_VM_SWAP; do
eval "_val=\${${_v}:-}"
[ -z "${_val}" ] || { safe_words "${_val}"; _env="${_env} ${_v}=${_val}"; }
done
if [ -n "${VMHOST}" ]; then
# shellcheck disable=SC2029
ssh -o BatchMode=yes "${VMHOST}" "env${_env} sh -s -- $*" < "${HERE}/release/vmhost.sh"
else
# shellcheck disable=SC2086
env${_env} sh "${HERE}/release/vmhost.sh" "$@"
fi
}
create()
{
mkdir -p "${KEYDIR}"
[ -f "${SSHKEY}" ] || ssh-keygen -q -t ed25519 -N '' -C gwmirror-build-vm -f "${SSHKEY}"
# shellcheck disable=SC2046
vmhost create "${OSREL}" $(cut -d' ' -f1-3 "${SSHKEY}.pub")
rm -f "${KNOWN}"
echo ">>> next: vm.sh start ${OSREL}"
}
start()
{
vmhost start "${OSREL}"
# the first boot applies freebsd-update patches and reboots before sshd
# starts, which takes long under emulation
echo ">>> waiting for ssh (up to 4 hours)"
i=0
until running; do
i=$((i + 1))
[ ${i} -lt 480 ] || die "no ssh after 4 hours; see console.log in the VM directory on the build host"
if [ $((i % 10)) = 0 ] && [ "$(vmhost status "${OSREL}")" = stopped ]; then
die "the VM stopped; see console.log in the VM directory and 'podman logs gwbuild-${OSREL}' on the build host"
fi
sleep 30
done
echo ">>> up: vm.sh ssh ${OSREL}"
}
stop()
{
vssh shutdown -p now 2> /dev/null || true
vmhost stop "${OSREL}"
}
sync_deps()
{
running || die "not running"
# owned by root in the VM: git refuses a repository owned by another user.
# The VM's build output is kept; dist/.keep is tracked (build.sh's clean check).
rsync -a --chown=0:0 --delete --include /dist/.keep --exclude '/dist/*' --exclude /_build/ \
--exclude /_vendor/ --exclude /.claude/ \
-e "ssh ${SSH_OPTS}" "${HERE}/" root@127.0.0.1:/usr/deps/
echo ">>> synced ${HERE} to /usr/deps ($(git -C "${HERE}" describe --always --dirty))"
}
run_build()
{
[ $# -ge 1 ] || usage
safe_words "$@"
running || die "not running"
if vssh 'test -f /root/gwbuild.pid && pgrep -F /root/gwbuild.pid > /dev/null'; then
die "a build is already running (vm.sh log ${OSREL})"
fi
vssh "daemon -f -o /root/gwbuild.log -p /root/gwbuild.pid sh /usr/deps/release/build.sh $*"
echo ">>> started build.sh $*; follow with: vm.sh log ${OSREL}"
}
sign()
{
[ $# -eq 2 ] || usage
safe_words "$@"
running || die "not running"
[ -f "${KEYDIR}/repo.key" ] && [ -f "${KEYDIR}/repo.pub" ] || die "no repo.key/repo.pub in ${KEYDIR}"
vssh 'umount /root/gwkey 2>/dev/null; mkdir -p /root/gwkey && mount -t tmpfs -o size=1m,mode=0700 tmpfs /root/gwkey'
trap 'vssh "umount -f /root/gwkey" || echo "vm.sh: WARNING: unmount /root/gwkey in the VM by hand" >&2' EXIT
# shellcheck disable=SC2086
scp -q ${SSH_OPTS} "${KEYDIR}/repo.key" "${KEYDIR}/repo.pub" root@127.0.0.1:/root/gwkey/
# the exit status of finish, not of tee
vssh "{ sh /usr/deps/release/build.sh finish $1 $2 /root/gwkey 2>&1; echo \$? > /root/gwfinish.rc; } |
tee -a /root/gwbuild.log; exit \$(cat /root/gwfinish.rc)"
}
pull_out()
{
[ $# -eq 1 ] || usage
safe_words "$@"
mkdir -p "${OUT}/${1}"
for d in sets images; do
rsync -a --delete --include '/base-*' --include '/kernel-*' --include '/packages-*' \
--include '/aux-*' --include '/OPNsense-*.img.gz*' --exclude '*' -e "ssh ${SSH_OPTS}" \
"root@127.0.0.1:/usr/local/opnsense/build/${1}/aarch64/${d}/" "${OUT}/${1}/${d}/"
done
ls -l "${OUT}/${1}/sets" "${OUT}/${1}/images"
}
case "${CMD}" in
create) [ $# -eq 0 ] || usage; create ;;
start) start ;;
stop) stop ;;
status) vmhost status "${OSREL}" ;;
ssh) vssh "$@" ;;
setup) running || die "not running"; vssh sh -s setup < "${HERE}/release/build.sh" ;;
sync) sync_deps ;;
run) run_build "$@" ;;
log) vssh tail -n 200 -F /root/gwbuild.log ;;
sign) sign "$@" ;;
pull-out) pull_out "$@" ;;
destroy) stop; vmhost destroy "${OSREL}"; rm -f "${KNOWN}" ;;
*) usage ;;
esac