Bookworm armhf cross compile kernel docker

From embeddedTS Manuals

To compile the kernel you should use the Debian cross compile docker from a linux workstation.

We can use a shell script to enter the docker, and compile the kernel into a tar that can be distributed to the board. For example:

mkdir ~/Projects/kernel-5.10/
cd ~/Projects/kernel-5.10/
git clone https://github.com/embeddedTS/linux-lts.git -b linux-5.10.y linux

Make a ~/Projects/kernel-5.10/build.sh with:

#!/bin/bash -e

if [ ! -e "/.dockerenv" ]; then
	exec docker-debian-bookworm "$0 $@";
fi

export ARCH=arm
export CROSS_COMPILE="arm-linux-gnueabihf-"
export LOADADDR=0x10008000

cd linux

if [ "$#" != "0" ]; then
        "$@"
        exit 0;
fi

## Make any changes in "make menuconfig" or driver modifications, then compile
make -j"$(nproc --all)" all uImage

# Install to a temporary directory
TEMPDIR=$(mktemp -d)
export TEMPDIR
mkdir "$TEMPDIR"/boot/
cp arch/arm/boot/uImage  "$TEMPDIR"/boot/uImage
cp arch/arm/boot/zImage  "$TEMPDIR"/boot/zImage
cp arch/arm/boot/dts/imx6*-ts*.dtb "$TEMPDIR"/boot/
INSTALL_MOD_PATH="${TEMPDIR}/usr/" make modules_install
cd ../

fakeroot sh -c "chmod 755 $TEMPDIR;
	chown -R root:root $TEMPDIR;
	tar cjf kernel.tar.bz2 -C $TEMPDIR .;
	rm -rf $TEMPDIR";

Make this executable:

chmod a+x ~/Projects/kernel-5.10/build.sh

The kernel can be compiled by running these two commands:

./build.sh make ts_defconfig
./build.sh # No arguments runs the build and packages the kernel

# This will output the build in the current directory as kernel.tar.bz2

Any arguments to this script will be executed in the linux path. For example:

./build.sh make ts_defconfig
# Customize the kernel config
./build.sh make menuconfig
# Test a compile (does not package the install)
./build.sh make
# Get a shell prompt in the kernel sources to run commands manually:
./build.sh bash

The kernel.tar.bz2 from this build script can be extracted from a board, or over an image to install this kernel. For example, copy this to the SBC and run:

tar -xhf kernel.tar.bz2 -C /

From a workstation this can similarly be used to install over a mounted SD image:

sudo mount /dev/sdc1 /mnt/sd/
sudo tar -xhf kernel.tar.bz2 -C /mnt/sd/
sudo umount /mnt/sd/