432. Local rebuilds and kdos march
A KDOS machine carries its own build recipes (ports, one directory per package under /ports) and can be given the compilers that build them, so any package can be built again on the machine itself. This page is for an administrator who wants to change how a package is built, check that a build is reproducible, or tune a package for their own processor. It covers three sizes of job:
- rebuilding one port as it ships, or from a recipe you changed;
- measuring whether building a port for this processor’s instruction-set level makes it faster, with
kdos march; - running the whole KDOS build from a developer medium, with
kdos rebuild.
Updating to newer recipes is Updating, and the commands of kpkg itself are on kpkg.
Installation
- kdos-tools installs
kdos marchandkdos rebuild, both subcommands of thekdoscommand. kpkg,kpkgbuildandkpkgdependsare installed by the bootstrap; see kpkg.- A port builds with the compilers and tools the machine has.
kdos rebuildrefuses to start withoutcc,make,bash,tarandxz. Whenmksquashfs(squashfs-tools),xorriso(libisoburn) ormkfs.fat(dosfstools) is missing it names the first one in anoteline and goes on: the packages still build, but no ISO comes out. The base the ISO carries has those three and no toolchain:sudo kdos update install build-toolsadds the assembler, the linker,make, the build systems and fortify-headers (the/usr/include/fortifythe build’s flags include), and restores the development files ofgcc,muslandlinux, soccis there with the C library’s and the kernel’s headers. A port whose recipe needs a compiler the base leaves out, such asrust,goornodejs, brings it in as a dependency:kpkg installbuilds it first, andkdos update install --sourcetakes it prebuilt underbuild-deps = prebuilt(see Installing software). kdos rebuildneeds a medium built withmake build KDOS_ISO_SOURCES=1, which carries the source tree under/mnt/iso/sources, or a checkout of the repository on the machine. The ordinary image carries neither: its medium has nosourcesdirectory, and/portsis recipes, not a treekdos rebuildaccepts.
None of these is a service.
Configuration
Where a port is found
kpkg looks for a port in the directories PORT_REPO names, in order, and the first that holds the name wins. The default, from /etc/kpkg.conf (src/system/kdos-kpkg/kpkg.conf in the tree), which a PORT_REPO in the environment replaces:
# Port repositories: the recipe tree every image carries at /ports, and the
# areas of src/ under it that hold ports of our own
PORT_REPO="${PORT_REPO:-/ports/core /ports/src/system /ports/src/art /ports/src/desktop /ports/src/daemons}"
src/system/kdos-kpkg/kpkg.conf, lines 11–13
A directory of your own named first in PORT_REPO therefore shadows a port of /ports, without touching /ports, which every kdos update fetch replaces whole.
The build’s flags
The tree’s own build exports its compiler and linker flags from script/env/chroot.env, which /ports does not carry. These are the C, C++, Fortran and link lines:
export CFLAGS="-O2 -pipe -std=gnu11 -fPIC -fno-semantic-interposition -fstack-clash-protection -D_FORTIFY_SOURCE=2 -isystem /usr/include/fortify"
export CXXFLAGS="-O2 -pipe -fPIC -fno-semantic-interposition -fstack-clash-protection -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -isystem /usr/include/fortify"
export FFLAGS="-O2 -pipe -fPIC -fno-semantic-interposition -fstack-clash-protection"
export FCFLAGS="$FFLAGS"
export LDFLAGS="-Wl,-O1,--sort-common,--as-needed,-z,now,-z,pack-relative-relocs"
script/env/chroot.env, lines 69–73
On a running machine nothing sets them. A port rebuilt there gets the environment of the shell that ran kpkg, and with no CFLAGS each build system uses its own defaults. The rest of chroot.env (the compiler names, CMAKE_BUILD_TYPE, the Go and Cargo settings, the locale) is missing too, and so is script/env/common.env, which holds the settings that make a package the same from one build to the next; see Reproducible packages.
A benchmark line
kdos march measures only a port whose recipe has a bench = line, and runs its bench_setup = line once per build, untimed, to make the fixtures. lz4’s, from ports/core/archiver/lz4/kpkgbuild:
bench_setup = dd if=/dev/urandom of=bench.bin bs=1M count=64 status=none
bench = lz4 -9 -f -q bench.bin bench.lz4 && lz4 -d -f -q bench.lz4 /dev/null
ports/core/archiver/lz4/kpkgbuild, lines 19–20
The ports that carry one can be listed from /ports:
grep -l '^bench ' /ports/core/*/*/kpkgbuild
The ledger and its variables
kdos march run appends its verdicts to /var/lib/kdos/march.ledger. KDOS_MARCH_RUNS (timed runs per build, default 5), KDOS_MARCH_LEVEL (the level to measure against) and KDOS_MARCH_LEDGER (another ledger) change it; see kdos march.
The developer medium
KDOS_ISO_SOURCES=1 makes the image’s last step copy the tree onto the medium beside the system image, and write a stamp kdos rebuild prints before it starts:
if [ "${KDOS_ISO_SOURCES:-0}" = "1" ]; then
echo "Copying the sources onto the ISO (this is the big one)..."
mkdir -p $ISO_ROOT/sources/ports
for d in src script fs; do
cp -a /kdos/$d $ISO_ROOT/sources/
done
for f in /ports/*; do
cp -a "$f" $ISO_ROOT/sources/ports/
done
for f in Makefile Dockerfile CLAUDE.md; do
[ -f /kdos/$f ] && cp -a /kdos/$f $ISO_ROOT/sources/
done
if [ "${KDOS_MAKE_BINHOST:-0}" = "1" ] && [ -f /kdos/build/binhost/PACKAGES ]; then
cp -a /kdos/build/binhost $ISO_ROOT/sources/binhost
fi
# A stamp, so `kdos rebuild` can say what it is about to rebuild FROM.
cat > $ISO_ROOT/sources/SOURCES <<EOS
# The KDOS tree that built this image.
ports $(find $ISO_ROOT/sources/ports/core -mindepth 2 -maxdepth 3 -name kpkgbuild | wc -l) ports
size $(du -sh $ISO_ROOT/sources 2>/dev/null | cut -f1)
built $(date -u +%Y-%m-%dT%H:%M:%SZ)
EOS
echo "Sources: $(du -sh $ISO_ROOT/sources | cut -f1)"
fi
script/phases/70_image/110_iso.sh, lines 214–237
Usage
Rebuilding a port as it ships
Fetch its sources and rebuild it.
-fforces the named port, not its dependencies: one is built only when it is not installed, and nothing beneath an installed one is reached.KPKG_FETCH=1fetches the sources of any dependency that has to be built on the way:sudo kpkg fetch pv sudo env KPKG_FETCH=1 kpkg install -f pvCheck the installed files against the new package:
kpkg check pv
Rebuilding from a recipe you changed
This works for a port under /ports/core. KDOS’s own programs under /ports/src reach the libraries beside them by a relative path ($PORT_SRC/../../libs), so a copy of one taken out of that tree does not find them; change those in a checkout of the repository instead.
Copy the port into a directory of your own. A repository is searched at
<name>/and one shelf down, so a flat directory will do:mkdir -p ~/ports cp -r /ports/core/cli/pv ~/ports/Edit
~/ports/pv/kpkgbuildor~/ports/pv/build.sh. A new version needs a newsha256 =line, andkpkgrefuses a source whose hash it does not have. The rules a recipe keeps are in Writing ports.Build and install it with your directory first in
PORT_REPO, and the shipped repositories after it so that its dependencies are still found:sudo env KPKG_FETCH=1 \ PORT_REPO="$HOME/ports /ports/core /ports/src/system /ports/src/art /ports/src/desktop /ports/src/daemons" \ kpkg install -f pvWith
release = 3in the copy (the shipped recipe hasrelease = 2),kpkg info pvthen reportsVersion: 1.12.0-3, and the package file is deleted from/var/cache/kpkg/packagesonce it is installed.To see whether a change alters the package, before or instead of installing it, keep the shipped recipe as the copy’s
kpkgbuild, put the changed one beside it askpkgbuild.new(andbuild.sh.newwhen the script changes too), and runkpkg verify, which builds both and compares the two packages:cp ~/ports/pv/kpkgbuild ~/ports/pv/kpkgbuild.new # the changed recipe cp /ports/core/cli/pv/kpkgbuild ~/ports/pv/kpkgbuild # the shipped one sudo env PORT_REPO="$HOME/ports /ports/core" kpkg verify pvIt prints
==> Building with kpkgbuild..., then==> Building with kpkgbuild.new..., and ends with one verdict line. A change ofreleasealone changes no file in the package, so that one ends==> pv: the two packages are BYTE-IDENTICAL (<hash>...).kpkg verify --repro pvbuilds the same recipe twice instead, and requires the two packages to be byte-identical;pvpasses with the same line.
A package installed from your own recipe is replaced by the next kdos update apply whose tree pins a newer version or release than the one installed. Until then kdos update check does not count it as behind: with pv at 1.12.0-3 against a tree’s 1.12.0-2, it reports 0 behind.
Building with the tree’s flags
To build a port with the same compiler flags the tree’s build uses, pass the values from the excerpt above in the build’s environment:
sudo env KPKG_FETCH=1 CFLAGS='<the CFLAGS value>' CXXFLAGS='<the CXXFLAGS value>' \
LDFLAGS='<the LDFLAGS value>' kpkg install -f pv
The rest of chroot.env and all of common.env are still unset, so the package can still differ from the one the image carries.
Measuring a port with kdos march
kdos march asks whether building a port for this processor’s instruction-set level (-march=x86-64-v2, -v3 or -v4) makes it faster here. It builds the port twice with kpkgbuild, once with the default CFLAGS and once with the flag added, runs the recipe’s benchmark against both, and records the verdict kept only when the win is more than 3 per cent plus the noise it measured on this machine.
See which levels this processor has:
kdos march probex86-64-v2 yes x86-64-v3 yes x86-64-v4 missing avx512f missing avx512bw missing avx512dq missing avx512vl highest usable: x86-64-v3 nothing is built with it until `kdos march run <port>` measures a winOn a processor with only the baseline level there is nothing to measure, and
runexits 1.Fetch the port’s sources.
/portsholds none, and thekpkgbuildthatkdos marchruns downloads only whenKPKG_FETCH=1is in its environment:sudo kpkg fetch lz4Quieten the machine (close what is running), then measure:
sudo kdos march run lz4Each build is timed
KDOS_MARCH_RUNStimes and the medians are compared;sudoclears the environment, so a different count is passed through it, assudo env KDOS_MARCH_RUNS=9 kdos march run lz4. A verdict is one ofkept,reverted(the win is inside the noise),unmeasurable(nobench =line, or no result) orunbuildable(the build with the flag failed). A baseline build that fails records nothing, and the run exits 1. Nothing is installed: both packages are written under a temporary directory,/tmp/kdos-march.*(under$TMPDIRwhen that is set), which the run removes, and the builds themselves use/var/cache/kpkg/work/<name>. The run ends with one line per port:measuring against x86-64-v3, 3 runs per build lz4: building twice (baseline, then -march=x86-64-v3)... lz4: benchmarking 3 runs each... lz4 x86-64-v3 baseline 0.380s -march=x86-64-v3 0.403s -5.9% (noise 9.0%) -> reverted this machine's noise floor is 9.0% — close things down or raise KDOS_MARCH_RUNSThat run,
sudo env KDOS_MARCH_RUNS=3 kdos march run lz4on an eight-thread virtual machine, took 2 minutes 40 seconds.Read the ledger:
kdos march reportIt prints one line per verdict and a count of kept, reverted and unmeasurable ones, and exits 1 with
nothing measured yet — `kdos march run <port>`when nothing has been measured:port level baseline optimised change lz4 x86-64-v3 0.380s 0.403s -5.9% reverted 0 kept, 1 reverted, 0 unmeasurableThe ledger itself holds one line per verdict:
lz4 x86-64-v3 0.3801 0.4026 -5.9 reverted.
The ledger is a record, and only kdos march report reads it: a kept verdict changes nothing about how kpkg or kdos update apply builds that port. To install the faster build, rebuild the port with the level added to the flags it was measured with. kdos march takes CFLAGS from its own environment, or -O2 -pipe when it is unset, as it is under sudo, and uses the same value for CXXFLAGS:
sudo env KPKG_FETCH=1 CFLAGS='-O2 -pipe -march=x86-64-v3' CXXFLAGS='-O2 -pipe -march=x86-64-v3' \
kpkg install -f lz4
A package built for a level runs only on a processor that has it, and dies with SIGILL on one that does not, so a disk moved to an older machine takes those packages with it.
Rebuilding KDOS from a developer medium
kdos rebuild runs the whole KDOS build on a booted machine, with no network, from a tree that carries every source. No rebuild from a medium has been run through every phase; see Known gaps.
On a build machine, in a checkout of the repository, make a developer medium:
make fetch make build KDOS_ISO_SOURCES=1Write
build/iso-build/kdos.isoto a USB stick and boot it.Mount a real disk with at least 25 GB free, not a tmpfs and not an overlay, for example at
/mnt/disk.Check first, then run. The build enters a chroot for most of its phases, which needs root, so
kdos rebuildwithout root stops before it copies anything; only--dry-run, which changes nothing, runs as any user:sudo kdos rebuild --dry-run /mnt/disk/work sudo kdos rebuild /mnt/disk/work
kdos rebuild looks for the tree in $KDOS_SOURCES (passed through sudo as sudo env KDOS_SOURCES=<dir> kdos rebuild ...), then /mnt/iso/sources, /kdos and the current directory, and takes the first that has script/kdosbuild.sh, ports/core, fs/etc and src/devtools/kdosbuild. It copies the tree to <work-dir>/kdos (a second run with the same work directory reuses the copy), compiles the build orchestrator into <work-dir>/kdosbuild, and runs it with --fresh. --iso-only runs only the image phase, 70_image. The job count is KDOS_JOBS when you set it, passed through sudo env in the same way; otherwise one job per thread, but no more than one per 2 GiB of memory. See kdos rebuild.
On the live medium and on an installed system
On the live medium /var/cache/kpkg is on the overlay in RAM, so a large build there compiles in memory, and what it installs is gone at power-off without a persistence store. On a 4 GB machine df -h / shows that overlay as 2.0 GB, which every source, work tree and package of a build shares. kdos rebuild refuses a work directory on the live root for that reason, and still refuses it with a persistence store, because / is then an overlay on the store. On an installed system the same commands write to the disk and stay.
Tips
- A failed build leaves
/var/cache/kpkg/work/<name>/, the unpacked source as the build left it. The next build of the port empties it. kdos march runtakes several ports at once:sudo kdos march run lz4 zip bzip2.kdos march decide <baseline> <optimised> <noise%>applies the decision rule to three numbers and exits 0 forkept; it is there for testing the rule.kdos march decide 100 90 1prints+10.0% kept, andkdos march decide 100 99 1prints+1.0% reverted.kdos rebuildon the same work directory a second time reuses the copy, so an edit made in<work-dir>/kdosis what the next run builds.
Troubleshooting
- A port that builds in the tree fails on the machine
-
The machine’s build has none of the tree’s environment: no
CFLAGS,CXXFLAGSorLDFLAGS, and none ofscript/env/common.env. Build it with the flags, as in Building with the tree’s flags, and compare. No sha256 for <file> in the recipe, orsha256 MISMATCH-
A changed recipe names a source it gives the wrong hash for, or none. Put the file’s
sha256sumon the recipe’ssha256 =line. See kpkg. kdos: kpkgbuild is not on PATH-
kdos marchrunskpkgbuild, a link to/usr/bin/kpkg. Check that/usr/bin/kpkgbuildexists. kdos march runreportsunmeasurable — no `bench =` line in the recipe-
Only a port with a
bench =line can be measured. Add one to a copy of the recipe, as in Rebuilding from a recipe you changed, and runkdos marchwith that copy first inPORT_REPO, passed throughsudoassudo env PORT_REPO="$HOME/ports /ports/core" kdos march run <port>. - Every verdict is
reverted, and the output warns about the noise floor -
The machine’s own variation is wider than the win. A floor above 5 per cent is reported with that advice; close what is running, or raise
KDOS_MARCH_RUNS(3 to 31; an even count is raised to the next odd one, so the median is one of the runs). kdos rebuildrefuses the work directory- It is on tmpfs, ramfs or an overlay, or has less than 25 GB free. A live medium’s root is an overlay in RAM: mount a real disk and name a directory on it.
kdos rebuildfinds no source tree-
It prints
kdos: no KDOS tree found.and exits 1. The medium was not built withKDOS_ISO_SOURCES=1; pointKDOS_SOURCESat a checkout of the repository. AKDOS_SOURCESthat lacks any of the four marks is named first, askdos: KDOS_SOURCES=/ports is not a KDOS tree, which is what/portsgives.
Known gaps
- An offline kdos rebuild from the medium has not been run to the end
- The kernel package differs between two builds of the same tree
- Build times and snapshot sizes are measured for part of the build only
See also
- kdos march and kdos rebuild: every option
- Tuning for this machine and kdos rebuild in the user guide
-marchmeasured per machine, not chosen for a population: why levels are measured per machine rather than chosen for the image- How KDOS is built: the build
kdos rebuildruns - Writing ports: the rules a changed recipe keeps
- kpkg, Updating