使用grub制作GPT分区格式的基于UEFI启动引导的系统盘

目前安装操作系统时,我们应当尽量使用UEFI+GPT这样的组合,这个是启动速度最快,扩展性最好的一种方式。上一篇,我写了一下假如只有Legacy BIOS的情况下,结合GPT应该如何去做,整个启动的流程大概是怎么样的。这篇文章,我们就言归正传,使用grub2把GPT和UEFI做下配合,手动把一块磁盘打造成一块可以启动的磁盘。

grub 被设计为适应 UEFI 和传统 BIOS 引导两种方式,因此它可以与多种硬件平台兼容。在 UEFI 模式下,grub 会与 UEFI 固件协同工作,负责启动操作系统。上次我们其实已经展示过grub与传统bios的结合,传统的bios的情况下也分成mbr及gpt两种情况,此处不再赘述了。

VMware是支持UEFI的,具体启用的方式请查看下图。

此次的实验其实非常的不顺利,因为我选择了新的rocky 9.2作为基础的实验环境,但是在进行grub2-install的操作时,不断的遇到下边的问题。

grub2-install: error: this utility cannot be used for EFI platforms because it does not support UEFI Secure Boot.

google对于这个问题,给出了很多莫名其妙的解决方案,其实都是无效的,浪费了至少两个小时。

实际上,这个问题是由于整个红帽系列新的grub uefi的安装已经使用dnf去进行了,导致我想要用命令行对于某个磁盘进行操作的尝试,彻底失败。红帽对于grub2与uefi的说明如下:

https://docs.fedoraproject.org/en-US/quick-docs/grub2-bootloader/

搞清楚错误的原因之后,重新下载安装了ubuntu22的版本。ubuntu中新版的grub命令直接就是grub-install。

parted /dev/sdb
mklabel gpt
mkpart primary fat32 1MiB 513MiB
set 1 boot on
set 1 esp on
mkpart primary xfs 513MiB 1GiB
mkpart primary linux-swap 1GiB 3GiB
mkpart primary xfs 3GiB 100%


mkfs.fat -F32 /dev/sdb1 
mkfs.xfs  /dev/sdb2 
mkswap  /dev/sdb3
mkfs.xfs  /dev/sdb4 

mkdir -pv /mnt/boot
mkdir -pv /mnt/sysroot
mount  /dev/sdb2  /mnt/boot
mount /dev/sdb4 /mnt/sysroot
mkdir -pv /mnt/boot/efi
mount  /dev/sdb1  /mnt/boot/efi

grub-install --target=x86_64-efi --efi-directory=/mnt/boot/efi  --boot-directory=/mnt/boot  /dev/sdb

cp /boot/vmlinuz-5.15.0-88-generic   /mnt/boot/
cp /boot/initrd.img-5.15.0-88-generic  /mnt/boot/
cp /boot/grub/grub.cfg   /mnt/boot/grub/

cd /mnt/sysroot/
# 进行linux基础目录的创建
mkdir -pv etc bin sbin lib lib64 dev proc sys tmp var usr home root mnt media
# 拷贝bash程序机器依赖的库文件
cp /bin/bash  /mnt/sysroot/bin/
ldd /bin/bash
mkdir -pv lib/x86_64-linux-gnu/
cp   /lib/x86_64-linux-gnu/libtinfo.so.6  /mnt/sysroot/lib/x86_64-linux-gnu/
cp  /lib/x86_64-linux-gnu/libc.so.6  /mnt/sysroot/lib/x86_64-linux-gnu/
cp  /lib64/ld-linux-x86-64.so.2  /mnt/sysroot/lib64/

切换根进行一下验证
chroot /mnt/sysroot/

grub.cfg的配置,基于ubuntu的基础做了精简,主要目的是验证能正常引导及启动。里面应该存在不少冗余信息。

#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#

### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
  set have_grubenv=true
  load_env
fi
if [ "${initrdfail}" = 2 ]; then
   set initrdfail=
elif [ "${initrdfail}" = 1 ]; then
   set next_entry="${prev_entry}"
   set prev_entry=
   save_env prev_entry
   if [ "${next_entry}" ]; then
      set initrdfail=2
   fi
fi
if [ "${next_entry}" ] ; then
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
else
   set default="0"
fi

if [ x"${feature_menuentry_id}" = xy ]; then
  menuentry_id_option="--id"
else
  menuentry_id_option=""
fi

export menuentry_id_option

if [ "${prev_saved_entry}" ]; then
  set saved_entry="${prev_saved_entry}"
  save_env saved_entry
  set prev_saved_entry=
  save_env prev_saved_entry
  set boot_once=true
