Skip to content

Commit

Permalink
Add a target for building kernel with buildx (#3792)
Browse files Browse the repository at this point in the history
* Use latest kernel in linuxkit

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>

* Parallelize kernel source compression

This surpringly saves a lot of time:
M1: from 340 to 90 seconds
Intel: from 527 to 222 seconds (2 cores 4 threads)

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>

* Add buildx target

buildx can use remote builders and automatically generate the multiarch manifest.
A properly configured builder is required :

First create docker context for the remote builders :
$ docker context create node-<arch> --docker "host=ssh://<user>@<host>"

Then create a buildx configuration using the remote builders:
$ docker buildx create --name kernel_builder --platform linux/amd64
$ docker buildx create --name kernel_builder --node node-arm64 --platform linux/arm64 --append
$ docker buildx use kernel_builder
$ docker buildx ls

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>

* Add a PLATFORMS variable to declare platforms needed for buildx

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>

* Make image name customizable

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>

* Do not tag use the architecture suffix for images built with buildx

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>

* Add make kconfigx to upgrade configs using buildx

To update configuration for 5.10 kernels use :
make -C kernel KERNEL_VERSIONS=5.10.104 kconfigx

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>

---------

Signed-off-by: Frédéric Dalleau <frederic.dalleau@docker.com>
  • Loading branch information
fredericdalleau committed Apr 28, 2023
1 parent eb81457 commit c2df261
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
2 changes: 1 addition & 1 deletion kernel/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ RUN set -e && \
fi

# Save kernel source
RUN tar cJf /out/src/linux.tar.xz /linux
RUN XZ_DEFAULTS="-T0" tar cJf /out/src/linux.tar.xz /linux

# Kernel config
RUN case $(uname -m) in \
Expand Down
71 changes: 71 additions & 0 deletions kernel/Dockerfile.kconfigx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# syntax=docker/dockerfile:1.3-labs

ARG BUILD_IMAGE

FROM ${BUILD_IMAGE} AS kernel-build
ARG KERNEL_VERSIONS
ARG TARGETARCH

RUN apk add \
argp-standalone \
bison \
build-base \
curl \
diffutils \
flex \
gmp-dev \
libarchive-tools \
mpc1-dev \
mpfr-dev \
ncurses-dev \
patch \
xz

COPY / /

# Unpack kernels (download if not present)
RUN <<EOF
set -e
for VERSION in ${KERNEL_VERSIONS}; do
MAJOR=$(echo ${VERSION} | cut -d . -f 1)
MAJOR=v${MAJOR}.x
echo "Downloading/Unpacking $VERSION"
KERNEL_SOURCE=https://www.kernel.org/pub/linux/kernel/${MAJOR}/linux-${VERSION}.tar.xz
if [ ! -f sources/linux-${VERSION}.tar.xz ] ; then
curl -fSLo sources/linux-${VERSION}.tar.xz --create-dirs ${KERNEL_SOURCE}
fi
bsdtar xf sources/linux-${VERSION}.tar.xz
done
EOF

# Apply patches to all kernels and move config files into place
RUN <<EOF
set -e
for VERSION in ${KERNEL_VERSIONS}; do
SERIES=${VERSION%.*}.x
echo "Patching $VERSION $SERIES"
cd /linux-${VERSION}
if [ -d /patches-${SERIES} ]; then
for patch in /patches-${SERIES}/*.patch; do
echo "Applying $patch"
patch -t -F0 -N -u -p1 < "$patch"
done
fi
if [ ${TARGETARCH} = "amd64" ] ; then
cp /config-${SERIES}-x86_64 .config
ARCH=x86 make oldconfig
ls
elif [ ${TARGETARCH} = "arm64" ] ; then
cp /config-${SERIES}-aarch64 .config
ARCH=arm64 make oldconfig
fi
done
EOF

ENTRYPOINT ["/bin/sh"]

FROM scratch
ARG KERNEL_VERSIONS
ARG TARGETARCH
WORKDIR /
COPY --from=kernel-build /linux-${KERNEL_VERSIONS}/.config config-${KERNEL_VERSIONS}-$TARGETARCH
34 changes: 33 additions & 1 deletion kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

# Name and Org on Hub
ORG?=linuxkit
IMAGE:=kernel
PLATFORMS?=linux/amd64,linux/arm64
IMAGE?=kernel
IMAGE_BCC:=kernel-bcc
IMAGE_PERF:=kernel-perf
IMAGE_ZFS:=zfs-kmod
Expand Down Expand Up @@ -98,6 +99,18 @@ ifeq ($(4),)
KERNEL_VERSIONS+=$(1)
endif

buildx_$(2)$(3)$(4): Dockerfile Makefile $(wildcard patches-$(2)/*) $(wildcard config-$(2)*) config-dbg
docker pull $(ORG)/$(IMAGE):$(1)$(3)$(4)-$(TAG) || \
docker buildx build \
--platform=$(PLATFORMS) --push \
--build-arg KERNEL_VERSION=$(1) \
--build-arg KERNEL_SERIES=$(2) \
--build-arg EXTRA=$(3) \
--build-arg DEBUG=$(4) \
--build-arg BUILD_IMAGE=$(IMAGE_BUILDER) \
$(LABELS) \
--no-cache -t $(ORG)/$(IMAGE):$(1)$(3)$(4)-$(TAG) .

build_$(2)$(3)$(4): Dockerfile Makefile $(wildcard patches-$(2)/*) $(wildcard config-$(2)*) config-dbg
docker pull $(ORG)/$(IMAGE):$(1)$(3)$(4)-$(TAG)$(SUFFIX) || \
docker build \
Expand Down Expand Up @@ -282,3 +295,22 @@ else
--build-arg BUILD_IMAGE=$(IMAGE_BUILDER) \
-t linuxkit/kconfig:${KCONFIG_TAG} .
endif

kconfigx:
ifeq (${KCONFIG_TAG},)
docker buildx build --no-cache -f Dockerfile.kconfigx \
--platform=$(PLATFORMS) \
--output . \
--build-arg KERNEL_VERSIONS="$(KERNEL_VERSIONS)" \
--build-arg BUILD_IMAGE=$(IMAGE_BUILDER) \
-t linuxkit/kconfigx .
cp linux_arm64/config-${KERNEL_VERSIONS}-arm64 config-${KERNEL_SERIES}-aarch64
cp linux_amd64/config-${KERNEL_VERSIONS}-amd64 config-${KERNEL_SERIES}-x86_64
else
docker buildx build --no-cache -f Dockerfile.kconfigx \
--platform=$(PLATFORMS) --push \
--output . \
--build-arg KERNEL_VERSIONS="$(KERNEL_VERSIONS)" \
--build-arg BUILD_IMAGE=$(IMAGE_BUILDER) \
-t linuxkit/kconfigx:${KCONFIG_TAG} .
endif
2 changes: 1 addition & 1 deletion linuxkit.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kernel:
image: linuxkit/kernel:5.10.104
image: linuxkit/kernel:5.15.27
cmdline: "console=tty0 console=ttyS0 console=ttyAMA0"
init:
- linuxkit/init:14df799bb3b9e0eb0491da9fda7f32a108a2e2a5
Expand Down

0 comments on commit c2df261

Please sign in to comment.