User Tools

Site Tools

한국어

comfilepi:compile_a_real-time_kernel:index

This is an old revision of the document!


Compile and Deploy a Real-Time Kernel to the ComfilePi

The generic kernel employed the the default ComfilePi operating system is not a real-time kernel. That is fine for many, if not most, use cases. For those use cases that may require real-time operation, we provide the following procedure.

In addition to compiling and deploying a real-timer kernel, other OS configurations may be necessary to improve the realtime performance.

Please note that COMFILE Technology does not maintain the Raspberry Pi kernel or the Linux real-time patches, so those wishing to employ this capability should assume responsibility for it.

Kernel Versioning

The Raspberry Pi Foundation maintains the kernel for Raspberry Pi-based products at https://github.com/raspberrypi/linux. The real-time kernel patches are maintained by a different group at https://cdn.kernel.org/pub/linux/kernel/projects/rt/. Compile errors and/or patch errors can arise if the 3 digit version number of the Raspberry Pi kernel and the real time patch do not match. The Raspberry Pi Foundation and the real-time patch maintainers do no coordinate their releases, so one must search the kernel's Makefile version history for a “Linux x.y.z” commit that matches the real-time kernel patch version, then use the “Merge remote-tracking…” commit that follows it. At the time of writing this, the latest such commit for the 5.10.y branch was here:

Build Script

Given the versioning limitations explained above, the following shell script cross-compiles kernel version 6.1.26. It will download all necessary software and source code, cross-compile the real-time kernel, and package the resulting assests for deployment to a ComfilePi panel PC. It was tested on a Debian instance of Windows Subsystem for Linux.

#!/bin/bash
 
# You may need to install the following packages
# apt update -y
# apt install -y wget git bc bison flex libssl-dev make libc6-dev libncurses5-dev crossbuild-essential-$ARCH
 
KERNEL_VERSION=6.1
RT_PATCH_VERSION=$KERNEL_VERSION.26-rt8
COMMIT_HASH=dbcb82357ef21be47841ba39d117b626d715af31
DEFCONFIG=bcm2711_defconfig
ARCH=arm64
TARGET=aarch64-linux-gnu
KERNEL_NAME=kernel8
 
KERNEL_BRANCH=rpi-$KERNEL_VERSION.y
COMPILER=$TARGET-
 
# All work will be done in $PWD/rtkernel.  The final result will be packaged as $PWD/rtkernel/result
WORK_DIR=`pwd`/rtkernel
rm -rf $WORK_DIR
mkdir $WORK_DIR
cd $WORK_DIR
 
# download the kernel
git clone --depth=1 --branch $KERNEL_BRANCH https://github.com/raspberrypi/linux
 
# download the RT patch
wget http://cdn.kernel.org/pub/linux/kernel/projects/rt/$KERNEL_VERSION/older/patch-$RT_PATCH_VERSION.patch.gz
gunzip patch-$RT_PATCH_VERSION.patch.gz
 
# Need this to get precisely the correct kernel version
cd linux
git fetch origin $COMMIT_HASH
git reset --hard $COMMIT_HASH
 
# Patch the kernel
patch -p1 < ../patch-$RT_PATCH_VERSION.patch
 
 # Prepare configuration
make ARCH=$ARCH CROSS_COMPILE=$COMPILER $DEFCONFIG
 
 # Configure to use the "Fully Preemptible Kernel (Real-Time)" kernel
./scripts/config --set-val CONFIG_PREEMPT_RT y
./scripts/config --set-val CONFIG_RCU_BOOST y
./scripts/config --set-val CONFIG_RCU_BOOST_DELAY 500
./scripts/config --set-val CONFIG_RCU_NOCB_CPU y
./scripts/config --set-val CONFIG_NO_HZ_FULL y
./scripts/config --set-val CONTEXT_TRACKING_USER_FORCE n
./scripts/config --set-val RCU_NOCB_CPU_DEFAULT_ALL n
./scripts/config --set-val RCU_NOCB_CPU_CB_BOOST y
 
# Compile
make -j32 ARCH=$ARCH CROSS_COMPILE=$COMPILER Image.gz modules dtbs
 
# copy assets to the root file system
make ARCH=$ARCH CROSS_COMPILE=$COMPILER INSTALL_MOD_PATH=modules_to_install modules_install
 
 # Out of linux, out of work_dir
cd ../../
 
# copy assets to the $PROJECT_DIR/result directory
RESULT_DIR=$WORK_DIR/result
cp "${WORK_DIR}/linux/arch/$ARCH/boot/Image.gz" "${RESULT_DIR}/boot/$KERNEL_NAME.img"
cp -r ${WORK_DIR}/linux/modules_to_install/lib/* "${RESULT_DIR}/lib/"
cp ${WORK_DIR}/linux/arch/$ARCH/boot/dts/broadcom/*.dtb "${RESULT_DIR}/boot/"
cp ${WORK_DIR}/linux/arch/$ARCH/boot/dts/overlays/*.dtb* "${RESULT_DIR}/boot/overlays/"
cp "${WORK_DIR}/linux/arch/$ARCH/boot/dts/overlays/README" "${RESULT_DIR}/boot/overlays/"

Deploy to the ComfilePi

After running the build script above, the rtkernel/result directory will contain 2 directories: fat32 and ext4. The fat32 directory contains assets that must be deployed to the ComfilePi's FAT32 partition (i.e. the /boot directory) and the ext4 directory contains assets that must be deployed to the ComfilePi's operating system root partition (i.e. the / directory).

# Package up the result into a single file
tar czf rtkernel.tgz rtkernel/result/*
 
# transfer the file to the ComfilePi
scp rtkernel.tgz pi@192.168.0.5:
# Unpack the files
tar xzf rtkernel.tgz
 
cd rtkernel/result
 
# Copy files to the /boot directory
sudo cp fat32/zImage /boot/kernel7.img
sudo cp fat32/*.dtb /boot/
sudo cp fat32/overlays/*.dtb* /boot/overlays/
sudo cp fat32/overlays/README /boot/overlays/
 
# Copy files to the / directory
sudo cp -rd ext4/lib/* /lib

After rebooting, verify that the new kernel is loaded and running

$uname -a
Linux raspberrypi 5.10.35-rt39-v7+ #1 SMP PREEMPT_RT Thu Jun 24 11:13:50 KST 2021 armv7l GNU/Linux

References

comfilepi/compile_a_real-time_kernel/index.1683854812.txt.gz · Last modified: 2023/05/12 10:26 by COMFILE Technology