GRUB (简体中文)

From ArchWiki
Jump to navigation Jump to search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.


Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어


External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Tango-preferences-desktop-locale.pngThis article or section needs to be translated.Tango-preferences-desktop-locale.png

Notes: please use the first argument of the template to provide more detailed indications. (Discuss in Talk:GRUB (简体中文)#)

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary text Template:Article summary heading Template:Article summary wiki - Burg 基于 GRUB2 的全新启动装载器。它使用了全新的格式,可以用于广泛的操作系统,包括Linux/Windows/OSX/Solaris/FreeBSD 等等。同时还具有一个可以高度配置的菜单系统,同时具有图形和文本模式的配置界面。 Template:Article summary heading Template:Article summary link Template:Article summary link Template:Article summary end

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

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

前言

GRUB (即版本 0.9x) 是事实上的 标准Linux 启动引导器,不过该开发团队已经将其标为过时。目前已经有许多发行版使用 GRUB2 或者syslinux替换。开发团队推荐使用 GRUB2 >=1.99~rc2,并推荐现在的 grub用户进行切换。
Note: 为何选择grub2: grub2支持btrfs (不需要给 /boot 单独分区), 支持很多格式的背景,比如 JPEG、PND、TGA 等格式,支持 24 色,支持 800x600、1024x768,支持中文菜单显示。syslinux基于设计的原因,无法支持中文等需占太多磁盘空间的功能(引导区一般就几十兆,无法装太多东西,而syslinux是把所有要加载的东西全部拷入,虽然速度快了,但功能上就只能留下遗憾了。)

当前 GRUB 用户请注意

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

安装

第一次安装 Arch系统 时

由于安装镜像默认不使用grub2,(最早是grub和lilo可选,现在是syslinux和grub可选),因此,要安装grub2,需要联网安装。下面过程:

  • 安装最后,跳过Install Bootloader(安装启动引导器)这步并退出安装程序。
  • 配置网络:
# aif -p partial-configure-network
  • 如果安装时没有配置系统的 /etc/resolv.conf 文件,(例如准备使用 DHCP 生成),需要将 AIF 配置网络生成的文件复制过来:
# cp /etc/resolv.conf /mnt/etc/resolv.conf
  • 从安装程序的 shell chroot 到刚安装的系统:
# mount -o bind /dev /mnt/dev
# chroot /mnt bash
  • 按下面所说安装 GRUB2。

在已有系统中安装grub2

BIOS 系统

用 pacman 安装 GRUB2(会替换掉 Template:Package Official):

# pacman -S grub2-bios
Note: 安装 1.99~rc1-3-x86_64 在我的系统上花了很长时间,安装程序进行了多次"grub-probe",可能所有人都会安装得很慢 (因为没有使用 --no-floppy 选项,会不断检测软驱)。

接着把 GRUB2 安装到磁盘或分区的引导扇区,并添加万国码支持:

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

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

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

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

--no-floppy 参数加上之后就不会查找软驱,可大大减少该安装所需时间(并避免了下面问题的发生) 而且 Template:Filename 是安装到目的磁盘(第一个 SATA 磁盘的 MBR)。如果Template:Filename 使用 LVM,可以将 GRUB2 安装到多个物理磁盘。

不带 --no-floppy 执行 grub_bios-install 可能导致软驱检查问题:

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.

所以如果不是将 GRUB 安装到软驱,建议使用 --no-floppy参数。如果确实需要软驱,请在 BIOS 中将其设为第一个启动设备。

最后,generate a configuration for grub2 (this is explained in greater detail in the Configuration section):

  1. grub-mkconfig -o /boot/grub/grub.cfg


GPT 专用分区

BIOS-GPT 配置中的 GRUB2 需要一个BIOS 启动分区以便植入core.img,因为 GPT 分区系统中 MBR 后面没有了 32 KiB 的空间。

用 GPT fdisk 或 GNU Parted 创建大约 1 MB 无文件系统分区。分区位置没有要求,但是建议放在磁盘开始, /boot 之前。GPT fdisk 中设置类型为 "EF02" 或在GNU Parted 和 GPartedor 中设置 "bios_grub" 标记。

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

Note: 这个分区应该在 grub2_bios-install 或 grub-setup 和 Archlinux 安装程序的Install Bootloader 部分执行之前建立。

从 grub-legacy (仅BIOS系统) 升级
  • 删除 MBR 中的 grub-legacy 残留
# dd if=/dev/zero of=<your_disk> bs=440 count=1
  • 参照 #BIOS 系统 指令。
  • 将 /boot/grub/{menu.lst 或 grub.conf} 转化到 grub.cfg 格式
# grub-menulst2cfg /boot/grub/menu.lst /boot/grub/grub.cfg

示例:

Template:File

Template:File

引导器配置

grub2的配置文件是:

/boot/grub/grub.cfg

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

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 Template:Filename file. For more complex uses, feel free to add descriptions here.

With other GNU/Linux distributions

This is done exactly the same way that Arch linux is loaded. Here we assume that the other distro is on partition Template:Filename.

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)
}

Background Image & Bitmap Fonts

GRUB2 comes with support for background images and bitmap fonts in pf2 format. The unifont font is included in the grub2 package under the filename Template:Filename, or, as only ascii characters under the name Template:Filename. Image formats supported include tga, png and jpeg, providing the correct modules are loaded. The maximum supported resolution depends on your hardware. A configuration sample setting a tga file as background is shown below.

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

菜单颜色

To change the colors in GRUB2 you would specify one option in Template:Filename:

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

These are the default colors for Arch's release of GRUB-legacy. The available colors for GRUB2 are at 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

Setting the Framebuffer Resolution

To change the framebuffer resolution in grub2, add a line similar to this to the linux line in grub.cfg:

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 Template:Filename or whatever, to Template:Filename (in /boot/grub/) and for each menu entry that you would like to use grub2, at the end type Template:Codeline. This will tell grub1 to forward control to grub2. This is an ugly hack though, so I advise setting the Template:Filename'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 Template:Codeline and your root is Template:Codeline (grub2 naming). grub-setup searches for Template:Filename. Just because it's on Template:Filename, 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

更多资源