# tools/vmbuild -- the NetBSD build host
`wm/cde` cannot be cross-built from a Linux host: pkgsrc's cross mode assumes
the build host runs the target operating system, and CDE carries
`USE_TOOLS+=awk:run`, which `mk/tools/replace.mk:142` refuses when
`OPSYS != NATIVE_OPSYS` (see "Where a Linux build host stops" in AGENTS.md).
So CDE is built on a NetBSD amd64 host that this tree produces itself: a live
image from `src/`, running under qemu with KVM, driven over its serial console.
Nothing in pkgsrc and nothing in the recipe changes; the recipes are pkgsrc's.
Everything below was run against the tree as it stands; the logs are in
`build/logs/` (`liveimage-vm-build.log`, `vm-console.log`, and the stage logs).
## 1. The host image
cd src
./build.sh -U -u -m amd64 -M ../build/obj -O ../build/obj -x -X ../xsrc \
-V IMGFILE_EXTRA=../tools/vm-boot.cfg live-image
Result: `build/obj/distrib/amd64/liveimage/emuimage/NetBSD-11.99.9-amd64-live.img`.
`-V IMGFILE_EXTRA=tools/vm-boot.cfg` exists because the live image has no VGA
console in qemu: `vm-boot.cfg` sends the loader and the kernel to `com0`, so
the console is the only way in and the only way to read the build.
## 2. Booting it
The raw image is kept pristine; the VM runs on an overlay:
qemu-img create -f qcow2 -b ../build/obj/.../NetBSD-11.99.9-amd64-live.img \
-F raw build/vm/live.qcow2
qemu-system-x86_64 -machine accel=kvm -cpu host -m 8192 -smp 4 \
-drive file=build/vm/live.qcow2,if=virtio,format=qcow2 \
-netdev user,id=net0,hostfwd=tcp:127.0.0.1:8022-:22 \
-device virtio-net-pci,netdev=net0 \
-serial unix:$PWD/build/vm/console.sock,server=on,wait=off \
-monitor unix:$PWD/build/vm/monitor.sock,server=on,wait=off \
-display none -vga none
`tools/vmctl.py` drives the serial console and appends the transcript to
`build/logs/vm-console.log`. Once `~/.ssh/authorized_keys` exists in the VM the
22 forward is the easier way in:
ssh -p 8022 -i build/vm/id_vm root@127.0.0.1
## 3. Handing files to the VM
The VM has no route out; qemu's user networking maps `10.0.2.2` to the host's
loopback, so the host serves what the VM fetches:
python3 -m http.server 8001 --bind 127.0.0.1 --directory tools/vmbuild
python3 -m http.server 8000 --bind 127.0.0.1 --directory <the distfiles dir>
python3 -m http.server 8002 --bind 127.0.0.1 \
--directory build/obj/releasedir/amd64/binary/sets
Port 8001 serves these scripts, `vm-mk.conf`, `mk-cde.conf` and the public key
installed as `/root/.ssh/authorized_keys`; 8000 serves pkgsrc's distfiles
(`DISTDIR` is the shared one, flat, which is the layout pkgsrc expects of a
master site); 8002 serves `xbase`, `xcomp`, `xetc` and `xfont` out of this
tree's own release directory, so the host's X is the X the target ships.
## 4. The stages, in order
Run each with `sh /tmp/stageN.sh` after fetching it from port 8001.
stage1-fetch.sh pkgsrc tree and vm-mk.conf into /usr
stage2-xsets.sh xbase, xcomp, xetc, xfont into /
stage3-bootstrap.sh pkgsrc bootstrapped into /usr/pkg, --prefer-native=yes
stage4-mkconf.sh the wm/cde block appended to /usr/pkg/etc/mk.conf
stage5-cde.sh bmake FORCE_C_STD=gnu11 install in wm/cde
stage6-pack.sh the runtime closure of cde packed into /var/tmp/usrpkg.tar.gz
Each is run over the console or with `sh -s < tools/vmbuild/stageN.sh` on the
ssh side; stage6 in particular is small enough to pipe in and needs no fetch.
`--prefer-native=yes` is not optional: without it bootstrap writes
`PREFER_PKGSRC=yes` on a non-NetBSD host, and every library base already ships
would be built a second time. On this host "native" is the running base, so
pkgsrc uses the tree's curses, zlib, expat, fontconfig and X libraries.
## 5. Getting the result into the sysroot
scp -P 8022 -i build/vm/id_vm root@127.0.0.1:/var/tmp/usrpkg.tar.gz build/
tar -xzpf build/usrpkg.tar.gz -C build/obj/destdir.amd64
tools/makronix-set.sh
The tar is the package's runtime closure only -- what `pkg_add` would pull in
when it installs CDE, walked over the dependency records -- not the build tools
that were needed to produce it. It is unpacked rather than `pkg_add`ed into the
sysroot because CDE's install script writes `/etc/dt` (CONF_FILES,
RCD_SCRIPTS), and
`pkg_add -p <prefix>` only relocates the prefix: the `/etc` paths would land on
the build host. `tools/makronix-set.sh` then writes the file list and the
METALOG records from what is in the tree.
## 6. What this produced, measured
The closure of `cde-2.5.3` is 16 packages (cde, motif, tcl, ksh93, opensp, lmdb,
libXft, libjpeg-turbo, png, freetype2, fontconfig, brotli, sqlite3,
pam-pwauth_suid, and the two Adobe bitmap font packs), 8410 files, 69 MB as a
tar.gz. Unpacked into `build/obj/destdir.amd64` (no name collided with what the
cross build had already put under `/usr/pkg`), it took `tools/makronix-set.sh`
to 10837 list entries and 10825 METALOG records, and `checkflist` then ran green
with `-L base,x`.
Two things worth knowing when reading that:
- The dependencies are the `@pkgdep` lines in `/usr/pkg/pkgdb/<pkg>/+CONTENTS`.
`+DEPENDS` is a member of the *binary package*, not of the registration
database, so a closure walked over it finds nothing.
- `--prefer-native=yes` did what it says where the base ships the library:
`libz.so.1` and `libsqlite3.so.1` are not in `/usr/pkg/lib` in the VM and the
CDE binaries link against `/usr/lib` for them. freetype, fontconfig, png and
brotli are not in this base at all -- only under `/usr/pkg/lib` -- so those
packages are the single copy, not a duplicate.
Every one of the 57 distinct `NEEDED` libraries across the 159 ELF objects under
`/usr/pkg` resolves inside the sysroot, and CDE's own libraries are on the
binaries' RPATH (`/usr/pkg/lib:/usr/X11R7/lib:/usr/pkg/dt/lib`).