opnsense-deps/release/build.sh
Jeremy Karst 3277066c5a Release tooling: commit lock per release, escaping symlinks, doc fixes
- K2: config/lock-<version> gives tools, src, ports, plugins and core
  their exact commits; build stops when a checkout differs (a tag moved
  upstream, or src moved on), and fetches tags without --force.
  build.sh lock resolves and writes them. lock-26.7.9 is what 26.7.9 was
  built from, checked against the upstream tags.
- K3: verify --root reports symlinks that lead outside the tree or
  nowhere, which publish (rsync -L) would follow; such a path is now
  reported instead of crashing verify.
- K6: RELEASE.md no longer says core trusts only our key (it also trusts
  upstream's, for changelog and bogons), and names no host or user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 21:30:15 -05:00

635 lines
26 KiB
Bash

#!/bin/sh
#
# build.sh: build and sign a complete OPNsense release for the Mono Gateway.
# Runs as root on the FreeBSD aarch64 build host: the emulated arm64 VM of
# release/vm.sh, or a real aarch64 machine. This repository must be at /usr/deps.
#
# build.sh setup install the host packages the build needs
# build.sh lock SERIES VERSION resolve every repository's ref for VERSION and
# write the commits to /root/gwlock-VERSION, for
# config/lock-VERSION (review it, then commit it)
# build.sh build SERIES VERSION check out every repository at VERSION and build
# base, kernel, ports, plugins, core and the
# mono-gateway package; nothing is signed yet
# build.sh finish SERIES VERSION KEYDIR
# sign every set with KEYDIR/repo.{key,pub} (must be
# the key trusted in keys/fingerprints), build and
# sign the eMMC image; vm.sh sign provides KEYDIR
#
# SERIES is e.g. 26.7, VERSION e.g. 26.7.9. Repositories are checked out at the
# VERSION tag unless overridden (upstream sometimes skips a tag in one of them):
# TOOLS_REF, CORE_REF, PORTS_REF, PLUGINS_REF (default VERSION) and SRC_REF
# (default stable/SERIES of our src fork). FORGE and UPSTREAM name the git hosts.
# Each checkout must be at the commit config/lock-VERSION gives it, so a moved
# upstream tag or a src branch that moved on stops the build.
#
# Copyright 2026 Karst Tech
# SPDX-License-Identifier: BSD-2-Clause
set -eu
# set -e stops at the first failed command without a word: say so (die has said why)
trap '_rc=$?; [ "${_rc}" -eq 0 ] || [ -n "${DIED:-}" ] || echo "build.sh: stopped by a failed command (exit ${_rc})" >&2' EXIT
FORGE=${FORGE:-https://code.karsttech.com/claude}
UPSTREAM=${UPSTREAM:-https://github.com/opnsense}
DEPS=/usr/deps
DEVICE=GATEWAY
SYSROOT=/usr/obj/usr/src/arm64.aarch64/tmp
COMMITTER="-c user.name=gwmirror-build -c user.email=build@opnsense.karsttech.com"
# git must fail instead of hanging: it never prompts for credentials (nobody is at
# the terminal of a daemon'd build), and a transfer that moves nothing for 10
# minutes is aborted
export GIT_TERMINAL_PROMPT=0 GIT_HTTP_LOW_SPEED_LIMIT=1 GIT_HTTP_LOW_SPEED_TIME=600
# for ports fetched on this host (make "${FETCH_REPAIR}" ...)
FETCH_REPAIR="FETCH_CMD=sh ${DEPS}/release/fetch-distfile.sh"
PORTS_LOG=/var/tmp/gwbuild-ports.log
PORTS_RUN=/var/tmp/gwbuild-ports.run
die()
{
echo "build.sh: $*" >&2
DIED=yes
exit 1
}
step()
{
echo ">>> [$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
usage()
{
sed -n '3,24p' "${0}" >&2
DIED=yes
exit 2
}
args()
{
SERIES=${1:-}
VERSION=${2:-}
echo "${SERIES}" | grep -Eqx '[0-9]{2}\.[0-9]' || die "bad series '${SERIES}'"
echo "${VERSION}" | grep -Eqx "${SERIES}(\.[0-9]+)?" || die "version '${VERSION}' is not in series ${SERIES}"
LOCK=${DEPS}/config/lock-${VERSION}
SETS=/usr/local/opnsense/build/${SERIES}/aarch64/sets
IMAGES=/usr/local/opnsense/build/${SERIES}/aarch64/images
# the signing key is never read from the tools tree; finish passes it explicitly
TOOLS="make -C /usr/tools DEVICE=${DEVICE} SETTINGS=${SERIES} VERSION=${VERSION}"
UNSIGNED="PRIVKEY=/nonexistent PUBKEY=/nonexistent"
}
# checkout DIR URL REF BRANCH [shallow]: DIR at REF (a branch or tag of URL), on a
# local branch named BRANCH as tools expects (it checks branch names and resets its
# copies to them). "shallow" fetches only REF's commit and no tags: core and tools
# derive versions from the tags of core, plugins and ports, but never describe src
# (VERSION is always given), so src's history, most of its 2 GB, is not needed.
checkout()
{
if [ ! -d "${1}/.git" ]; then
[ ! -e "${1}" ] || [ -z "$(ls -A "${1}")" ] || die "${1} exists and is not a git checkout"
git init -q "${1}"
git -C "${1}" remote add origin "${2}"
fi
git -C "${1}" remote set-url origin "${2}"
if [ "${5:-}" = shallow ]; then
git -C "${1}" fetch -q --depth 1 --no-tags --force origin "${3}"
_ref=FETCH_HEAD
else
# no --force: a tag that moved upstream fails the fetch instead of
# silently replacing ours (and the lock would catch it as well)
git -C "${1}" fetch -q --tags origin
if git -C "${1}" rev-parse -q --verify "refs/remotes/origin/${3}" > /dev/null; then
_ref=refs/remotes/origin/${3}
else
_ref=refs/tags/${3}
fi
fi
git -C "${1}" rev-parse -q --verify "${_ref}^{commit}" > /dev/null || die "no ${3} in ${2}"
git -C "${1}" checkout -q --force -B "${4}" "${_ref}^{commit}"
git -C "${1}" clean -xdqf
echo " ${1}: ${4} = $(git -C "${1}" describe --tags --always) ($(git -C "${1}" rev-parse --short HEAD))"
}
# lock_check NAME DIR: DIR must be at the commit LOCK gives NAME ("NAME <40 hex>").
lock_check()
{
_want=$(sed -n "s/^${1}[[:space:]][[:space:]]*\([0-9a-f]\{40\}\)[[:space:]]*\$/\1/p" "${LOCK}")
[ "$(echo "${_want}" | grep -c .)" -eq 1 ] || die "$(basename "${LOCK}") must give exactly one commit for ${1}"
_have=$(git -C "${2}" rev-parse HEAD)
[ "${_have}" = "${_want}" ] ||
die "${2} is at ${_have}, but $(basename "${LOCK}") says ${_want}: a ref moved (build.sh lock shows the new commits; review them before updating the lock)"
echo " ${2}: locked commit"
}
# lock_step NAME DIR: check DIR against the lock, or record it (build.sh lock)
lock_step()
{
if [ -n "${WRITE_LOCK:-}" ]; then
printf '%s %s\n' "${1}" "$(git -C "${2}" rev-parse HEAD)" >> "${WRITE_LOCK}"
else
lock_check "${1}" "${2}"
fi
}
checkout_all()
{
step "checking out ${SERIES} ${VERSION}"
[ -f "${DEPS}/release/build.sh" ] || die "this repository must be at ${DEPS} (vm.sh sync)"
[ -n "${WRITE_LOCK:-}" ] || [ -f "${LOCK}" ] ||
die "no config/lock-${VERSION}: run build.sh lock ${SERIES} ${VERSION}, review it and commit it"
checkout /usr/tools "${UPSTREAM}/tools.git" "${TOOLS_REF:-${VERSION}}" master
lock_step tools /usr/tools
checkout /usr/src "${FORGE}/opnsense-src.git" "${SRC_REF:-stable/${SERIES}}" "stable/${SERIES}" shallow
lock_step src /usr/src
checkout /usr/ports "${UPSTREAM}/ports.git" "${PORTS_REF:-${VERSION}}" master
lock_step ports /usr/ports
checkout /usr/plugins "${UPSTREAM}/plugins.git" "${PLUGINS_REF:-${VERSION}}" "stable/${SERIES}"
lock_step plugins /usr/plugins
checkout /usr/core "${UPSTREAM}/core.git" "${CORE_REF:-${VERSION}}" "stable/${SERIES}"
lock_step core /usr/core # before our patches
[ -z "${WRITE_LOCK:-}" ] || return 0
PATCHES=$(ls "${DEPS}/patches/${SERIES}"/*.patch 2>/dev/null) || die "no patches/${SERIES}"
# shellcheck disable=SC2086
git ${COMMITTER} -C /usr/core am -q --committer-date-is-author-date ${PATCHES}
echo " /usr/core: $(git -C /usr/core describe --tags) with $(echo "${PATCHES}" | wc -l | tr -d ' ') patches"
cp "${DEPS}/config/GATEWAY.conf" /usr/tools/device/
ports_make_conf "/usr/tools/config/${SERIES}/make.conf"
ports_clean /usr/tools/build/ports.sh
echo " /usr/deps: $(git -C "${DEPS}" describe --tags --always --dirty)"
}
# ports_make_conf FILE: settings for the ports builds, added to tools' make.conf
# (checkout resets it): this repository's config/make-SERIES.conf, if there is one,
# and a job limit for lang/rust, which builds its own LLVM with a job per CPU; 24
# jobs outgrew the 12 GB VM (2026-11-20). One job per 768 MB of memory fits.
ports_make_conf()
{
[ ! -f "${DEPS}/config/make-${SERIES}.conf" ] || cat "${DEPS}/config/make-${SERIES}.conf" >> "${1}"
_jobs=$(($(sysctl -n hw.physmem) / 805306368))
[ "${_jobs}" -ge 1 ] || _jobs=1
printf 'lang_rust_VARS=\tMAKE_JOBS_NUMBER_LIMIT=%s\n' "${_jobs}" >> "${1}"
}
# ports_clean FILE: tools' ports.sh (FILE) removes every work directory after each
# port instead of running its make clean, which leaves some behind (RELEASE.md,
# build step 4); /usr/obj is WRKDIRPREFIX in tools' make.conf. Checkout resets it.
ports_clean()
{
# shellcheck disable=SC2016
if ! OLD='make -C ${PORTSDIR}/\${PORT} clean \${MAKE_ARGS}' NEW='rm -rf /usr/obj${PORTSDIR}' awk '
{ line = $0; sub(/^[ \t]+/, "", line) }
line == ENVIRON["OLD"] { sub(/[^ \t].*/, ENVIRON["NEW"]); n++ }
{ print }
END { exit n != 2 }' "${1}" > "${1}.new"; then
rm -f "${1}.new"
die "${1}: not the two port cleans expected"
fi
mv "${1}.new" "${1}"
}
# tools insists on a host pkg of the same major.minor as the ports tree's, which
# its chroots build from PKG_PORT (tools' PKG_ORIGIN; not ports-mgmt/pkg). Its
# distfiles and work directory stay out of /usr/ports: tools mounts the tree
# into its chroots with unionfs and replaces distfiles/ there, which fails on
# files in the lower layer.
PKG_PORT=opnsense/pkg
host_pkg()
{
_want=$(make -C "/usr/ports/${PKG_PORT}" -v PORTVERSION | cut -d. -f1-2)
_have=$(pkg -v | cut -d. -f1-2)
if [ "${_want}" != "${_have}" ]; then
step "building host pkg ${_want} from the ports tree (have ${_have})"
# its distfiles come from the distfiles set, as the ports step's do: GitHub
# turns away its patches for minutes at a time
repair_distfiles "${PKG_PORT}"
mkdir -p /var/tmp/gwbuild-host
# shellcheck disable=SC2046
tar -xf "$(distfiles_set)" -C /var/tmp/gwbuild-host $(port_distfiles "${PKG_PORT}")
make -C "/usr/ports/${PKG_PORT}" BATCH=yes "${FETCH_REPAIR}" \
DISTDIR=/var/tmp/gwbuild-host/distfiles WRKDIRPREFIX=/var/tmp/gwbuild-host \
clean all reinstall clean
rm -rf /var/tmp/gwbuild-host
fi
}
# check_kernel_bin SET: U-Boot boots boot/kernel/kernel.bin, and the gateway's
# 20-kernel-update skips a kernel set without it, leaving the old kernel booting.
# FreeBSD 15 builds it only with WITH_KERNEL_BIN in the kernel config.
check_kernel_bin()
{
tar -tf "${1}" ./boot/kernel/kernel.bin > /dev/null 2>&1 ||
die "$(basename "${1}") has no boot/kernel/kernel.bin (makeoptions WITH_KERNEL_BIN=t in the kernel config)"
}
# build_ports: tools' ports step, over the whole ports list in one run. Tools caches
# the build-only dependencies it builds (cmake, autoconf, the Rust and Go compilers)
# for the rest of a run only, and keeps just the listed ports and their runtime
# dependencies in the packages set, so splitting the list into several runs rebuilds
# those dependencies in each one: days under emulation. The set is written when the
# run ends; after a reboot of the build host during the run, salvage_ports adds the
# packages it had finished, and the build-only dependencies are rebuilt. A port that
# fails is recorded and the run goes on with the others (tools stops at the first
# failure only when its step is given an argument, which is why none is given here);
# the step fails at the end.
#
# The ports fetch their distfiles as they build. Tools' distfiles step, which would
# fetch them all beforehand for offline rebuilds, is not run: it walks the whole
# dependency tree of every port again, one make at a time, which takes days under
# emulation, and for flavored ports it walks the default flavor's tree (QEMU's
# desktop dependencies for qemu@guestagent), where it fails on ports the build
# never needs.
build_ports()
{
salvage_ports "/usr/obj/usr/tools/config/${SERIES}/aarch64/.pkg/All" \
"${SETS}/packages-${VERSION}-aarch64.tar" \
"${SERIES} ${VERSION} $(git -C /usr/ports rev-parse HEAD)"
# the ports step's chroot builds pkg before anything else
step "distfiles of ${PKG_PORT}"
repair_distfiles "${PKG_PORT}"
step "ports"
ports_run ports
}
# salvage_ports STAGE SET ID: add to the packages SET what an interrupted ports
# step had finished. Tools copies the runtime packages it keeps to its stage's
# packages directory (STAGE) as it goes, and makes the set of them when the step
# ends; a reboot of the build host before then leaves them only in the stage,
# which the next step clears. They are added if the stage belongs to this build:
# PORTS_RUN holds the ID (series, version, ports commit) of the last ports step
# started. Packages the set has are skipped, and so is one that is not a whole
# package archive with its +MANIFEST (written when the host went down). The build-only dependencies in the
# step's cache are not added (they are not part of the set); the next step
# rebuilds the ones it needs.
salvage_ports()
{
if [ -f "${PORTS_RUN}" ] && [ "$(cat "${PORTS_RUN}")" = "${3}" ] && [ -d "${1}" ]; then
_have=
[ ! -f "${2}" ] || _have=$(tar -tf "${2}")
_n=0
for _pkg in "${1}"/*.pkg; do
[ -f "${_pkg}" ] || continue
_entry=./All/$(basename "${_pkg}")
printf '%s\n' "${_have}" | grep -qxF "${_entry}" && continue
# the whole archive is read (a cut one fails), and it is a package
if ! _list=$(tar -tf "${_pkg}" 2> /dev/null) ||
! printf '%s\n' "${_list}" | grep -qx '+MANIFEST'; then
echo ">>> not salvaged, incomplete: ${_entry}"
continue
fi
if [ -f "${2}" ]; then _op=-rf; else _op=-cf; fi
tar -C "${1}/.." ${_op} "${2}" "${_entry}"
_n=$((_n + 1))
done
[ "${_n}" -eq 0 ] || step "salvaged ${_n} packages of an interrupted ports step"
fi
echo "${3}" > "${PORTS_RUN}"
}
# ports_run TARGET [ARG...]: a tools ports step, its output also in PORTS_LOG. The
# ports whose distfiles failed to fetch are repaired, and the step runs once more;
# it skips the packages it has already built.
ports_run()
{
ports_step "$@" && return 0
_ports=$(fetch_failures "${PORTS_LOG}")
[ -n "${_ports}" ] || die "a port failed to build (${PORTS_LOG})"
step "repairing the distfiles of:" ${_ports}
# shellcheck disable=SC2086
repair_distfiles ${_ports}
step "the same ports again, after the repair"
ports_step "$@" || die "a port failed to build (${PORTS_LOG})"
}
ports_step()
{
# shellcheck disable=SC2086
{ _rc=0; ${TOOLS} ${UNSIGNED} "$@" 2>&1 || _rc=$?; echo "${_rc}" > "${PORTS_LOG}.rc"; } |
tee "${PORTS_LOG}"
return "$(cat "${PORTS_LOG}.rc")"
}
# fetch_failures LOG: the ports whose distfiles could not be fetched, as the ports
# framework reports them in LOG: "Couldn't fetch it" (or "Giving up on fetching
# files" after checksum mismatches), then make's "stopped in" that port's directory.
# Only port names are accepted (they become arguments): no "..", globs or paths.
fetch_failures()
{
awk '/^=> (Couldn.t fetch it|Giving up on fetching files)/ { f = 1 }
f && /stopped in \/usr\/ports\/[a-z0-9][a-z0-9_-]*\/[A-Za-z0-9][A-Za-z0-9_.+-]*$/ {
sub(/.*stopped in \/usr\/ports\//, ""); print; f = 0 }' "${1}" | sort -u
}
# repair_distfiles PORT...: fetch the distfiles of the PORTs on this host with
# fetch-distfile.sh, which recovers the GitHub patches and Codeberg archives their
# sites now generate differently from the ones recorded in distinfo (the ports'
# checksum target verifies the results),
# and add them to the distfiles set, which tools extracts into the ports step's
# chroot. They are appended: a file the set already has is extracted twice, both
# copies verified. A PORT whose distfiles the set has already is skipped, so a
# rerun needs no download for it.
repair_distfiles()
{
_set=$(distfiles_set)
_listing=
[ ! -f "${_set}" ] || _listing=$(tar -tf "${_set}")
_dir=$(mktemp -d /var/tmp/gwdistfiles.XXXXXX)
for _rport; do
if distfiles_in_set "${_rport}" "${_listing}"; then
echo ">>> the distfiles set has the distfiles of ${_rport}"
continue
fi
make -C "/usr/ports/${_rport}" BATCH=yes ARCH=aarch64 DISTDIR="${_dir}/distfiles" \
"${FETCH_REPAIR}" checksum || die "cannot fetch the distfiles of ${_rport}"
done
if [ -d "${_dir}/distfiles" ]; then
mkdir -p "${SETS}"
if [ -f "${_set}" ]; then _op=-rf; else _op=-cf; fi
tar -C "${_dir}" ${_op} "${_set}" distfiles
fi
rm -rf "${_dir}"
}
# distfiles_set: the distfiles set (tools names it after the version it was made for)
distfiles_set()
{
_dset=$(ls "${SETS}"/distfiles-*.tar 2> /dev/null | head -1)
echo "${_dset:-${SETS}/distfiles-${VERSION}.tar}"
}
# port_distfiles PORT: the entries PORT's distfiles and patch files have in the
# distfiles set, distfiles/[DIST_SUBDIR/]NAME, one per line
port_distfiles()
{
_sub=$(make -C "/usr/ports/${1}" ARCH=aarch64 -V DIST_SUBDIR) || return 1
_files=$(make -C "/usr/ports/${1}" ARCH=aarch64 -V ALLFILES) || return 1
echo "${_files}" | tr -s ' \t' '\n\n' | sed -e '/^$/d' -e "s|^|distfiles/${_sub:+${_sub}/}|"
}
# distfiles_in_set PORT LISTING: whether LISTING, the distfiles set's "tar -t", has
# every distfile and patch file of PORT
distfiles_in_set()
{
_entries=$(port_distfiles "${1}") || return 1
printf '%s\n' "${2}" | ENTRIES=${_entries} awk '
BEGIN { n = split(ENVIRON["ENTRIES"], e, "\n"); for (i = 1; i <= n; i++) if (e[i] != "") need[e[i]] = 1 }
{ delete need[$0] }
END { for (f in need) exit 1 }'
}
# the board package is cross-compiled against the base set's headers and libraries
sysroot_base()
{
_base=$(ls "${SETS}"/base-"${VERSION}"-aarch64-"${DEVICE}".txz) || die "no base set"
step "populating ${SYSROOT} from $(basename "${_base}")"
mkdir -p "${SYSROOT}"
tar -xf "${_base}" -C "${SYSROOT}" --no-fflags --include './usr/include/*' \
--include './usr/lib/*.a' --include './usr/lib/*.so' --include './usr/lib/*.o' --include './lib/*.so*'
}
# ... and against libxml2 from our own packages set (fmc links it)
sysroot_libxml2()
{
_tmp=$(mktemp -d /tmp/gwbuild.XXXXXX)
_set=${SETS}/packages-${VERSION}-aarch64.tar
_member=$(tar -tf "${_set}" | grep -E '^(\./)?All/libxml2-[0-9][^/]*\.pkg$' | head -1)
[ -n "${_member}" ] || die "no libxml2 in the packages set"
tar -xf "${_set}" -C "${_tmp}" "${_member}"
_pkg=${_tmp}/${_member#./}
step "adding $(basename "${_pkg}") to ${SYSROOT}"
mkdir -p "${_tmp}/x" "${SYSROOT}/usr/local/include" "${SYSROOT}/usr/local/lib"
tar -xf "${_pkg}" -C "${_tmp}/x"
cp -R "${_tmp}/x/usr/local/include/libxml2" "${SYSROOT}/usr/local/include/"
cp -a "${_tmp}"/x/usr/local/lib/libxml2* "${SYSROOT}/usr/local/lib/"
rm -rf "${_tmp}"
}
# replace any mono-gateway package in the packages set with the one just built;
# "make sign-packages" later rebuilds the catalogue from the set's All/
inject_board_package()
{
_set=${SETS}/packages-${VERSION}-aarch64.tar
_pkg=$(ls "${DEPS}"/dist/mono-gateway-*.pkg) || die "no mono-gateway package in ${DEPS}/dist"
step "adding $(basename "${_pkg}") to $(basename "${_set}")"
_tmp=$(mktemp -d /tmp/gwbuild.XXXXXX)
tar -xpf "${_set}" -C "${_tmp}"
rm -f "${_tmp}"/All/mono-gateway-*.pkg "${_tmp}"/Latest/mono-gateway.pkg "${_tmp}"/Latest/mono-gateway.pkg.sig
cp "${_pkg}" "${_tmp}/All/"
tar -C "${_tmp}" -cf "${_set}.new" .
mv "${_set}.new" "${_set}"
rm -f "${_set}.sig"
rm -rf "${_tmp}"
}
# release_repo CONF VERSION: a repository configuration that points FreeBSD's ports
# repository (the first one CONF defines: "FreeBSD" up to 14, "FreeBSD-ports" from
# 15) at the packages built for exactly this release (VERSION, freebsd-version -u),
# not for the newest minor of the branch. A pkg+https URL needs mirror_type "srv".
release_repo()
{
_name=$(sed -n 's/^\([A-Za-z0-9_-]*\):[[:space:]]*{[[:space:]]*$/\1/p' "${1}" | head -1)
[ -n "${_name}" ] || die "no repository in ${1}"
_minor=$(echo "${2}" | sed -nE 's/^[0-9]+\.([0-9]+)-.*/\1/p')
[ -n "${_minor}" ] || die "unexpected FreeBSD version '${2}'"
printf '%s: { url: "pkg+https://pkg.FreeBSD.org/${ABI}/release_%s", mirror_type: "srv" }\n' \
"${_name}" "${_minor}"
}
setup()
{
[ "$(id -u)" = 0 ] || die "run as root"
mkdir -p /usr/local/etc/pkg/repos
release_repo /etc/pkg/FreeBSD.conf "$(freebsd-version -u)" > /usr/local/etc/pkg/repos/FreeBSD.conf
env ASSUME_ALWAYS_YES=yes pkg bootstrap -f
[ "$(uname -p)" = aarch64 ] || die "the build host must be aarch64 (release/vm.sh)"
pkg install -y git python3 rsync
_pool=$(zpool list -Ho name | head -1)
zfs set compression=zstd "${_pool}"
zpool set autotrim=on "${_pool}"
zpool list
vm_memory
step "host ready: $(freebsd-version -u), $(sysctl -n hw.ncpu) CPUs, $(($(sysctl -n hw.physmem) / 1073741824)) GB"
}
# vm_memory: in the build VM (it has the disk with serial gwswap that vmhost.sh
# attaches), swap on that disk and a ZFS cache of at most 2 GB. The build host backs
# every page of the VM's memory, and a cache grown into all of it leaves the host
# short; swap on ZFS itself can deadlock when memory runs out. A process waiting for
# a page is not killed: FreeBSD gives up after 30 seconds, which the emulated VM's
# pageout can take (2026-11-20: two llvm-tblgen, with 16 GB of swap unused).
# Elsewhere, nothing.
vm_memory()
{
for _d in $(sysctl -n kern.disks); do
[ "$(diskinfo -s "/dev/${_d}" 2> /dev/null)" = gwswap ] || continue
if [ ! -e /dev/gpt/gwswap ]; then
gpart create -s gpt "${_d}"
gpart add -t freebsd-swap -l gwswap "${_d}"
fi
grep -q '^/dev/gpt/gwswap[[:space:]]' /etc/fstab ||
printf '/dev/gpt/gwswap\tnone\tswap\tsw\t0\t0\n' >> /etc/fstab
swapctl -l | grep -q '^/dev/gpt/gwswap[[:space:]]' || swapon /dev/gpt/gwswap
# (sysrc refuses dotted names; the last setting in loader.conf counts)
grep -qx 'vfs.zfs.arc_max="2147483648"' /boot/loader.conf ||
echo 'vfs.zfs.arc_max="2147483648"' >> /boot/loader.conf
sysctl vfs.zfs.arc_max=2147483648
grep -qx 'vm.pfault_oom_attempts=-1' /etc/sysctl.conf ||
echo 'vm.pfault_oom_attempts=-1' >> /etc/sysctl.conf
sysctl vm.pfault_oom_attempts=-1
swapctl -lh
return 0
done
}
build()
{
args "$@"
[ "$(id -u)" = 0 ] || die "run as root"
_status=$(git -C "${DEPS}" status --porcelain) || die "git cannot read ${DEPS} (vm.sh sync)"
if [ -z "${ALLOW_DIRTY:-}" ] && [ -n "${_status}" ]; then
die "${DEPS} has uncommitted changes (commit them, or ALLOW_DIRTY=1 for a test build)"
fi
checkout_all
host_pkg
step "base kernel"
# shellcheck disable=SC2086
${TOOLS} ${UNSIGNED} base kernel
check_kernel_bin "${SETS}/kernel-${VERSION}-aarch64-GATEWAY.txz"
sysroot_base
step "mono-gateway kernel modules (early check against the new kernel)"
make -C "${DEPS}" -j"$(sysctl -n hw.ncpu)" modules OPS_SETTINGS="${SERIES}"
build_ports
step "plugins core packages"
# shellcheck disable=SC2086
${TOOLS} ${UNSIGNED} plugins core packages
sysroot_libxml2
step "mono-gateway package"
make -C "${DEPS}" -j"$(sysctl -n hw.ncpu)" package OPS_SETTINGS="${SERIES}" OPS_VERSION="${VERSION}"
inject_board_package
step "build done, unsigned; next: vm.sh sign <osrel> ${SERIES} ${VERSION}"
ls -l "${SETS}"
}
# scan_image IMAGE: no amd64 (or other non-aarch64) ELF file may end up in the image
# (firmware blobs aside). The image's pool is imported read-only without mounting
# anything, and each dataset is mounted read-only by itself: mounting them all at
# import (zpool import -R) needs mountpoints the read-only root lacks, and leaves
# nothing mounted. The root dataset must have /bin/sh, so an empty mount cannot pass.
scan_image()
{
step "scanning $(basename "${1}") for foreign ELF files"
_md=$(mdconfig -a -t vnode -o readonly -f "${1}")
_mnt=$(mktemp -d /tmp/gwimage.XXXXXX)
_bad=${_mnt}.bad
_root=
: > "${_bad}"
if zpool import -N -o readonly=on -R "${_mnt}" -d "/dev/${_md}p2" pool0 gwscan; then
for _ds in $(zfs list -H -r -t filesystem -o name gwscan); do
if ! mount -t zfs -o ro "${_ds}" "${_mnt}"; then
echo "cannot mount ${_ds}" >> "${_bad}"
continue
fi
[ "${_ds}" != gwscan/ROOT/default ] || [ ! -f "${_mnt}/bin/sh" ] || _root=yes
find "${_mnt}" -xdev -type f -size +0 -print0 | xargs -0 file -N | grep ': ELF' |
grep -v -e 'ARM aarch64' -e '/firmware/' | sed "s|^${_mnt}|${_ds}:|" >> "${_bad}" || true
umount "${_mnt}"
done
[ -n "${_root}" ] || echo "no /bin/sh in gwscan/ROOT/default" >> "${_bad}"
# right after the import, and after an unmount, the pool is "busy" for a while
_try=1
until zpool export gwscan; do
[ "${_try}" -lt 10 ] || die "cannot export the image's pool gwscan"
_try=$((_try + 1))
sleep 3
done
else
echo "cannot import the image's pool" > "${_bad}"
fi
mdconfig -d -u "${_md}"
rmdir "${_mnt}"
if [ -s "${_bad}" ]; then
cat "${_bad}" >&2
rm -f "${_bad}"
die "image contains non-aarch64 ELF files (or could not be scanned)"
fi
rm -f "${_bad}"
}
# sign_aux_repo SET: sign the catalogue of the aux repository in SET (KEYDIR has the
# key). Tools signs it only in a packages step run with the key; its sign-packages
# signs the packages set's catalogue again, but not the aux set's, which build
# leaves unsigned. The set's own signature comes after, from tools' sign-aux.
sign_aux_repo()
{
[ -f "${1}" ] || return 0
step "signing the catalogue of $(basename "${1}")"
_dir=$(mktemp -d /tmp/gwaux.XXXXXX)
if ! tar -C "${_dir}" -xpf "${1}" ||
! pkg repo "${_dir}/" signing_command: /usr/tools/scripts/pkg_sign.sh \
"${KEYDIR}/repo.pub" "${KEYDIR}/repo.key"; then
rm -rf "${_dir}"
die "cannot sign the catalogue of ${1}"
fi
tar -C "${_dir}" -cf "${1}.new" .
mv "${1}.new" "${1}"
rm -rf "${_dir}"
}
finish()
{
args "${1:-}" "${2:-}"
KEYDIR=${3:-}
[ "$(id -u)" = 0 ] || die "run as root"
[ -f "${KEYDIR}/repo.key" ] && [ -f "${KEYDIR}/repo.pub" ] || die "no repo.key/repo.pub in '${KEYDIR}'"
_fp=$(sha256 -q "${KEYDIR}/repo.pub")
grep -rqx "fingerprint: \"${_fp}\"" "${DEPS}/keys/fingerprints/trusted" ||
die "key ${_fp} is not in keys/fingerprints/trusted"
SIGNED="PRIVKEY=${KEYDIR}/repo.key PUBKEY=${KEYDIR}/repo.pub"
for _set in base kernel packages; do
ls "${SETS}/${_set}-${VERSION}-aarch64"*.t* > /dev/null || die "no ${_set} set; run build first"
done
sign_aux_repo "${SETS}/aux-${VERSION}-aarch64.tar"
step "signing sets"
# shellcheck disable=SC2086
${TOOLS} ${SIGNED} sign-base,kernel,packages,aux
step "building the eMMC image"
cp "${DEPS}/mk-image.sh" /usr/tools/build/arm.sh
cp "${DEPS}/tools/mkgpt.py" /usr/tools/build/mkgpt.py
rm -f "${IMAGES}"/OPNsense-*-"${DEVICE}".img*
# shellcheck disable=SC2086
${TOOLS} ${SIGNED} arm-5G || die "image build failed"
git -C /usr/tools checkout -q -- build/arm.sh
rm -f /usr/tools/build/mkgpt.py
_img=$(ls "${IMAGES}"/OPNsense-"${VERSION}"-arm-aarch64-"${DEVICE}".img) || die "no image produced"
scan_image "${_img}"
step "compressing and signing the image"
gzip -9f "${_img}"
rm -f "${_img}.sig"
sha256 -q "${_img}.gz" | /usr/tools/scripts/pkg_sign.sh "${KEYDIR}/repo.pub" "${KEYDIR}/repo.key" > "${_img}.gz.sig"
step "release ${VERSION} signed; next on the workstation: vm.sh pull-out <osrel> ${SERIES}"
ls -l "${SETS}" "${IMAGES}"
}
lock()
{
args "$@"
[ "$(id -u)" = 0 ] || die "run as root"
WRITE_LOCK=/root/gwlock-${VERSION}
: > "${WRITE_LOCK}"
checkout_all
step "commits for ${VERSION}; review them, then: vm.sh ssh <osrel> cat ${WRITE_LOCK} > config/lock-${VERSION}"
cat "${WRITE_LOCK}"
}
[ $# -ge 1 ] || usage
CMD=${1}
shift
case "${CMD}" in
setup) [ $# -eq 0 ] || usage; setup ;;
lock) [ $# -eq 2 ] || usage; lock "$@" ;;
build) [ $# -eq 2 ] || usage; build "$@" ;;
finish) [ $# -eq 3 ] || usage; finish "$@" ;;
*) usage ;;
esac