一、下载内核(如果不需要更换内核版本则跳到二)

参考文章【ubuntu内核切换】切换到目标内核,并通过如下命令下载源码,保证和Ubuntu的兼容性,否则会导致各种问题。

git clone git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy

注意:这个地址可能发生变化,通过命令如果有提示,则按提示下载,否则正常下载即可,也可以访问 https://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy/

sudo apt source linux-source-5.15.0

正在读取软件包列表... 完成
选择 linux 作为源代码包而非 linux-source-5.15.0
提示:linux 的打包工作被维护于以下位置的 Git 版本控制系统中:
git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy
请使用:
git clone git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy

git checkout Ubuntu-5.15.0-161.171

二、下载实时内核补丁
https://cdn.kernel.org/pub/linux/kernel/projects/rt
(选择patch版本为单个补丁,patchs为多个补丁)

查找 Ubuntu-5.15.0-161.171对应的内核版本:
(已更新最快办法,往下看)
https://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy/refs/heads
在这里插入图片描述
点击进入后查看日志
在这里插入图片描述
考虑是5.15.189版本。

更新一个最快的办法:

$ head -n 5 Makefile
# SPDX-License-Identifier: GPL-2.0
VERSION = 5
PATCHLEVEL = 15
SUBLEVEL = 189
EXTRAVERSION =

下载补丁并解压 patch-5.15.189-rt87.patch.gz
拷贝到linux目录下,应用补丁

patch -p1 < ./patch-*.patch
rm ./patch-*.patch

拷贝基础配置文件

#拷贝当前内核配置文件
cp /boot/config-5.15.0-161-generic .config
#应用.config
make olddefconfig

修改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

->  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

->  Cryptographic API 
 ->  Certificates for signature checking (at the very bottom of the list) 
  -> Provide system-wide ring of trusted keys 
   -> X.509 certificates to be preloaded into the system blacklist keyring 
   Remove the “debian/canonical-certs.pem” from the prompt and press Ok

编译内核

#编译环境
sudo apt install autoconf bison build-essential dkms dwarves fakeroot flex libelf-dev libiberty-dev  libncurses-dev libpci-dev libssl-dev libudev-dev minizip openssl zstd libdw-dev libdwarf-dev gawk
#编译
make -j10
make modules -j10

#生成deb包(可以跳过上面的编译环节)

#make clean 如果出现问题
make -j10 bindeb-pkg

安装

sudo make modules_install
sudo make install

更新引导,参考如下文章(建议跳过,确认内核没问题后再更新,使用下一步的手动临时修改内核)
https://blog.csdn.net/qq_38292379/article/details/154347967?fromshare=blogdetail&sharetype=blogdetail&sharerId=154347967&sharerefer=PC&sharesource=qq_38292379&sharefrom=from_link

避坑!!!!!!!
安装完成后,在/boot目录下initrd明显大于apt install的内核版本,使用虚拟机可能能够正常运行,但是物理可能会报错 “内存耗尽”。
在这里插入图片描述
解决方法一是关闭内核所有调试信息,二是使用如下命令

sudo apt install dracut #安装完成后会自动进行一次压缩
sudo dracut --strip --compress lz4 --force /boot/initrd.img-5.15.12-rt25+ 5.15.12-rt25+

(编译为Ubuntu安装包也可以避免上述问题,并且会生成调试和发布两个版本安装包)

# make clean 如果出现问题
make -j10 bindeb-pkg

重启系统,一直按住shift建,选择Advanced options for Ubuntu,然后选择内核。

实时内核测试

#CPU带负载
stress --cpu 12 --io 2 --vm 1 --vm-bytes 128M --timeout 120s
#测试(跟非实时内核对比;实时内核带负载和不带负责测试结果应该差不多)
#sudo apt-get install rt-tests
sudo cyclictest -t 5 -p 95 -m -D 1m -i 1000

三、卸载自己安装的内核

sudo rm -f /boot/vmlinuz-要删除的内核版本
sudo rm -f /boot/initrd.img-要删除的内核版本
sudo rm -f /boot/System.map-要删除的内核版本
sudo rm -f /boot/config-要删除的内核版本
sudo rm -rf /lib/modules/要删除的内核版本
sudo rm -rf /usr/src/linux-headers-要删除的内核版本
sudo update-grub
sudo update-initramfs -u

四、问题解决

 HDRINST usr/include/asm/unistd_x32.h
make[3]: *** [debian/rules:76:build-arch] 错误 2
dpkg-buildpackage: 错误: make -f debian/rules binary subprocess returned exit status 2
make[2]: *** [scripts/Makefile.package:126:bindeb-pkg] 错误 2
make[1]: *** [/home/user/work/linux-noble/Makefile:1630:bindeb-pkg] 错误 2
make: *** [Makefile:251:__sub-make] 错误 2
user@user:~/work/linux-noble$

解决:rm debian -rf

scripts/gendwarfksyms/gendwarfksyms.h:6:10: fatal error: dwarf.h: 没有那个文件或目录
    6 | #include <dwarf.h>

解决 :sudo apt install libdw-dev libdwarf-dev

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