The by-file index beside the package database is a cache, and pkg_add trusts it without checking the database: after a `force' run had removed packages, it still named digest-20220214 as the owner of /usr/pkg/bin/digest -- a package that was not registered and a file that did not exist. pkg_add then reported "digest-20220214: conflicting PLIST with digest-20220214: bin/digest" against itself and would not install the tool, so every build that needed something from the sysroot stopped. Measured 2026-11-22: 45 packages were left deleted, and none of them could be reinstalled. sandbox_index() rebuilds the index from the +CONTENTS of the packages that are registered, so it can only name packages that exist. It runs at the end of sandbox_mount(), before any build starts, and uses the sysroot's own pkg_admin (/usr/sbin: the package tools are part of a base set since they moved there), so the index format matches the pkg_add inside the chroot. Measured: with the stale index, `tools/pkgchroot.sh force pkgtools/digest' failed with the self-conflict above; after `pkg_admin -K ... rebuild' it built and installed (rc=0, 14131 files from 86 packages).
171 lines
6.7 KiB
Bash
Executable File
171 lines
6.7 KiB
Bash
Executable File
#!/bin/sh
|
|
# Build pkgsrc packages inside the target sysroot (build/obj/destdir.<arch>),
|
|
# the way NetBSD builds its own packages: chroot into the target system, build
|
|
# with its compiler, its libraries and its package tools, install into its
|
|
# /usr/pkg. Nothing of the build host ends up in the package.
|
|
#
|
|
# License: CNCL 1.0 (see LICENSE at the root of the tree)
|
|
set -eu
|
|
|
|
# mount(8), umount(8) and chroot(8) live in /sbin and /usr/sbin; a build
|
|
# started from cron or over ssh may come with a PATH that has neither
|
|
PATH=$PATH:/sbin:/usr/sbin
|
|
export PATH
|
|
|
|
TOP=$(cd "$(dirname "$0")/.." && pwd)
|
|
ARCH=${ARCH:-amd64}
|
|
D=$TOP/build/obj/destdir.$ARCH
|
|
SB=$TOP/build/sandbox
|
|
MNT=$D/mnt
|
|
|
|
umount_all()
|
|
{
|
|
# NetBSD's mount(8) prints "source on target type fs (local)": the target is
|
|
# the field after "on", not $2. Unmounting a parent before its children
|
|
# leaves mounts on a detached tree, and a second mount on the same target
|
|
# stacks -- lookups then find the bottom layer, an empty old directory.
|
|
# So: deepest first (reverse mount order), repeatedly, until nothing is left.
|
|
targets=$(mount |
|
|
awk -v p=" on $D/" 'index($0, p) {
|
|
sub(/^.* on /, ""); sub(/ type .*/, ""); print
|
|
}' | sed -n '1!G;h;$p')
|
|
for t in $targets; do
|
|
while mount | awk -v x=" on $t type" 'index($0, x)' | grep -q .; do
|
|
umount "$t" || break
|
|
done
|
|
done
|
|
}
|
|
|
|
sandbox_mount()
|
|
{
|
|
# A mount that outlived its directory shows an empty tree (the null mount
|
|
# keeps the vnode of the old one): start from nothing, every time.
|
|
umount_all
|
|
mkdir -p "$SB/work" "$TOP/build/packages"
|
|
# The sysroot's /dev belongs to the sets and must stay as they describe it.
|
|
# The build needs working device nodes, so it gets a tmpfs that is thrown
|
|
# away afterwards (the nodes come from the sysroot's own MAKEDEV).
|
|
if ! mount | grep -q " on $D/dev "; then
|
|
cp -p "$D/dev/MAKEDEV" "$SB/MAKEDEV"
|
|
cp -p "$D/dev/MAKEDEV.local" "$SB/MAKEDEV.local" 2>/dev/null || true
|
|
mount -t tmpfs tmpfs "$D/dev"
|
|
cp -p "$SB/MAKEDEV" "$D/dev/MAKEDEV"
|
|
cp -p "$SB/MAKEDEV.local" "$D/dev/MAKEDEV.local" 2>/dev/null || true
|
|
if ! chroot "$D" /bin/sh -c 'cd /dev && ./MAKEDEV all' >/dev/null 2>&1; then
|
|
echo "pkgchroot: MAKEDEV failed in the sysroot" >&2
|
|
fi
|
|
fi
|
|
mount | grep -q " on $MNT " || {
|
|
mount -t tmpfs tmpfs "$MNT"
|
|
mkdir -p "$MNT/pkgsrc" "$MNT/distfiles" "$MNT/work" \
|
|
"$MNT/packages" "$MNT/tmp"
|
|
}
|
|
mount | grep -q " on $MNT/pkgsrc " ||
|
|
mount -t null "$TOP/pkgsrc" "$MNT/pkgsrc"
|
|
mount | grep -q " on $MNT/distfiles " ||
|
|
mount -t null "${DISTFILES:-$TOP/distfiles}" "$MNT/distfiles"
|
|
trap 'umount_all' EXIT INT TERM HUP
|
|
mount | grep -q " on $MNT/work " ||
|
|
mount -t null "$SB/work" "$MNT/work"
|
|
# the binary packages go where the cross build puts them, so tools/make_repo
|
|
# and the sysinst repository see one directory on either host
|
|
mount | grep -q " on $MNT/packages " ||
|
|
mount -t null "$TOP/build/packages" "$MNT/packages"
|
|
# The file index beside the package database is a cache, and pkg_add
|
|
# trusts it blindly: after a `force' run removed packages, the index still
|
|
# named digest-20220214 as the owner of /usr/pkg/bin/digest -- a package
|
|
# that was not registered and a file that did not exist. pkg_add then
|
|
# reported "digest-20220214: conflicting PLIST with digest-20220214" when
|
|
# bootstrapping digest itself, and every build that needed a tool from the
|
|
# sysroot stopped (measured 2026-11-22: tools/pkgchroot.sh force wm/mcde
|
|
# left 45 packages deleted and could not reinstall any of them).
|
|
sandbox_index
|
|
}
|
|
|
|
# Rebuild the by-file index from the +CONTENTS of the installed packages, so it
|
|
# can only name packages that are registered. pkg_admin lives in the sysroot
|
|
# (/usr/sbin, part of a base set since the package tools moved there); the
|
|
# sysroot's own copy is used so the index format matches the pkg_add inside.
|
|
sandbox_index()
|
|
{
|
|
[ -x "$D/usr/sbin/pkg_admin" ] || return 0
|
|
chroot "$D" /usr/sbin/pkg_admin -K /usr/pkg/pkgdb rebuild >/dev/null 2>&1 ||
|
|
echo "pkgchroot: rebuild of pkgdb.byfile.db failed" >&2
|
|
}
|
|
|
|
# The base bmake does not read MAKECONF, and a mk.conf in the sysroot's /etc
|
|
# would be an extra file for checkflist: the settings travel as make arguments,
|
|
# which every sub-make inherits. `install' builds the package and installs it
|
|
# into /usr/pkg -- which, inside the chroot, is the sysroot's /usr/pkg.
|
|
sandbox_run()
|
|
{
|
|
# The names are prefixed on purpose: a plain `p=$1` here overwrote the
|
|
# caller's loop variable, so `force wm/mcde` ran its second sandbox_run
|
|
# with p=wm and reinstalled the whole category instead of the package
|
|
# (measured 2026-11-22, the MacBook began to build 2bwm and afterstep).
|
|
sr_path=$1
|
|
sr_dir=${1%/*}
|
|
mkdir -p "$SB/work/$sr_dir"
|
|
# docbook's stylesheets are local: without the catalog libxslt resolves the
|
|
# URL in the stylesheet PI and stops with "failed to load external entity
|
|
# http://docbook.sourceforge.net/..." when the build host has no route to it
|
|
# (measured 2026-11-21: xmlto, a dependency of wm/mcde, on the MacBook).
|
|
catalog=/usr/pkg/share/xsl/docbook/catalog.xml
|
|
[ -f "$D$catalog" ] || catalog=
|
|
chroot "$D" /usr/bin/env -i \
|
|
PATH=/usr/sbin:/usr/bin:/sbin:/bin \
|
|
HOME=/mnt/work \
|
|
TMPDIR=/mnt/tmp \
|
|
XML_CATALOG_FILES=$catalog \
|
|
/usr/bin/make -C "/mnt/pkgsrc/$sr_path" \
|
|
WRKOBJDIR=/mnt/work \
|
|
PACKAGES=/mnt/packages \
|
|
DISTDIR=/mnt/distfiles \
|
|
"${2:-install}"
|
|
}
|
|
|
|
# Programs the packages run while they are installed write state into the
|
|
# sysroot: fontconfig's cache (a font tool runs), MCDE's /var/dt (dtappgather
|
|
# at install) and the temporary files of the guide build. None of it is part
|
|
# of a set, and checkflist counts every one of them as an extra file (602 on
|
|
# 2026-11-20); none of these three paths is in a base set.
|
|
sandbox_clean()
|
|
{
|
|
rm -rf "$D/var/cache" "$D/var/dt" "$D/var/spool/calendar"
|
|
# the refcount index the package tools keep beside the database: no
|
|
# record of it is in METALOG, so checkflist counts it as an extra file
|
|
rm -rf "$D/usr/pkg/pkgdb.refcount"
|
|
rm -rf "$D"/tmp/dtinfogen-* "$D"/tmp/style_sheet.*
|
|
}
|
|
|
|
case ${1:-} in
|
|
umount) umount_all ;;
|
|
mount) sandbox_mount; echo "pkgchroot: mounted" ;;
|
|
build)
|
|
shift
|
|
sandbox_mount
|
|
for p in "$@"; do
|
|
echo "=== pkgchroot: $p"
|
|
sandbox_run "$p" install
|
|
done
|
|
sandbox_clean
|
|
echo "pkgchroot: packages in build/packages/All"
|
|
;;
|
|
force)
|
|
# the recipe changed but the version did not: `install' sees the package
|
|
# already recorded and does nothing, so the work directory is thrown away
|
|
# and the package installed again (measured 2026-11-21: the System
|
|
# Settings change went into no build at all)
|
|
shift
|
|
sandbox_mount
|
|
for p in "$@"; do
|
|
echo "=== pkgchroot (force): $p"
|
|
sandbox_run "$p" clean
|
|
sandbox_run "$p" reinstall
|
|
done
|
|
sandbox_clean
|
|
echo "pkgchroot: packages in build/packages/All"
|
|
;;
|
|
*) echo "usage: $0 mount|umount|build|force <pkgsrc/path>..." >&2; exit 2 ;;
|
|
esac
|