Netkit Kernel module on Alpine

By default Alpine's virt distribution doesn't come with the netkit linux kernel module installed which is one of the modules used by cilium to improve cilium's performance.

Here's a script which recompiles the kernel with netkit support enabled:

#!/bin/sh
set -eu

# ─── SETTINGS ──────────────────────────────────────────────────────────────────
KVER="$(uname -r)"                  # e.g. 6.12.5-virt
KSRC="https://cdn.kernel.org/pub/linux/kernel/v${KVER%%.*}.x/linux-${KVER%%-*}.tar.xz"
BUILD_PACKAGES="alpine-sdk ncurses-dev bc linux-virt-dev"
WORKDIR="/usr/src/build-netkit"
# ───────────────────────────────────────────────────────────────────────────────

info() { printf "\033[1;34m➜ %s\033[0m\n" "$*"; }

# 1. install build deps only if not present
need_pkgs=""
for p in $BUILD_PACKAGES; do
  apk info -e "$p" >/dev/null 2>&1 || need_pkgs="$need_pkgs $p"
done
[ -n "$need_pkgs" ] && { info "Installing build packages:$need_pkgs"; apk add --no-cache $need_pkgs; }

# remember what we installed so we can remove it later
INSTALLED_PKGS="$need_pkgs"

# 2. download & unpack kernel source
rm -rf "$WORKDIR"
mkdir -p "$WORKDIR"
info "Downloading $KSRC"
wget -qO- "$KSRC" | tar -xJ -C "$WORKDIR" --strip-components=1
cd "$WORKDIR"

# 3. start from the virt config shipped by Alpine, enable NETKIT
info "Preparing .config"
zcat /usr/src/linux-virt-*/virt*.config.gz > .config
yes "" | make olddefconfig >/dev/null
scripts/config --module NETKIT

# 4. build & install
info "Building kernel $(make -s kernelrelease)"
make -j"$(nproc)" >/dev/null
make modules_install install >/dev/null

# 5. regenerate initramfs for the new vmlinuz (mkinitfs is called by make install)
info "Kernel installed as /boot/vmlinuz-$(make -s kernelrelease)"

# 6. CLEAN‑UP
info "Cleaning up build files"
cd /
rm -rf "$WORKDIR"

if [ -n "$INSTALLED_PKGS" ]; then
  info "Removing build packages:$INSTALLED_PKGS"
  apk del --no-network $INSTALLED_PKGS
fi

info "All done. Reboot to use the new kernel."