fi

function savedefault {
  if [ -z "${boot_once}" ]; then
    saved_entry="${chosen}"
    save_env saved_entry
  fi
}
function initrdfail {
    if [ -n "${have_grubenv}" ]; then if [ -n "${partuuid}" ]; then
      if [ -z "${initrdfail}" ]; then
        set initrdfail=1
        if [ -n "${boot_once}" ]; then
          set prev_entry="${default}"
          save_env prev_entry
        fi
      fi
      save_env initrdfail
    fi; fi
}
function recordfail {
  set recordfail=1
  if [ -n "${have_grubenv}" ]; then if [ -z "${boot_once}" ]; then save_env recordfail; fi; fi
}
function load_video {
  if [ x$feature_all_video_module = xy ]; then
    insmod all_video
  else
    insmod efi_gop
    insmod efi_uga
    insmod ieee1275_fb
    insmod vbe
    insmod vga
    insmod video_bochs
    insmod video_cirrus
  fi
}

if [ x$feature_default_font_path = xy ] ; then
   font=unicode
else
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
  search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2  a054304f-cc82-43e5-b84a-0a33c7aba640
else
  search --no-floppy --fs-uuid --set=root a054304f-cc82-43e5-b84a-0a33c7aba640
fi
    font="/usr/share/grub/unicode.pf2"
fi

if loadfont $font ; then
  set gfxmode=auto
  load_video
  insmod gfxterm
  set locale_dir=$prefix/locale
  set lang=en_US
  insmod gettext
fi
terminal_output gfxterm
if [ "${recordfail}" = 1 ] ; then
  set timeout=30
else
  if [ x$feature_timeout_style = xy ] ; then
    set timeout_style=hidden
    set timeout=0
  # Fallback hidden-timeout code in case the timeout_style feature is
  # unavailable.
  elif sleep --interruptible 0 ; then
    set timeout=0
  fi
fi
### END /etc/grub.d/00_header ###

### BEGIN /etc/grub.d/05_debian_theme ###
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
### END /etc/grub.d/05_debian_theme ###

### BEGIN /etc/grub.d/10_linux ###
function gfxmode {
	set gfxpayload="${1}"
	if [ "${1}" = "keep" ]; then
		set vt_handoff=vt.handoff=7
	else
		set vt_handoff=
	fi
}
if [ "${recordfail}" != 1 ]; then
  if [ -e ${prefix}/gfxblacklist.txt ]; then
    if [ ${grub_platform} != pc ]; then
      set linux_gfx_mode=keep
    elif hwmatch ${prefix}/gfxblacklist.txt 3; then
      if [ ${match} = 0 ]; then
        set linux_gfx_mode=keep
      else
        set linux_gfx_mode=text
      fi
    else
      set linux_gfx_mode=text
    fi
  else
    set linux_gfx_mode=keep
  fi
else
  set linux_gfx_mode=text
fi
export linux_gfx_mode
menuentry 'Ubuntu'  {
	linux	/vmlinuz-5.15.0-88-generic root=/dev/sda4 ro  selinux=0   init=/bin/bash   
	initrd	/initrd.img-5.15.0-88-generic
}

### END /etc/grub.d/10_linux ###

### BEGIN /etc/grub.d/10_linux_zfs ###
### END /etc/grub.d/10_linux_zfs ###

### BEGIN /etc/grub.d/20_linux_xen ###

### END /etc/grub.d/20_linux_xen ###

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

### BEGIN /etc/grub.d/30_uefi-firmware ###
menuentry 'UEFI Firmware Settings' $menuentry_id_option 'uefi-firmware' {
	fwsetup
}
### END /etc/grub.d/30_uefi-firmware ###

### BEGIN /etc/grub.d/35_fwupd ###
### END /etc/grub.d/35_fwupd ###

### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
### END /etc/grub.d/40_custom ###

### BEGIN /etc/grub.d/41_custom ###
if [ -f  ${config_directory}/custom.cfg ]; then
  source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f  $prefix/custom.cfg ]; then
  source $prefix/custom.cfg
fi
### END /etc/grub.d/41_custom ###

https://watermelonwater.tech/%e8%af%a6%e8%a7%a3%e4%bd%bf%e7%94%a8grub2%e5%9f%ba%e4%ba%8egpt%e5%88%86%e5%8c%ba%e6%a0%bc%e5%bc%8f%e5%88%9b%e5%bb%ba%e7%ac%a6%e5%90%88legacy-bios%e7%9a%84%e7%b3%bb%e7%bb%9f%e5%90%af%e5%8a%a8%e7%9b%98/