Skip to content

Commit d104cab

Browse files
committed
Use build arg to specify base image for linux_headers_image to avoid multiple Dockerfiles
Signed-off-by: Dom Del Nano <ddelnano@gmail.com>
1 parent 6be9029 commit d104cab

4 files changed

Lines changed: 95 additions & 119 deletions

File tree

tools/docker/Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ upload_graalvm_archive: build_graalvm_archive_image
206206
##############################################
207207
# Linux Headers Build
208208
##############################################
209+
LINUX_HEADERS_BASE_PRE_v6_3 := ubuntu:18.04@sha256:152dc042452c496007f07ca9127571cb9c29697f42acbfad72324b2bb2e43c98
210+
LINUX_HEADERS_BASE_POST_v6_3 := ubuntu:20.04@sha256:8feb4d8ca5354def3d8fce243717141ce31e2c428701f6682bd2fafe15388214
209211
$(LINUX_HEADER_ASSETS_BUILD_DIR)/linux-headers-x86_64-%.tar.gz: linux_headers_image/Dockerfile linux_headers_image/x86_64_config
210212
ifndef LINUX_HEADERS_REV
211213
$(error set LINUX_HEADERS_REV to a valid value)
@@ -215,15 +217,15 @@ endif
215217
# We need to use a different Dockerfile for these versions.
216218
KERN_MAJ=$$(echo $* | cut -d. -f1); \
217219
KERN_MIN=$$(echo $* | cut -d. -f2); \
218-
HEADERS_DOCKERFILE_SUFFIX=""; \
220+
BASE_IMAGE=$$(echo $(LINUX_HEADERS_BASE_POST_v6_3)); \
219221
if [ $${KERN_MAJ} -lt 6 ] || { [ $${KERN_MAJ} -le 6 ] && [ $${KERN_MIN} -lt 3 ]; }; then \
220-
HEADERS_DOCKERFILE_SUFFIX=".bionic"; \
222+
BASE_IMAGE=$$(echo $(LINUX_HEADERS_BASE_PRE_v6_3)); \
221223
fi; \
222224
linux_headers_image_tag="gcr.io/pixie-oss/pixie-dev-public:$*-$(LINUX_HEADERS_REV)"; \
223225
$(DOCKER) build --build-arg KERN_VERSION=$* \
224226
--build-arg ARCH=x86_64 linux_headers_image \
225-
-t "$${linux_headers_image_tag}" \
226-
-f linux_headers_image/Dockerfile$${HEADERS_DOCKERFILE_SUFFIX} && \
227+
--build-arg BASE_IMAGE=$${BASE_IMAGE} \
228+
-t "$${linux_headers_image_tag}" && \
227229
$(DOCKER) run --rm -v $(PWD)/$(LINUX_HEADER_ASSETS_BUILD_DIR):/output "$${linux_headers_image_tag}"
228230

229231
$(LINUX_HEADER_ASSETS_BUILD_DIR)/linux-headers-arm64-%.tar.gz: linux_headers_image/Dockerfile linux_headers_image/arm64_config

tools/docker/linux_headers_image/Dockerfile

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17-
# debhelper-compat (= 12) needed for kernels >= 6.3
18-
FROM ubuntu:20.04@sha256:8feb4d8ca5354def3d8fce243717141ce31e2c428701f6682bd2fafe15388214
17+
ARG BASE_IMAGE
18+
FROM ${BASE_IMAGE}
19+
20+
ARG KERN_VERSION
21+
ARG ARCH
22+
ARG CROSS_COMPILE="false"
1923

2024
# Install required packages
2125
ENV DEBIAN_FRONTEND=noninteractive
26+
ENV KERN_VERSION=${KERN_VERSION}
27+
ENV ARCH=${ARCH}
28+
ENV CROSS_COMPILE=${CROSS_COMPILE}
29+
2230
RUN apt-get update
2331
RUN apt-get upgrade -y -q
2432
RUN apt-get install -y -q build-essential \
@@ -37,47 +45,15 @@ RUN apt-get install -y -q build-essential \
3745
debhelper \
3846
python3
3947

40-
ARG KERN_VERSION
41-
42-
# Download Linux sources
43-
WORKDIR /px/src
44-
RUN KERN_MAJ="$(echo "${KERN_VERSION}" | cut -d'.' -f1)"; \
45-
wget -nv http://mirrors.edge.kernel.org/pub/linux/kernel/v${KERN_MAJ}.x/linux-${KERN_VERSION}.tar.gz
46-
RUN tar zxf linux-${KERN_VERSION}.tar.gz
47-
4848
WORKDIR /configs
4949
ADD x86_64_config /configs/x86_64
5050
ADD arm64_config /configs/arm64
5151

52-
ARG ARCH
53-
ARG CROSS_COMPILE
54-
55-
# Build Linux kernel
56-
WORKDIR /px/src/linux-${KERN_VERSION}
57-
RUN cp /configs/${ARCH} .config
58-
RUN make ARCH=${ARCH} olddefconfig
59-
RUN make ARCH=${ARCH} clean
60-
RUN make ARCH=${ARCH} -j $(nproc) bindeb-pkg LOCALVERSION=-pl
61-
62-
# Extract headers into a tarball
6352
WORKDIR /px
64-
# Unlike the bionic container build, Linux 6.3 and later name the resulting package without the additional -pl suffix.
65-
# This difference stems from changes in the Linux build process itself, not from any modifications on our end.
66-
RUN DEB_ARCH=$(echo ${ARCH} | sed 's/x86_64/amd64/g'); dpkg -x src/linux-headers-${KERN_VERSION}-pl_${KERN_VERSION}-1_${DEB_ARCH}.deb .
6753

