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 用户进行切换。
/dev/sda1
应该是 (hd0,msdos1)
(用于MBR) 或 (hd0,gpt1)
(用于 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
标记。
只有在 BIOS-GPT 配置中 GRUB2 才会使用这个分区。MBR 分区中不存在这种分区类型(至少 GRUB2 中如此)。如果系统基于 UEFI,也是不需要这个分区,因为没有嵌入。GRUB-legacy 和 SYSLINUX 也都不需要这个分区。
通常情况下,在许多 MBR(或 msdos 磁盘标签)分区的系统中,只要 DOS 兼容扇区对齐问题能够满足,MBR 后面就有 32KiB 空白(MBR 512字节区域之后,第一个分区之前)。然而,要放置 grub2 的 core.img, 推荐使用 1MiB 或 2 MiB。建议使用支持 1MiB 分区对齐的分区工具获取所需空间,同时满足其他非 512 字节簇的问题(与嵌入 core.img 无关)。
如果不双启动 MS Windows,建议切换到 GPT 分区方式 - 无损数据切换到 GPT
Linux 中创建 UEFI 系统分区 包含了创建指令。创建后将 UEFI 系统分区挂载到 /boot/efi
。这个分区应该是 FAT32 格式,至少 200 MiB 大小。如果已经在其它地方挂载了这个分区,用挂载点替换掉下面的 /boot/efi
:
# mkdir -p /boot/efi # mount -t vfat/boot/efi
创建
# mkdir -p /boot/efi/efi
grub 安装应该很顺利,但是极个别时候会导致系统无法启动。所以强烈建议安装 grub-bios 前备份数据。
# cp -a /boot/grub /path/to/backup/
# dd if=/dev/sdX of=/path/to/backup/mbr-boot-code bs=440 count=1
现在可以删除 /boot/grub
了:
# rm -rf /boot/grub
如果后面的指令出了问题,可以通过如下方式恢复:
# mv /boot/grub /boot/grub.nonfunctional
# cp -a /path/to/backup/grub /boot/
# dd if=/path/to/backup/mbr-boot-code of=/dev/sdX bs=440 count=1
用 pacman 安装 GRUB2(会替换掉 grub):
# pacman -S grub-bios
装入 device-mapper 内核模块,有了这个模块 grub-probe 才能更好的检测磁盘和分区:
# modprobe dm-mod
# 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
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 0set default=n
is the default boot entry, that is choosen after timeout for user actionset timeout=m
– time m
to wait in seconds for a user selection, before default is bootedmenuentry "str"{entry options}
– title string str
for a boot entry & basic layoutset root=(hdn,m)
– base partition, where the kernel is stored tolinux /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.
These are the two most common ways of configuring the grub.cfg
file. For more complex uses, feel free to add descriptions here.
这和载入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 查看
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
想要在 grub2 中改变 framebuffer 分辨率,在 grub.cfg 中 linux 行加入一行:
video=vesafb:mode=1024x768-32 vga=790
In the preceeding statement, the format mode=
+-------------------------------------------------+ | 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
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.
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