The rt kernel I tested at this moment is 5.15.21-rt30 (Feb 2022).
Follow a community contributed tutorial to install the latest stable RT_PREEMPT version https://docs.ros.org/en/foxy/Tutorials/Building-Realtime-rt_preempt-kernel-for-ROS-2.html
Open Software & Updates. in the Ubuntu Software menu tick the ‘Source code’ box.
Install dependencies:
sudo apt-get update
sudo apt-get build-dep linux
sudo apt-get install build-essential bc curl ca-certificates gnupg2 lsb-release libncurses-dev flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf fakeroot
sudo apt update
sudo apt install zstd
cd ~
mkdir rt_kernel
cd rt_kernel
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.15.21.tar.gz
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.15.21.tar.sign
wget https://cdn.kernel.org/pub/linux/kernel/projects/rt/5.15/older/patch-5.15.21-rt30.patch.gz
wget https://cdn.kernel.org/pub/linux/kernel/projects/rt/5.15/older/patch-5.15.21-rt30.patch.sign
gunzip linux-5.15.21.tar.gz
# tar -xzf linux-5.15.21.tar.gz
gunzip patch-5.15.21-rt30.patch.gz
Verify the kernel files integrity
gpg2 --verify linux-*.tar.sign
gpg2 --verify patch-*.patch.sign
tar xf linux-*.tar
cd linux-*/
patch -p1 < ../patch-*.patch
cp /boot/config-$(uname -r) .config
yes '' | make oldconfig
make menuconfig
apply these modifications:
- general setup / preemption model
- Fully preemptible Kernel (Real-time)
- general setup / Timers subsystem
- High Resolution Timer Support
- general setup / Timers subsystem / Timer tick handling
- Full dynticks system (tickless)
- Processor type and features / Timer frequency
- 1000 Hz
- Power management and ACPI options
- CPU Frequency scaling, CPU Frequency scaling (CPU_FREQ [=y]) -> Default CPUFreq governor ( [=y]) (X) performance
- Cryptographic API* > Certificates for signature checking (at the very bottom of the list) > Provide system-wide ring of trusted keys > Additional X.509 keys for default system keyring
- Remove the “debian/canonical-certs.pem” from the prompt and press Ok.
save and exit.
make -j$(nproc) deb-pkg
or
make -j $(nproc)
sudo make bzImage
sudo make INSTALL_MOD_STRIP=1 modules_install -j $(nproc)
sudo make install
According to the post by cacao:
the
INSTALL_MOD_STRIP
is important - it shrinks the initial ramdisk size by ~90%. In some cases, when not used, the initial ramdisk would not load and the patched kernel would not boot.
If you see these errors:
- if you see this error:
CONFIG_X86_X32 enabled but no binutils support
, changeconfig_x86_x32
ton
sed: can't read modules.order: No such file or directory
- set
CONFIG_SYSTEM_TRUSTED_KEY=""
- set
CONFIG_SYSTEM_REVOCATION_KEYS=""
- set
Missing file: arch/x86/boot/bzImage
, you need to runsudo make bzImage
beforemodules_install
- if you see this error:
bin/sh: 1: zstd: not found
, you need to install Zstandard bysudo apt install zstd
Edit /etc/default/grub file Change GRUB_DEFAULT=0 to GRUB_DEFAULT=saved. Add GRUB_SAVEDEFAULT=true
According to https://frankaemika.github.io/docs/installation_linux.html#setting-up-the-real-time-kernel:
sudo addgroup realtime
sudo usermod -a -G realtime $(whoami)
Afterwards, add the following limits to the realtime group in /etc/security/limits.conf
:
@realtime soft rtprio 99
@realtime soft priority 99
@realtime soft memlock 102400
@realtime hard rtprio 99
@realtime hard priority 99
@realtime hard memlock 102400