Skip to the text
KDOS
Menu
kdos.dev KDOS from Scratch KDOS 0.2 · 4dd4a10 · 2026-11-30 Contents Index

372. The udev hardware database

This page explains how the image gets the hardware database that makes laptop keys, touchpads and device names work. Read it if a machine’s special keys do nothing or its devices show no vendor names.

udev, the device manager, names devices and applies per-model quirks from the hardware database (hwdb): text files under hwdb.d that match a device by its identifiers and give it properties, such as a laptop’s key mapping, a touchpad’s real axis ranges, or a vendor and model name.

udev does not read the text files at run time. It reads a compiled trie, hwdb.bin, and eudev ships the text files and no trie. script/phases/70_image/060_udev_hwdb.sh compiles the trie over the finished tree and checks the result.

Why the trie matters

Many of eudev’s rules begin with IMPORT{builtin}="hwdb ...". With no hwdb.bin that import returns nothing and logs nothing above debug level. Nothing fails; the machine simply behaves as hardware with no quirks and no names:

kpkg’s hwdb trigger keeps the trie current as packages are installed and removed during the build. This step rebuilds it from nothing over the finished tree, so the image never carries a trie that an interrupted build left half-written.

The step

# The two directories udevadm searches, in its own order. Named here only to
# report what was found — udevadm is not told where to look.
DIRS="/etc/udev/hwdb.d /lib/udev/hwdb.d"
BIN=/etc/udev/hwdb.bin

if ! command -v udevadm >/dev/null 2>&1; then
    echo "[hwdb] udevadm is not installed — skipping" >&2
    exit 0
fi

script/phases/70_image/060_udev_hwdb.sh, lines 42–50

DIRS names the two directories udevadm reads source files from, in its own order; the step uses the list only to count and report what is there. Without eudev’s udevadm, the step says so and exits 0.

# THE SOURCE COUNT IS THE ASSERTION, NOT THE EXIT STATUS AND NOT THE FILE.
# eudev's `udevadm hwdb --update` has no empty-input shortcut: given no *.hwdb
# to read it still stores an 80-byte header for an empty trie and exits 0. A
# step that trusted either would report success over an image on which every
# `IMPORT{builtin}="hwdb ..."` returns nothing.
sources=$(find $DIRS -name '*.hwdb' 2>/dev/null | wc -l)
if [ "$sources" -eq 0 ]; then
    echo "FATAL: no *.hwdb under $DIRS;" >&2
    echo "       check that eudev installed its hwdb.d." >&2
    exit 1
fi

script/phases/70_image/060_udev_hwdb.sh, lines 52–62

The source count is the check. Given no *.hwdb file at all, udevadm hwdb --update still writes an 80-byte header for an empty trie and exits 0, so neither its status nor the file’s existence would show that nothing was compiled.

echo "[hwdb] compiling $BIN from $sources source file(s)"

# THE STALE TRIE GOES FIRST. The build tree is incremental, so the file
# measured below is this run's product rather than an earlier build's.
rm -f "$BIN"
udevadm hwdb --update

if [ ! -s "$BIN" ]; then
    echo "FATAL: $BIN was not written from $sources source file(s)." >&2
    exit 1
fi

echo "[hwdb] $BIN: $(wc -c < "$BIN") bytes"

script/phases/70_image/060_udev_hwdb.sh, lines 64–76

The old trie is removed first, so the file measured afterwards is the one this run wrote. A trie that was not written stops the phase. The log ends with its size.

Why /etc/udev and not /usr/lib/udev

udevadm hwdb --update without --usr writes /etc/udev/hwdb.bin. libudev reads that path first and its library directory’s hwdb.bin second. The eudev port’s --with-rootlibdir=/lib makes that second path /lib/udev/hwdb.bin, which is /usr/lib/udev/hwdb.bin through the merged-/usr link. The initramfs build copies /usr/lib/udev whole, so a trie there, about 10 MB, would be loaded into memory at every boot for an early stage that uses no hwdb property; in /etc/udev it stays in the root filesystem.

Why it runs here

After 040_orphans.sh, so a removed package’s hwdb.d files are not compiled in, and before 090_initramfs.sh and 110_iso.sh, which carry the tree into the image.

What it leaves

/etc/udev/hwdb.bin on the image.

See also