Compile a real-time Linux kernel for ROS2

This tutorial was performed on a freshly installed clean Ubuntu 20.04.1 running on Intel x86_64. At the same time, I also tested it on Ubuntu 18.04, and it can be installed smoothly. The actual kernel is 5.4.0-54-generic, but we install the latest stable RT_PREEMPT version. In order to build this kernel, you need at least 30GB of free disk space.

Check out https://wiki.linuxfoundation.org/realtime/start to see which is the latest stable version, which at the time of writing is "Latest Stable Version 5.4-rt". If we click on the link link , we can get the specific version. Currently patch-5.4.78-rt44.patch.gz.

eclipse-1

We create a folder,

mkdir ~/kernel

Then, switch to this folder:

cd ~/kernel

We can first check https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/ through the browser to confirm whether this version exists. You can download it directly from the website and put it into /kernel manually, or via wget, e.g.:

wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.78.tar.gz

unzip it:

tar -xzf linux-5.4.78.tar.gz

Download the rt_preempt patch that matches the kernel we just downloaded from http://cdn.kernel.org/pub/linux/kernel/projects/rt/5.4/.

wget http://cdn.kernel.org/pub/linux/kernel/projects/rt/5.4/older/patch-5.4.78-rt44.patch.gz

Unzip it:

gunzip patch-5.4.78-rt44.patch.gz

Then enter the linux folder:

cd linux-5.4.78/

Then patch the kernel with live patch:

patch -p1 < ../patch-5.4.78-rt44.patch

We simply use the config file of the Ubuntu we installed, so we get the Ubuntu config file in the following way:

cp /boot/config-5.4.0-54-generic .config

Open the software and update (Software & Updates), tick the 'Source code' selection box in the Ubuntu software menu.
We need some tools to compile the kernel, install them by:

sudo apt-get build-dep linux
sudo apt-get install libncurses-dev flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf fakeroot

To drive all Ubuntu configurations, we simply use:

yes '' | make oldconfig

Then we need to enable rt_preempt in the kernel, we run:

make menuconfig

Then make the following settings:

# Enable CONFIG_PREEMPT_RT
 -> General Setup
  -> Preemption Model (Fully Preemptible Kernel (Real-Time))
   (X) Fully Preemptible Kernel (Real-Time)

# Enable CONFIG_HIGH_RES_TIMERS
 -> General setup
  -> Timers subsystem
   [*] High Resolution Timer Support

# Enable CONFIG_NO_HZ_FULL
 -> General setup
  -> Timers subsystem
   -> Timer tick handling (Full dynticks system (tickless))
    (X) Full dynticks system (tickless)

# Set CONFIG_HZ_1000 (note: this is no longer in the General Setup menu, go back twice)
 -> Processor type and features
  -> Timer frequency (1000 HZ)
   (X) 1000 HZ

# Set CPU_FREQ_DEFAULT_GOV_PERFORMANCE [=y]
 ->  Power management and ACPI options
  -> CPU Frequency scaling
   -> CPU Frequency scaling (CPU_FREQ [=y])
    -> Default CPUFreq governor (<choice> [=y])
     (X) performance

Save and exit the configuration menu. Now, we're going to build the kernel, which will take some time. (The current mainstream CPU is about 10-30 minutes).

make -j `nproc` deb-pkg

Note, I encountered an error when compiling, and it took me a long time to find the problem. If you also encounter the problem, please scroll to the end to see the problem and the solution. I hope it can help you.
After compiling, check out the debian package:

ls ../*deb
../linux-headers-5.4.78-rt41_5.4.78-rt44-1_amd64.deb  ../linux-image-5.4.78-rt44-dbg_5.4.78-rt44-1_amd64.deb
../linux-image-5.4.78-rt41_5.4.78-rt44-1_amd64.deb    ../linux-libc-dev_5.4.78-rt44-1_amd64.deb

Then we install all debian packages.

sudo dpkg -i ../*.deb

Now the live kernel should be installed, reboot the system and check for the new kernel version.

sudo reboot
uname -a
Linux ros2host 5.4.78-rt44 #1 SMP PREEMPT_RT Fri Nov 6 10:37:59 CET 2020 x86_64 xx

problem solved

Question one

If you encounter the following error:

make[1]: *** No rule to make target 'debian/canonical-certs.pem', needed by 'certs/x509_certificate_list'.  Stop.
make: *** [Makefile:1809: certs] Error 2

The solution is:

In your kernel configuration file you will find this line:

CONFIG_SYSTEM_TRUSTED_KEYS="debian/canonical-certs.pem"

Change it to this:

CONFIG_SYSTEM_TRUSTED_KEYS=""

The most direct way is to open the .config file with an editor and modify it manually. If you want to modify through the command line, you can refer to the following command:

Depending on your source structure you might be able to do it via command line. Examples:

scripts/config --disable SYSTEM_TRUSTED_KEYS

or

scripts/config --set-str SYSTEM_TRUSTED_KEYS ""

question two

If the kernel does not change after installation and restart, it may be that grup will select a higher version kernel by default. Therefore, you can modify the default kernel used at startup by installing grub-customizer.

The installation command for 20.04 is:

sudo apt install grub-customizer

Because it has been included in the default software source.

The installation of 18.04 is a little more troublesome, and the following operations are required:

sudo add-apt-repository ppa:danielrichter2007/grub-customizer
sudo apt-get update
sudo apt-get install grub-customizer

After opening grub-customizer, select general settings, default items, and select rt kernel.

question three

If you encounter the situation that the rt kernel cannot be started, first check whether it shows that it cannot be verified.

The solution is: enter the BIOS, and then close the security boot. Then restart the computer.

Guess you like

Origin blog.csdn.net/weixin_42499608/article/details/122383329