Try Embedded

Linux build systems

Yocto, Buildroot and OpenWrt: layers, recipes and images.

Why not just install a distribution

On a PC you install Ubuntu. On a product you need an image that is small, reproducible, contains exactly the packages you chose with your patches, uses a kernel and bootloader configured for your board, and can be rebuilt identically in five years for a security fix. Embedded Linux build systems produce that: a cross toolchain, bootloader, kernel, root filesystem and SDK, all built from source.

The three main tools

BuildrootYocto Project / OpenEmbeddedOpenWrt
PhilosophySimple, Makefile and Kconfig basedFlexible framework of layers and recipesLinux distribution for routers and network devices
OutputA root filesystem image (no package manager by default)Images, plus packages (rpm/ipk/deb), SDKsFirmware image + package manager (opkg)
Learning curveLowSteepModerate
Build timeFast (minutes to an hour)Slow first build (hours), cached afterwardsModerate
Customisationmake menuconfig, external tree (BR2_EXTERNAL)Layers (.bbappend), distro and machine configsFeeds, packages, UCI configuration
Industry useSmall products, quick startMost commercial SoC vendors' BSPs, larger productsRouters, gateways, network appliances

Buildroot

Configure, build, flash:

make raspberrypi4_64_defconfig     # start from a board configuration
make menuconfig                    # choose toolchain, kernel, packages, filesystem
make                               # everything: toolchain, U-Boot, kernel, rootfs
ls output/images/                  # sdcard.img, Image, *.dtb, rootfs.ext4

Key ideas:

  • Everything is selected in one Kconfig menu, saved as a defconfig.
  • Each package is a Config.in (menu entry) plus a .mk file (how to download and build).
  • Board customisations go in an external tree: post-build scripts, rootfs overlays (files copied into the image), kernel and U-Boot configs, patches.
  • A change in configuration often needs a clean rebuild (make clean all), since Buildroot does not track every dependency.

Yocto Project

Yocto is a framework for building your own distribution. The reference build system is BitBake plus the OpenEmbedded-Core metadata; Poky is the reference distribution bundling them.

Vocabulary:

TermMeaning
Recipe (.bb)How to fetch, patch, configure, compile and install one piece of software
Append (.bbappend)Modifies another layer's recipe without editing it
Layer (meta-*)A collection of recipes and configuration; e.g. meta-openembedded, a vendor's BSP meta-imx, your meta-mycompany
MachineThe target hardware configuration (MACHINE = "imx8mm-evk")
DistroPolicy: init system, libc, features (DISTRO = "poky" or your own)
ImageA recipe listing what goes into a root filesystem (core-image-minimal)
sstate cacheReusable build outputs, making rebuilds fast

A minimal recipe:

# meta-mycompany/recipes-apps/sensord/sensord_1.0.bb
SUMMARY = "Sensor daemon"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://LICENSE;md5=0835ade698e0bcf8506ecda2f7b4f302"

SRC_URI = "git://git.example.com/sensord.git;protocol=https;branch=main"
SRCREV = "4a1c2e9f0d3b7a6e5c8d9f0a1b2c3d4e5f6a7b8c"
S = "${WORKDIR}/git"

inherit cmake systemd
SYSTEMD_SERVICE:${PN} = "sensord.service"

Typical workflow:

source poky/oe-init-build-env build
bitbake-layers add-layer ../meta-mycompany
bitbake core-image-minimal         # the image
bitbake -c populate_sdk my-image   # an SDK with cross compiler and sysroot for app developers
devtool modify sensord             # edit a recipe's source in place while developing

Why companies accept Yocto's complexity: vendor BSP layers come for it, layers keep your changes separate from upstream (so updating is feasible), it handles licence compliance (manifests, source archives), and builds are reproducible.

OpenWrt

OpenWrt targets network devices: routers, access points, gateways. It provides a writable overlay filesystem on top of a read-only squashfs, a package manager (opkg), the UCI unified configuration system, the LuCI web interface, and strong networking features (firewall, VLANs, Wi-Fi, mesh). Choose it when the product is fundamentally a network appliance.

Things every build setup needs

  • Pinned versions: a fixed release branch of the build system and fixed revisions (SRCREV, hashes) of every source, so a build is reproducible.
  • Your changes in your own layer or external tree, never edits to upstream files.
  • A download mirror and caches (DL_DIR, sstate), so builds do not depend on external servers still hosting old tarballs.
  • CI builds of images and SDKs.
  • Licence compliance: the image contains GPL software; you must be able to provide sources and licence texts. Both Yocto and Buildroot generate manifests (make legal-info in Buildroot).
  • Security updates: track CVEs for your package versions (Yocto has cve-check), and plan how updated images reach devices (see OTA updates).
