操作系統開發 - Grub2 bootloader之安裝

https://wiki.archlinux.org/index.php/GRUB2_(简体中文)


GRUB2 下一代 GRand Unified Bootloader (GRUB)。 GRUB2 来自下一代 GRUB 研究项目 PUPA,代码全部重写,实现了模块化和增强了移植性。[1].

简单的说,启动引导器是电脑启动时第一个运行的程序。负责装载内核并将控制权转入。内核再初始化操作系统的其它部分。

Contents

 [hide

前言

GRUB Legacy(如0.9x)已经被上游开发团队标为过时,有许多发行版使用 GRUB2 或者syslinux替换。Arch 现在提供的 grub 已经是 Grub2,推荐现在的 grub 用户进行切换。

注意: grub2 支持 btrfs 根分区,不需要给 /boot 单独分区。

当前 GRUB 用户请注意

  • GRUB 和 GRUB2 的内置命令有所不同(例如 "find" 已经变成 "search"),详情可见: GRUB2 命令
  • GRUB2 已经模块化,不再需要"stage 1.5"。所以,启动引导器可以变得很小 -- 模块按需从硬盘导入以扩展功能。(例如LVM 或 RAID 支持)。
  • GRUB 和 GRUB2 的分区命名有变化。分区从 1开始编号,并以分区类型开始,原来是从 0 开始编号。注意:磁盘还是从 0 编号。例如,/dev/sda1应该是 (hd0,msdos1) (用于MBR) 或 (hd0,gpt1) (用于 GPT)。

GRUB2 所需条件

BIOS 系统
GPT 专用指令

BIOS-GPT 配置中的 GRUB2 需要一个BIOS 启动分区以便植入core.img,因为 GPT 分区系统中 MBR 后面没有了 32 KiB 的空间(已经被 GPT 主头部和主分区表占据)。此分区仅在 BIOS-GPT 配置中使用,如果使用 MBR 分区方式,此分区不被使用。同时如果系统是基于 UEFI 的,也无需创建这个分区,因为其中并没有嵌入启动扇区。 Syslinux 也不使用这个分区。

用 cgdisk 或 GNU Parted 创建 2 MB 的无文件系统分区。只要位于磁盘的前 2TiB 空间内,分区位置没有要求。但是建议放在磁盘开始位置,/boot 之前。cgdisk 中设置类型为 "EF02" 或在GNU Parted 中设置set <BOOT_PART_NUM> bios_grub on标记。

只有在 BIOS-GPT 配置中 GRUB2 才会使用这个分区。MBR 分区中不存在这种分区类型(至少 GRUB2 中如此)。如果系统基于 UEFI,也是不需要这个分区,因为没有嵌入。GRUB-legacy 和 SYSLINUX 也都不需要这个分区。

注意: 这个分区应该在 grub2_bios-install 或 grub-setup 和 Archlinux 安装程序的 Install Bootloader 部分执行之前建立。
注意: 如果选择 GRUB2 作为启动加载器,这个分区应该在 grub2_bios-install 或 grub-setup 和 Archlinux 安装程序的 Install Bootloader 部分执行之前建立。
MBR 即 msdos 分区专用指令

通常情况下,在许多 MBR(或 msdos 磁盘标签)分区的系统中,只要 DOS 兼容扇区对齐问题能够满足,MBR 后面就有 32KiB 空白(MBR 512字节区域之后,第一个分区之前)。然而,要放置 grub2 的 core.img, 推荐使用 1MiB 或 2 MiB。建议使用支持 1MiB 分区对齐的分区工具获取所需空间,同时满足其他非 512 字节簇的问题(与嵌入 core.img 无关)。

如果不双启动 MS Windows,建议切换到 GPT 分区方式 - 无损数据切换到 GPT

注意: 转换到 GPT 之前就要创建上面说的 2MiB 分区。否则 gparted 认不出启动分区,重启时 grub2 也不知道到哪里找数据。
UEFI 系统
创建并挂载 UEFI 系统分区

Linux 中创建 UEFI 系统分区 包含了创建指令。创建后将 UEFI 系统分区挂载到 /boot/efi。这个分区应该是 FAT32 格式,至少 200 MiB 大小。如果已经在其它地方挂载了这个分区,用挂载点替换掉下面的 /boot/efi:

# mkdir -p /boot/efi
# mount -t vfat <UEFISYS_PART_DEVICE> /boot/efi

创建 <UEFI_SYSTEM_PARTITION>/efi 目录:

# mkdir -p /boot/efi/efi

安装

在已有系统中安装grub2

BIOS 系统
备份之前的数据

grub 安装应该很顺利,但是极个别时候会导致系统无法启动。所以强烈建议安装 grub-bios 前备份数据。

  • 复制 grub 模块和数据
# cp -a /boot/grub /path/to/backup/
  • 备份 MBR 和 GRUB-Legacy stage 1.5
# dd if=/dev/sdX of=/path/to/backup/mbr-boot-code bs=440 count=1

现在可以删除 /boot/grub 了:

# rm -rf /boot/grub

如果后面的指令出了问题,可以通过如下方式恢复:

  • 删除无法工作的 grub-legacy 或 grub2 文件
# mv /boot/grub /boot/grub.nonfunctional
  • 将备份的 grub 复制到 /boot
# cp -a /path/to/backup/grub /boot/
  • 将 MBR 和它后面的 62 簇 sda 数据替换为备份数据(危险!)
# dd if=/path/to/backup/mbr-boot-code of=/dev/sdX bs=440 count=1
grub_bios 安装前

用 pacman 安装 GRUB2(会替换掉 grub):

# pacman -S grub-bios
注意: 安装 1.99~rc1-3-x86_64 在一些系统上花了很长时间,因为安装中调用的脚步grub-mkconfig,没有使用  --no-floppy选项,会不断检测软驱)。
注意: 仅安装软件不会更新 /boot/grub/core.img 文件和 /boot/grub 中的 grub 模块。需要用下面的 grub_bios-install 手动更新它们。

