knaxuslearn · build · ship

Projects / Embedded Linux image / Phase 4

Phase 4 of 4 · about 2.5 hours

Your own package and a repeatable build

You end with a project folder that holds your overlay, your package and one config file, and a second build from that folder that boots the same system.

Right now the system lives in three places: Buildroot's generated .config, a loose overlay folder, and a kernel edit in output/. If the container is deleted, the system is gone. Real teams keep Buildroot untouched, and keep their own work in a separate external tree.

Upstream stays clean. Everything of ours lives in one folder that we can put under git.

Step 4.1: An external tree (about 25 minutes)

Make the folder with the three files that Buildroot requires:

mkdir -p /work/knaxus-os/{configs,package,board/knaxus}
cd /work/knaxus-os
cat > external.desc <<'EOF'
name: KNAXUS
desc: Knaxus training image
EOF
cat > Config.in <<'EOF'
source "$BR2_EXTERNAL_KNAXUS_PATH/package/knaxus-hello/Config.in"
EOF
cat > external.mk <<'EOF'
include $(sort $(wildcard $(BR2_EXTERNAL_KNAXUS_PATH)/package/*/*.mk))
EOF

external.desc gives the tree a name. Buildroot then sets BR2_EXTERNAL_KNAXUS_PATH to its folder, so settings and files can refer to it without a fixed path. Config.in adds our packages to the menus. external.mk loads our package recipes.

Move the overlay in. We point Buildroot at its new place in Step 4.3, once the tree is complete:

mv /work/overlay /work/knaxus-os/board/knaxus/overlay

Step 4.2: Your own package (about 40 minutes)

A package is two small files and your source code. We write a tiny C program that prints facts about the machine it runs on. Make the folder:

mkdir -p /work/knaxus-os/package/knaxus-hello/src
cd /work/knaxus-os/package/knaxus-hello

The program, in src/hello.c:

#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <unistd.h>

int main(void)
{
	struct utsname u;
	struct sysinfo si;
	char host[64];

	if (uname(&u) != 0 || sysinfo(&si) != 0 ||
	    gethostname(host, sizeof host) != 0) {
		perror("knaxus-hello");
		return 1;
	}
	printf("host:    %s\n", host);
	printf("kernel:  %s %s\n", u.sysname, u.release);
	printf("machine: %s\n", u.machine);
	printf("uptime:  %ld seconds\n", si.uptime);
	printf("memory:  %lu MB free of %lu MB\n",
	       (unsigned long)(si.freeram * si.mem_unit / (1024 * 1024)),
	       (unsigned long)(si.totalram * si.mem_unit / (1024 * 1024)));
	return 0;
}

It asks the kernel three questions, with uname, sysinfo and gethostname. Each can fail, so we check them all, print a reason and return 1. A program on a device that fails without a word is hard to debug later.

Its build file, src/Makefile. The indented line starts with a real Tab, not spaces:

CFLAGS ?= -O2

knaxus-hello: hello.c
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<

The package recipe, knaxus-hello.mk. Again the command lines start with a Tab:

KNAXUS_HELLO_VERSION = 1.0
KNAXUS_HELLO_SITE = $(BR2_EXTERNAL_KNAXUS_PATH)/package/knaxus-hello/src
KNAXUS_HELLO_SITE_METHOD = local

define KNAXUS_HELLO_BUILD_CMDS
	$(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(@D)
endef

define KNAXUS_HELLO_INSTALL_TARGET_CMDS
	$(INSTALL) -D -m 0755 $(@D)/knaxus-hello $(TARGET_DIR)/usr/bin/knaxus-hello
endef

$(eval $(generic-package))

Read it line by line. The name of the file sets the prefix KNAXUS_HELLO. SITE_METHOD = local says the source is a folder on disk, so there is nothing to download. TARGET_CONFIGURE_OPTS hands our Makefile the cross compiler and its flags, so $(CC) is the Buildroot compiler and not the one on your machine. $(@D) is the build folder, and $(TARGET_DIR) is the folder that becomes the root filesystem.

The menu entry, Config.in. Here the indented lines start with a Tab, and the help text with a Tab and two spaces:

config BR2_PACKAGE_KNAXUS_HELLO
	bool "knaxus-hello"
	help
	  Prints the host name, kernel, uptime and memory.
A package is a recipe that Buildroot runs stage by stage.

Step 4.3: Build it and run it (about 15 minutes)

Register the external tree with Buildroot. Run this in the Buildroot folder:

cd /work/buildroot-2025.02.14
make BR2_EXTERNAL=/work/knaxus-os menuconfig

While the menu is open, do two things. Press /, search for KNAXUS_HELLO, jump to it, and select it. Then, under System configuration, set Root filesystem overlay directories to the overlay's new place:

$(BR2_EXTERNAL_KNAXUS_PATH)/board/knaxus/overlay

Save. Build only the package first:

make knaxus-hello
ls output/build/knaxus-hello-1.0
file output/target/usr/bin/knaxus-hello

The folder holds your source, the compiled program and small hidden files named .stamp_... that record which stages are done. file should say the program is an ELF executable for your target architecture. Look at the compiler Buildroot used:

ls output/host/bin | grep gcc | head -3

Think it through: your build machine and the target may be the same kind of CPU. Buildroot still builds its own compiler and C library instead of using the ones on your machine. Why? What would go wrong if the image used libraries from the host?

Pack the image and boot:

make
./output/images/start-qemu.sh serial-only

Log in and run knaxus-hello. It prints five lines. The host should read knaxus, the kernel your -knaxus release, and the machine your architecture.

Now change the program. Edit hello.c, for example by adding one more printf. Then:

make knaxus-hello-rebuild
make

The first command copies the source again and rebuilds only that package. The second repacks the image. Boot and check the new output.

Step 4.4: Save the whole config in one file (about 25 minutes)

All your settings can now be written into a short file, kept in the external tree:

make savedefconfig BR2_DEFCONFIG=/work/knaxus-os/configs/knaxus_defconfig
cat /work/knaxus-os/configs/knaxus_defconfig

You should see a short list with your hostname, banner, overlay path, strace and knaxus-hello, and the settings from the QEMU board. Everything that is default is left out.

Pitfall: the BR2_DEFCONFIG= part matters. Without it, savedefconfig writes over the file in Buildroot's own configs/ folder, which is where this config came from. You would change upstream by accident.

There is one more trap, and it fails quietly. The QEMU boards generate start-qemu.sh with a script that looks up the QEMU command line by the name of the defconfig. Our config has a new name. When we build from it in Step 4.6, the build would finish without an error, but start-qemu.sh would not be made. The fix is to keep telling the script the original name. In make menuconfig, under System configuration, set Extra arguments passed to custom scripts to the original defconfig name, that is qemu_x86_64_defconfig or qemu_aarch64_virt_defconfig. Then save the config again with the same savedefconfig command.

Check the file once more. It now holds a line with your board's defconfig name. That line is why the boot script keeps being produced.

Step 4.5: Put the last pieces in files (about 25 minutes)

Two things still live outside the project: the kernel edit and the facts about the image. Fix both.

For the kernel, a config fragment is a file with only the lines you want to change. Create /work/knaxus-os/board/knaxus/linux.fragment:

CONFIG_LOCALVERSION="-knaxus"

In make menuconfig, under Kernel, set Additional configuration fragment files to $(BR2_EXTERNAL_KNAXUS_PATH)/board/knaxus/linux.fragment.

For the image facts, a post-build script runs after all packages are installed and before the image is made. Buildroot passes it the path of the target folder as its first argument. Create /work/knaxus-os/board/knaxus/post-build.sh:

#!/bin/sh
set -eu
TARGET_DIR="$1"
printf 'KNAXUS_IMAGE=training\nKNAXUS_IMAGE_VERSION=1\n' > "$TARGET_DIR/etc/knaxus-release"

Make it executable, then set Custom scripts to run before creating filesystem images in the same menu to $(BR2_EXTERNAL_KNAXUS_PATH)/board/knaxus/post-build.sh.

Rebuild the kernel from scratch to prove the fragment does the job. This takes a while, so start it and read on:

make linux-dirclean
make

Boot. uname -r still ends in -knaxus, and cat /etc/knaxus-release shows two lines. Save the config again with the savedefconfig command from Step 4.4.

Step 4.6: A second build, from the files only (about 20 minutes of your time)

The test of a repeatable build is a second build that uses nothing from the first. Build into a new, empty output folder:

cd /work/buildroot-2025.02.14
make O=/work/out-clean BR2_EXTERNAL=/work/knaxus-os knaxus_defconfig
cd /work/out-clean
make -j"$(nproc)"

Downloads are shared with the first build, so it does not fetch anything twice, but it builds every package again. Start it and do something else for an hour.

Then run a boot check that does not need you. It starts QEMU with no keyboard, stops it after two minutes, and looks for the login prompt in the log:

timeout --kill-after=10 120 ./images/start-qemu.sh serial-only < /dev/null > boot.log 2>&1
grep -c "knaxus login:" boot.log

The number should be at least 1. Boot by hand once as well, log in, run knaxus-hello and cat /etc/knaxus-release. If a QEMU process is still running after the check, pkill qemu ends it.

If this works, your project is repeatable. The image comes from the files in /work/knaxus-os and an unmodified Buildroot, and nothing else.

Phase 4 done

We began with a tool that builds Linux from source. We booted the stock image, changed it with settings, an overlay and a package, followed the boot from kernel to login, and then moved everything into one small project that rebuilds the same system in an empty folder. That is how embedded Linux teams work.