grub.cfg 实例

优质
小牛编辑
149浏览
2023-12-01

下面是本文作者实际使用的一个"grub.cfg"文件,通用于BIOS与UEFI模式,放在这里当作一个实例,供读者参考:

#由于"$prefix"的值是在"grub-install"安装时确定的,并且嵌入'core.img'中的模块也是随硬件变化的,
#所以不要只是简单的复制'grub'目录到处使用,而应该在每一个介质上都使用"grub-install"进行安装。
###############################################################################
#(1)本配置文件要求"grub"目录所在分区的卷标必须是"GRUB2"。
#(2)为了保持最大程度的BIOS兼容性,"GRUB2"分区必须位于磁盘的前 137GB 范围。
###############################################################################
# 如果要在windows上安装GRUB2的话,必须首先以管理员身份打开命令提示符,然后运行
# wmic diskdrive list brief
# 命令查看安装目标,最后再使用例如下面这样的命令进行安装:
# grub-install.exe --boot-directory=g: --recheck --target=x86_64-efi --efi-directory=g: --no-nvram --removable \\.\PHYSICALDRIVE5
# grub-install.exe --boot-directory=g: --recheck --target=i386-pc \\.\PHYSICALDRIVE5
# 如果"GRUB2"分区不在磁盘的前 137GB 范围,可以尝试使用下面的命令安装(不完美的补救措施):
# grub-install.exe --boot-directory=g: --recheck --target=i386-pc --disk-module=native \\.\PHYSICALDRIVE5
###############################################################################
# binwalk initrd.img
# https://unix.stackexchange.com/questions/163346/why-is-it-that-my-initrd-only-has-one-directory-namely-kernel


set label=GRUB2
set check_signatures=no
set default=0
set fallback=1
set gfxmode=1024x768,auto
set gfxterm_font=WenQuanYiMicroHeiMono
set lang=zh_CN
set locale_dir=$prefix/locale
set pager=1
set superusers=root
set timeout=5


insmod part_gpt
insmod part_msdos
insmod fat
insmod exfat
insmod ntfs
insmod iso9660
insmod ext2
insmod xfs
insmod all_video
insmod gfxterm
insmod png


terminal_output  gfxterm
background_image $prefix/themes/wmoon.png
loadfont  24px
password_pbkdf2 root grub.pbkdf2.sha512.69.3AE287EEFF7205CC.C0C0B1517369963D14FADE30BE588E56D88E86451C9D3FB9B51917DBBF2F8E8A


menuentry '正常启动(Windows)' --unrestricted {
  if [ 'pc' == $grub_platform ] ; then
    if search --file --set /bootmgr ; then
      chainloader +1
    elif search --file --set /ntldr ; then
      chainloader +1
    fi
  elif [ 'efi' == $grub_platform ] ; then
    if search --file --set /EFI/Microsoft/Boot/bootmgfw.efi ; then
      chainloader /EFI/Microsoft/Boot/bootmgfw.efi
    fi
  fi
}


#http://www.wepe.com.cn/download.html
if [ 'pc' == $grub_platform -a -f $prefix/winpe/memdisk ] ; then
  if [ -f $prefix/winpe/win8pe64.iso ] ; then
    menuentry '[BIOS]启动64位 Win 8 PE (微PE)' --unrestricted {
      linux16  $prefix/winpe/memdisk iso raw
      initrd16 $prefix/winpe/win8pe64.iso
    }
  fi
  if [ -f $prefix/winpe/win10pe64.iso ] ; then
    menuentry '[BIOS]启动64位 Win 10 PE (微PE)' --unrestricted {
      linux16  $prefix/winpe/memdisk iso raw
      initrd16 $prefix/winpe/win10pe64.iso
    }
  fi
fi
if [ 'efi' == $grub_platform -a -f $prefix/winpe/bootmgfw.efi -a -f $prefix/winpe/BCD ] ; then
  if [ -f $prefix/winpe/win10pe64.wim -a -f $prefix/winpe/win8pe64.wim ] ; then
    menuentry '[UEFI]启动64位 Windows 8/10 PE →' --unrestricted {
      chainloader $prefix/winpe/bootmgfw.efi
    }
  fi
fi


#https://mirrors.tuna.tsinghua.edu.cn/archlinux/iso/latest/
if [ -f $prefix/linux/archlinux.iso ] ; then
  menuentry 'Arch Linux LiveCD [root/空]' --unrestricted {
    loopback loop0 $prefix/linux/archlinux.iso
    linux  (loop0)/arch/boot/x86_64/vmlinuz img_label=$label img_loop=/grub/linux/archlinux.iso   systemd.wants=sshd.service
    initrd (loop0)/arch/boot/x86_64/archiso.img
  }
fi

