Skip to content

Commit

Permalink
Add build infra
Browse files Browse the repository at this point in the history
Mostly a copy over from vmtest repo with a few pathing changes.
  • Loading branch information
danobi committed Jan 2, 2025
1 parent d06a1b3 commit 183a442
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 0 deletions.
68 changes: 68 additions & 0 deletions .github/workflows/kernels.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# This job builds test kernels and uploads them to the `assets` dummy release.
#
# It works by looking at the KERNELS file in the repository and checks it
# against all the already-uploaded kernels.

name: Kernels

on:
push:
branches: [ "master" ]

concurrency:
# Ensure only a single instance of this job is run at any time
group: ${{ github.ref }}

permissions:
contents: write

jobs:
build-upload:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Calculate needed kernels
id: calculate
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
existing=$(gh release view test_assets --json assets --jq '.[][].name')
# Begin multiline output parameter
echo "NEEDED_KERNELS<<MULTILINE_EOF" >> "$GITHUB_OUTPUT"
echo "$existing" | python3 -c '
import sys
# Calculate kernels that have already been uploaded
existing = {line.strip() for line in sys.stdin}
with open("./KERNELS", "r") as f:
lines = {line.strip() for line in f if line.strip()}
# Print to stdout kernel we need to build and upload
for line in lines:
version = line.strip()
name = f"bzImage-{version}"
if name not in existing:
print(line)
' | tee -a "$GITHUB_OUTPUT"
# End multiline output parameter
echo "MULTILINE_EOF" >> "$GITHUB_OUTPUT"
- name: Build needed kernels
run: |
while IFS= read -r version; do
echo "Building: ${version}"
./build.sh "$version"
done <<< "${{ steps.calculate.outputs.NEEDED_KERNELS }}"
- name: Upload freshly built kernels
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
for kernel in bzImage-*; do
gh release upload test_assets "$kernel"
done
1 change: 1 addition & 0 deletions KERNELS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v5.15
49 changes: 49 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
#
# Build a vmtest ready kernel.
#
# Usage:
# ./build.sh v6.2

set -eu

# Formats a version string to a comparable number
# eg. v1.2.3 => 1002003
function version {
# Strip the leading 'v'
local vstripped="${1:1}"
echo "$vstripped" | awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }'
}

# Go to repo root
cd "$(git rev-parse --show-toplevel)"

if [[ $# -ne 1 ]]; then
echo "Usage: $0 <kernel-tag>"
exit 1
fi

VERSION="$1"

# Anything older than 5.15 we need to use alpine 3.16, which at the time of
# writing is the oldest supported alpine version that contains all the build
# deps (pahole in particular). We need old alpines to use an older GCC. Newer
# GCCs have better warnings and they break old kernel builds.
ALPINE_VERSION=3.18
if [[ $(version "$1") -le $(version "v5.15") ]]; then
ALPINE_VERSION=3.16
fi

# Build builder
docker build \
--build-arg ALPINE_VERSION="$ALPINE_VERSION" \
--build-arg KERNEL_TAG="$1" \
-t vmtest-kernel-builder-"$VERSION" \
-f docker/Dockerfile.kernel \
.

# Run builder
docker run --rm -v "$(pwd):/output" vmtest-kernel-builder-"$VERSION"

# Rename bzImage appropriately
mv -f bzImage bzImage-"$VERSION"
49 changes: 49 additions & 0 deletions build_kernel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
#
# Build a vmtest ready kernel.
#
# Usage:
# ./build.sh v6.2

set -eu

# Formats a version string to a comparable number
# eg. v1.2.3 => 1002003
function version {
# Strip the leading 'v'
local vstripped="${1:1}"
echo "$vstripped" | awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }'
}

# Go to repo root
cd "$(git rev-parse --show-toplevel)"

if [[ $# -ne 1 ]]; then
echo "Usage: $0 <kernel-tag>"
exit 1
fi

VERSION="$1"

# Anything older than 5.15 we need to use alpine 3.16, which at the time of
# writing is the oldest supported alpine version that contains all the build
# deps (pahole in particular). We need old alpines to use an older GCC. Newer
# GCCs have better warnings and they break old kernel builds.
ALPINE_VERSION=3.18
if [[ $(version "$1") -le $(version "v5.15") ]]; then
ALPINE_VERSION=3.16
fi

# Build builder
docker build \
--build-arg ALPINE_VERSION="$ALPINE_VERSION" \
--build-arg KERNEL_TAG="$1" \
-t vmtest-kernel-builder-"$VERSION" \
-f docker/Dockerfile.kernel \
.

# Run builder
docker run --rm -v "$(pwd):/output" vmtest-kernel-builder-"$VERSION"

# Rename bzImage appropriately
mv -f bzImage bzImage-"$VERSION"
1 change: 1 addition & 0 deletions config/config.x86_64
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ CONFIG_CRYPTO_XXHASH=y
CONFIG_DCB=y
CONFIG_DEBUG_INFO_BTF=y
CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
CONFIG_X86_DECODER_SELFTEST=n
CONFIG_DEFAULT_FQ_CODEL=y
CONFIG_DEFAULT_RENO=y
CONFIG_DEFAULT_SECURITY_DAC=y
Expand Down
36 changes: 36 additions & 0 deletions docker/Dockerfile.kernel
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
ARG ALPINE_VERSION
FROM alpine:${ALPINE_VERSION}

RUN apk update && apk add \
bash \
bison \
build-base \
diffutils \
elfutils-dev \
findutils \
flex \
git \
gzip \
linux-headers \
pahole \
perl \
python3 \
openssl \
openssl-dev \
xz \
zstd

WORKDIR /

ARG KERNEL_REPO=https://github.com/torvalds/linux.git
ARG KERNEL_TAG

RUN git clone --depth 1 ${KERNEL_REPO} linux --branch ${KERNEL_TAG}
WORKDIR linux

COPY ./docker/config_kernel.sh config_kernel.sh
COPY ./docker/build_kernel_container.sh build_kernel_container.sh
COPY ./config config
RUN ./config_kernel.sh

ENTRYPOINT ["./build_kernel_container.sh"]
12 changes: 12 additions & 0 deletions docker/build_kernel_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
#
# Build kernel from inside container.
#
# Note this script expects the bind mounted output directly to be mounted
# at /output.

set -eux

make -j "$(nproc)" bzImage
bzimage=$(find . -type f -name bzImage)
cp "$bzimage" /output
11 changes: 11 additions & 0 deletions docker/config_kernel.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
#
# Configure a vmtest ready kernel.
#
# Run this inside kernel source tree root.

set -eux

# Set kconfig
cat config/config config/config.x86_64 config/config.vm > .config
make olddefconfig

0 comments on commit 183a442

Please sign in to comment.