本教程从在 Intel x86_64 上的干净 Ubuntu 20.04.1 安装开始。实际内核是 5.4.0-54-generic,但我们将安装最新的稳定 RT_PREEMPT 版本。构建内核至少需要 30GB 的可用磁盘空间。
请查看 https://wiki.linuxfoundation.org/realtime/start 以获取最新的稳定版本,在撰写本文时为“最新稳定版本 5.4-rt”。如果我们点击链接 https://cdn.kernel.org/pub/linux/kernel/projects/rt/5.4/ ,我们会得到确切的版本。目前是 patch-5.4.78-rt44.patch.gz。
我们在主目录中创建一个目录
mkdir ~/kernel
并切换到它与
cd ~/kernel
我们可以使用浏览器访问 https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/,看看版本是否在那里。您可以从该网站下载并手动将其从 /Downloads 移动到 /kernel 文件夹,或者通过右键单击链接使用“复制链接位置”使用 wget 下载。示例:
wget https://mirrors.edge.kernel.org/pub/linux/kernel/v5.x/linux-5.4.78.tar.gz
用...解压缩它
tar -xzf linux-5.4.78.tar.gz
下载与我们刚刚在 http://cdn.kernel.org/pub/linux/kernel/projects/rt/5.4/ 下载的内核版本匹配的 rt_preempt 补丁。
wget http://cdn.kernel.org/pub/linux/kernel/projects/rt/5.4/older/patch-5.4.78-rt44.patch.gz
用...解压它
gunzip patch-5.4.78-rt44.patch.gz
然后切换到 linux 目录
cd linux-5.4.78/
然后切换到 linux 目录
cd linux-5.4.78/
并使用实时补丁修补内核
patch -p1 < ../patch-5.4.78-rt44.patch
我们只想使用我们 Ubuntu 安装的配置,所以我们获取 Ubuntu 配置与
cp /boot/config-5.4.0-54-generic .config
打开软件和更新。在 Ubuntu 软件菜单中勾选“源代码”框。
我们需要一些工具来构建内核,请使用以下命令安装它们
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
要启用所有 Ubuntu 配置,我们只需使用
yes '' | make oldconfig
然后我们需要在内核中启用 rt_preempt。我们称之为
make menuconfig
并设置以下内容
# 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
保存并退出 menuconfig。现在我们将构建内核,这将花费相当长的时间。(在现代 CPU 上需要 10-30 分钟)
make -j `nproc` deb-pkg
构建完成后检查 debian 包
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
然后我们安装所有内核 debian 包
sudo dpkg -i ../*.deb
现在应该安装实时内核。重新启动系统并检查新内核版本。
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
附录
Building a real-time Linux kernel on Ubuntu 24.04 involves several steps. Here's a general guide to help you get started:
1. Install Necessary Packages
First, ensure you have the required packages for building the kernel:
sudo apt update
sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev
2. Download the Kernel Source
Download the latest real-time kernel source from the official Linux kernel website or use the Ubuntu source:
wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.8.tar.xz
tar -xf linux-6.8.tar.xz
cd linux-6.8
3. Apply the Real-Time Patches
Real-time patches are essential for enabling real-time capabilities. You can download the PREEMPT_RT patches and apply them:
wget https://cdn.kernel.org/pub/linux/kernel/projects/rt/6.8/patch-6.8-rt1.patch.xz
xzcat patch-6.8-rt1.patch.xz | patch -p1
4. Configure the Kernel
Configure the kernel with the necessary options for real-time support:
make menuconfig
In the menu, navigate to:
Processor type and features
Enable
Preemption Model (Fully Preemptible Kernel)
5. Build the Kernel
Compile the kernel and modules:
make -j$(nproc)
make modules_install
make install
6. Update Bootloader
Update your bootloader to include the new kernel:
sudo update-grub
7. Reboot
Reboot your system to use the new real-time kernel:
sudo reboot
Additional Resources
For more detailed instructions and tuning guidelines, you can refer to the official Ubuntu documentation on real-time kernels¹².
If you encounter any issues or need further assistance, feel free to ask!
¹: Ubuntu Real-Time Kernel Technical Guide
²: Canonical Real-Time Ubuntu 24.04 LTS Release
Are you planning to use this for a specific project or application?
Source: Conversation with Copilot, 2024/7/27
(1) Technical deep-dive into a real-time kernel | Ubuntu. https://ubuntu.com/blog/real-time-kernel-technical.
(2) Canonical releases Real-time Ubuntu 24.04 LTS | Ubuntu. https://ubuntu.com/blog/real-time-24-04.
(3) Discover real-time kernel on Ubuntu 24.04 LTS and Raspberry Pi. https://www.sebae.net/videos/discover-real-time-kernel-on-ubuntu-24-04-lts-and-raspberry-pi/.
(4) Discover real-time kernel on Ubuntu 24.04 LTS and Raspberry Pi. https://ubuntu.com/engage/real-time-24-webinar.
(5) Real-Time Kernel Now Available On Ubuntu 24.04 LTS - Phoronix. https://www.phoronix.com/news/Ubuntu-24.04-LTS-Real-Time.