1. 查看当前系统版本
uname -a
当前系统支持的其他内核版本
dpkg --get-selections | grep linux-image
可见当前系统默认 6.5.0 版本,同时支持 5.19.0
2. 修改默认启动
ubuntu 通过 grub 启动系统,默认直接进入,这里把 grub 交互对话框加个延时。
sudo vim /etc/default/grub
默认隐藏关掉,设置 3s 延时;
更新 grup, 重启。
sudo update-grub
reboot
进入 grub,3s 内按上下键选中,进入“Advance option for Ubuntu”
选中 5.19 版本内核
进入系统后,再次查看内核版本
可见,修改内核项生效;
3. 获取源码
主流 Linux 发行版通常不直接使用社区内核,通常会打一些自己的补丁。以 Ubuntu 为例,官方有自己的 Linux Kernl 代码仓库,可以在 https://kernel.ubuntu.com/git/ 找到对应的源码。
git clone https://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy
切换到某个分支
git checkout -b v5.15.0-115.125 origin/master-prep
4. 编译 kernel
安装要用到的依赖工具:
sudo apt install libncurses-dev gawk flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf llvm
获取本机配置文件:
cp /boot/config-$(uname -r) .config
make menuconfig 打开,然后保存;
修改.config 文件:
编译
sudo make -j$(nproc) deb-pkg LOCALVERSION=-millionv01
报错:
sudo apt install dwarves
清理编译中间件,重新编译。
编译完成,会在上一层目录生层多个包
包说明:
linux-image-version:
contains the kernel image and the associated modules,
linux-headers-version:
contains the header files required to build external modules,
linux-firmware-image-version:
contains the firmware files needed by some drivers (this package might be missing when you build from the kernel sources provided by Debian),
linux-image-version-dbg:
contains the debugging symbols for the kernel image and its modules (only created if CONFIG_DEBUG_INFO=y), and
linux-libc-dev:
contains headers relevant to some user-space libraries like GNU glibc.
https://www.debian.org/doc/manuals/debian-handbook/sect.kernel-compilation.zh-cn.html
5. 安装升级内核:
sudo dpkg -i linux-headers-5.15.158-millionv01_5.15.158-millionv01-1_amd64.deb
sudo dpkg -i linux-image-5.15.158-millionv01_5.15.158-millionv01-1_amd64.deb
sudo dpkg -i linux-libc-dev_5.15.158-millionv01-1_amd64.deb
sudo dpkg -i linux-image-5.15.158-millionv01-dbg_5.15.158-millionv01-1_amd64.deb
再次查看系统已安装内核,发现多了两个刚编译的内核镜像
重启,选中刚编译的镜像
进系统后,再次查看验证当前内核版本:
可见,已更换成自编译内核版本。
6. 设置默认启动项
修改 /etc/default/grub:
GRUB_DEFAULT="1> 2" // 数字表示使用 menuentry 作为默认启动选项排列序号。2 前必须要有空格。
修改完成,sudo update-grub,重启生效。
https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel