当前位置:首页 > 芯闻号 > 充电吧
[导读]https://wiki.archlinux.org/index.php/GRUB2_(简体中文) GRUB2 下一代 GRand Unified Bootloader (GRUB)。 GRUB

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


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

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

Contents  [hide]  1 前言 1.1 当前 GRUB 用户请注意1.2 GRUB2 所需条件 1.2.1 BIOS 系统 1.2.1.1 GPT 专用指令1.2.1.2 MBR 即 msdos 分区专用指令1.2.2 UEFI 系统 1.2.2.1 创建并挂载 UEFI 系统分区2 安装 2.1 在已有系统中安装grub2 2.1.1 BIOS 系统 2.1.1.1 备份之前的数据2.1.1.2 grub_bios 安装前2.1.1.3 Run grub-install3 引导器配置 3.1 Dual booting 3.1.1 其他GNU/Linux发行版3.2 背景图像和点阵字体3.3 菜单颜色3.4 Hidden menu3.5 设置 Framebuffer 分辨率4 常见问题 4.1 msdos-style Error Message4.2 Other5 更多资源 前言

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 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  /boot/efi

创建 /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 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.
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=- vga= 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
更多资源 GRUB主页GRUB维基
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

为了满足日益增长的数据处理需求,铁威马NAS推出了全新的性能巅峰2024年旗舰之作F4-424 Pro,并搭载了最新的操作系统--TOS 6。这款高效办公神器的问世,无疑将为企业和专业人士带来前所未有的便捷与效率。

关键字: 存储 Linux 服务器

双系统将是下述内容的主要介绍对象,通过这篇文章,小编希望大家可以对双系统的相关情况以及信息有所认识和了解,详细内容如下。

关键字: 双系统 Windows Linux

安装Linux操作系统并不复杂,下面是一个大致的步骤指南,以帮助您完成安装。1. 下载Linux发行版:首先,您需要从Linux发行版官方网站下载最新的ISO镜像文件。

关键字: Linux 操作系统 ISO镜像

计算机是由一堆硬件组成的,为了有限的控制这些硬件资源,于是就有了操作系统的产生,操作系统是软件子系统的一部分,是硬件基础上的第一层软件。

关键字: Linux 操作系统 计算机

Linux操作系统是一套免费使用和自由传播的类Unix操作系统,通常被称为GNU/Linux。它是由林纳斯·托瓦兹在1991年首次发布的,并基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。Lin...

关键字: Linux 操作系统

所谓进程间通信就是在不同进程之间传播或交换信息,它是一组编程接口,让程序员能够协调不同的进程,使之能在一个操作系统里同时运行,并相互传递、交换信息;还可以让一个程序能够在同一时间里处理许多用户的需求。

关键字: Linux 进程通信 编程接口

串口通信作为一种最传统的通信方式,在工业自动化、通讯、控制等领域得到广泛使用。

关键字: Linux 串口通信 通讯

2023年11月16日: MikroElektronika(MIKROE) ,作为一家通过提供基于成熟标准的创新式硬软件产品来大幅缩短开发时间的嵌入式解决方案公司,今天宣布推出一款基于单线设备的软硬件开源解决方案Cli...

关键字: 嵌入式 Linux 操作系统

Linux是一种免费使用和自由传播的类Unix操作系统,其内核由林纳斯·本纳第克特·托瓦兹于1991年10月5日首次发布。它主要受到Minix和Unix思想的启发,是一个基于POSIX的多用户、多任务、支持多线程和多CP...

关键字: Linux 操作系统

本文中,小编将对嵌入式予以介绍,如果你想对它的详细情况有所认识,或者想要增进对它的了解程度,不妨请看以下内容哦。

关键字: 嵌入式 Linux
关闭
关闭