Debian From Scratch 升级内核的两条路径
- 在 DFS 环境中,内核升级通常有两种做法:
- 在 chroot 里用 Debian 包管理器 apt 安装发行版提供的内核(简单、可回滚);
- 在 chroot 里或从外部交叉编译 Linux 源码 并安装(可控性强,适合定制)。
无论哪种方式,完成后都要在目标系统里生成 GRUB 配置并保留旧内核以便回滚。DFS 本质是一个最小化 Debian 根文件系统,因此包管理方式与常规 Debian 一致;若需要深度定制,再选择源码编译。
方案一 使用 apt 安装发行版内核(推荐)
- 在构建机的 chroot 中(或目标系统 chroot)准备环境并更新索引:
- apt update && apt install -y ca-certificates lsb-release
- 查看可用的内核包:
- apt list | grep linux-image
- 安装合适的内核(示例,按你的架构与需求选择,如 amd64):
- apt install -y linux-image-6.1.0-31-amd64 linux-headers-6.1.0-31-amd64
- 如需较新特性可启用 backports(示例):
- echo “deb https://deb.debian.org/debian bookworm-backports main” >/etc/apt/sources.list.d/backports.list
- apt update && apt install -y -t bookworm-backports linux-image-amd64
- 安装完成后在目标系统生成引导(两种常见情况,二选一):
- 传统 BIOS:update-grub
- UEFI:grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=debian && update-grub
- 重启并在 GRUB 菜单选择新内核,登录后验证:
- 建议保留旧内核,便于回滚;确认新内核稳定后再清理旧包。
方案二 从源码编译并安装内核(可控定制)
- 准备依赖(在 chroot 内):
- apt install -y build-essential libncurses-dev bc flex libssl-dev libelf-dev kmod cpio rsync debhelper git
- 获取源码(选择 stable 或 longterm 版本,示例为 6.13.3):
- wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.13.3.tar.xz
- tar -xf linux-6.13.3.tar.xz && cd linux-6.13.3
- 配置:
- 以当前运行配置为基线:cp /boot/config-$(uname -r) .config
- 交互升级配置:yes ‘’ | make oldconfig
- 如需自定义:make menuconfig
- 方式 A(推荐)—— 打包为 deb 再用 apt 安装(便于多版本管理与回滚):
- make -j$(nproc) deb-pkg LOCALVERSION=-custom KDEB_PKGVERSION=$(make kernelversion)
- 在 chroot 内安装生成的 .deb:dpkg -i …/linux-image-.deb …/linux-headers-.deb
- 方式 B —— 直接安装到 /boot(不生成 deb,管理不如方式 A 方便):
- make -j$(nproc)
- make modules_install
- make install
- 为目标系统生成 initramfs(Debian 常用工具):
- update-initramfs -c -k $(make kernelversion)$(echo $LOCALVERSION | sed ‘s/^-//’)
- 为目标系统安装/更新 GRUB(同方案一),重启并验证:uname -r。
版本选择与回滚要点
- 版本选择:生产环境优先 longterm(LT) 或发行版稳定分支,功能诉求再考虑 主线(mainline);避免 RC/Prepatch 开发版。示例 LT 系列如 5.4.x、6.1.x。
- 回滚与多版本共存:
- 始终保留至少一个已知可用的旧内核;出现问题时在 GRUB 菜单选择旧内核启动即可恢复。
- 使用 apt 安装新内核是“新增”而非“替换”,便于回滚;确认稳定后再清理旧包。
- 若需指定默认启动项:
- GRUB 2 可用 grub2-set-default(BIOS)或在 /etc/default/grub 设置 GRUB_DEFAULT 后 update-grub。
常见故障排查
- 启动失败并出现 “Kernel panic - not syncing: VFS: Unable to mount root fs …”
- 通常是缺少或错误的 initramfs。在目标系统执行:
- update-initramfs -c -k <内核版本>
- 然后 update-grub 并重启。
- 新内核未出现在 GRUB 菜单
- 确认已安装 linux-image 包,且在目标系统执行了 update-grub;UEFI 还需确保 efivars 挂载正确并完成 grub-install。
- 闭源驱动(如 NVIDIA/AMD)不兼容新内核
- 优先选择发行版内核或等待驱动更新;必要时回滚到旧内核再排查。