From 309157b88f2ed48a705c5fd1147fb5adc1abb4b9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 6 Nov 2026 21:44:29 +0000 Subject: [PATCH] kernel/linux/scripts: build tswg.ko for gokrazy kernels The natlab gokrazy image runs a recent upstream kernel (7.2.8 today) that gokrazy rebuilds and tailscale.com bumps regularly, which makes it a better primary test target than a distro kernel: it keeps us honest against new kernel APIs, and gokrazy has no userspace tools, as in the end state where tailscaled drives the kernel entirely. gokrazy kernels are kernel.org releases plus the patches in _build/series, built with an embedded config and without MODVERSIONS or module signing. gokrazy-kdir.sh downloads the release, applies the patches, installs the config pulled out of vmlinuz with the kernel's extract-ikconfig (vendored), and runs modules_prepare, caching the result by release and config hash. It takes about 20 seconds the first time. The only config differences after olddefconfig with the host's gcc 13 (gokrazy uses Debian 12's gcc 12) are toolchain capability bits. Factor the generic part of build-for-ubuntu.sh into build.sh, which now copies only sources (not objects from an in-place build in src/), and sets KBUILD_MODPOST_WARN=1 for trees without a Module.symvers. Add build-for-gokrazy.sh. Updates #426 Signed-off-by: Brad Fitzpatrick Change-Id: Id28d9c013bdfaa7cec28aeff846f7f6087e25518 --- kernel/linux/scripts/build-for-gokrazy.sh | 18 ++++++ kernel/linux/scripts/build-for-ubuntu.sh | 14 +---- kernel/linux/scripts/build.sh | 38 +++++++++++++ kernel/linux/scripts/extract-ikconfig | 69 +++++++++++++++++++++++ kernel/linux/scripts/gokrazy-kdir.sh | 67 ++++++++++++++++++++++ 5 files changed, 195 insertions(+), 11 deletions(-) create mode 100755 kernel/linux/scripts/build-for-gokrazy.sh create mode 100755 kernel/linux/scripts/build.sh create mode 100755 kernel/linux/scripts/extract-ikconfig create mode 100755 kernel/linux/scripts/gokrazy-kdir.sh diff --git a/kernel/linux/scripts/build-for-gokrazy.sh b/kernel/linux/scripts/build-for-gokrazy.sh new file mode 100755 index 000000000..142c870e6 --- /dev/null +++ b/kernel/linux/scripts/build-for-gokrazy.sh @@ -0,0 +1,18 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# build-for-gokrazy.sh builds tswg.ko for a gokrazy kernel package (see +# gokrazy-kdir.sh) in a private copy of src/ under OUTDIR, and prints the +# path of the resulting tswg.ko to stdout. All build output goes to stderr. +# +# usage: build-for-gokrazy.sh GOKRAZY_KERNEL_PACKAGE_DIR OUTDIR + +set -eu + +if [ $# -ne 2 ]; then + echo "usage: $0 GOKRAZY_KERNEL_PACKAGE_DIR OUTDIR" >&2 + exit 2 +fi +scripts=$(dirname "$0") +kdir=$("$scripts/gokrazy-kdir.sh" "$1") +exec "$scripts/build.sh" "$kdir" "$2" diff --git a/kernel/linux/scripts/build-for-ubuntu.sh b/kernel/linux/scripts/build-for-ubuntu.sh index f43be7399..ee1d06fb9 100755 --- a/kernel/linux/scripts/build-for-ubuntu.sh +++ b/kernel/linux/scripts/build-for-ubuntu.sh @@ -14,14 +14,6 @@ if [ $# -ne 2 ]; then echo "usage: $0 KERNEL_RELEASE OUTDIR" >&2 exit 2 fi -kver=$1 -out=$2 -top=$(cd "$(dirname "$0")/.." && pwd) - -kdir=$("$top/scripts/ubuntu-headers.sh" "$kver") - -mkdir -p "$out" -rm -rf "$out/src" -cp -r "$top/src" "$out/src" -make -C "$top" KDIR="$kdir" SRC="$out/src" >&2 -echo "$out/src/tswg.ko" +scripts=$(dirname "$0") +kdir=$("$scripts/ubuntu-headers.sh" "$1") +exec "$scripts/build.sh" "$kdir" "$2" diff --git a/kernel/linux/scripts/build.sh b/kernel/linux/scripts/build.sh new file mode 100755 index 000000000..66ef31a93 --- /dev/null +++ b/kernel/linux/scripts/build.sh @@ -0,0 +1,38 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# build.sh builds tswg.ko against the kernel build directory KDIR in a +# private copy of src/ under OUTDIR, and prints the path of the resulting +# tswg.ko to stdout. All build output goes to stderr. +# +# usage: build.sh KDIR OUTDIR + +set -eu + +if [ $# -ne 2 ]; then + echo "usage: $0 KDIR OUTDIR" >&2 + exit 2 +fi +kdir=$1 +out=$2 +top=$(cd "$(dirname "$0")/.." && pwd) + +# Copy only sources, not any objects from an in-place build in src/. +mkdir -p "$out" +rm -rf "$out/src" +tar -C "$top" -cf - \ + --exclude='*.o' --exclude='*.ko' --exclude='*.mod' --exclude='*.mod.c' \ + --exclude='*.cmd' --exclude='*.o.d' --exclude=modules.order \ + --exclude=Module.symvers --exclude=generated/autoconf.h \ + src | tar -C "$out" -xf - + +# A tree prepared with "make modules_prepare" (as for gokrazy kernels) has +# no Module.symvers, so modpost can't resolve the kernel's exports. That's +# fine without MODVERSIONS; unresolved symbols then show up at load time. +modpost_warn= +if [ ! -f "$kdir/Module.symvers" ]; then + modpost_warn=KBUILD_MODPOST_WARN=1 +fi + +make -C "$top" KDIR="$kdir" SRC="$out/src" $modpost_warn >&2 +echo "$out/src/tswg.ko" diff --git a/kernel/linux/scripts/extract-ikconfig b/kernel/linux/scripts/extract-ikconfig new file mode 100755 index 000000000..8df33e7d6 --- /dev/null +++ b/kernel/linux/scripts/extract-ikconfig @@ -0,0 +1,69 @@ +#!/bin/sh +# ---------------------------------------------------------------------- +# extract-ikconfig - Extract the .config file from a kernel image +# +# This will only work when the kernel was compiled with CONFIG_IKCONFIG. +# +# The obscure use of the "tr" filter is to work around older versions of +# "grep" that report the byte offset of the line instead of the pattern. +# +# (c) 2009,2010 Dick Streefland +# Licensed under the terms of the GNU General Public License. +# ---------------------------------------------------------------------- + +cf1='IKCFG_ST\037\213\010' +cf2='0123456789' + +dump_config() +{ + if pos=`tr "$cf1\n$cf2" "\n$cf2=" < "$1" | grep -abo "^$cf2"` + then + pos=${pos%%:*} + tail -c+$(($pos+8)) "$1" | zcat > $tmp1 2> /dev/null + if [ $? != 1 ] + then # exit status must be 0 or 2 (trailing garbage warning) + cat $tmp1 + exit 0 + fi + fi +} + +try_decompress() +{ + for pos in `tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"` + do + pos=${pos%%:*} + tail -c+$pos "$img" | $3 > $tmp2 2> /dev/null + dump_config $tmp2 + done +} + +# Check invocation: +me=${0##*/} +img=$1 +if [ $# -ne 1 -o ! -s "$img" ] +then + echo "Usage: $me " >&2 + exit 2 +fi + +# Prepare temp files: +tmp1=/tmp/ikconfig$$.1 +tmp2=/tmp/ikconfig$$.2 +trap "rm -f $tmp1 $tmp2" 0 + +# Initial attempt for uncompressed images or objects: +dump_config "$img" + +# That didn't work, so retry after decompression. +try_decompress '\037\213\010' xy gunzip +try_decompress '\3757zXZ\000' abcde unxz +try_decompress 'BZh' xy bunzip2 +try_decompress '\135\0\0\0' xxx unlzma +try_decompress '\211\114\132' xy 'lzop -d' +try_decompress '\002\041\114\030' xyy 'lz4 -d -l' +try_decompress '\050\265\057\375' xxx unzstd + +# Bail out: +echo "$me: Cannot find kernel config." >&2 +exit 1 diff --git a/kernel/linux/scripts/gokrazy-kdir.sh b/kernel/linux/scripts/gokrazy-kdir.sh new file mode 100755 index 000000000..3bff07fa5 --- /dev/null +++ b/kernel/linux/scripts/gokrazy-kdir.sh @@ -0,0 +1,67 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# gokrazy-kdir.sh prepares a kernel build directory matching a gokrazy +# kernel package (such as the github.com/gokrazy/kernel.amd64 module that +# tailscale.com's natlab gokrazy image uses) and prints its path to stdout. +# +# gokrazy kernels are upstream kernel.org releases plus the patches listed in +# _build/series, and embed their .config (CONFIG_IKCONFIG). So we download +# that release, apply the patches, install the embedded config and run +# "make modules_prepare". That's enough to build tswg.ko for that kernel, +# since gokrazy kernels don't use MODVERSIONS or module signing. +# +# Results are cached in $TSLINUX_CACHE (default ~/.cache/tslinux), keyed by +# the kernel release and a hash of the config. +# +# usage: gokrazy-kdir.sh GOKRAZY_KERNEL_PACKAGE_DIR + +set -eu + +if [ $# -ne 1 ]; then + echo "usage: $0 GOKRAZY_KERNEL_PACKAGE_DIR" >&2 + exit 2 +fi +pkg=$1 +cache=${TSLINUX_CACHE:-$HOME/.cache/tslinux} + +url=$(cat "$pkg/_build/upstream-url.txt") +tarball=${url##*/} # linux-7.2.8.tar.xz +srcname=${tarball%.tar.*} # linux-7.2.8 + +config=$(mktemp) +trap 'rm -f "$config"' EXIT +"$(dirname "$0")/extract-ikconfig" "$pkg/vmlinuz" > "$config" +hash=$(sha256sum "$config" | cut -c1-12) + +kdir=$cache/gokrazy/$srcname-$hash +if [ -f "$kdir/.tswg-prepared" ]; then + echo "$kdir" + exit 0 +fi + +mkdir -p "$cache/tarballs" +if [ ! -f "$cache/tarballs/$tarball" ]; then + echo "downloading $url" >&2 + curl -sSfL -o "$cache/tarballs/$tarball.tmp" "$url" + mv "$cache/tarballs/$tarball.tmp" "$cache/tarballs/$tarball" +fi + +# Build in place (kbuild state records absolute paths, so the tree can't +# be moved afterwards) and mark it complete at the end. +rm -rf "$kdir" +mkdir -p "$kdir" +echo "unpacking $tarball" >&2 +tar -xf "$cache/tarballs/$tarball" -C "$kdir" --strip-components=1 +if [ -f "$pkg/_build/series" ]; then + while read -r patch; do + [ -n "$patch" ] || continue + patch -s -d "$kdir" -p1 < "$pkg/_build/$patch" + done < "$pkg/_build/series" +fi +cp "$config" "$kdir/.config" +echo "preparing $srcname for modules" >&2 +make -s -C "$kdir" olddefconfig >&2 +make -s -C "$kdir" -j"$(nproc)" modules_prepare >&2 +touch "$kdir/.tswg-prepared" +echo "$kdir"