当前位置: 首页 > 工具软件 > X-Boot > 使用案例 >

Arch Linux 使用 systemd-boot 替代 grub 启动系统

慕容晔
2023-12-01

安装 systemd-boot

检测系统是否支持 EFI 启动

sudo efivar --list

没有 efivar 请安装 efivar

使用 ls /sys/firmware/efi/efivars 如果目录存在,系统是以 UEFI 模式启动的)。

详情参见 Arch WiKi: systemd-boot (简体中文))

通过 AUR 或 ArchLinuxCN 安装 systemd-boot

yay -S systemd-boot-pacman-hook

详情参见 Arch WiKi: systemd-boot (简体中文))

GPT 磁盘的 EFI 分区挂载到 /boot/efi

** 建议将 ESP 挂载到 /boot. 如果没将 ESP 挂载到 /boot,需要手动将内核和 initramfs 复制到 ESP.**

详情参见 Arch WiKi: systemd-boot (简体中文))

如果按照这种方式挂载 EFI 分区到 /boot/efi 下需要修改 [/etc/mkinitcpio.conf](# 这样修改后能 systemd-boot 启动,但是内核更新和原有的 grub 是会报错。) 和 [/etc/grub.d/10_linux](## 修改 /etc/grub.d/10_linux 文件,让其能检测到我们自定义 linux/linux-lts 固件的位置)

# 假设 /dev/sdb1 是 EFI 分区 
sudo mkdir -pv /boot/efi
sudo mount /dev/sdb1 /boot/efi

如果是直接将 EFI 分区挂载 /boot 下,修改的配置文件相对会少甚至不用修改。
现有系统下修改:

# 假设 /dev/sdb1 是 EFI 分区 
sudo  cp -rv /boot /boot-bak
sudo umount -r /boot 
sudo mount /dev/sdb1 /boot
sudo cp -rv /boot-bak/* /boot
sudo rm -rf /boot-bak
# EFI 分区下默认应该是没有文件夹,如果有那需要将默认的文件夹(EFI),需要手动改成小写 efi。以便 bootctl 检测。
sudo mv -rv /boot/EFI /boot/efi

使用 arch-chroot

# 假设 /dev/sdb1 是 EFI 分区 
# 再 archlinux 启动盘中挂载相应分区后进行上面类似的操作。
sudo  cp -rv /boot /boot-bak
sudo umount -r /boot 
sudo mount /dev/sdb1 /boot
sudo cp -rv /boot-bak/* /boot
sudo rm -rf /boot-bak
# EFI 分区下默认应该是没有文件夹,如果有那需要将默认的文件夹(EFI),需要手动改成小写 efi。以便 bootctl 检测。
sudo mv -rv /boot/EFI /boot/efi

使用 bootctl 安装 systemd-boot

sudo bootctl install     
[sudo] taotieren 的密码:
Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/efi/EFI/systemd/systemd-bootx64.efi".
Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/efi/EFI/BOOT/BOOTX64.EFI".
Random seed file /boot/efi/loader/random-seed successfully written (512 bytes).
Created EFI boot entry "Linux Boot Manager".

/boot 目录下的 linux/linux-lfs 固件移动到 /boot/efi/EFI/arch 目录下

sudo mkdir -pv /boot/efi/EFI/arch
sudo mv -rv /boot/*.img /boot/efi/EFI/arch
sudo mv -rv /boot/vmlinuz* /boot/efi/EFI/arch
ls /boot/efi/EFI/arch/
initramfs-linux-fallback.img      initramfs-linux-lts.img  intel-ucode.img  vmlinuz-linux-lts
initramfs-linux-lts-fallback.img  initramfs-linux.img      vmlinuz-linux

修改 systemd-boot/boot/efi/loader/loader.conf 配置文件

sudo cat >/boot/efi/loader/loader.conf << EOF
#timeout 3
#console-mode keep
default arch-lts.conf
timeout 4
console-mode max
editor no
EOF

如果上述命令无法使用请使用自己熟悉的文本编辑器修改 /boot/efi/loader/loader.conf 配置文件

获取当前磁盘的根(/)分区的 UUID

# 假设当前磁盘的`根(/)`分区是 /dev/sdb3 
sudo blkid /dev/sdb3
[sudo] taotieren 的密码:
/dev/sdb3: UUID="9574b053-e45c-4689-96c1-6b56c9122a25" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="40d2d5c0-7d8e-a249-99b4-caa190dd1257"

修改 linux-lts启动引导 /boot/efi/loader/entries/arch-lts.conf 文件

sudo cat > /boot/efi/loader/entries/arch-lts.conf << EOF
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title Arch Linux LTS
linux /EFI/arch/vmlinuz-linux-lts
initrd /EFI/arch/intel-ucode.img
initrd /EFI/arch/initramfs-linux-lts.img
#initrd /initramfs-linux-lts-fallback.img
options root=PARTUUID=40d2d5c0-7d8e-a249-99b4-caa190dd1257 rw
EOF

如果上述命令无法使用请使用自己熟悉的文本编辑器修改 /boot/efi/loader/entries/arch-lts.conf 配置文件
注意:

options root=PARTUUID=你当前磁盘的`根(/)`分区 UUID rw

修改 linux启动引导 /boot/efi/loader/entries/arch.conf 文件

sudo cat > /boot/efi/loader/entries/arch.conf << EOF   
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title Arch Linux
linux /EFI/arch/vmlinuz-linux
initrd /EFI/arch/intel-ucode.img
initrd /EFI/arch/initramfs-linux.img
#initrd /initramfs-linux-fallback.img
options root=PARTUUID=40d2d5c0-7d8e-a249-99b4-caa190dd1257 rw
EOF

修改其他启动引导文件

sudo cat > /boot/efi/loader/entries/systemd.conf << EOF
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title   systemd
efi     /EFI/systemd/systemd-bootx64.efi
EOF
sudo cat >/boot/efi/loader/entries/windows.conf << EOF 
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title   Windows 10 Pro
efi     /EFI/BOOT/BOOTX64.EFI
EOF

bootctl 更新

sudo bootctl update 
Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/efi/EFI/systemd/systemd-bootx64.efi".
Copied "/usr/lib/systemd/boot/efi/systemd-bootx64.efi" to "/boot/efi/EFI/BOOT/BOOTX64.EFI".

查看 bootctl 启动列表

sudo bootctl list  
Boot Loader Entries:
        title: Arch Linux LTS
           id: arch-lts.conf
       source: /boot/efi/loader/entries/arch-lts.conf
        linux: /EFI/arch/vmlinuz-linux-lts
       initrd: /EFI/arch/intel-ucode.img
               /EFI/arch/initramfs-linux-lts.img
      options: root=PARTUUID=40d2d5c0-7d8e-a249-99b4-caa190dd1257 rw

        title: Arch Linux
           id: arch.conf
       source: /boot/efi/loader/entries/arch.conf
        linux: /EFI/arch/vmlinuz-linux
       initrd: /EFI/arch/intel-ucode.img
               /EFI/arch/initramfs-linux.img
      options: root=PARTUUID=40d2d5c0-7d8e-a249-99b4-caa190dd1257 rw

        title: systemd (default)
           id: systemd.conf
       source: /boot/efi/loader/entries/systemd.conf

        title: Windows 10 Pro
           id: windows.conf
       source: /boot/efi/loader/entries/windows.conf

        title: Reboot Into Firmware Interface
           id: auto-reboot-to-firmware-setup
       source: /sys/firmware/efi/efivars/LoaderEntries-4a67b082-0a4c-41cf-b6c7-440b29bb8c4f

查看 bootctl 状态

sudo bootctl status 
System:
     Firmware: UEFI 2.31 (American Megatrends 4.653)
  Secure Boot: disabled
   Setup Mode: user
 Boot into FW: supported

Current Boot Loader:
      Product: systemd-boot 247.3-1-arch
     Features: ✓ Boot counting
               ✓ Menu timeout control
               ✓ One-shot menu timeout control
               ✓ Default entry control
               ✓ One-shot entry control
               ✓ Support for XBOOTLDR partition
               ✓ Support for passing random seed to OS
               ✓ Boot loader sets ESP partition information
          ESP: /dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325
         File: └─/EFI/systemd/systemd-bootx64.efi

Random Seed:
 Passed to OS: yes
 System Token: set
       Exists: yes

Available Boot Loaders on ESP:
          ESP: /boot/efi (/dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325)
         File: └─/EFI/systemd/systemd-bootx64.efi (systemd-boot 247.3-1-arch)
         File: └─/EFI/BOOT/BOOTX64.EFI (systemd-boot 247.3-1-arch)

Boot Loaders Listed in EFI Variables:
        Title: Linux Boot Manager
           ID: 0x0002
       Status: active, boot-order
    Partition: /dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325
         File: └─/EFI/systemd/systemd-bootx64.efi

        Title: ArchLinux
           ID: 0x0001
       Status: active, boot-order
    Partition: /dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325
         File: └─/EFI/ArchLinux/grubx64.efi

        Title: Windows Boot Manager
           ID: 0x0000
       Status: active, boot-order
    Partition: /dev/disk/by-partuuid/37ac3214-8a21-4b89-b379-8cfc664a7b79
         File: └─/EFI/Microsoft/Boot/bootmgfw.efi

Boot Loader Entries:
        $BOOT: /boot/efi (/dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325)

Default Boot Loader Entry:
        title: systemd
           id: systemd.conf
       source: /boot/efi/loader/entries/systemd.conf

这样修改后能 systemd-boot 启动,但是内核更新和原有的 grub 是会报错。

/etc/mkinitcpio.d/linux.preset 的修改

sudo sed -i s|/boot|/boot/efi/EFI/arch|g /etc/mkinitcpio.d/linux.preset

详细修改:

sudo cat >/etc/mkinitcpio.d/linux.preset<< EOF
# mkinitcpio preset file for the 'linux' package

ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/efi/EFI/arch/vmlinuz-linux"

PRESETS=('default' 'fallback')

#default_config="/etc/mkinitcpio.conf"
default_image="/boot/efi/EFI/arch/initramfs-linux.img"
#default_options=""

#fallback_config="/etc/mkinitcpio.conf"
fallback_image="/boot/efi/EFI/arch/initramfs-linux-fallback.img"
fallback_options="-S autodetect"
EOF

/etc/mkinitcpio.d/linux-lts.preset 的修改

sudo sed -i s|/boot|/boot/efi/EFI/arch|g /etc/mkinitcpio.d/linux-lts.preset

详细修改:

sudo cat >/etc/mkinitcpio.d/linux-lts.preset<<EOF
# mkinitcpio preset file for the 'linux-lts' package

ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/efi/EFI/arch/vmlinuz-linux-lts"

PRESETS=('default' 'fallback')

#default_config="/etc/mkinitcpio.conf"
default_image="/boot/efi/EFI/arch/initramfs-linux-lts.img"
#default_options=""

#fallback_config="/etc/mkinitcpio.conf"
fallback_image="/boot/efi/EFI/arch/initramfs-linux-lts-fallback.img"
fallback_options="-S autodetect"
EOF

/etc/mkinitcpio.conf 的修改

sudo sed -i s|HOOKS=(base udev autodetect modconf block filesystems keyboard fsck )|HOOKS=(base systemd udev autodetect modconf block filesystems keyboard fsck )|g /etc/mkinitcpio.conf

详细修改:

sudo cat > /etc/mkinitcpio.conf <<EOF
# vim:set ft=sh
# MODULES
# The following modules are loaded before any boot hooks are
# run.  Advanced users may wish to specify all system modules
# in this array.  For instance:
#     MODULES=(piix ide_disk reiserfs)
MODULES=()

# BINARIES
# This setting includes any additional binaries a given user may
# wish into the CPIO image.  This is run last, so it may be used to
# override the actual binaries included by a given hook
# BINARIES are dependency parsed, so you may safely ignore libraries
BINARIES=()

# FILES
# This setting is similar to BINARIES above, however, files are added
# as-is and are not parsed in any way.  This is useful for config files.
FILES=()

# HOOKS
# This is the most important setting in this file.  The HOOKS control the
# modules and scripts added to the image, and what happens at boot time.
# Order is important, and it is recommended that you do not change the
# order in which HOOKS are added.  Run 'mkinitcpio -H <hook name>' for
# help on a given hook.
# 'base' is _required_ unless you know precisely what you are doing.
# 'udev' is _required_ in order to automatically load modules
# 'filesystems' is _required_ unless you specify your fs modules in MODULES
# Examples:
##   This setup specifies all modules in the MODULES setting above.
##   No raid, lvm2, or encrypted root is needed.
#    HOOKS=(base)
#
##   This setup will autodetect all modules for your system and should
##   work as a sane default
#    HOOKS=(base udev autodetect block filesystems)
#
##   This setup will generate a 'full' image which supports most systems.
##   No autodetection is done.
#    HOOKS=(base udev block filesystems)
#
##   This setup assembles a pata mdadm array with an encrypted root FS.
##   Note: See 'mkinitcpio -H mdadm' for more information on raid devices.
#    HOOKS=(base udev block mdadm encrypt filesystems)
#
##   This setup loads an lvm2 volume group on a usb device.
#    HOOKS=(base udev block lvm2 filesystems)
#
##   NOTE: If you have /usr on a separate partition, you MUST include the
#    usr, fsck and shutdown hooks.
HOOKS=(base systemd udev autodetect modconf block filesystems keyboard fsck )

# COMPRESSION
# Use this to compress the initramfs image. By default, gzip compression
# is used. Use 'cat' to create an uncompressed image.
#COMPRESSION="gzip"
#COMPRESSION="bzip2"
#COMPRESSION="lzma"
#COMPRESSION="xz"
#COMPRESSION="lzop"
#COMPRESSION="lz4"
#COMPRESSION="zstd"

# COMPRESSION_OPTIONS
# Additional options for the compressor
#COMPRESSION_OPTIONS=()
#COMPRESSION_OPTIONS=(-c -z - --threads=0)
#COMPRESSION_OPTIONS=(-c -T0 -18 -)
EOF

使用 mkinitcpio 更新 linux 内核固件

sudo mkinitcpio -p linux                                      
[sudo] taotieren 的密码:
==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'
  -> -k /boot/efi/EFI/arch/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/efi/EFI/arch/initramfs-linux.img
==> Starting build: 5.10.13-arch1-1
  -> Running build hook: [base]
  -> Running build hook: [systemd]
  -> Running build hook: [autodetect]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/efi/EFI/arch/initramfs-linux.img
==> Image generation successful
==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'fallback'
  -> -k /boot/efi/EFI/arch/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/efi/EFI/arch/initramfs-linux-fallback.img -S autodetect
==> Starting build: 5.10.13-arch1-1
  -> Running build hook: [base]
  -> Running build hook: [systemd]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/efi/EFI/arch/initramfs-linux-fallback.img
==> Image generation successful

使用 mkinitcpio 更新 linux-lts 内核固件

sudo mkinitcpio -p linux-lts
==> Building image from preset: /etc/mkinitcpio.d/linux-lts.preset: 'default'
  -> -k /boot/efi/EFI/arch/vmlinuz-linux-lts -c /etc/mkinitcpio.conf -g /boot/efi/EFI/arch/initramfs-linux-lts.img
==> Starting build: 5.4.94-1-lts
  -> Running build hook: [base]
  -> Running build hook: [systemd]
  -> Running build hook: [autodetect]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/efi/EFI/arch/initramfs-linux-lts.img
==> Image generation successful
==> Building image from preset: /etc/mkinitcpio.d/linux-lts.preset: 'fallback'
  -> -k /boot/efi/EFI/arch/vmlinuz-linux-lts -c /etc/mkinitcpio.conf -g /boot/efi/EFI/arch/initramfs-linux-lts-fallback.img -S autodetect
==> Starting build: 5.4.94-1-lts
  -> Running build hook: [base]
  -> Running build hook: [systemd]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/efi/EFI/arch/initramfs-linux-lts-fallback.img
==> Image generation successful

修改 grub 配置文件使其也支持启动

修改 /etc/grub.d/10_linux 文件,让其能检测到我们自定义 linux/linux-lts 固件的位置

sudo sed -i s|/boot/vmlinuz-* /vmlinuz-* /boot/kernel-* ;|/boot/vmlinuz-* /vmlinuz-* /boot/kernel-* /boot/efi/EFI/arch/vmlinuz-* ;|g /etc/grub.d/10_linux
sudo sed -i s|/boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* ;|/boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* /boot/efi/EFI/arch/vmlinux-* ;|g /etc/grub.d/10_linux

详细修改:

sudo cat >/etc/grub.d/10_linux << EOF
#! /bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"

. "$pkgdatadir/grub-mkconfig_lib"

export TEXTDOMAIN=grub
export TEXTDOMAINDIR="${datarootdir}/locale"

CLASS="--class gnu-linux --class gnu --class os"

if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  OS=Linux
else
  OS="${GRUB_DISTRIBUTOR} Linux"
  CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
fi

# loop-AES arranges things so that /dev/loop/X can be our root device, but
# the initrds that Linux uses don't like that.
case ${GRUB_DEVICE} in
  /dev/loop/*|/dev/loop[0-9])
    GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
  ;;
esac

# Default to disabling partition uuid support to maintian compatibility with
# older kernels.
GRUB_DISABLE_LINUX_PARTUUID=${GRUB_DISABLE_LINUX_PARTUUID-true}

# btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
# and mounting btrfs requires user space scanning, so force UUID in this case.
if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ) \
    || ( [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
        && [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ] ) \
    || ( ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
        && ! test -e "/dev/disk/by-partuuid/${GRUB_DEVICE_PARTUUID}" ) \
    || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  LINUX_ROOT_DEVICE=${GRUB_DEVICE}
elif [ "x${GRUB_DEVICE_UUID}" = "x" ] \
    || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
  LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
else
  LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi

case x"$GRUB_FS" in
    xbtrfs)
        rootsubvol="`make_system_path_relative_to_its_root /`"
        rootsubvol="${rootsubvol#/}"
        if [ "x${rootsubvol}" != x ]; then
            GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
        fi;;
    xzfs)
        rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
        bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
        LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
        ;;
esac

title_correction_code=

linux_entry ()
{
  os="$1"
  version="$2"
  type="$3"
  args="$4"

  if [ -z "$boot_device_id" ]; then
      boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  fi
  if [ x$type != xsimple ] ; then
      case $type in
          recovery)
              title="$(gettext_printf "%s, with Linux %s (recovery mode)" "${os}" "${version}")" ;;
          fallback)
              title="$(gettext_printf "%s, with Linux %s (fallback initramfs)" "${os}" "${version}")" ;;
          *)
              title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
      esac
      if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
          replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
          quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
          title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
          grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
      fi
      echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  else
      echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  fi      
  if [ x$type != xrecovery ] ; then
      save_default_entry | grub_add_tab
  fi

  # Use ELILO's generic "efifb" when it's known to be available.
  # FIXME: We need an interface to select vesafb in case efifb can't be used.
  if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
      echo "    load_video" | sed "s/^/$submenu_indentation/"
      if grep -qx "CONFIG_FB_EFI=y" "${config}" 2> /dev/null \
          && grep -qx "CONFIG_VT_HW_CONSOLE_BINDING=y" "${config}" 2> /dev/null; then
          echo "        set gfxpayload=keep" | sed "s/^/$submenu_indentation/"
      fi
  else
      if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
          echo "        load_video" | sed "s/^/$submenu_indentation/"
      fi
      echo "    set gfxpayload=$GRUB_GFXPAYLOAD_LINUX" | sed "s/^/$submenu_indentation/"
  fi

  echo "        insmod gzio" | sed "s/^/$submenu_indentation/"

  if [ x$dirname = x/ ]; then
    if [ -z "${prepare_root_cache}" ]; then
      prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
    fi
    printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
  else
    if [ -z "${prepare_boot_cache}" ]; then
      prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
    fi
    printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
  fi
  message="$(gettext_printf "Loading Linux %s ..." ${version})"
  sed "s/^/$submenu_indentation/" << EOF
        echo    '$(echo "$message" | grub_quote)'
        linux   ${rel_dirname}/${basename} root=${linux_root_device_thisversion} rw ${args}
EOF
  if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    message="$(gettext_printf "Loading initial ramdisk ...")"
    initrd_path=
    for i in ${initrd}; do
      initrd_path="${initrd_path} ${rel_dirname}/${i}"
    done
    sed "s/^/$submenu_indentation/" << EOF
        echo    '$(echo "$message" | grub_quote)'
        initrd  $(echo $initrd_path)
EOF
  fi
  sed "s/^/$submenu_indentation/" << EOF
}
EOF
}

machine=`uname -m`
case "x$machine" in
    xi?86 | xx86_64)
        list=
        for i in /boot/vmlinuz-* /vmlinuz-* /boot/kernel-* /boot/efi/EFI/arch/vmlinuz-* ; do
            if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
        done ;;
    *) 
        list=
        for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* /boot/efi/EFI/arch/vmlinux-* ; do
                  if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
        done ;;
esac

case "$machine" in
    i?86) GENKERNEL_ARCH="x86" ;;
    mips|mips64) GENKERNEL_ARCH="mips" ;;
    mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
    arm*) GENKERNEL_ARCH="arm" ;;
    *) GENKERNEL_ARCH="$machine" ;;
esac

prepare_boot_cache=
prepare_root_cache=
boot_device_id=
title_correction_code=

# Extra indentation to add to menu entries in a submenu. We're not in a submenu
# yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
submenu_indentation=""

is_top_level=true
while [ "x$list" != "x" ] ; do
  linux=`version_find_latest $list`
  gettext_printf "Found linux image: %s\n" "$linux" >&2
  basename=`basename $linux`
  dirname=`dirname $linux`
  rel_dirname=`make_system_path_relative_to_its_root $dirname`
  version=`echo $basename | sed -e "s,vmlinuz-,,g"`
  alt_version=`echo $version | sed -e "s,\.old$,,g"`
  linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"

  initrd_early=
  for i in ${GRUB_EARLY_INITRD_LINUX_STOCK} \
           ${GRUB_EARLY_INITRD_LINUX_CUSTOM}; do
    if test -e "${dirname}/${i}" ; then
      initrd_early="${initrd_early} ${i}"
    fi
  done

  initrd_real=
  for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
           "initrd-${version}" "initramfs-${version}.img" \
           "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
           "initrd-${alt_version}" "initramfs-${alt_version}.img" \
           "initramfs-genkernel-${version}" \
           "initramfs-genkernel-${alt_version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
    if test -e "${dirname}/${i}" ; then
      initrd_real="${i}"
      break
    fi
  done

  initrd=
  if test -n "${initrd_early}" || test -n "${initrd_real}"; then
    initrd="${initrd_early} ${initrd_real}"

    initrd_display=
    for i in ${initrd}; do
      initrd_display="${initrd_display} ${dirname}/${i}"
    done
    gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
  fi

  config=
  for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
    if test -e "${i}" ; then
      config="${i}"
      break
    fi
  done

  initramfs=
  if test -n "${config}" ; then
      initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \"`
  fi

  if test -z "${initramfs}" && test -z "${initrd_real}" ; then
    # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
    # no initrd or builtin initramfs, it can't work here.
    if [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] \
        || [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ]; then

        linux_root_device_thisversion=${GRUB_DEVICE}
    else
        linux_root_device_thisversion=PARTUUID=${GRUB_DEVICE_PARTUUID}
    fi
  fi

  if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
    linux_entry "${OS}" "${version}" simple \
    "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"

    submenu_indentation="$grub_tab"
    
    if [ -z "$boot_device_id" ]; then
        boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
    fi
    # TRANSLATORS: %s is replaced with an OS name
    echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
    is_top_level=false
  fi

  linux_entry "${OS}" "${version}" advanced \
              "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"

  if test -e "${dirname}/initramfs-${version}-fallback.img" ; then
    initrd="initramfs-${version}-fallback.img"

    if test -n "${initrd}" ; then
      gettext_printf "Found fallback initrd image(s) in %s:%s\n" "${dirname}" "${initrd_extra} ${initrd}" >&2
    fi

    linux_entry "${OS}" "${version}" fallback \
                "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
  fi

  if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
    linux_entry "${OS}" "${version}" recovery \
                "single ${GRUB_CMDLINE_LINUX}"
  fi

  list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
done

# If at least one kernel was found, then we need to
# add a closing '}' for the submenu command.
if [ x"$is_top_level" != xtrue ]; then
  echo '}'
fi

echo "$title_correction_code"
EOF

更新 /boot/grub/grub.cfg 文件

sudo grub-mkconfig -o /boot/grub/grub.cfg 
Generating grub configuration file ...
Found theme: /boot/grub/themes/Archlinux/theme.txt
Found linux image: /boot/efi/EFI/arch/vmlinuz-linux-lts
Found initrd image: /boot/efi/EFI/arch/intel-ucode.img /boot/efi/EFI/arch/initramfs-linux-lts.img
Found fallback initrd image(s) in /boot/efi/EFI/arch: initramfs-linux-lts-fallback.img
Found linux image: /boot/efi/EFI/arch/vmlinuz-linux
Found initrd image: /boot/efi/EFI/arch/intel-ucode.img /boot/efi/EFI/arch/initramfs-linux.img
Found fallback initrd image(s) in /boot/efi/EFI/arch: initramfs-linux-fallback.img
Found Windows Boot Manager on /dev/sda1@/efi/Microsoft/Boot/bootmgfw.efi
done

至此,Arch Linux 同时支持 systemd-bootgrub 引导启动。其他 Linux 发行版可以参照这个详细的修改过程。

紧急更新

前几天 ArchLinux 发布新的内核,使用了 glibc-2.33 版本。修改 /etc/mkinitcpoio.d/*.preset/etc/grub.d/10_linux 会导致使用新库的内核报安装错误。

systemd-boot 安装方式需要安装 Arch WiKi: systemd-boot (简体中文)) 中的说明将 EFI 分区挂载到 /boot 下。详细过程:

  • 备份原有的 /boot 下的文件,并将 /boot/grub 复制到 /boot/efi/ 下。
sudo cp -rv /boot/* ~/boot
sudo cp -rv /boot/grub /boot/efi
  • 卸载 EFI 的挂载
sudo umonut -R /boot

  • 清空 /boot 目录
sudo rm -rf /boot/*

  • EFI 分区挂载到 /boot
# 假设 EFI 在 /dev/sdb1
sudo mount /dev/sdb1 /boot
  • /boot/EFI/arch 内核镜像全部移动到 /boot
sudo cp -rv /boot/EFI/arch/* /boot
sudo rm -rf /boot/EFI/arch
  • 修改 /boot/loader/entries/arch*.conf 文件
sudo sed -i s|/boot/efi/EFI/arch/|/|g  /boot/loader/entries/arch*.conf
sudo cat > /boot/loader/entries/arch-lts.conf << EOF
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title Arch Linux LTS
linux /vmlinuz-linux-lts
initrd /intel-ucode.img
initrd /initramfs-linux-lts.img
#initrd /initramfs-linux-lts-fallback.img
options root=PARTUUID=40d2d5c0-7d8e-a249-99b4-caa190dd1257 rw

EOF
sudo cat > /boot/loader/entries/arch.conf << EOF 
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title Arch Linux
linux /vmlinuz-linux
initrd /intel-ucode.img
initrd /initramfs-linux.img
#initrd /initramfs-linux-fallback.img
options root=PARTUUID=40d2d5c0-7d8e-a249-99b4-caa190dd1257 rw

EOF
sudo cat > /boot/loader/entries/boot.conf << EOF
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title   BOOT
efi     /EFI/BOOT/BOOTX64.EFI

EOF
sudo  cat > /boot/loader/entries/systemd.conf << EOF
## This is just an example config file.
## Please edit the paths and kernel parameters according to your system.

title   systemd
efi     /EFI/systemd/systemd-bootx64.efi

EOF
  • 更新并查看 bootctl 状态
sudo bootctl update

sudo bootctl status                   
System:
     Firmware: UEFI 2.31 (American Megatrends 4.653)
  Secure Boot: disabled
   Setup Mode: user
 Boot into FW: supported

Current Boot Loader:
      Product: systemd-boot 247.3-1-arch
     Features: ✓ Boot counting
               ✓ Menu timeout control
               ✓ One-shot menu timeout control
               ✓ Default entry control
               ✓ One-shot entry control
               ✓ Support for XBOOTLDR partition
               ✓ Support for passing random seed to OS
               ✓ Boot loader sets ESP partition information
          ESP: /dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325
         File: └─/EFI/systemd/systemd-bootx64.efi

Random Seed:
 Passed to OS: yes
 System Token: set
       Exists: yes

Available Boot Loaders on ESP:
          ESP: /boot (/dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325)
         File: └─/EFI/systemd/systemd-bootx64.efi (systemd-boot 247.3-1-arch)
         File: └─/EFI/BOOT/BOOTX64.EFI (systemd-boot 247.3-1-arch)

Boot Loaders Listed in EFI Variables:
        Title: Linux Boot Manager
           ID: 0x0002
       Status: active, boot-order
    Partition: /dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325
         File: └─/EFI/systemd/systemd-bootx64.efi

        Title: ArchLinux
           ID: 0x0001
       Status: active, boot-order
    Partition: /dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325
         File: └─/EFI/ArchLinux/grubx64.efi

        Title: Windows Boot Manager
           ID: 0x0000
       Status: active, boot-order
    Partition: /dev/disk/by-partuuid/37ac3214-8a21-4b89-b379-8cfc664a7b79
         File: └─/EFI/Microsoft/Boot/bootmgfw.efi

Boot Loader Entries:
        $BOOT: /boot (/dev/disk/by-partuuid/65791d8f-8b4f-45c8-9145-b8444a6a8325)

Default Boot Loader Entry:
        title: systemd
           id: systemd.conf
       source: /boot/loader/entries/systemd.conf

  • /etc/mkinitcpio.conf 的修改并还原 /etc/mkinitcpio.d/* 下的修改
sudo cat > /etc/mkinitcpio.conf << EOF
# vim:set ft=sh
# MODULES
# The following modules are loaded before any boot hooks are
# run.  Advanced users may wish to specify all system modules
# in this array.  For instance:
#     MODULES=(piix ide_disk reiserfs)
MODULES=()

# BINARIES
# This setting includes any additional binaries a given user may
# wish into the CPIO image.  This is run last, so it may be used to
# override the actual binaries included by a given hook
# BINARIES are dependency parsed, so you may safely ignore libraries
BINARIES=()

# FILES
# This setting is similar to BINARIES above, however, files are added
# as-is and are not parsed in any way.  This is useful for config files.
FILES=()

# HOOKS
# This is the most important setting in this file.  The HOOKS control the
# modules and scripts added to the image, and what happens at boot time.
# Order is important, and it is recommended that you do not change the
# order in which HOOKS are added.  Run 'mkinitcpio -H <hook name>' for
# help on a given hook.
# 'base' is _required_ unless you know precisely what you are doing.
# 'udev' is _required_ in order to automatically load modules
# 'filesystems' is _required_ unless you specify your fs modules in MODULES
# Examples:
##   This setup specifies all modules in the MODULES setting above.
##   No raid, lvm2, or encrypted root is needed.
#    HOOKS=(base)
#
##   This setup will autodetect all modules for your system and should
##   work as a sane default
#    HOOKS=(base udev autodetect block filesystems)
#
##   This setup will generate a 'full' image which supports most systems.
##   No autodetection is done.
#    HOOKS=(base udev block filesystems)
#
##   This setup assembles a pata mdadm array with an encrypted root FS.
##   Note: See 'mkinitcpio -H mdadm' for more information on raid devices.
#    HOOKS=(base udev block mdadm encrypt filesystems)
#
##   This setup loads an lvm2 volume group on a usb device.
#    HOOKS=(base udev block lvm2 filesystems)
#
##   NOTE: If you have /usr on a separate partition, you MUST include the
#    usr, fsck and shutdown hooks.
HOOKS=(base udev autodetect modconf block filesystems keyboard fsck systemd)

# COMPRESSION
# Use this to compress the initramfs image. By default, gzip compression
# is used. Use 'cat' to create an uncompressed image.
#COMPRESSION="gzip"
#COMPRESSION="bzip2"
#COMPRESSION="lzma"
#COMPRESSION="xz"
#COMPRESSION="lzop"
#COMPRESSION="lz4"
#COMPRESSION="zstd"

# COMPRESSION_OPTIONS
# Additional options for the compressor
#COMPRESSION_OPTIONS=()
#COMPRESSION_OPTIONS=(-c -z - --threads=0)
#COMPRESSION_OPTIONS=(-c -T0 -18 -)

EOF
sudo cat > /etc/mkinitcpio.d/linux.preset << EOF
# mkinitcpio preset file for the 'linux' package

ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"

PRESETS=('default' 'fallback')

#default_config="/etc/mkinitcpio.conf"
default_image="/boot/initramfs-linux.img"
#default_options=""

#fallback_config="/etc/mkinitcpio.conf"
fallback_image="/boot/initramfs-linux-fallback.img"
fallback_options="-S autodetect"

EOF

sudo cat > /etc/mkinitcpio.d/linux-lts.preset << EOF
# mkinitcpio preset file for the 'linux-lts' package

ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux-lts"

PRESETS=('default' 'fallback')

#default_config="/etc/mkinitcpio.conf"
default_image="/boot/initramfs-linux-lts.img"
#default_options=""

#fallback_config="/etc/mkinitcpio.conf"
fallback_image="/boot/initramfs-linux-lts-fallback.img"
fallback_options="-S autodetect"

EOF
  • 更新内核固件
sudo mkinitcpio -P      
==> Building image from preset: /etc/mkinitcpio.d/linux-lts.preset: 'default'
  -> -k /boot/vmlinuz-linux-lts -c /etc/mkinitcpio.conf -g /boot/initramfs-linux-lts.img
==> Starting build: 5.4.97-1-lts
  -> Running build hook: [base]
  -> Running build hook: [udev]
  -> Running build hook: [autodetect]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
  -> Running build hook: [systemd]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/initramfs-linux-lts.img
==> Image generation successful
==> Building image from preset: /etc/mkinitcpio.d/linux-lts.preset: 'fallback'
  -> -k /boot/vmlinuz-linux-lts -c /etc/mkinitcpio.conf -g /boot/initramfs-linux-lts-fallback.img -S autodetect
==> Starting build: 5.4.97-1-lts
  -> Running build hook: [base]
  -> Running build hook: [udev]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
  -> Running build hook: [systemd]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/initramfs-linux-lts-fallback.img
==> Image generation successful
==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'default'
  -> -k /boot/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/initramfs-linux.img
==> Starting build: 5.10.15-arch1-1
  -> Running build hook: [base]
  -> Running build hook: [udev]
  -> Running build hook: [autodetect]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
  -> Running build hook: [systemd]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/initramfs-linux.img
==> Image generation successful
==> Building image from preset: /etc/mkinitcpio.d/linux.preset: 'fallback'
  -> -k /boot/vmlinuz-linux -c /etc/mkinitcpio.conf -g /boot/initramfs-linux-fallback.img -S autodetect
==> Starting build: 5.10.15-arch1-1
  -> Running build hook: [base]
  -> Running build hook: [udev]
  -> Running build hook: [modconf]
  -> Running build hook: [block]
  -> Running build hook: [filesystems]
  -> Running build hook: [keyboard]
  -> Running build hook: [fsck]
  -> Running build hook: [systemd]
==> Generating module dependencies
==> Creating gzip-compressed initcpio image: /boot/initramfs-linux-fallback.img
==> Image generation successful

  • 还原/etc/grub.d/10_linux 的修改并更新 grub
sudo cat >/etc/grub.d/10_linux << EOF
#! /bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# GRUB is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GRUB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GRUB.  If not, see <http://www.gnu.org/licenses/>.

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"

. "$pkgdatadir/grub-mkconfig_lib"

export TEXTDOMAIN=grub
export TEXTDOMAINDIR="${datarootdir}/locale"

CLASS="--class gnu-linux --class gnu --class os"

if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  OS=Linux
else
  OS="${GRUB_DISTRIBUTOR} Linux"
  CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
fi

# loop-AES arranges things so that /dev/loop/X can be our root device, but
# the initrds that Linux uses don't like that.
case ${GRUB_DEVICE} in
  /dev/loop/*|/dev/loop[0-9])
    GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
  ;;
esac

# Default to disabling partition uuid support to maintian compatibility with
# older kernels.
GRUB_DISABLE_LINUX_PARTUUID=${GRUB_DISABLE_LINUX_PARTUUID-true}

# btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
# and mounting btrfs requires user space scanning, so force UUID in this case.
if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] ) \
    || ( [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
        && [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ] ) \
    || ( ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
        && ! test -e "/dev/disk/by-partuuid/${GRUB_DEVICE_PARTUUID}" ) \
    || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
  LINUX_ROOT_DEVICE=${GRUB_DEVICE}
elif [ "x${GRUB_DEVICE_UUID}" = "x" ] \
    || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
  LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
else
  LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
fi

case x"$GRUB_FS" in
    xbtrfs)
        rootsubvol="`make_system_path_relative_to_its_root /`"
        rootsubvol="${rootsubvol#/}"
        if [ "x${rootsubvol}" != x ]; then
            GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
        fi;;
    xzfs)
        rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
        bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
        LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs%/}"
        ;;
esac

title_correction_code=

linux_entry ()
{
  os="$1"
  version="$2"
  type="$3"
  args="$4"

  if [ -z "$boot_device_id" ]; then
      boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
  fi
  if [ x$type != xsimple ] ; then
      case $type in
          recovery)
              title="$(gettext_printf "%s, with Linux %s (recovery mode)" "${os}" "${version}")" ;;
          fallback)
              title="$(gettext_printf "%s, with Linux %s (fallback initramfs)" "${os}" "${version}")" ;;
          *)
              title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
      esac
      if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
          replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
          quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
          title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
          grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
      fi
      echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  else
      echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
  fi      
  if [ x$type != xrecovery ] ; then
      save_default_entry | grub_add_tab
  fi

  # Use ELILO's generic "efifb" when it's known to be available.
  # FIXME: We need an interface to select vesafb in case efifb can't be used.
  if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
      echo "    load_video" | sed "s/^/$submenu_indentation/"
      if grep -qx "CONFIG_FB_EFI=y" "${config}" 2> /dev/null \
          && grep -qx "CONFIG_VT_HW_CONSOLE_BINDING=y" "${config}" 2> /dev/null; then
          echo "        set gfxpayload=keep" | sed "s/^/$submenu_indentation/"
      fi
  else
      if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
          echo "        load_video" | sed "s/^/$submenu_indentation/"
      fi
      echo "    set gfxpayload=$GRUB_GFXPAYLOAD_LINUX" | sed "s/^/$submenu_indentation/"
  fi

  echo "        insmod gzio" | sed "s/^/$submenu_indentation/"

  if [ x$dirname = x/ ]; then
    if [ -z "${prepare_root_cache}" ]; then
      prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
    fi
    printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
  else
    if [ -z "${prepare_boot_cache}" ]; then
      prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
    fi
    printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
  fi
  message="$(gettext_printf "Loading Linux %s ..." ${version})"
  sed "s/^/$submenu_indentation/" << EOF
        echo    '$(echo "$message" | grub_quote)'
        linux   ${rel_dirname}/${basename} root=${linux_root_device_thisversion} rw ${args}
EOF
  if test -n "${initrd}" ; then
    # TRANSLATORS: ramdisk isn't identifier. Should be translated.
    message="$(gettext_printf "Loading initial ramdisk ...")"
    initrd_path=
    for i in ${initrd}; do
      initrd_path="${initrd_path} ${rel_dirname}/${i}"
    done
    sed "s/^/$submenu_indentation/" << EOF
        echo    '$(echo "$message" | grub_quote)'
        initrd  $(echo $initrd_path)
EOF
  fi
  sed "s/^/$submenu_indentation/" << EOF
}
EOF
}

machine=`uname -m`
case "x$machine" in
    xi?86 | xx86_64)
        list=
        for i in /boot/vmlinuz-* /vmlinuz-* /boot/kernel-* ; do
            if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
        done ;;
    *) 
        list=
        for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* ; do
                  if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
        done ;;
esac

case "$machine" in
    i?86) GENKERNEL_ARCH="x86" ;;
    mips|mips64) GENKERNEL_ARCH="mips" ;;
    mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
    arm*) GENKERNEL_ARCH="arm" ;;
    *) GENKERNEL_ARCH="$machine" ;;
esac

prepare_boot_cache=
prepare_root_cache=
boot_device_id=
title_correction_code=

# Extra indentation to add to menu entries in a submenu. We're not in a submenu
# yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
submenu_indentation=""

is_top_level=true
while [ "x$list" != "x" ] ; do
  linux=`version_find_latest $list`
  gettext_printf "Found linux image: %s\n" "$linux" >&2
  basename=`basename $linux`
  dirname=`dirname $linux`
  rel_dirname=`make_system_path_relative_to_its_root $dirname`
  version=`echo $basename | sed -e "s,vmlinuz-,,g"`
  alt_version=`echo $version | sed -e "s,\.old$,,g"`
  linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"

  initrd_early=
  for i in ${GRUB_EARLY_INITRD_LINUX_STOCK} \
           ${GRUB_EARLY_INITRD_LINUX_CUSTOM}; do
    if test -e "${dirname}/${i}" ; then
      initrd_early="${initrd_early} ${i}"
    fi
  done

  initrd_real=
  for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
           "initrd-${version}" "initramfs-${version}.img" \
           "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
           "initrd-${alt_version}" "initramfs-${alt_version}.img" \
           "initramfs-genkernel-${version}" \
           "initramfs-genkernel-${alt_version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
           "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
    if test -e "${dirname}/${i}" ; then
      initrd_real="${i}"
      break
    fi
  done

  initrd=
  if test -n "${initrd_early}" || test -n "${initrd_real}"; then
    initrd="${initrd_early} ${initrd_real}"

    initrd_display=
    for i in ${initrd}; do
      initrd_display="${initrd_display} ${dirname}/${i}"
    done
    gettext_printf "Found initrd image: %s\n" "$(echo $initrd_display)" >&2
  fi

  config=
  for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
    if test -e "${i}" ; then
      config="${i}"
      break
    fi
  done

  initramfs=
  if test -n "${config}" ; then
      initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \"`
  fi

  if test -z "${initramfs}" && test -z "${initrd_real}" ; then
    # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
    # no initrd or builtin initramfs, it can't work here.
    if [ "x${GRUB_DEVICE_PARTUUID}" = "x" ] \
        || [ "x${GRUB_DISABLE_LINUX_PARTUUID}" = "xtrue" ]; then

        linux_root_device_thisversion=${GRUB_DEVICE}
    else
        linux_root_device_thisversion=PARTUUID=${GRUB_DEVICE_PARTUUID}
    fi
  fi

  if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
    linux_entry "${OS}" "${version}" simple \
    "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"

    submenu_indentation="$grub_tab"
    
    if [ -z "$boot_device_id" ]; then
        boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
    fi
    # TRANSLATORS: %s is replaced with an OS name
    echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
    is_top_level=false
  fi

  linux_entry "${OS}" "${version}" advanced \
              "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"

  if test -e "${dirname}/initramfs-${version}-fallback.img" ; then
    initrd="initramfs-${version}-fallback.img"

    if test -n "${initrd}" ; then
      gettext_printf "Found fallback initrd image(s) in %s:%s\n" "${dirname}" "${initrd_extra} ${initrd}" >&2
    fi

    linux_entry "${OS}" "${version}" fallback \
                "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
  fi

  if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
    linux_entry "${OS}" "${version}" recovery \
                "single ${GRUB_CMDLINE_LINUX}"
  fi

  list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
done

# If at least one kernel was found, then we need to
# add a closing '}' for the submenu command.
if [ x"$is_top_level" != xtrue ]; then
  echo '}'
fi

echo "$title_correction_code"

EOF
sudo grub-mkconfig -o /boot/grub/grub.cfg 
Generating grub configuration file ...
Found theme: /boot/grub/themes/Archlinux/theme.txt
Found linux image: /boot/vmlinuz-linux-lts
Found initrd image: /boot/intel-ucode.img /boot/initramfs-linux-lts.img
Found fallback initrd image(s) in /boot: initramfs-linux-lts-fallback.img
Found linux image: /boot/vmlinuz-linux
Found initrd image: /boot/intel-ucode.img /boot/initramfs-linux.img
Found fallback initrd image(s) in /boot: initramfs-linux-fallback.img
Found Windows Boot Manager on /dev/sda1@/efi/Microsoft/Boot/bootmgfw.efi
done

增加 rEFInd 启动引导管理器

yay -S refind  
resolving dependencies...
looking for conflicting packages...

Packages (1) refind-0.13.2-1

Total Installed Size:  5.01 MiB

:: Proceed with installation? [Y/n] 
(1/1) checking keys in keyring                     100%
(1/1) checking package integrity                   100%
(1/1) loading package files                        100%
(1/1) checking for file conflicts                  100%
(1/1) checking available disk space                100%
:: Processing package changes...
(1/1) installing refind                            100%
Optional dependencies for refind
    gptfdisk: for finding non-vfat ESP with refind-install
    [installed]
    imagemagick: for refind-mkfont [installed]
    openssl: for generating local certificates with
    refind-install [installed]
    python: for refind-mkdefault [installed]
    sbsigntools: for EFI binary signing with
    refind-install [installed]
    sudo: for privilege elevation in refind-install and
    refind-mkdefault [installed]
:: Running post-transaction hooks...
(1/2) Arming ConditionNeedsUpdate...
(2/2) Refreshing PackageKit...
sudo refind-install         
ShimSource is none
Installing rEFInd on Linux....
ESP was found at /boot using vfat
Copied rEFInd binary files

Copying sample configuration file as refind.conf; edit this file to configure
rEFInd.

Keeping existing NVRAM entry
rEFInd is set as the default boot manager.
Existing //boot/refind_linux.conf found; not overwriting.
cat /boot/refind_linux.conf
"Boot with standard options"  "root=UUID=9de8a3e9-dd43-7d4c-ba52-2d40ea4895d4 rw loglevel=5 nowatchdog"
"Boot to single-user mode"    "root=UUID=9de8a3e9-dd43-7d4c-ba52-2d40ea4895d4 rw loglevel=5 nowatchdog single"
"Boot with minimal options"   "ro root=UUID=9de8a3e9-dd43-7d4c-ba52-2d40ea4895d4"

sudo blkid /dev/sdb2
/dev/sdb2: UUID="9de8a3e9-dd43-7d4c-ba52-2d40ea4895d4" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="Linux data partition" PARTUUID="630c7f4e-5c47-0a42-b64f-e2c01a16f944"

启用安装启动:Arch WiKi

sudo refind-install --shim /usr/share/shim-signed/shimx64.efi

后续如果有问题会继续更新。

 类似资料: