Skip to content

Commit

Permalink
neonvm: makefile targets to introduce cross-compilation of the linux …
Browse files Browse the repository at this point in the history
…kernel

Signed-off-by: Misha Sakhnov <[email protected]>
  • Loading branch information
mikhail-sakhnov committed Sep 25, 2024
1 parent 3e6b8ff commit 1e76bd5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
24 changes: 21 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -244,16 +249,29 @@ 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##*-} \
iidfile=$$(mktemp /tmp/iid-XXXXXX); \
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 \
Expand Down
27 changes: 22 additions & 5 deletions neonvm/hack/kernel/Dockerfile.kernel-builder
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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

0 comments on commit 1e76bd5

Please sign in to comment.