为 Ubuntu 安装 RT Linux 补丁

Install RT Linux patch for Ubuntu

试图通过修补/安装 RT Linux 修改内核,使我的通用 Ubuntu 实时 Ubuntu,但无法找到直接设置。有人可以帮助完成这些步骤吗?

步骤 0 - 创建工作目录

创建一个工作目录

#Move to working directory
mkdir ~/kernel && cd ~/kernel

第 1 步 - 下载内核和补丁

前往https://www.kernel.org/pub/linux/kernel/ and download a desired version of kernel to ~/kernel. Similarly, go to https://www.kernel.org/pub/linux/kernel/projects/rt/下载与下载的内核版本相同版本的RT补丁。我使用的内核和补丁是 linux-4.9.115.tar.gz 和 patch-4.9.155-rt93.patch.gz。

步骤 2 - 解压缩内核

tar -xzvf linux-4.9.115.tar.gz

步骤 3 - 给内核打补丁

#Move to kernel source directory
cd linux-4.9.115
gzip -cd ../patch-4.9.115-rt93.patch.gz | patch -p1 --verbose

步骤 4 - 启用实时处理 此步骤需要 libncurses-dev

sudo apt-get install libncurses-dev libssl-dev

下一个命令在终端中启动图形菜单以生成配置文件。

make menuconfig

转到该位置并进行相应的更改

##Graphical Menu##

Processor type and features ---> [Enter]
Preemption Model (Voluntary Kernel Preemption (Desktop)) [Enter]
Fully Preemptible Kernel (RT) [Enter] #Select

[Esc][Esc]

Kernel hacking --> [Enter]
Memory Debugging [Enter]
Check for stack overflows #Already deselected - do not select


[Esc][Esc]


[Right Arrow][Right Arrow]

<Save> [Enter]

.config

<Okay> [Enter]

<Exit> [Enter]


[Esc][Esc]


[Right Arrow]
<Exit> [Enter]

步骤 5 - 编译内核

make -j20
sudo make modules_install -j20
sudo make install -j20

第 6 步 - 验证和更新 验证/boot目录下生成initrd.img-4.9.115-rt93、vmlinuz-4.9.115-rt93、config-4.9.115-rt93,更新grub

cd /boot
ls
sudo update-grub

验证在 /boot/grub/grub.cfg 文件

中是否存在包含文本 "menuentry 'Ubuntu, with Linux 4.9.115-rt93'" 的菜单项

要更改 grub 中的默认内核,请将 /etc/default/grub 中的 GRUB_DEFAULT 值编辑为您想要的内核。

注意: 0 是第一个菜单项

7 - 重启并验证

sudo reboot

系统重新启动后,打开终端并使用uname -a检查内核版本,它应该如下所示

Linux abhay-home 4.9.115-rt93 #1 SMP PREEMPT RT Mon May 13 03:32:57 EDT 2019 x86_64 x86_64 x86_64 GNU/Linux

注意: "SMP PREEMPT RT" 验证您的系统是 运行 实时内核。

安装新内核后(就像@Abhay Nayak 发布的那样),我陷入了内核恐慌。 问题是 initrd 映像太大。 我解决了这个问题:

第 1 步 - 剥离内核模块

cd /lib/modules/<new_kernel>
find . -name *.ko -exec strip --strip-unneeded {} +

步骤 2 - 更改 initramfs 压缩

编辑文件/etc/initramfs-tools/initramfs.conf:

COMPRESS=xz

步骤 3 - 更新 initramfs

sudo update-initramfs -u
sudo update-grub2

这是 Ubuntu 19.10 及更高版本,我修补了 Linux 5.4.5 rt 内核补丁,因为 Linux 5.3 -- Ubuntu 19.10 的基础 -- 有没有 rt 内核补丁。

0。创建一个工作目录

# Make dir and move to working directory
$ mkdir ~/kernel && cd ~/kernel

1.下载内核和补丁

https://www.kernel.org/ 下载内核和 rt 补丁。 您可以在下面获得这些:

(内核)https://www.kernel.org/pub/linux/kernel/
(RT 补丁)https://www.kernel.org/pub/linux/kernel/projects/rt/

