Skip to content

Commit

Permalink
Move distro hack to tools/vmlinux-distro-hack
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Jul 23, 2024
1 parent f0360a1 commit 5b108e9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion debian/patches/series
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dkms-use-debian-hack.patch
dkms-use-distro-hack.patch
82 changes: 36 additions & 46 deletions kmod/Makefile
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion kmod/dkms.conf
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
35 changes: 35 additions & 0 deletions tools/vmlinux-distro-hack
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5b108e9

Please sign in to comment.