本次修改了三个地方:

① set_boot_kernel(去掉确认,新增禁止自动选最新内核)

移除 read -p 确认,直接写入 /etc/default/grub
同时将 GRUB_SAVEDEFAULT 强制写为 false,防止 GRUB 记住上次选择 干扰指定内核
新增 UUID 有效性检查,获取失败时报错退出而非写入空值
② hold_default_boot_kernel(新增锁定元包)

原来只锁定具体版本的 linux-image-6.8.0-52-generic
新增锁定 linux-image-generic 和 linux-headers-generic 两个元包——这才是 apt 升级时切换内核的根本原因
③ 主流程(去掉两个交互确认)

安装完成后自动调用 set_default_boot_kernel + hold_default_boot_kernel,无需用户输入
This commit is contained in:
beyondx123
2026-05-15 17:42:40 +08:00
parent c0f38632db
commit ca1c38417c
2 changed files with 74 additions and 53 deletions
+30 -22
View File
@@ -444,28 +444,34 @@ get_kernel_uuid() {
set_boot_kernel() {
local grub_path="/etc/default/grub"
local default="GRUB_DEFAULT="
local newlines=""
local uuid_list
uuid_list=$(get_kernel_uuid)
if [[ -z "$uuid_list" || "$uuid_list" == ">" ]]; then
log_err "无法从 grub.cfg 中获取内核 UUID,请确认内核已安装并运行过 update-grub。"
return 1
fi
# 重写 GRUB_DEFAULT,同时确保 GRUB_SAVEDEFAULT 不干扰
while read -r l; do
if [[ $l == $default* ]]; then
newlines+="$default\"$uuid_list\"\n"
if [[ $l == GRUB_DEFAULT=* ]]; then
newlines+="GRUB_DEFAULT=\"$uuid_list\"\n"
elif [[ $l == GRUB_SAVEDEFAULT=* ]]; then
newlines+="GRUB_SAVEDEFAULT=false\n"
else
newlines+="$l\n"
fi
done < "$grub_path"
echo -e "\n${YELLOW}新的 /etc/default/grub 配置预览:${NC}"
echo -e "$newlines"
read -p "请确认配置是否正确,输入Y确认回写文件,按任意键跳过: " answer
if [[ "$answer" == "Y" || "$answer" == "y" ]]; then
echo -e "$newlines" > "$grub_path"
return 0
# 如果文件里没有 GRUB_SAVEDEFAULT 这行,追加一条
if ! grep -q "^GRUB_SAVEDEFAULT=" "$grub_path"; then
newlines+="GRUB_SAVEDEFAULT=false\n"
fi
return 1
log_info "写入 GRUB_DEFAULT: $uuid_list"
echo -e "$newlines" > "$grub_path"
return 0
}
set_default_boot_kernel() {
@@ -482,7 +488,6 @@ set_default_boot_kernel() {
log_info "正在更新 GRUB 引导程序..."
update-grub 2>&1 | tee -a "$LOG_FILE"
# 如果没有跳过 DKMS,尝试为新内核编译驱动
if [[ "$SKIP_DKMS" == false ]]; then
log_info "正在为新内核 ($KERNEL_VERSION) 构建驱动程序 (DKMS)..."
if dkms autoinstall -k "$KERNEL_VERSION" 2>&1 | tee -a "$LOG_FILE"; then
@@ -494,14 +499,22 @@ set_default_boot_kernel() {
else
log_info "已跳过 DKMS 自动编译(可重启后手动运行: sudo dkms autoinstall -k $KERNEL_VERSION)。"
fi
else
log_err "GRUB 配置写入失败,请手动检查 /etc/default/grub。"
exit 1
fi
}
hold_default_boot_kernel() {
log_info "正在锁定内核版本,防止被系统更新替换..."
# 锁定具体版本包
apt-mark hold linux-image-"$KERNEL_VERSION"
apt-mark hold linux-headers-"$KERNEL_VERSION"
apt-mark hold linux-modules-extra-"$KERNEL_VERSION"
# 锁定元包,防止 apt 升级时将默认内核切换到新版本
apt-mark hold linux-image-generic
apt-mark hold linux-headers-generic
log_ok "内核版本已锁定。"
}
# --- 主程序 ---
@@ -607,17 +620,12 @@ else
fi
fi
# 设置默认启动内核
read -p "是否设置 $KERNEL_VERSION 为默认启动内核(Y/N): " answer
if [[ "$answer" == "Y" || "$answer" == "y" ]]; then
set_default_boot_kernel
fi
# 安装成功后:自动设置默认启动内核 + 锁定版本(无需用户确认)
log_step "设置 $KERNEL_VERSION 为默认启动内核..."
set_default_boot_kernel
# 锁定内核版本
read -p "是否锁定该内核版本(防止系统自动更新内核)?(Y/N): " answer
if [[ "$answer" == "Y" || "$answer" == "y" ]]; then
hold_default_boot_kernel
fi
log_step "锁定内核版本,防止 apt 自动升级切换..."
hold_default_boot_kernel
log_ok "操作完成!"
echo -e "${YELLOW}注意:如果重启后显卡驱动不正常,请检查 Secure Boot 是否已关闭。${NC}"