注意rt补丁和kernel的版本要一致。我使用了 linux-5.4.5.tar.gzpatch-5.4.5-rt3.patch.gz.

如果您在 ~/kernel 上下载这些,请跳过下面并转到第 2 步。

# Move these zip file to ~/kernel
$ cd ~/Download
$ mv linux-5.4.5.tar.gz patch-5.4.5-rt3.patch.gz ~/kernel/.

2。提取内核源代码并修补 rt kernel

# Extract kernel sources
$ cd ~/kernel
$ tar xvzf linux-5.4.5.tar.gz

# Patch rt kernel
$ cd linux-5.4.5
$ gzip -cd ../patch-5.4.5-rt3.patch.gz | patch -p1 --verbose {}

3。安装所需的包

要使用 menuconfig GUI,需要 libncurses-devflexbison 编译内核时需要

# For using gui
$ sudo apt install libncurses-dev libssl-dev

# For compiling kernel
$ sudo apt install flex bison

4.为 RT

配置内核
$ make menuconfig

并进入菜单配置 GUI。

# Make preemptible kernel setup
General setup ---> [Enter]
Preemption Model (Voluntary Kernel Preemption (Desktop)) [Enter]
Fully Preemptible Kernel (RT) [Enter] #Select

# Select <SAVE> and <EXIT>
# Check .config file is made properly

请注意,GUI 配置上不再有 Check for stack overflows 选项。您可以通过搜索“溢出”来查看它。在图形菜单上键入 /overflow

5.编译内核

$ make -j20
$ sudo make modules_install -j20
$ sudo make install -j20

6.使内核图像更轻

正如@mrRo8o7 之前所说,大 initrd 图像可能会发生内核崩溃。所以你可以通过以下方式解决这个问题:

# Strip unneeded symbols of object files
$ cd /lib/modules/5.4.5-rt3  # or your new kernel
$ sudo find . -name *.ko -exec strip --strip-unneeded {} +

# Change the compression format
$ sudo vi /etc/initramfs-tools/initramfs.conf
# Modify COMPRESS=lz4 to COMPRESS=xz (line 53)

COMPRESS=xz 

[:wq]

然后更新 initramfs

$ sudo update-initramfs -u

7.验证并更新 grub

验证该目录并更新 grub。

# Make sure that initrd.img-5.4.5-rt3, vmlinuz-5.4.5-rt3, and config-5.4.5-rt3 are generated in /boot
$ cd /boot
$ ls

# Update grub
$ sudo update-grub

8.重启并验证

$ sudo reboot

# After the reboot
$ uname -a

然后你可以检查你的新内核版本

Linux [PROMPT] 5.4.5-rt3 …

PREEMPT_RT 补丁已慢慢流向 Linux 主线 从 Ubuntu 22.04 开始 它可以通过一些简单的命令激活,如 here 所述。


对于 Ubuntu 的早期版本,请注意您还可以从官方 Debian packages web page 下载 某些 PREEMPT_RT 内核 的 Debian 软件包。如果您不关心精确的内核版本,我宁愿选择 re-compiling 您自己的内核! 下载 Debian 文件后,您可以使用

从下载目录安装它
$ sudo dpkg -i linux-image-rt-amd64_5.10.106-1_amd64.deb
$ sudo apt-get install -f

其中 linux-image-rt-amd64_5.10.106-1_amd64.deb 显然取决于您下载的版本。重新启动到新安装的内核(验证当前内核名称确实包含 rt$ uname -r),你应该准备好了!


如果您真的want/have要编译内核,您可能必须.config$ make oldconfig 之后(或 $ make menuconfig

CONFIG_SYSTEM_TRUSTED_KEYS=""
CONFIG_SYSTEM_REVOCATION_KEYS=""

否则,按照其他评论员给出的步骤,编译过程可能会失败并出现神秘的错误消息。

我最近写了一份更详细的指南,介绍了为此所需的步骤here. In the process I have also written two scripts that allow you to install it from existing Debian packages or compile the kernel yourself with a graphic user interface. Have a look at the Github repository