From 5b108e9bcce8b499dac3db4107aa6fbc3e550576 Mon Sep 17 00:00:00 2001 From: Eric Long Date: Tue, 23 Jul 2024 23:10:40 +0800 Subject: [PATCH] Move distro hack to tools/vmlinux-distro-hack --- ...-hack.patch => dkms-use-distro-hack.patch} | 2 +- debian/patches/series | 2 +- kmod/Makefile | 82 ++++++++----------- kmod/dkms.conf | 2 +- tools/vmlinux-distro-hack | 35 ++++++++ 5 files changed, 74 insertions(+), 49 deletions(-) rename debian/patches/{dkms-use-debian-hack.patch => dkms-use-distro-hack.patch} (81%) create mode 100755 tools/vmlinux-distro-hack diff --git a/debian/patches/dkms-use-debian-hack.patch b/debian/patches/dkms-use-distro-hack.patch similarity index 81% rename from debian/patches/dkms-use-debian-hack.patch rename to debian/patches/dkms-use-distro-hack.patch index 94b9c1b..1e0d7b7 100644 --- a/debian/patches/dkms-use-debian-hack.patch +++ b/debian/patches/dkms-use-distro-hack.patch @@ -4,7 +4,7 @@ PACKAGE_NAME="mimic" PACKAGE_VERSION="0.4.2" -MAKE[0]="make KERNEL_UNAME=$kernelver" -+MAKE[0]="make KERNEL_UNAME=$kernelver DEBIAN_HACK=1" ++MAKE[0]="make KERNEL_UNAME=$kernelver DISTRO_HACK=1" CLEAN="make clean" BUILT_MODULE_NAME[0]="mimic" DEST_MODULE_LOCATION[0]="/kernel/drivers/misc" diff --git a/debian/patches/series b/debian/patches/series index 3d09eca..00ae1c3 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -1 +1 @@ -dkms-use-debian-hack.patch +dkms-use-distro-hack.patch diff --git a/kmod/Makefile b/kmod/Makefile index 4df74a4..feb8d04 100644 --- a/kmod/Makefile +++ b/kmod/Makefile @@ -1,62 +1,52 @@ obj-m += mimic.o +ccflags-y := -D_MIMIC_KMOD KERNEL_UNAME ?= $(shell uname -r) - SYSTEM_BUILD_DIR := /lib/modules/$(KERNEL_UNAME)/build -PWD := $(CURDIR) - -ccflags-y := -D_MIMIC_KMOD -# Notes on Debian hack: +# Notes on distro hack: # -# Debian does not ship vmlinux nor resolve_btfids in -# linux-headers like Arch Linux does, so we need to refer to externally -# built one in order to build kernel module BTF successfully. +# - Debian does not ship vmlinux nor resolve_btfids in +# linux-headers like Arch Linux does, so we need to refer to externally +# built one in order to build kernel module BTF successfully. # -# Ubuntu does not ship vmlinux, but provides resolve_btfid, and there -# might be dead symlinks (e.g. rust/) inside. This needs to work around as -# well. +# - Ubuntu does not ship vmlinux, but provides resolve_btfids, and there +# might be dead symlinks (e.g. rust/) inside. This needs to work around as +# well. +# +# - Fedora does not provide resolve_btfids. However they ship the source code +# of it inside /lib/modules/*. +# +# Other distributions lacking components necessary to build kernel module BTF +# can also use the hack in packaging. + +ifdef DISTRO_HACK + +build_dir := build + +VMLINUX_DISTRO_HACK ?= /usr/lib/mimic/vmlinux-distro-hack +RESOLVE_BTFIDS ?= /usr/lib/mimic/resolve_btfids + +export EXTRACT_VMLINUX ?= /usr/lib/mimic/extract-vmlinux +export EXTRACT_BTF ?= /usr/lib/mimic/extract-btf +export VMLINUX_SUFFIX ?= -$(KERNEL_UNAME) -ifdef DEBIAN_HACK -BUILD := build else -BUILD := $(SYSTEM_BUILD_DIR) +build_dir := $(SYSTEM_BUILD_DIR) endif all: -ifdef DEBIAN_HACK - cp -rL $(SYSTEM_BUILD_DIR) $(BUILD) || true - - if [ ! -f $(BUILD)/vmlinux ]; then \ - if [ -f /boot/vmlinuz-$(KERNEL_UNAME) ]; then \ - /usr/lib/mimic/extract-vmlinux /boot/vmlinuz-$(KERNEL_UNAME) > $(BUILD)/vmlinux; \ - [ $? -eq 0 ] || /usr/lib/mimic/extract-btf /boot/vmlinuz-$(KERNEL_UNAME) > $(BUILD)/vmlinux; \ - if [ $? -ne 0 ]; then \ - echo ERROR: cannot extract BTF from boot image; \ - exit 1; \ - fi \ - elif [ -f /boot/vmlinux-$(KERNEL_UNAME) ]; then \ - if readelf -h /boot/vmlinux-$(KERNEL_UNAME) >/dev/null 2>/dev/null; then \ - cp /boot/vmlinux-$(KERNEL_UNAME) $(BUILD)/vmlinux; \ - else \ - /usr/lib/mimic/extract-btf /boot/vmlinux-$(KERNEL_UNAME) > $(BUILD)/vmlinux; \ - if [ $? -ne 0 ]; then \ - echo ERROR: cannot extract BTF from boot image; \ - exit 1; \ - fi \ - fi \ - else \ - echo ERROR: no boot image found; \ - exit 1; \ - fi \ - fi - - if [ ! -f $(BUILD)/tools/bpf/resolve_btfids/resolve_btfids ]; then \ - install -Dm755 /usr/lib/mimic/resolve_btfids $(BUILD)/tools/bpf/resolve_btfids/resolve_btfids; \ +ifdef DISTRO_HACK + cp -rL $(SYSTEM_BUILD_DIR) $(build_dir) || true + $(VMLINUX_DISTRO_HACK) $(build_dir) + if [ ! -f $(build_dir)/tools/bpf/resolve_btfids/resolve_btfids ]; then \ + install -Dm755 $(RESOLVE_BTFIDS) $(build_dir)/tools/bpf/resolve_btfids/resolve_btfids; \ fi endif - $(MAKE) -C $(BUILD) M=$(PWD) modules + $(MAKE) -C $(build_dir) M=$(CURDIR) modules clean: - rm -rf build - [ ! -d $(SYSTEM_BUILD_DIR) ] || $(MAKE) -C $(SYSTEM_BUILD_DIR) M=$(PWD) clean +ifdef DISTRO_HACK + rm -rf $(build_dir) +endif + [ ! -d $(SYSTEM_BUILD_DIR) ] || $(MAKE) -C $(SYSTEM_BUILD_DIR) M=$(CURDIR) clean diff --git a/kmod/dkms.conf b/kmod/dkms.conf index 1319ada..1e351a2 100644 --- a/kmod/dkms.conf +++ b/kmod/dkms.conf @@ -1,6 +1,6 @@ PACKAGE_NAME="mimic" PACKAGE_VERSION="0.4.2" -MAKE[0]="make KERNEL_UNAME=$kernelver" +MAKE[0]="make KERNEL_UNAME=$kernelver DISTRO_HACK=1" CLEAN="make clean" BUILT_MODULE_NAME[0]="mimic" DEST_MODULE_LOCATION[0]="/kernel/drivers/misc" diff --git a/tools/vmlinux-distro-hack b/tools/vmlinux-distro-hack new file mode 100755 index 0000000..0df7816 --- /dev/null +++ b/tools/vmlinux-distro-hack @@ -0,0 +1,35 @@ +#!/bin/sh +# +# Extract vmlinux (either ELF with .BTF section or entirely BTF blob) when +# distributions do not provide it in its module path. +# +# See kmod/Makefile for usage with automatic kernel building tool e.g. DKMS. + +module_dir="$1" +: "${VMLINUX_SUFFIX:=-$(uname -r)}" +: "${EXTRACT_VMLINUX:="$module_dir/scripts/extract-vmlinux"}" +: "${EXTRACT_BTF:="out/extract-btf"}" + +if [ ! -f "$module_dir/vmlinux" ]; then + if [ -f "/boot/vmlinuz$VMLINUX_SUFFIX" ]; then + "$EXTRACT_VMLINUX" "/boot/vmlinuz$VMLINUX_SUFFIX" >"$module_dir/vmlinux" + [ $? -eq 0 ] || "$EXTRACT_BTF" "/boot/vmlinuz$VMLINUX_SUFFIX" >"$module_dir/vmlinux" + if [ $? -ne 0 ]; then + >&2 echo "ERROR: cannot extract BTF from '/boot/vmlinuz$VMLINUX_SUFFIX'" + exit 1 + fi + elif [ -f "/boot/vmlinux$VMLINUX_SUFFIX" ]; then + if readelf -h "/boot/vmlinux$VMLINUX_SUFFIX" >/dev/null 2>/dev/null; then + cp "/boot/vmlinux$VMLINUX_SUFFIX" "$module_dir/vmlinux" + else + "$EXTRACT_BTF" "/boot/vmlinux$VMLINUX_SUFFIX" >"$module_dir/vmlinux" + if [ $? -ne 0 ]; then + >&2 echo "ERROR: cannot extract BTF from '/boot/vmlinux$VMLINUX_SUFFIX'" + exit 1 + fi + fi + else + >&2 echo "ERROR: no boot image found" + exit 1 + fi +fi