Files
makronix/tools/makronix-set.sh
T

411 lines
17 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# Generate the "makronix" set from what pkgsrc installed into the sysroot.
#
# Makronix ships two kinds of software: what build.sh builds from src/ (the
# NetBSD sets), and what pkgsrc builds for the target and installs
# under /usr/pkg. The second kind belongs to a set of its own, "makronix",
# and NetBSD's release machinery needs two inputs for a set:
#
# src/distrib/sets/lists/makronix/mi the file list: makeflist (and with it
# checkflist, maketars, installsets)
# ${DESTDIR}/METALOG the metadata records: checkflist -M
# and maketars (pax -M) read modes,
# owners, sizes and checksums here
#
# Neither can be written by hand -- they follow the packages -- and neither is
# written by pkg_add(1): a normal build gets its METALOG lines from install(1)
# -U -M, which pkgsrc's cross installation never runs. So the tree is read
# back and the records are written in the format install(1) would have written:
# mtree(8) specification lines, one per file.
#
# Owners are root:wheel, not the unprivileged user the cross build ran as: on a
# Makronix system pkg_add runs as root and the package's files are the root's
# files. Modes, sizes, checksums and link targets are taken from the tree.
#
# Usage: tools/makronix-set.sh [DESTDIR]
# (default DESTDIR: build/obj/destdir.amd64)
#
# Re-running is safe: the /usr/pkg records are replaced. Entries that were in
# the previous list but are no longer present are kept as obsolete, as in the
# NetBSD set lists.
set -eu
TOP=$(cd "$(dirname "$0")/.." && pwd)
DESTDIR=${1:-${TOP}/build/obj/destdir.amd64}
PKGDIR=${DESTDIR}/usr/pkg
DB=${PKGDIR}/pkgdb
METALOG=${DESTDIR}/METALOG
LISTDIR=${TOP}/src/distrib/sets/lists/makronix
LIST=${LISTDIR}/mi
PKG_INFO=${TOP}/build/pkg/sbin/pkg_info
if [ ! -d "${PKGDIR}" ]; then
echo "makronix-set: ${PKGDIR} does not exist -- no packages installed" >&2
exit 1
fi
if [ ! -f "${METALOG}" ]; then
echo "makronix-set: ${METALOG} does not exist -- build the distribution first" >&2
exit 1
fi
if [ ! -x "${PKG_INFO}" ]; then
echo "makronix-set: missing ${PKG_INFO}" >&2
exit 1
fi
tmp=$(mktemp -d "${TMPDIR:-/tmp}/makronix-set.XXXXXX")
trap 'rm -rf "${tmp}"' EXIT
# Which stat(1) and which sha256 this host has, decided once: the loop below
# runs for every file of the set (about 12000), and the helpers used to ask
# again each time -- ten processes a file, 95 of the 97 s of the step
# (build/logs/userland-2026-11-17j.log, "set and repository"). Now two.
if stat -c '%04a' / > /dev/null 2>&1; then GNUSTAT=yes; else GNUSTAT=; fi
if command -v sha256sum > /dev/null 2>&1; then HAVE_SHA256SUM=yes; else HAVE_SHA256SUM=; fi
sha256_file()
{
if [ -n "$HAVE_SHA256SUM" ]; then
s=$(sha256sum "$1")
echo "${s%% *}"
else
cksum -a sha256 "$1" | awk '{print $NF}'
fi
}
# mode, type and size in one call: "MODE|TYPE|SIZE", the type as GNU stat
# names it. NetBSD's %Lp is already octal digits ("755"): printed as a
# decimal with four places it is 0755 (the old printf "%04o" made it 1363).
stat_all()
{
if [ -n "$GNUSTAT" ]; then
stat -c '%04a|%F|%s' "$1"
else
stat -f '%Lp|%HT|%z' "$1" | awk -F'|' '{
t = $2
if (t == "Directory") t = "directory"
else if (t == "Symbolic Link") t = "symbolic link"
else if (t == "Regular File") t = (($3 + 0) == 0) ? "regular empty file" : "regular file"
printf "%04d|%s|%s\n", $1, t, $3 }'
fi
}
# Resolve set package base names to installed full package names in sysroot db.
: > "${tmp}/seed"
# A package's name is not always the last part of its PKGPATH (filesystems/
# afpfs-ng is fuse-afpfs-ng, build/logs/userland-2026-11-14q.log): the name is
# tried first, then the installed package whose PKGPATH it is.
: > "${tmp}/pnp"
for d in "${DB}"/*/; do
q=${d%/}; q=${q##*/}
[ -f "${d}+BUILD_INFO" ] || continue
printf '%s %s\n' "$q" "$(sed -n 's/^PKGPATH=//p' "${d}+BUILD_INFO" | head -1)" >> "${tmp}/pnp"
done
for pp in $("${TOP}/tools/pkglist.sh" set); do
n=${pp##*/}
full=$("${PKG_INFO}" -K "${DB}" -E "$n" 2>/dev/null | head -1 || true)
[ -n "${full}" ] || full=$(awk -v p="$pp" '$2 == p { print $1; exit }' "${tmp}/pnp")
[ -n "${full}" ] || { echo "makronix-set: set package '${pp}' is not registered in ${DB}" >&2; exit 1; }
printf '%s\n' "${full}" >> "${tmp}/seed"
done
# The seeds are the packages named "set" and the NetBSD binaries of kind "bin"
# (packages.list; vim, git-base, sysupgrade -- the base set's own -- and flex,
# ksh93, tcl, which packages built here need): tools/userland.sh puts exactly
# those into the sysroot (t0205). The closure below adds what they need to run.
for pp in $("${TOP}/tools/pkglist.sh" sysroot); do
n=${pp##*/}
full=$("${PKG_INFO}" -K "${DB}" -E "$n" 2>/dev/null | head -1 || true)
[ -n "${full}" ] || full=$(awk -v p="$pp" '$2 == p { print $1; exit }' "${tmp}/pnp")
[ -n "${full}" ] || { echo "makronix-set: binary package '${pp}' is not registered in ${DB}" >&2; exit 1; }
printf '%s\n' "${full}" >> "${tmp}/seed"
done
LC_ALL=C sort -u "${tmp}/seed" > "${tmp}/seed.u" && mv "${tmp}/seed.u" "${tmp}/seed"
# Runtime dependency closure (by installed package dir names).
cp "${tmp}/seed" "${tmp}/queue"
: > "${tmp}/pkgs"
while [ -s "${tmp}/queue" ]; do
p=$(head -1 "${tmp}/queue")
tail -n +2 "${tmp}/queue" > "${tmp}/queue.next" || true
mv "${tmp}/queue.next" "${tmp}/queue"
grep -qx "$p" "${tmp}/pkgs" 2>/dev/null && continue
printf '%s\n' "$p" >> "${tmp}/pkgs"
[ -f "${DB}/$p/+CONTENTS" ] || { echo "makronix-set: missing +CONTENTS for $p" >&2; exit 1; }
# @pkgdep pattern (e.g. fontconfig>=2.14.2nb3)
awk '/^@pkgdep /{print $2}' "${DB}/$p/+CONTENTS" | while read -r dep; do
# strip version constraint to base match pattern for pkg_info -E
base=$(printf '%s\n' "$dep" | sed 's/[<>=!].*$//')
[ -n "$base" ] || continue
depfull=$("${PKG_INFO}" -K "${DB}" -E "$base" 2>/dev/null | head -1 || true)
[ -n "${depfull}" ] || continue
grep -qx "$depfull" "${tmp}/pkgs" 2>/dev/null && continue
grep -qx "$depfull" "${tmp}/queue" 2>/dev/null || printf '%s\n' "$depfull" >> "${tmp}/queue"
done
done
# What is registered in the sysroot but not in the closure is not part of the set,
# and its files would be "extra files in DESTDIR" for checkflist: a package that
# an earlier run installed (cups, cups-filters ... when userland.sh still put
# every "fetch" package there, t0205) is removed with pkg_delete.
for d in "${DB}"/*/; do
[ -f "${d}+CONTENTS" ] || continue
q=${d%/}; q=${q##*/}
grep -qx "$q" "${tmp}/pkgs" 2>/dev/null && continue
# the build's own: the shadow tree of the native X that every package of the
# cross build is configured against (x11-links, its files are kept out of the
# set above), and pkgsrc's ABI marker
case "$q" in x11-links-*|osabi-*) continue ;; esac
echo "makronix-set: $q is not in the set, removed from the sysroot" >&2
"${TOP}/build/pkg/sbin/pkg_delete" -f -P "${DESTDIR}" -K /usr/pkg/pkgdb "$q" > "${TOP}/build/logs/pkg-delete-$q.log" 2>&1 ||
{ echo "makronix-set: could not remove $q (build/logs/pkg-delete-$q.log)" >&2; exit 1; }
done
mkdir -p "${LISTDIR}"
{
cat <<'HEADER'
# $Makronix$
#
# The "makronix" set: what pkgsrc built for the target and installed under
# /usr/pkg. Generated by tools/makronix-set.sh -- do not edit by hand, the
# file follows the installed packages. Entries that were in a previous
# revision of this list but are no longer present are kept as "obsolete".
#
# Note: ./usr/pkg/pkgdb is deliberately absent: it is pkgsrc's registration
# database for the build, not part of the distribution (checkflist
# ignores it, like var/db/syspkg).
# Note: ./usr/pkg/share/x11-links is deliberately absent: pkgtools/x11-links
# builds a build-only shadow tree of symlinks to the native X. In a
# cross build these point at absolute paths under the build sysroot
# (${CROSS_DESTDIR}/usr/X11R7, e.g. build/obj/destdir.*), so shipping
# them would leave dead links on the installed system. pkgdb is not
# shipped either, so an installed system would reinstall x11-links and
# hit PLIST conflicts with leftover files from the set. The target
# rebuilds x11-links against its own /usr/X11R7.
#
./etc/mtree/set.makronix makronix-sys-root
./usr/pkg makronix-sys-root
HEADER
} > "${tmp}/list"
# The prefix itself belongs to no package, so +CONTENTS never names it -- but
# every entry of the set is below it, and nbmtree wants a directory record
# before anything under it: "nbmtree: ./usr/pkg: missing directory in
# specification" stopped build.sh release at the distribution step
# (measured 2026-11-20 on makronix-build).
printf './usr/pkg type=dir uname=root gname=wheel mode=0755\n' > "${tmp}/metalog"
# Collect entries from +CONTENTS of all selected packages.
LC_ALL=C sort -u "${tmp}/pkgs" | while IFS= read -r pkg; do
cont="${DB}/${pkg}/+CONTENTS"
cwd=/usr/pkg
prev=
prev_kind=
while IFS= read -r line || [ -n "$line" ]; do
# @ignore: the next line is a file of the package database, not of the
# package (pkg_add -P records +COMMENT and +DESC this way, which is how
# Xft2 and the other stand-ins for the native X libraries look)
if [ -n "${skip_next:-}" ]; then skip_next=; continue; fi
case "$line" in '@ignore') skip_next=1; continue ;; esac
case "$line" in
'@cwd '*)
cwd=${line#@cwd }
# a package installed before pkg_add -P was used (vim, 2026-11-13) has
# the sysroot in its @cwd, relative: build/obj/destdir.amd64/usr/pkg
case "$cwd" in */usr/pkg) cwd=/usr/pkg ;; esac
prev=; prev_kind=
continue ;;
'@pkgdir '*)
# a directory the package owns, empty or not (MAKE_DIRS:
# /usr/pkg/etc/dt/cde/fontaliases): without it checkflist stops with
# "extra files in DESTDIR" for the directory
# (build/logs/release-2026-11-15b-chain.log:79042)
rel="${cwd}/${line#@pkgdir }"
case "$rel" in *//*) rel=$(printf '%s\n' "$rel" | sed 's#//*#/#g') ;; esac
case "$rel" in /usr/pkg/pkgdb/*|/usr/pkg/pkgdb|/usr/pkg/share/x11-links/*|/usr/pkg/share/x11-links) ;; *)
if [ -d "${DESTDIR}${rel}" ]; then
printf './%s\tmakronix-sys-root\n' "${rel#/}" >> "${tmp}/list"
printf '.%s type=dir uname=root gname=wheel mode=0755\n' "$rel" >> "${tmp}/metalog"
fi
esac
prev=; prev_kind=
continue ;;
'@name '*|'@blddep '*|'@pkgdep '*|'@pkgcfl '*|'@option '*|'@exec '*|'@unexec '*|'@ignore'*)
prev=; prev_kind=
continue ;;
'@comment Symlink:'*)
if [ -n "$prev" ]; then
target=${line#@comment Symlink:}
rel="${cwd}/${prev}"
case "$rel" in *//*) rel=$(printf '%s\n' "$rel" | sed 's#//*#/#g') ;; esac
case "$rel" in /usr/pkg/pkgdb/*|/usr/pkg/pkgdb|/usr/pkg/share/x11-links/*|/usr/pkg/share/x11-links) ;; *)
entry=".${rel}"
sp=makronix-util-data
case "$rel" in /usr/pkg/lib/*) sp=makronix-lib-shlib ;; /usr/pkg/include/*) sp=makronix-util-include ;; /usr/pkg/share/man/*) sp=makronix-util-man ;; /usr/pkg/*bin/*) sp=makronix-util-bin ;; esac
printf './%s\t%s\n' "${rel#/}" "$sp" >> "${tmp}/list"
printf '%s type=link uname=root gname=wheel mode=0777 link=%s\n' "$entry" "$target" >> "${tmp}/metalog"
esac
fi
prev=; prev_kind=
continue ;;
'@comment MD5:'*)
# file md5 follows file entry; hash computed from actual file below via prev_kind=file
prev=; prev_kind=
continue ;;
'@comment'*|'@'*)
prev=; prev_kind=
continue ;;
'' )
prev=; prev_kind=
continue ;;
esac
# path entry
rel="${cwd}/${line}"
case "$rel" in *//*) rel=$(printf '%s\n' "$rel" | sed 's#//*#/#g') ;; esac
full="${DESTDIR}${rel}"
# a name with a space fits neither a set list nor METALOG (nbmtree: unknown
# keyword `ipsum.txt', build/logs/checkflist 2026-11-14); three templates of
# python setuptools are the only ones, and they are not shipped
case "$rel" in *[[:space:]]*) echo "makronix-set: not shipped (a space in the name): $rel" >&2; prev=; prev_kind=; continue ;; esac
case "$rel" in /usr/pkg/pkgdb/*|/usr/pkg/pkgdb|/usr/pkg/share/x11-links/*|/usr/pkg/share/x11-links) prev=; prev_kind=; continue ;; esac
[ -e "$full" ] || [ -L "$full" ] || { echo "makronix-set: ${rel}: listed in ${pkg} but missing in ${DESTDIR}" >&2; exit 1; }
sp=makronix-util-data
case "$rel" in /usr/pkg/lib/*) sp=makronix-lib-shlib ;; /usr/pkg/include/*) sp=makronix-util-include ;; /usr/pkg/share/man/*) sp=makronix-util-man ;; /usr/pkg/*bin/*) sp=makronix-util-bin ;; esac
entry=".${rel}"
st=$(stat_all "$full")
mode=${st%%|*}
st=${st#*|}
tt=${st%%|*}
size=${st#*|}
case "$tt" in
directory)
printf './%s\t%s\n' "${rel#/}" "makronix-sys-root" >> "${tmp}/list"
printf '%s type=dir uname=root gname=wheel mode=%s\n' "$entry" "$mode" >> "${tmp}/metalog"
;;
"symbolic link")
printf './%s\t%s\n' "${rel#/}" "$sp" >> "${tmp}/list"
printf '%s type=link uname=root gname=wheel mode=0777 link=%s\n' "$entry" "$(readlink "$full")" >> "${tmp}/metalog"
;;
"regular file"|"regular empty file")
sum=$(sha256_file "$full")
printf './%s\t%s\n' "${rel#/}" "$sp" >> "${tmp}/list"
printf '%s type=file uname=root gname=wheel mode=%s size=%s sha256=%s\n' "$entry" "$mode" "$size" "$sum" >> "${tmp}/metalog"
;;
*)
echo "makronix-set: ${rel}: unsupported file type, skipped" >&2
;;
esac
prev=$line; prev_kind=entry
done < "$cont"
done
# The directories: packages do not list them, a set list must (checkflist: "extra
# files in DESTDIR" for ./usr/pkg, ./usr/pkg/bin ...,
# build/logs/release-2026-11-14d.log:79423). Every directory above an entry
# under /usr/pkg, /usr/pkg itself included, in the set the earlier lists used.
awk -F'\t' '
/^#/ || NF < 2 { next }
{ have[$1] = 1; name[++n] = $1 }
END {
for (i = 1; i <= n; i++) {
p = name[i]
while ((k = match(p, /\/[^\/]*$/)) > 0) {
p = substr(p, 1, k - 1)
if (p == "." || p == "" || p == "./usr") break
if (substr(p, 1, 9) != "./usr/pkg") break
if (!(p in have)) {
have[p] = 1
printf "%s\tmakronix-sys-root\n", p
}
}
}
}
' "${tmp}/list" > "${tmp}/list.dirs"
cat "${tmp}/list.dirs" >> "${tmp}/list"
# Deduplicate list and metalog (different packages can share dirs like /usr/pkg/bin).
LC_ALL=C sort -u "${tmp}/list" > "${tmp}/list.u"
LC_ALL=C sort -u "${tmp}/metalog" > "${tmp}/metalog.u"
mv "${tmp}/list.u" "${tmp}/list"
mv "${tmp}/metalog.u" "${tmp}/metalog"
# Mark entries removed since previous list as obsolete (NetBSD sets style).
if [ -f "${LIST}" ]; then
awk 'NF>=2 {print $1 "\t" $2}' "${tmp}/list" | LC_ALL=C sort -u > "${tmp}/new.keys"
awk '
NF < 2 { next }
$2 == "obsolete" { next }
{ print $1 "\t" $2 }
' "${LIST}" | LC_ALL=C sort -u > "${tmp}/old.keys"
# old entries not in new
awk 'NR==FNR {n[$1]=1; next} !($1 in n) {print $1}' "${tmp}/new.keys" "${tmp}/old.keys" | cut -f1 > "${tmp}/gone.paths"
if [ -s "${tmp}/gone.paths" ]; then
while IFS= read -r p; do
printf '%s\tmakronix-obsolete\tobsolete\n' "$p" >> "${tmp}/list"
done < "${tmp}/gone.paths"
LC_ALL=C sort -u "${tmp}/list" > "${tmp}/list.u2" && mv "${tmp}/list.u2" "${tmp}/list"
fi
fi
mv "${tmp}/list" "${LIST}"
# Packages do not list the directories their files are in, and nbmtree stops at
# a file whose directory the specification does not have: "./usr/pkg/bin:
# missing directory in specification" (build/logs/release-2026-11-14b.log:19797).
# Every directory above a record under /usr/pkg is a record of its own.
awk '
{ name = $1; have[name] = 1; line[++n] = $0 }
END {
for (i = 1; i <= n; i++) {
split(line[i], f, " ")
p = f[1]
while ((k = match(p, /\/[^\/]*$/)) > 0) {
p = substr(p, 1, k - 1)
if (p == "./usr/pkg" || p == "." || p == "") break
if (!(p in have)) {
have[p] = 1
print p " type=dir uname=root gname=wheel mode=0755"
}
}
}
}
' "${tmp}/metalog" > "${tmp}/metalog.dirs"
cat "${tmp}/metalog.dirs" >> "${tmp}/metalog"
# Replace the /usr/pkg part of METALOG, leave every other set's records alone.
grep -v '^\./usr/pkg' "${METALOG}" > "${tmp}/metalolog" || true
cat "${tmp}/metalog" >> "${tmp}/metalolog"
mv "${tmp}/metalolog" "${METALOG}"
# The set is the whole truth about /usr/pkg. A file the package tools left
# there that the set does not name is not shipped -- and checkflist counts it
# as an extra file. Measured 2026-11-20 on makronix-build: a chroot build
# installs its build tools (perl, autoconf, ...) to build the set, the prune
# above deregisters them, and 526 of their files stayed (perl's cpan, perldoc,
# its man pages) together with pkgdb.refcount, the index the package tools
# keep beside the database. What the set does not name, is removed.
LC_ALL=C sed -n 's/^\(\.\/usr\/pkg[^ ]*\)[ ].*/\1/p' "${tmp}/metalog" |
LC_ALL=C sort -u > "${tmp}/ship"
(cd "${DESTDIR}" && find ./usr/pkg \
! -path './usr/pkg/pkgdb*' ! -path './usr/pkg/share/x11-links*') |
LC_ALL=C sort -u > "${tmp}/there"
LC_ALL=C comm -13 "${tmp}/ship" "${tmp}/there" > "${tmp}/extra"
n_extra=0
sort -r "${tmp}/extra" | while IFS= read -r f; do
[ "$f" = "./usr/pkg" ] && continue
if [ -d "${DESTDIR}/$f" ]; then
rmdir "${DESTDIR}/$f" 2>/dev/null || true
else
rm -f "${DESTDIR}/$f"
fi
done
[ -s "${tmp}/extra" ] &&
echo "makronix-set: $(wc -l < "${tmp}/extra") entries not in the set, removed from the sysroot"
echo "makronix-set: $(wc -l < "${LIST}") list entries, $(wc -l < "${tmp}/metalog") METALOG records"