#https://mirrors.163.com/gentoo/releases/amd64/autobuilds/current-install-amd64-minimal/
if [ -f $prefix/linux/install-amd64-minimal.iso ] ; then
  menuentry 'Mini Gentoo LiveCD [root/123]' --unrestricted {
    loopback loop0 $prefix/linux/install-amd64-minimal.iso
    linux  (loop0)/boot/gentoo cdroot isoboot=/grub/linux/install-amd64-minimal.iso  dosshd nokeymap passwd=123
    initrd (loop0)/boot/gentoo.igz
  }
fi

#https://mirror-hk.koddos.net/calculate-linux/release/
#https://www.calculate-linux.org/main/en/download
# Calculate Linux Scratch 是基于Gentoo制作的 LiveCD ,包含编译工具、支持WiFi
# 既可用作LFS(Linux From Scratch)的宿主系统、也可用于安装 Gentoo
if [ -f $prefix/linux/cls.iso ] ; then
  menuentry 'Live GCC x64 [root/root]' --unrestricted {
    loopback loop0 $prefix/linux/cls.iso
    linux  (loop0)/boot/vmlinuz root=live iso-scan/filename=/grub/linux/cls.iso   vga=current nodevfs noresume nodmraid
    initrd (loop0)/boot/initrd
  }
fi

#https://mirrors.huaweicloud.com/centos/7/isos/x86_64/
if [ -f $prefix/linux/CentOS-LiveGNOME.iso ] ; then
  menuentry 'CentOS 7.8 GNOME LiveCD [root/空](可退出liveuser后再用root登录)' --unrestricted {
    loopback loop0 $prefix/linux/CentOS-LiveGNOME.iso
    linux  (loop0)/isolinux/vmlinuz0 rd.live.image root=live:CDLABEL=CentOS-7-x86_64-LiveGNOME-2003 iso-scan/filename=/grub/linux/CentOS-LiveGNOME.iso   systemd.wants=sshd.service inst.lang=zh_CN
    initrd (loop0)/isolinux/initrd0.img
  }
fi

#https://ftp.sjtu.edu.cn/fedora/linux/releases/32/Spins/x86_64/iso/
#只有 Xfce LiveCD 可以设置中文界面
if [ -f $prefix/linux/Fedora-Xfce-Live.iso ] ; then
  menuentry 'Fedora 32 Xfce LiveCD [root/空](自动liveuser登录但可切换至root用户)' --unrestricted {
    loopback loop0 $prefix/linux/Fedora-Xfce-Live.iso
    linux  (loop0)/isolinux/vmlinuz rd.live.image root=live:CDLABEL=Fedora-Xfce-Live-32-1-6 iso-scan/filename=/grub/linux/Fedora-Xfce-Live.iso   systemd.wants=sshd.service locale.LANG=zh_CN.utf8 inst.lang=zh_CN
    initrd (loop0)/isolinux/initrd.img
  }
fi

#https://anaconda-installer.readthedocs.io/en/latest/boot-options.html
#硬盘安装通用于所有包含"Packages"或"BaseOS"目录的 CentOS/Fedora ISO映像(Minimal,DVD,Everything)
#https://mirrors.zju.edu.cn/centos/8/isos/x86_64/
if [ -f $prefix/linux/CentOS-dvd1.iso ] ; then
  menuentry '硬盘安装 CentOS' --unrestricted {
    loopback loop0 $prefix/linux/CentOS-dvd1.iso
    linux  (loop0)/isolinux/vmlinuz inst.repo=hd:LABEL=$label:/grub/linux/CentOS-dvd1.iso   inst.lang=zh_CN
    initrd (loop0)/isolinux/initrd.img
  }
fi

#网络安装通用于所有包含"images"目录的 CentOS/Fedora ISO映像(Minimal,DVD,Everything,NetInstall,boot)
#https://mirrors.cqu.edu.cn/CentOS/8/isos/x86_64/
if [ -f $prefix/linux/CentOS-boot.iso ] ; then
  menuentry '网络安装 CentOS [支持WiFi]' --unrestricted {
    loopback loop0 $prefix/linux/CentOS-boot.iso
    linux  (loop0)/images/pxeboot/vmlinuz inst.stage2=hd:LABEL=$label:/grub/linux/CentOS-boot.iso   inst.repo=https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/ inst.lang=zh_CN ip=dhcp nameserver=223.6.6.6
    initrd (loop0)/images/pxeboot/initrd.img
  }
fi

#https://mirrors.ustc.edu.cn/debian-cd/current-live/amd64/iso-hybrid/
#也适用于'Kali LiveCD [root/toor]'( username=root hostname=kali ) https://cdimage.kali.org/current/
if [ -f $prefix/linux/debian-live-gnome.iso ] ; then
  menuentry 'Debian 10.5 GNOME LiveCD (NO SSH)' --unrestricted {
    loopback loop0 $prefix/linux/debian-live-gnome.iso
    linux  (loop0)/live/vmlinuz-4.19.0-10-amd64 boot=live findiso=/grub/linux/debian-live-gnome.iso   components locales=zh_CN.UTF-8
    initrd (loop0)/live/initrd.img-4.19.0-10-amd64
  }
