【ubuntu】如何构建自己的ubuntu内核

1. 准备环境

sudo apt update
sudo apt install build-essential fakeroot dpkg-dev kernel-package libncurses-dev bison flex libssl-dev libelf-dev

2. 下载内核源码

确认源码包, 下载源码包。

dpkg -S /boot/vmlinuz-$(uname -r)
linux-image-5.15.0-46-generic: /boot/vmlinuz-5.15.0-46-generic

apt show linux-image-$(uname -r)
Package: linux-image-5.15.0-46-generic
Version: 5.15.0-46.49~20.04.1
## 这个内核包是基于 linux-hwe-5.15 源码构建出来的
Built-Using: linux-hwe-5.15 (= 5.15.0-46.49~20.04.1)
Priority: optional
Section: kernel
Source: linux-signed-hwe-5.15
# 你正在查看的这个 .deb 包(签名内核)是由 linux-signed-hwe-5.15 源码包生成的。
# 也就是说,linux-signed-hwe-5.15 并不自己生成 vmlinuz,而是从 linux-hwe-5.15 拿结果来签名并打包。
# 它的主要职责是调用 sbsign、生成 .deb、对齐 Ubuntu Secure Boot 签名流程。

apt-get source linux-image-$(uname -r)
# or apt-get source linux-image-unsigned-$(uname -r)

# 或者从ubuntu 的git 仓库下载
git clone git://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/focal
cd focal
git checkout <对应版本tag>

3. 安装内核构建依赖

sudo apt build-dep linux-image-unsigned-$(uname -r)

4. 编译内核

cd linux-hwe-5.15-5.15.0/
fakeroot debian/rules clean
fakeroot debian/rules binary-headers binary-generic binary-perarch

生成未签名版本的linux-image-unsigned-5.15.0-140-generic_*.deb。

5. 生成签名版内核的完整流程

Ubuntu 用一个独立的“签名包”来将 unsigned 镜像进行 UEFI 安全启动签名。
根据apt show linux-image-$(uname -r), 签名包是linux-signed-hwe-5.15。

apt-get source linux-signed-hwe-5.15
cd linux-signed-hwe-5.15-5.15.0/

确保你先构建好的 vmlinuz-* 放在构建目录的上级路径.

安装签名构建依赖:

sudo apt build-dep linux-signed-hwe-5.15

编译:

cd linux-signed-hwe-5.15-5.15.0
fakeroot debian/rules clean
fakeroot debian/rules binary

这一步会生成 linux-image-5.15.0-140-generic_*.deb,而且是带有 UEFI 安全启动签名的版本。
linux-generate-hwe-5.15 是 Ubuntu 内部构建流程使用的工具包,并不在公开的软件仓库中提供。

如果你 手动编译 Linux 内核(无论是通过 dpkg-buildpackage 还是 debian/rules binary),默认生成的 内核镜像(linux-image)不会自动签名,因为:
Ubuntu 官方内核签名由 Canonical 私钥完成,普通用户无法获取该密钥。
Secure Boot 依赖签名验证,未签名的内核在 Secure Boot 启用时无法启动。

Ubuntu 使用的是 Canonical 的 UEFI 签名密钥和工具(如 sbsign)。默认签名规则在 debian/rules 中会调用如下命令:

sbsign --key /path/to/key --cert /path/to/cert vmlinuz > vmlinuz.signed

不过 如果你是自己构建并签名的,需要准备自己的签名密钥,并在 BIOS 设置中加入你的签名(通过 MOK)。

你可能感兴趣的:(【ubuntu】如何构建自己的ubuntu内核)