68-
# Remove broken symlinks
69-
RUN find usr/src/linux-headers-${KERN_VERSION}-pl -xtype l -exec rm {} +
54+
ADD build_linux_headers.sh /px/build_linux_headers.sh
7055

71-
# Remove uneeded files to reduce size
72-
# Keep only:
73-
# - usr/src/linux-headers-x.x.x-pl/include
74-
# - usr/src/linux-headers-x.x.x-pl/arch/${ARCH}
75-
# This reduces the size by a little over 2x.
76-
RUN rm -rf usr/share
77-
RUN find usr/src/linux-headers-${KERN_VERSION}-pl -maxdepth 1 -mindepth 1 ! -name include ! -name arch -type d \
78-
-exec rm -rf {} +
79-
RUN find usr/src/linux-headers-${KERN_VERSION}-pl/arch -maxdepth 1 -mindepth 1 ! -name $(echo ${ARCH} | sed 's/x86_64/x86/g') -type d -exec rm -rf {} +
80-
RUN tar zcf linux-headers-${ARCH}-${KERN_VERSION}.tar.gz usr
56+
RUN chmod +x /px/build_linux_headers.sh
8157

8258
VOLUME /output
83-
CMD ["sh", "-c", "cp linux-headers-*.tar.gz /output/"]
59+
CMD ["sh", "-c", "/px/build_linux_headers.sh /px ${KERN_VERSION} ${ARCH} ${CROSS_COMPILE} /output"]

tools/docker/linux_headers_image/Dockerfile.bionic

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
3+
usage() {
4+
echo "Usage: $0 [workspace] [kernel_version] [arch] [cross_compile] [output_dir]"
5+
echo "Example: $0 /px 5.4.0-42-generic x86_64 /usr/bin/x86_64-linux-gnu- /output"
6+
}
7+
8+
if [ "$#" -ne 5 ]; then
9+
usage
10+
exit 1
11+
fi
12+
13+
WORKSPACE=$1
14+
KERN_VERSION=$2
15+
ARCH=$3
16+
# shellcheck disable=SC2034
17+
CROSS_COMPILE=$4
18+
OUTPUT_DIR=$5
19+
20+
if [ "${CROSS_COMPILE}" == "false" ]; then
21+
CROSS_COMPILE=""
22+
fi
23+
echo "Cross compile: ${CROSS_COMPILE}"
24+
25+
mkdir -p "${WORKSPACE}"/src
26+
pushd "${WORKSPACE}"/src || exit
27+
28+
KERN_MAJ=$(echo "${KERN_VERSION}" | cut -d'.' -f1);
29+
KERN_MIN=$(echo "${KERN_VERSION}" | cut -d'.' -f2);
30+
wget -nv http://mirrors.edge.kernel.org/pub/linux/kernel/v"${KERN_MAJ}".x/linux-"${KERN_VERSION}".tar.gz
31+
32+
tar zxf linux-"${KERN_VERSION}".tar.gz
33+
34+
pushd linux-"${KERN_VERSION}" || exit
35+
36+
cp /configs/"${ARCH}" .config
37+
make ARCH="${ARCH}" olddefconfig
38+
make ARCH="${ARCH}" clean
39+
40+
LOCALVERSION="-pl"
41+
42+
DEB_ARCH="${ARCH//x86_64/amd64}"
43+
# binary builds are required for non git trees after linux v6.3 (inclusive).
44+
# The .deb file suffix is also different.
45+
TARGET='bindeb-pkg'
46+
DEB_SUFFIX="-1_${DEB_ARCH}.deb"
47+
if [ "${KERN_MAJ}" -lt 6 ] || { [ "${KERN_MAJ}" -le 6 ] && [ "${KERN_MIN}" -lt 3 ]; }; then
48+
TARGET='deb-pkg'
49+
DEB_SUFFIX="${LOCALVERSION}-1_${DEB_ARCH}.deb"
50+
fi
51+
echo "Building ${TARGET} for ${KERN_VERSION}${LOCALVERSION} (${ARCH})"
52+
53+
make ARCH="${ARCH}" -j "$(nproc)" "${TARGET}" LOCALVERSION="${LOCALVERSION}"
54+
55+
popd || exit
56+
popd || exit
57+
58+
# Extract headers into a tarball
59+
dpkg -x src/linux-headers-"${KERN_VERSION}${LOCALVERSION}_${KERN_VERSION}${DEB_SUFFIX}" .
60+
61+
# Remove broken symlinks
62+
find usr/src/linux-headers-"${KERN_VERSION}${LOCALVERSION}" -xtype l -exec rm {} +
63+
64+
# Remove uneeded files to reduce size
65+
# Keep only:
66+
# - usr/src/linux-headers-x.x.x-pl/include
67+
# - usr/src/linux-headers-x.x.x-pl/arch/${ARCH}
68+
# This reduces the size by a little over 2x.
69+
rm -rf usr/share
70+
find usr/src/linux-headers-"${KERN_VERSION}${LOCALVERSION}" -maxdepth 1 -mindepth 1 ! -name include ! -name arch -type d \
71+
-exec rm -rf {} +
72+
find usr/src/linux-headers-"${KERN_VERSION}${LOCALVERSION}"/arch -maxdepth 1 -mindepth 1 ! -name "${ARCH//x86_64/x86}" -type d -exec rm -rf {} +
73+
74+
tar zcf linux-headers-"${ARCH}"-"${KERN_VERSION}".tar.gz usr
75+
76+
cp linux-headers-*.tar.gz "${OUTPUT_DIR}"/

0 commit comments

Comments
 (0)