fi

#https://mirrors.cloud.tencent.com/ubuntu-cdimage/lubuntu/releases/
#也适用于 'KDE neon LiveCD'(initrd.lz; apparmor=0) https://files.kde.org/neon/images/user/current/neon-user-current.iso
if [ -f $prefix/linux/lubuntu.iso ] ; then
  menuentry 'Ubuntu LXQt LiveCD (NO SSH)' --unrestricted {
    loopback loop0 $prefix/linux/lubuntu.iso
    linux  (loop0)/casper/vmlinuz boot=casper iso-scan/filename=/grub/linux/lubuntu.iso   username=root locale=zh_CN keyboard-configuration/layoutcode=us
    initrd (loop0)/casper/initrd
  }
fi

#https://www.debian.org/releases/stable/amd64/ch05s03.zh-cn.html
#https://www.debian.org/releases/stable/amd64/ch06s03.zh-cn.html
#最好将用于硬盘安装的ISO映像(例如 debian-10.4.0-amd64-xfce-CD-1.iso)放在文件系统的根目录或第一层子目录中(可简化ISO映像的搜索)
#http://mirrors.yun-idc.com/debian/dists/stable/main/installer-amd64/current/images/hd-media/
#[注意]此方法不适用于 Ubuntu
if [ -f $prefix/linux/debian/vmlinuz -a -f $prefix/linux/debian/initrd.gz ] ; then
  menuentry '硬盘安装 Debian [自动搜索ISO映像(最好放在根目录)]' --unrestricted {
    linux  $prefix/linux/debian/vmlinuz   priority=low vga=791 locale=zh_CN
    initrd $prefix/linux/debian/initrd.gz
  }
fi

#https://mirrors.163.com/debian/dists/stable/main/installer-amd64/current/images/netboot/
#https://mirrors.163.com/ubuntu/dists/focal/main/installer-amd64/current/legacy-images/netboot/
#网络安装也适用于 Ubuntu 的 mini.iso 映像(支持WiFi)
if [ -f $prefix/linux/debian/mini.iso ] ; then
  menuentry '网络安装 Debian [不支持WiFi]' --unrestricted {
    loopback loop0 $prefix/linux/debian/mini.iso
    linux  (loop0)/linux   priority=low vga=791 locale=zh_CN
    initrd (loop0)/initrd.gz
  }
fi

#https://mirrors.aliyun.com/opensuse/distribution/openSUSE-stable/live/
if [ -f $prefix/linux/openSUSE-Leap-KDE-Live.iso ] ; then
  menuentry 'openSUSE Leap 15.2 KDE LiveCD [root/空]' --unrestricted {
    loopback loop0 $prefix/linux/openSUSE-Leap-KDE-Live.iso
    linux  (loop0)/boot/x86_64/loader/linux root=live:CDLABEL=openSUSE_Leap_15.2_KDE_Live iso-scan/filename=/grub/linux/openSUSE-Leap-KDE-Live.iso   systemd.wants=sshd.service lang=zh_CN
    initrd (loop0)/boot/x86_64/loader/initrd
  }
fi

#https://doc.opensuse.org/documentation/leap/startup/html/book.opensuse.startup/cha-boot-parameters.html
#https://ftp.sjtu.edu.cn/opensuse/distribution/openSUSE-stable/iso/
if [ -f $prefix/linux/openSUSE-Leap-DVD.iso ] ; then
  menuentry '硬盘安装 openSUSE Leap' --unrestricted {
    loopback loop0 $prefix/linux/openSUSE-Leap-DVD.iso
    linux  (loop0)/boot/x86_64/loader/linux install=hd:/grub/linux/openSUSE-Leap-DVD.iso   lang=zh_CN
    initrd (loop0)/boot/x86_64/loader/initrd
  }
fi

#https://mirrors.nju.edu.cn/opensuse/distribution/openSUSE-stable/iso/
if [ -f $prefix/linux/openSUSE-Leap-NET.iso ] ; then
  menuentry '网络安装 openSUSE Leap [支持WiFi]' --unrestricted {
    loopback loop0 $prefix/linux/openSUSE-Leap-NET.iso
    linux  (loop0)/boot/x86_64/loader/linux install=https://ftp.sjtu.edu.cn/opensuse/distribution/openSUSE-stable/repo/oss/   lang=zh_CN netsetup=dhcp nameserver=223.6.6.6
    initrd (loop0)/boot/x86_64/loader/initrd
  }
fi

menuentry '关机' --unrestricted { halt ; }
menuentry '重新启动' --unrestricted { reboot ; }