Files
vm/scripts/build-kernel.sh
zeekayandHanzo Dev d1191359f4 kernel: add x86_64 defconfig and arch-aware build script
Same feature set as the arm64 config (virtio-pci, virtio-console, vsock,
netfilter, cgroup2 + k3s set); the arch section swaps GICv3/PL011 for
PVH direct boot, kvmclock, and ACPI. cloud-hypervisor loads the
uncompressed ELF vmlinux via its PVH entry, so the build strips vmlinux
into Image instead of taking arch/*/boot/Image.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-01 16:08:47 -07:00

87 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
KERNEL_VERSION="${KERNEL_VERSION:-6.12.17}"
KERNEL_MAJOR="${KERNEL_VERSION%%.*}"
DATA_DIR="${HANZO_VM_HOME:-$HOME/.hanzo/vm}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_DIR="$(dirname "$SCRIPT_DIR")"
DEFCONFIG="${REPO_DIR}/kernel/vm_defconfig"
KERNEL_URL="https://cdn.kernel.org/pub/linux/kernel/v${KERNEL_MAJOR}.x/linux-${KERNEL_VERSION}.tar.xz"
BUILD_DIR="${DATA_DIR}/kernel-build"
echo "==> Building custom kernel ${KERNEL_VERSION} for Hanzo VM"
if [ ! -f "$DEFCONFIG" ]; then
echo "ERROR: Defconfig not found at ${DEFCONFIG}"
exit 1
fi
mkdir -p "$DATA_DIR"
if [ ! -d "${BUILD_DIR}/linux-${KERNEL_VERSION}" ]; then
echo " Downloading kernel source..."
mkdir -p "$BUILD_DIR"
curl -sL "$KERNEL_URL" -o "${BUILD_DIR}/linux-${KERNEL_VERSION}.tar.xz"
echo " Extracting..."
tar xf "${BUILD_DIR}/linux-${KERNEL_VERSION}.tar.xz" -C "$BUILD_DIR"
rm -f "${BUILD_DIR}/linux-${KERNEL_VERSION}.tar.xz"
fi
if [ "$(uname -m)" = "x86_64" ] && [ "$(uname -s)" = "Linux" ]; then
echo " Native x86_64 Linux detected, building without Docker"
cd "${BUILD_DIR}/linux-${KERNEL_VERSION}"
cp "${REPO_DIR}/kernel/vm_defconfig_x86" arch/x86/configs/vm_defconfig
make ARCH=x86_64 vm_defconfig
echo " Compiling kernel (this takes a few minutes)..."
make ARCH=x86_64 -j"$(nproc)" vmlinux 2>&1 | tail -5
# cloud-hypervisor direct-boots the uncompressed ELF via its PVH entry.
strip -o "${DATA_DIR}/Image" vmlinux
echo " Kernel built: $(du -h "${DATA_DIR}/Image" | cut -f1)"
elif [ "$(uname -m)" = "aarch64" ] && [ "$(uname -s)" = "Linux" ]; then
echo " Native aarch64 Linux detected, building without Docker"
cd "${BUILD_DIR}/linux-${KERNEL_VERSION}"
cp "$DEFCONFIG" arch/arm64/configs/vm_defconfig
make ARCH=arm64 vm_defconfig
echo " Compiling kernel (this takes a few minutes)..."
make ARCH=arm64 -j"$(nproc)" Image 2>&1 | tail -5
cp arch/arm64/boot/Image "${DATA_DIR}/Image"
echo " Kernel built: $(du -h "${DATA_DIR}/Image" | cut -f1)"
else
echo " Building in Docker (arm64 container)"
docker run --rm \
--platform linux/arm64/v8 \
-v "${DEFCONFIG}:/tmp/vm_defconfig:ro" \
-v "${BUILD_DIR}/linux-${KERNEL_VERSION}:/src:rw" \
-v "${DATA_DIR}:/output" \
debian:trixie-slim /bin/sh -c '
set -e
apt-get update -qq > /dev/null 2>&1
apt-get install -y -qq build-essential bc flex bison libelf-dev \
libssl-dev > /dev/null 2>&1
cd /src
cp /tmp/vm_defconfig arch/arm64/configs/vm_defconfig
make ARCH=arm64 vm_defconfig > /dev/null 2>&1
echo " Compiling kernel (this takes a few minutes)..."
make ARCH=arm64 -j$(nproc) Image 2>&1 | tail -5
cp arch/arm64/boot/Image /output/Image
echo " Kernel built: $(du -h /output/Image | cut -f1)"
'
fi
echo "==> Kernel ready at ${DATA_DIR}/Image"