装入 device-mapper 内核模块,有了这个模块 grub-probe 才能更好的检测磁盘和分区:

# modprobe dm-mod
Run grub-install
# grub-install --boot-directory=/boot --no-floppy --recheck --debug /dev/sda
# cp /usr/share/grub/{unicode.pf2,ascii.pf2} /boot/grub/

附加说明:若只是生成 core.img 文件而不将 grub2 安装到 MBR,加上--grub-setup=/bin/true 参数到 grub2_bios-install:

# grub-install --grub-setup=/bin/true --boot-directory=/boot --no-floppy --recheck /dev/sda

这样就可以从 grub-legacy 像多内核启动一样载入 grub2 的 core.img。

/dev/sda是安装到目的磁盘(第一个 SATA 磁盘的 MBR)。如果/boot 使用 LVM,可以将 GRUB2 安装到多个物理磁盘。

--no-floppy参数加上之后就不会查找软驱,可大大减少该安装所需时间,并避免了下面问题的发生:

grub-probe: error: Cannot get the real path of '/dev/fd0'
Auto-detection of a filesystem module failed.
Please specify the module with the option '--modules' explicitly.

执行“生成GRUB2配置文件”步骤。

# GRUB_PREFIX="/boot/grub" grub-mkconfig -o /boot/grub/grub.cfg

如果失败,用下面脚本将/boot/grub/menu.lst 转换为 /boot/grub/grub.cfg

# grub-menulst2cfg /boot/grub/menu.lst /boot/grub/grub.cfg

例如:

/boot/grub/menu.lst
default=0
timeout=5

title  Arch Linux Stock Kernel
root   (hd0,0)
kernel /vmlinuz-linux root=/dev/sda2 ro
initrd /initramfs-linux.img

title  Arch Linux Stock Kernel Fallback
root   (hd0,0)
kernel /vmlinuz-linux root=/dev/sda2 ro
initrd /initramfs-linux-fallback.img
/boot/grub/grub.cfg
set default='0'; if [ x"$default" = xsaved ]; then load_env; set default="$saved_entry"; fi
set timeout=5

menuentry 'Arch Linux Stock Kernel' {
  set root='(hd0,1)'; set legacy_hdbias='0'
  legacy_kernel   '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'
  legacy_initrd '/initramfs-linux.img' '/initramfs-linux.img'
  
}

menuentry 'Arch Linux Stock Kernel Fallback' {
  set root='(hd0,1)'; set legacy_hdbias='0'
  legacy_kernel   '/vmlinuz-linux' '/vmlinuz-linux' 'root=/dev/sda2' 'ro'
  legacy_initrd '/initramfs-linux-fallback.img' '/initramfs-linux-fallback.img'
}

如果忘记创建/boot/grub/grub.cfg配置文件就重启,进入 GRUB2命令后执行:

sh:grub> insmod legacycfg
sh:grub> legacy_configfile ${prefix}/menu.lst

启动后可以重新修改 /boot/grub/grub.cfg 配置文件。

如果想生成新的GRUB2配置文件,请执行:

# grub-mkconfig -o /boot/grub/grub.cfg
注意: 仅在 BIOS 系统工作,UEFI 系统不能这么做。

引导器配置

grub2的配置文件是:

/boot/grub/grub.cfg

Be warned this section is incomplete, feel free to put all missing configuration options here!

  • (hdn,m) is the partition m on disc n, partition numbers starting with 1, disc numbers starting with 0
  • set default=n is the default boot entry, that is choosen after timeout for user action
  • set timeout=m – time m to wait in seconds for a user selection, before default is booted
  • menuentry "str"{entry options} – title string str for a boot entry & basic layout
  • set root=(hdn,m) – base partition, where the kernel is stored to
  • linux /path ro root=/dev/device initrd /initrd.img – use the root option, if the kernel not placed in /
  • chainloader +1 sets root active and gives booting procedure to its boot-loader (for Windows, e.g.)

