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"