#!/bin/bash # SPDX-License-Identifier: Apache-2.0 set -euo pipefail ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../.." && pwd) MKOSI_DIR="$ROOT/os/mkosi" # shellcheck source=/dev/null source "$MKOSI_DIR/versions.env" BUILD_DIR=${1:?build directory required} STAGING=${2:?staging tree required} DEVEL_STAGING=${3:?kernel-devel staging tree required} BUILD_DIR=$(realpath -m "$BUILD_DIR") STAGING=$(realpath -m "$STAGING") DEVEL_STAGING=$(realpath -m "$DEVEL_STAGING") JOBS=${JOBS:-$(nproc)} export KBUILD_BUILD_TIMESTAMP="@${SOURCE_DATE_EPOCH:?SOURCE_DATE_EPOCH required}" export KBUILD_BUILD_USER=dstack KBUILD_BUILD_HOST=reproducible export KBUILD_BUILD_VERSION=1 export KCONFIG_NOTIMESTAMP=1 # Neither the source nor output directory may leak into DWARF/BTF or module # metadata: repro-check deliberately builds in two differently named trees. kernel_map="-fdebug-prefix-map=$BUILD_DIR=/usr/src/linux -fmacro-prefix-map=$BUILD_DIR=/usr/src/linux" export KCFLAGS="${KCFLAGS:-} $kernel_map" export KAFLAGS="${KAFLAGS:-} $kernel_map" export KCPPFLAGS="${KCPPFLAGS:-} $kernel_map" mkdir -p "$BUILD_DIR/downloads" "$BUILD_DIR/kernel-build" "$STAGING" "$DEVEL_STAGING" # pahole 1.25 produces a different BTF type order when its encoder runs with # Kbuild's parallel job count. Keep compilation parallel, but serialize BTF # encoding so vmlinux and bzImage are byte-for-byte reproducible. pahole_wrapper="$BUILD_DIR/pahole-reproducible" cat > "$pahole_wrapper" <<'EOF' #!/bin/bash set -euo pipefail args=() for arg; do # dwarves 1.25's parallel and optimized-function encoders both depend on # unstable traversal order. Neither is needed for BTF correctness. [[ $arg == -j* || $arg == --btf_gen_optimized ]] || args+=("$arg") done exec pahole -j1 "${args[@]}" EOF chmod 0755 "$pahole_wrapper" tarball="$BUILD_DIR/downloads/linux-$KERNEL_VERSION.tar.xz" if [[ ! -f $tarball ]]; then curl --fail --location --retry 3 -o "$tarball.tmp" \ "https://cdn.kernel.org/pub/linux/kernel/v${KERNEL_VERSION%%.*}.x/linux-$KERNEL_VERSION.tar.xz" mv "$tarball.tmp" "$tarball" fi echo "$KERNEL_SHA256 $tarball" | sha256sum --check --status || { echo "kernel source checksum mismatch" >&2; exit 1; } src="$BUILD_DIR/linux-$KERNEL_VERSION" rm -rf "$src" "$BUILD_DIR/kernel-build" "$STAGING/usr/lib/modules/$KERNEL_VERSION-dstack" \ "$DEVEL_STAGING/usr/src" tar -C "$BUILD_DIR" --no-same-owner -xf "$tarball" for patch in \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0001-x86-tdx-select-dma-direct-remap.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0002-acpi-sandbox-block-aml-systemmemory-ram-access.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0004-dma-pool-free-atomic-pool-pages-by-physical-address.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0006-swiotlb-free-dynamic-pools-from-process-context.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0007-sched-smp-batch-wake-q-ipis-in-tdx-guests.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0008-x86-kvm-runtime-pv-single-target-ipi-in-tdx-guests.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0009-mm-page-alloc-keep-accepted-reserve-before-watermarks.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0010-x86-tdx-convert-swiotlb-to-shared-after-smp-bringup.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0011-ovl-handle-idmapped-mounts-in-create-object-and-tmpfile.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0012-ovl-handle-idmapped-mounts-in-permission.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0013-ovl-handle-idmapped-mounts-in-setattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0014-ovl-handle-idmapped-mounts-in-getattr.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0015-ovl-handle-idmapped-mounts-in-set-acl.patch" \ "$ROOT/os/yocto/layers/meta-dstack/recipes-kernel/linux/files/0016-ovl-allow-idmapping-overlay-mounts.patch"; do patch -d "$src" -p1 --fuzz=0 < "$patch" done make -C "$src" O="$BUILD_DIR/kernel-build" PAHOLE="$pahole_wrapper" x86_64_defconfig "$src/scripts/kconfig/merge_config.sh" -m -O "$BUILD_DIR/kernel-build" \ "$BUILD_DIR/kernel-build/.config" "$MKOSI_DIR/components/kernel/kernel.config" make -C "$src" O="$BUILD_DIR/kernel-build" PAHOLE="$pahole_wrapper" olddefconfig "$ROOT/os/common/scripts/check-kernel-config.sh" "$BUILD_DIR/kernel-build/.config" \ "$MKOSI_DIR/components/kernel/kernel.config" "$ROOT/os/common/scripts/check-lxc-kernel-config.sh" "$BUILD_DIR/kernel-build/.config" make -C "$src" O="$BUILD_DIR/kernel-build" PAHOLE="$pahole_wrapper" -j"$JOBS" bzImage modules make -C "$src" O="$BUILD_DIR/kernel-build" \ PAHOLE="$pahole_wrapper" INSTALL_MOD_PATH="$STAGING" modules_install # Debian is usr-merged: /lib is a symlink. Normalize modules into /usr/lib so # ExtraTrees never tries to replace that symlink with a directory. if [[ -d $STAGING/lib/modules ]]; then mkdir -p "$STAGING/usr/lib/modules" cp -a "$STAGING/lib/modules/." "$STAGING/usr/lib/modules/" rm -rf "${STAGING:?}/lib" fi install -Dm0644 "$BUILD_DIR/kernel-build/arch/x86/boot/bzImage" \ "$STAGING/usr/lib/modules/$KERNEL_VERSION-dstack/vmlinuz" install -Dm0644 "$BUILD_DIR/kernel-build/.config" \ "$STAGING/usr/lib/modules/$KERNEL_VERSION-dstack/config" # Install the external-module build tree where Debian's linux-headers package # puts it, using the same upstream helper `make bindeb-pkg` uses. mkosi.finalize # also archives it as a release artifact. release=$(cat "$BUILD_DIR/kernel-build/include/config/kernel.release") headers="$DEVEL_STAGING/usr/src/linux-headers-$release" make -C "$src" O="$BUILD_DIR/kernel-build" run-command \ KBUILD_RUN_COMMAND="$src/scripts/package/install-extmod-build $headers" install -m0644 "$BUILD_DIR/kernel-build/.config" "$headers/.config" # A -fverbose-asm intermediate that records the absolute build path, which no # prefix map rewrites. Only its generated .h is needed. rm -f "$headers/scripts/mod/devicetable-offsets.s" # Relative, so the link also resolves when the rootfs is inspected in place. ln -sfn "../../../src/linux-headers-$release" \ "$STAGING/usr/lib/modules/$release/build" rm -f "$STAGING/usr/lib/modules/$release/source" find "$STAGING" "$DEVEL_STAGING" -print0 | \ xargs -0r touch --no-dereference --date="@$SOURCE_DATE_EPOCH"