For UUID entries:

# blkid

This gives you the UUID. We were doing the following command to set the root device:

# root=/dev/device
Instead, replace it with this:
# root=/dev/disk/by-uuid/bc285871-413 .... and so on, for your UUID.

Dual booting

These are the two most common ways of configuring the grub.cfg file. For more complex uses, feel free to add descriptions here.

其他GNU/Linux发行版

这和载入Arch linux一样。假设另一个发行版在分区[s/h]da2.

menuentry "Other Linux" {
set root=(hd0,2)
linux /boot/vmlinuz (add other options here as required)
initrd /boot/initrd.img (if the other kernel uses/needs one)
}

背景图像和点阵字体

GRUB2支持背景图像和pf2格式的点阵字体。unifont字体以unicode.pf2的文件名包含于grub2的安装包,也可能只是文件名为ascii.pf2的ascii字体。只要载入适合的模块,Grub2支持tga、png和jpeg的图像格式。支持的最大分辨率取决于你的硬件配置。下面是一个以tga格式图片为背景的配置的例子:

if loadfont /usr/share/grub/unicode.pf2 ; then
  set gfxmode="1024x768x32"
  insmod gfxterm
  insmod vbe
  terminal_output gfxterm
  if terminal_output gfxterm; then true ; else
     terminal gfxterm
  fi
fi
insmod tga
background_image /boot/grub/archlinux.tga

菜单颜色

想要改变 GRUB2 菜单颜色,你需要在 /boot/grub/grub.cfg 指定一个选项:

set menu_color_normal=light-blue/black
set menu_color_highlight=light-cyan/blue

这是Arch's 官方 GRUB-legacy 的默认颜色。可用的颜色可以在http://www.gnu.org/software/grub/manual/html_node/color.html 查看

Hidden menu

For hiding menu put that code in grub.cfg after picture initialization but before menuentries (e.g. background_image /boot/grub/archlinux.tga).

  set timeout=5
  echo -n "Press ESC to see the menu... "
if sleep --verbose --interruptible 5 ; then
  set timeout=0
fi

设置 Framebuffer 分辨率

想要在 grub2 中改变 framebuffer 分辨率,在 grub.cfg 中 linux 行加入一行:

video=vesafb:mode=1024x768-32 vga=790

In the preceeding statement, the format mode=<resolution>-<colordepth> vga=<fbresolution> is used where fbresolution follows the following scheme:

+-------------------------------------------------+
     | 640x480    800x600    1024x768   1280x1024
 ----+--------------------------------------------
 256 | 0x301=769  0x303=771  0x305=773   0x307=775
 32K | 0x310=784  0x313=787  0x316=790   0x319=793
 64K | 0x311=785  0x314=788  0x317=791   0x31A=794
 16M | 0x312=786  0x315=789  0x318=792   0x31B=795
+-------------------------------------------------+

Make sure you add the following somewhere, (insmod statements are usually found at the top of the grub.cfg file):

insmod vbe

常见问题

msdos-style Error Message

grub-setup: warn: This msdos-style partition label has no post-MBR gap; embedding won't be possible!
grub-setup: warn: Embedding is not possible. GRUB can only be installed in this setup by using blocklists.
            However, blocklists are UNRELIABLE and its use is discouraged.
grub-setup: error: If you really want blocklists, use --force.

This error may occur when you try installing GRUB2 in a VMware container. Read more about it here. Hopefully a fix will be provided soon.

Other

I couldn't figure out how to uninstall grub1, and install grub2 to the MBR, as it isn't being booted by default. It is still booting grub1. So, an easy work-around, is rename menu.lst.pacsave or whatever, to menu.lst (in /boot/grub/) and for each menu entry that you would like to use grub2, at the end type "chainloader +1". This will tell grub1 to forward control to grub2. This is an ugly hack though, so I advise setting the menu.lst's timout as 0, otherwise the total timeout would be grub1's time out + grub2's which, for me would equal more than 18 seconds, which is quite a bit.

P.S. hopefully someone figures out how to pry grub1's dead fingers off of my MBR, and place grub2 on it :) .

In my case it had to do with my boot partition. Say boot-partition is (hd0,1) and your root is (hd0,3) (grub2 naming). grub-setup searches for (hd0,3)/boot/grub/core.img. Just because it's on (hd0,1)/grub/core.img, it is unable to find it. So I copied the grub-folder to my root partition and everything worked fine:

E.g. (as root:)

# mount /boot
# cp -a /boot/grub /
# umount /boot
# mv /grub /boot/
# grub-install /dev/sda

更多资源


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值