From 1e76bd58cc9ddce2e889a3782642a9fd82d35c57 Mon Sep 17 00:00:00 2001 From: Misha Sakhnov Date: Thu, 5 Sep 2024 17:55:04 +0300 Subject: [PATCH] neonvm: makefile targets to introduce cross-compilation of the linux kernel Signed-off-by: Misha Sakhnov --- Makefile | 24 ++++++++++++++--- neonvm/hack/kernel/Dockerfile.kernel-builder | 27 ++++++++++++++++---- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 8adb85c1f..2234c5c38 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,14 @@ GO_BASE_IMG ?= autoscaling-go-base:dev E2E_TESTS_VM_IMG ?= vm-postgres:15-bullseye PG16_DISK_TEST_IMG ?= pg16-disk-test:dev -## Golang details +## Golang details (for local tooling) GOARCH ?= $(shell go env GOARCH) GOOS ?= $(shell go env GOOS) + +# The target architecture for linux kernel. Possible values: amd64 or arm64. +# Any other supported by linux kernel architecture could be added by introducing new build step into neonvm/hack/kernel/Dockerfile.kernel-builder +KERNEL_TARGET_ARCH ?= amd64 + # Get the currently used golang base path GOPATH=$(shell go env GOPATH) # Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) @@ -244,8 +249,21 @@ ifndef ignore-not-found ignore-not-found = false endif +# Build the kernel for the amd64 architecture. Uses generic target. +.PHONY: kernel_amd64 +kernel_amd64: KERNEL_TARGET_ARCH=amd64 +kernel_amd64: kernel + +# Build the kernel for the arm64 architecture. Uses generic target. +.PHONY: kernel_arm64 +kernel_arm64: KERNEL_TARGET_ARCH=arm64 +kernel_arm64: kernel + +# Build the kernel for the target architecture. +# The builder image platform is not specified because the kernel is built for the target architecture using crosscompilation. +# Target is generic and can be used for any supported architecture by specifying the KERNEL_TARGET_ARCH variable. .PHONY: kernel -kernel: ## Build linux kernel. +kernel: rm -f neonvm/hack/kernel/vmlinuz; \ linux_config=$$(ls neonvm/hack/kernel/linux-config-*) \ kernel_version=$${linux_config##*-} \ @@ -253,7 +271,7 @@ kernel: ## Build linux kernel. trap "rm $$iidfile" EXIT; \ docker buildx build \ --build-arg KERNEL_VERSION=$$kernel_version \ - --platform linux/amd64 \ + --target "kernel_${KERNEL_TARGET_ARCH}" \ --pull \ --load \ --iidfile $$iidfile \ diff --git a/neonvm/hack/kernel/Dockerfile.kernel-builder b/neonvm/hack/kernel/Dockerfile.kernel-builder index 74b03ef9d..3c314638f 100644 --- a/neonvm/hack/kernel/Dockerfile.kernel-builder +++ b/neonvm/hack/kernel/Dockerfile.kernel-builder @@ -15,6 +15,8 @@ RUN apt-get update && apt-get -y install \ curl \ ca-certificates \ build-essential \ + gcc-x86-64-linux-gnu \ + gcc-aarch64-linux-gnu \ flex \ bison \ libelf-dev \ @@ -46,16 +48,31 @@ RUN set -e \ && tar --strip-components=1 -C linux-${KERNEL_VERSION} -xf linux-${KERNEL_VERSION}.tar.xz -FROM build-deps AS build -ARG KERNEL_VERSION +### Cross-compilation related steps +# Build the kernel on amd64 +FROM build-deps AS build_amd64 +ARG KERNEL_VERSION ADD linux-config-${KERNEL_VERSION} linux-${KERNEL_VERSION}/.config +RUN cd linux-${KERNEL_VERSION} && make ARCH=x86_64 CROSS_COMPILE=x86_64-linux-gnu- -j `nproc` + +# Copy the kernel image to a separate step +# Use alpine so that `cp` is available when loading custom kernels for the runner pod. +# See the neonvm controller's pod creation logic for more detail. +FROM alpine:3.19 AS kernel_amd64 +ARG KERNEL_VERSION +COPY --from=build_amd64 /build/linux-${KERNEL_VERSION}/arch/x86/boot/bzImage /vmlinuz -RUN cd linux-${KERNEL_VERSION} && make -j `nproc` +# Build the kernel on amd64 +FROM build-deps AS build_arm64 +ARG KERNEL_VERSION +ADD linux-config-${KERNEL_VERSION} linux-${KERNEL_VERSION}/.config +RUN cd linux-${KERNEL_VERSION} && make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j `nproc` +# Copy the kernel image to a separate step # Use alpine so that `cp` is available when loading custom kernels for the runner pod. # See the neonvm controller's pod creation logic for more detail. -FROM alpine:3.19 +FROM alpine:3.19 AS kernel_arm64 ARG KERNEL_VERSION -COPY --from=build /build/linux-${KERNEL_VERSION}/arch/x86/boot/bzImage /vmlinuz +COPY --from=build_arm64 /build/linux-${KERNEL_VERSION}/arch/arm64/boot/Image /vmlinuz