#include <stdio.h>

int main(void) {
  /* Why caches matter: a typical Yocto build, minutes. */
  const char *what[] = { "first build, empty caches", "rebuild after changing one app", "rebuild with shared sstate on CI" };
  int minutes[] = { 180, 4, 12 };
  for (int i = 0; i < 3; i++) printf("%-34s %4d min\n", what[i], minutes[i]);
  return 0;
}

Pitfall: Hand-editing files in a running device's root filesystem "just to test" and then forgetting to put the change in the build is a classic source of works-on-my-board bugs. Every change goes into the recipe, overlay or configuration, and the device is reflashed.

Interview question: Buildroot or Yocto for a new product?

Answer: Buildroot when the system is relatively simple, the team is small, a single image with fixed contents is enough, and fast builds and a gentle learning curve matter. Yocto when the SoC vendor's BSP is delivered as Yocto layers (common for NXP, TI, ST, Xilinx), the product family has several machines or variants, packages and SDKs are needed, licence compliance and long-term maintenance with upstream updates matter, or several teams contribute. Both produce reproducible images; Yocto costs more up front in learning and build time.

Interview question: What makes an embedded Linux build reproducible and maintainable over years?

Answer: Pinning everything: the build system release, every layer revision and every source revision or checksum; keeping all customisations in your own layer or external tree; mirroring source downloads and caching build outputs; building in a defined environment such as a container; running builds in CI; generating licence manifests and source archives; and tracking CVEs against the package versions. Then a security fix can be rebuilt years later with only the intended change.

Build it: build the packages in an order that works

This is step 2 of the module project, the smart-home gateway. Its image is built from a few dozen packages: the Zigbee stack, an MQTT broker, the gateway's own daemons, and the libraries under them. Buildroot and Yocto build each package only after everything it depends on, and stop with an error, rather than guess, when the dependencies loop or name a package that does not exist. The gateway team wants the same check in CI for their own package list, before a two-hour build fails at minute ninety.

Lab: Order the build, and catch loops and missing packages

build_order(pkgs, n, order) fills order with the package indices in the order to build them and returns n. Each package names up to MAX_DEPS dependencies, ended by a null pointer.

  • Repeatedly pick the first package in the list, by index, that is not built yet and whose dependencies are all built, and build it. Always taking the first one makes the order the same on every run, so two builds of the same list can be compared.
  • Before anything else, check every dependency names a package in the list. If one does not, return -2 (and print nothing yourself).
  • If at some point packages remain but none can be built, the dependencies form a loop: return -1.

Going deeper

Interview question: How would you make an industrial gateway's Linux image reproducible?

Answer: Build it with Yocto or Buildroot from pinned layer and package versions in a container, in CI, with a shared download and state cache. Every image records the exact inputs (a manifest and the Git revisions), so any release can be rebuilt bit for bit, and licence and security information comes from the same build.

Interview question: What is the Yocto shared state cache and why does it matter?

Answer: BitBake stores the output of each task (fetch, compile, install and so on) in the sstate cache, keyed by a hash of all its inputs. When the inputs have not changed, later builds restore the result instead of rebuilding, so a rebuild after a small change takes minutes instead of hours. Sharing the cache on a server between developers and CI greatly reduces build times.

Interview question: How do you track security vulnerabilities in an embedded Linux image?

Answer: Generate a software bill of materials listing every package and version (Yocto's create-spdx, Buildroot's legal-info), run CVE checks against it (Yocto's cve-check class, or external scanners), triage the results for applicability, apply updates or patches in your layer, and rebuild and ship through the OTA mechanism. Doing it continuously in CI, not just before release, keeps the backlog small.

Explain it: In your own words, explain why products use Yocto or Buildroot instead of installing a desktop distribution.

Answer: A product needs an image that contains exactly the software it requires and nothing else, built for its specific board with the right bootloader, kernel configuration and patches, small enough for its storage and fast to boot. It must be possible to rebuild exactly the same image years later to fix a security problem, and to account for every open-source licence. Build systems such as Yocto and Buildroot build everything from source from pinned versions and recipes, which gives that control and reproducibility; a desktop distribution is too large and changes outside your control.