Skip to content

Commit

Permalink
fix make update-kernel-deps
Browse files Browse the repository at this point in the history
Switching to OCI images to distribute kernels broke the update-kernel-deps
target. Move it to a script and fix it to work with OCI images.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Dec 15, 2023
1 parent 8ebad5c commit 40c401c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 30 deletions.
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ testdata/loader-%-eb.elf: testdata/loader.c
$(STRIP) -g $@

.PHONY: update-kernel-deps
update-kernel-deps: KERNEL_VERSION?=6.6
update-kernel-deps: export KERNEL_VERSION?=6.6
update-kernel-deps:
$(eval TMP := $(shell mktemp -d))
curl -fL https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/plain/tools/lib/bpf/libbpf.c?h=v$(KERNEL_VERSION) -o "$(TMP)/libbpf.c"
"$(CURDIR)/internal/cmd/gensections.awk" "$(TMP)/libbpf.c" | gofmt > "$(CURDIR)/elf_sections.go"
curl -fL "$(CI_KERNEL_URL)/linux-$(KERNEL_VERSION)-amd64.tgz" -o "$(TMP)/linux.tgz"
tar xvf "$(TMP)/linux.tgz" -C "$(TMP)" --strip-components=2 ./boot/vmlinuz ./lib/modules
/lib/modules/$(shell uname -r)/build/scripts/extract-vmlinux "$(TMP)/vmlinuz" > "$(TMP)/vmlinux"
$(OBJCOPY) --dump-section .BTF=/dev/stdout "$(TMP)/vmlinux" /dev/null | gzip > "btf/testdata/vmlinux.btf.gz"
find "$(TMP)/modules" -type f -name bpf_testmod.ko -exec $(OBJCOPY) --dump-section .BTF="btf/testdata/btf_testmod.btf" {} /dev/null \;
$(RM) -r "$(TMP)"
./testdata/sh/update-kernel-deps.sh
Binary file modified btf/testdata/btf_testmod.btf
Binary file not shown.
Binary file modified btf/testdata/vmlinux.btf.gz
Binary file not shown.
22 changes: 2 additions & 20 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ set -euo pipefail
script="$(realpath "$0")"
readonly script

source "$(dirname "$script")/testdata/sh/lib.sh"

quote_env() {
for var in "$@"; do
if [ -v "$var" ]; then
Expand Down Expand Up @@ -120,26 +122,6 @@ fi
input="$(mktemp -d)"
readonly input

readonly docker="${CONTAINER_ENGINE:-docker}"

extract_oci_image() {
local image_name=$1
local target_directory=$2

echo -n "Fetching $image_name... "

# We abuse the --output flag of docker buildx to obtain a copy of the image.
# This is simpler than creating a temporary container and using docker cp.
# It also automatically fetches the image for us if necessary.
if ! echo "FROM $image_name" | "$docker" buildx build --quiet --pull --output="$target_directory" - &> /dev/null; then
echo "failed"
return 1
fi

echo "ok"
return 0
}

if [[ -f "${1}" ]]; then
# First argument is a local file.
readonly kernel="${1}"
Expand Down
23 changes: 23 additions & 0 deletions testdata/sh/lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

set -euo pipefail

readonly docker="${CONTAINER_ENGINE:-docker}"

extract_oci_image() {
local image_name=$1
local target_directory=$2

echo -n "Fetching $image_name... "

# We abuse the --output flag of docker buildx to obtain a copy of the image.
# This is simpler than creating a temporary container and using docker cp.
# It also automatically fetches the image for us if necessary.
if ! echo "FROM $image_name" | "$docker" buildx build --quiet --pull --output="$target_directory" - &> /dev/null; then
echo "failed"
return 1
fi

echo "ok"
return 0
}
28 changes: 28 additions & 0 deletions testdata/sh/update-kernel-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

set -euo pipefail

source "$(dirname "$(realpath "$0")")/lib.sh"

# Create a temporary directory
TMP=$(mktemp -d)

cleanup() {
rm -r "$TMP"
}

trap cleanup EXIT

# Download and process libbpf.c
curl -fL "https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/plain/tools/lib/bpf/libbpf.c?h=v$KERNEL_VERSION" -o "$TMP/libbpf.c"
"./internal/cmd/gensections.awk" "$TMP/libbpf.c" | gofmt > "./elf_sections.go"

# Download the specified version of the Linux kernel
extract_oci_image "ghcr.io/cilium/ci-kernels:$KERNEL_VERSION" "$TMP"

# Extract vmlinux
"/lib/modules/$(uname -r)/build/scripts/extract-vmlinux" "$TMP/boot/vmlinuz" > "$TMP/vmlinux"

# Extract BTF
objcopy --dump-section .BTF=/dev/stdout "$TMP/vmlinux" /dev/null | gzip > "btf/testdata/vmlinux.btf.gz"
find "$TMP/lib/modules" -type f -name bpf_testmod.ko -exec objcopy --dump-section .BTF="btf/testdata/btf_testmod.btf" {} /dev/null \;

0 comments on commit 40c401c

Please sign in to comment.