Linux打实时补丁RT

一、安装环境

  • ubantu20.04(arrch64架构)

  • 内核版本:4.19.232(uname -a)

示例图片

二、内核和补丁下载

1、内核下载

下载4.19.232的内核版本:Index of /pub/linux/kernel/v4.x/

示例图片

2、补丁下载

必须与内核版本完全一致:Index of /pub/linux/kernel/projects/rt/4.19/older/

示例图片

三、配置Linux内核

1、新建文件夹

1
2
mkdir rt_kernal
cd rt_kernal

2、将内核和补丁移至该目录下

3、解压内核

1
2
3
xz -cd linux-4.19.232.tar.xz | tar xvf -

tar -xzvf linux-4.4.206.tar.gz

4、将补丁移至linux-4.19.232目录下,然后解压

1
2
3
cp -r patch-4.19.232-rt104.patch.gz ./linux-4.19.232
cd linux-4.19.232
gunzip patch-4.19.232-rt104.patch.gz

5、打补丁

1
sudo patch -p1 < patch-4.19.232-rt104.patch

四、配置内核

1、在boot文件夹下可以找到与当前内核版本匹配的 config 文件,将其拷贝到当前目录~/rt_kernal/linux-4.19.232 下并命名为 .config

1
sudo cp /boot/config-5.4.0-193-generic .config

2、安装相关依赖包(图形化配置)

1
2
sudo apt-get install libncurses-dev bison flex bc libelf-dev
sudo apt-get install libssl-dev

2.1、注释.config文件下的内容

1
直接修改 .config 文件,把CONFIG_MODULE_SIG_ALL,CONFIG_MODULE_SIG_KEY和CONFIG_SYSTEM_TRUSTED_KEYS三项注释掉,编译时系统会自动生成一次性密钥来加密,另外记得把CONFIG_DEBUG_INFO=y去掉,不然新内核带debug信息超大

3、开始配置

1
2
3
4
5
6
//X86
make menuconfig
//ARM
make ARCH=arm64 menuconfig

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig(交叉编译)

进入 > General setup > Preemption Model

选择 (X) Fully Preemptible Kernel (RT) ,保存配置文件

4、编译RTLinux内核

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
sudo make  -j4

//清理内核编译环境
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- clean

//编译
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j8


//3、生成image文件和dtbs文件
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- defconfig -j8
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j$(nproc) Image dtbs modules

//4、生成dpk文件(推荐)
make -j12 deb-pkg

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- -j8 deb-pkg

5、安装成功测试

1
2
sudo make modules_install -j4  (编译好的模块拷贝到系统目录下(一般是/lib/modules/))
sudo make install -j4(安装内核二进制映像, 生成并安装boot初始化文件系统映像文件)

5.1、安装dpkg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 安装新内核
sudo dpkg -i *.deb

# 更新grub
sudo update-grub

//1、出现问题
dpkg -l|grep linux-image //检查已安装的内核版本

//2、修改grub引导界面参数
vim /etc/default/grub
# 显示grub菜单
GRUB_TIMEOUT_STYLE=menu
# 0代表立即进系统,不会显示menu,这里设置成menu即可
# -1代表一直显示menu,直到用户做出选择
GRUB_TIMEOUT=-1

//3、更新修改的配置文件
sudo update-grub

//4、重启电脑
reboot

6、移植至开发板

1
boot文件夹下的

7、复制内核文件到设备中

1
2
cp arch/ arm64/ boot/1mage /boot/ Image
cp arch/arm64/boot/dts/rockchip/rk3568-xxx.dtb /boot/rk3568-xxx.dtb

8、更新启动项

1
2
3
4
5
6
vim /boot/extlinux/extlinux.conf
修改文件内容为:
LABEL kernel
LINUX /boot/Image
FDT /boot/rk3568-xxx.dtb
APPEND root=/dev/mmcblkep1 rw rootwait console=ttys2,1500000n8

五、验证实时性

1、利用指令查看内核

1
uname -a

2、测试实时性

1
2
3
sudo apt-get install rt-tests

sudo cyclictest -t 10 -p 80 -N # -N 是纳秒显示; -n 是微妙显示
示例图片

六、更换源

1、备份原来的源

1
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup

2、更换源

1
sudo vim /etc/apt/sources.list

更换为arm架构的清华源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal universe
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates universe
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security universe
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security multiverse

更换为X86架构的清华源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal universe
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates universe
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu focal partner
# deb-src http://archive.canonical.com/ubuntu focal partner

deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security universe
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security multiverse

3、激活更换源

1
sudo apt-get update

七、出不来Fully Preemptible Kernel

1、选择-Configure standard kernel features (expert users)

示例图片

2、将里面能不选的就全部不选

示例图片

3、再重新进入进入 > General setup > Preemption Model

选择 (X) Fully Preemptible Kernel (RT) ,保存配置文件

4、选择完RT之后,再将Configure standard kernel features全部选回来

八、参考补丁

1、关于RK3568 Linux内核打实时补丁RT Preempt介绍_linux实时补丁-CSDN博客

2、关于Linux安装RT-PREEMPT实时补丁-CSDN博客

3、Ubuntu 18.04安装 RT-PREEMPT 实时内核及补丁【过程记录】_csdn 黄小白的进阶之路 实时内核-CSDN博客

4、ubuntu20 下从linux 5.14.0-1045 源码编译 linux-5.15.56 内核_ubuntu20源码-CSDN博客

5、用make-kpkg制作Ubuntu的内核安装包deb-CSDN博客

6、linux开启PREEMPT_RT_linux开启rt内核-CSDN博客

7、安装了内核重启后内核版本没有更新 调处grub引导界面 (已解决)_ubuntu18.04安装了新的内核,但重启之后内核版本不变-CSDN博客