From cfd24214620561f50bd0a1ff8cee5e4b20359734 Mon Sep 17 00:00:00 2001 From: bin456789 Date: Wed, 5 Jun 2024 22:23:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=A4=84=E7=90=86=E5=A4=96?= =?UTF-8?q?=E9=83=A8=20grub=20=E5=BC=95=E5=AF=BC=E7=9A=84=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E4=BE=8B=E5=A6=82=20=20cloudcone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- debian.cfg | 5 +++++ reinstall.sh | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/debian.cfg b/debian.cfg index a0d1098..849ed49 100644 --- a/debian.cfg +++ b/debian.cfg @@ -157,7 +157,12 @@ d-i partman/early_command string true; \ # debian 10 没有 /target/etc/ssh/sshd_config.d/ 文件夹 # kali ssh 默认关闭 +# 另一种方法处理 cloudcone +# if [ "$link_grub_dir" = 1 ]; then mkdir /target/boot/grub2; echo 'chainloader (hd0)+1' >/target/boot/grub2/grub.cfg; fi; \ d-i preseed/late_command string true; \ + link_grub_dir="$(grep -o 'extra\.link_grub_dir=[^ ]*' /proc/cmdline | cut -d= -f2)"; \ + if [ "$link_grub_dir" = 1 ]; then ln -s grub /target/boot/grub2; fi; \ + in-target systemctl enable ssh; \ echo "PermitRootLogin yes" >/target/etc/ssh/sshd_config.d/01-permitrootlogin.conf || \ echo "PermitRootLogin yes" >>/target/etc/ssh/sshd_config diff --git a/reinstall.sh b/reinstall.sh index 81cff84..f89bbed 100644 --- a/reinstall.sh +++ b/reinstall.sh @@ -1435,6 +1435,14 @@ is_efi() { fi } +is_grub_dir_linked() { + # cloudcone 重装前/重装后(方法1) + [ "$(readlink -f /boot/grub/grub.cfg)" = /boot/grub2/grub.cfg ] || + [ "$(readlink -f /boot/grub2/grub.cfg)" = /boot/grub/grub.cfg ] || + # cloudcone 重装后(方法2) + { [ -f /boot/grub2/grub.cfg ] && [ "$(cat /boot/grub2/grub.cfg)" = 'chainloader (hd0)+1' ]; } +} + is_secure_boot_enabled() { if is_efi; then if is_in_windows; then @@ -1919,6 +1927,11 @@ build_extra_cmdline() { elif [ -n "$nextos_mirrorlist" ]; then extra_cmdline+=" extra.mirrorlist='$nextos_mirrorlist'" fi + + # cloudcone 特殊处理 + if is_grub_dir_linked; then + finalos_cmdline+=" extra.link_grub_dir=1" + fi } echo_tmp_ttys() { @@ -2961,10 +2974,49 @@ if is_use_grub; then fi fi + # cloudcone 从光驱的 grub 启动,再加载硬盘的 grub.cfg + # menuentry "Grub 2" --id grub2 { + # set root=(hd0,msdos1) + # configfile /boot/grub2/grub.cfg + # } + + # 加载后 $prefix 依然是光驱的 (hd96)/boot/grub + # 导致找不到 $prefix 目录的 grubenv,因此读取不到 next_entry + # 以下方法为 cloudcone 重新加载 grubenv + + # 需查找 2*2 个文件夹 + # 分区:系统 / boot + # 文件夹:grub / grub2 + # shellcheck disable=SC2121,SC2154 + # cloudcone debian 能用但 ubuntu 模板用不了 + # ubuntu 模板甚至没显示 reinstall menuentry + load_grubenv_if_not_loaded() { + if ! [ -s $prefix/grubenv ]; then + for dir in /boot/grub /boot/grub2 /grub /grub2; do + set grubenv="($root)$dir/grubenv" + if [ -s $grubenv ]; then + load_env --file $grubenv + if [ "${next_entry}" ]; then + set default="${next_entry}" + set next_entry= + save_env --file $grubenv next_entry + else + set default="0" + fi + return + fi + done + fi + } + # 生成 grub 配置 # 实测 centos 7 lvm 要手动加载 lvm 模块 echo $target_cfg - del_empty_lines <$target_cfg + + del_empty_lines <