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

CPU亲和度通过限制进程或线程可以运行的CPU核心集合,使得它们只能在指定的CPU核心上执行。这可以减少CPU缓存的失效次数,提高缓存命中率,从而提升系统性能。

关键字: Linux 嵌入式

在Linux系统性能优化中,内存管理与网络连接处理是两大核心领域。vm.swappiness与net.core.somaxconn作为关键内核参数,直接影响系统在高负载场景下的稳定性与响应速度。本文通过实战案例解析这两个...

关键字: Linux 内存管理

对于LLM,我使用b谷歌Gemini的免费层,所以唯一的成本是n8n托管。在使用了n8n Cloud的免费积分后,我决定将其托管在Railway上(5美元/月)。然而,由于n8n是开源的,您可以在自己的服务器上托管它,而...

关键字: 人工智能 n8n Linux

在Linux系统管理中,权限控制是安全运维的核心。本文通过解析/etc/sudoers文件配置与组策略的深度应用,结合某金融企业生产环境案例(成功拦截98.7%的非法提权尝试),揭示精细化权限管理的关键技术点,包括命令别...

关键字: Linux 用户权限 sudoers文件

Linux内核中的信号量(Semaphore)是一种用于资源管理的同步原语,它允许多个进程或线程对共享资源进行访问控制。信号量的主要作用是限制对共享资源的并发访问数量,从而防止系统过载和数据不一致的问题。

关键字: Linux 嵌入式

在云计算与容器化技术蓬勃发展的今天,Linux网络命名空间(Network Namespace)已成为构建轻量级虚拟网络的核心组件。某头部互联网企业通过命名空间技术将测试环境资源消耗降低75%,故障隔离效率提升90%。本...

关键字: Linux 云计算

在Linux内核4.18+和主流发行版(RHEL 8/Ubuntu 20.04+)全面转向nftables的背景下,某电商平台通过迁移将防火墙规则处理效率提升40%,延迟降低65%。本文基于真实生产环境案例,详解从ipt...

关键字: nftables Linux

在Linux设备驱动开发中,等待队列(Wait Queue)是实现进程睡眠与唤醒的核心机制,它允许进程在资源不可用时主动放弃CPU,进入可中断睡眠状态,待资源就绪后再被唤醒。本文通过C语言模型解析等待队列的实现原理,结合...

关键字: 驱动开发 C语言 Linux

在Unix/Linux进程间通信中,管道(pipe)因其简单高效被广泛使用,但默认的半双工特性和无同步机制容易导致数据竞争。本文通过父子进程双向通信案例,深入分析互斥锁与状态机在管道同步中的应用,实现100%可靠的数据传...

关键字: 管道通信 父子进程 Linux

RTOS :RTOS的核心优势在于其实时性。它采用抢占式调度策略,确保高优先级任务能够立即获得CPU资源,从而在最短时间内完成处理。RTOS的实时性是通过严格的时间管理和任务调度算法实现的,能够满足对时间敏感性要求极高的...

关键字: Linux RTOS
关闭