Grub Legacy 简介

Grub ,是 GRand Unified Bootloader 的简称,作用于系统启动的 Boot Loader 阶段,有着承上启下的作用。 Grub 有两个版本,分别是 Grub0.XGrub Legacy )和 Grub1.XGrub2 )。本文将围绕使用最广泛的 Grub Legacy 进行讲解。

stage

由于 MBR 给予的空间非常少,所以 Grub 将程序分为了两段, stage1 放置于 MBR 上及存放在 MBR 之后的扇区;stage2 放置于文件系统 /boot/grub 中。并且在 /boot/grub 中包含了两个阶段所需要的文件,作为备份。

[hzz@magedu ~]$ ls /boot/grub/|grep ^stage
stage1
stage2
[hzz@magedu ~]$

由于 stage1 的内容是固定的,并且没有放在文件系统上,所以我们很难进行修改,也不需要修改,有问题直接覆盖安装即可。我们能自定义的,也就是下面主要说的,就是 stage2

stage2 简介

stage2 及内核等通常放置于一个基本磁盘分区(不能放于逻辑分区,如果根分区采用逻辑分区,则必须把 /boot 单独分区),其主要功能如下:

  1. 提供菜单、并提供交互式接口;
    • e :编辑模式,用于编辑菜单;
    • c :命令模式,交互式接口;
  2. 加载用户选择的内核或操作系统;
    • 允许传递参数给内核;
    • 可隐藏此菜单;
  3. 为菜单提供了保护机制;
    • 为编辑菜单进行认证;
    • 为启用内核或操作系统进行认证;

命令行接口

简介

Grub stage2 提供交互式接口,以便用户进行操作。在系统启动进入道 Grub 系统选择菜单倒计时的时候随便按个键,进入道选择菜单界面,然后按 c 即可进入交互式命令。

倒计时/ 菜单选择界面/ Grub

进入到 Grub 命令行接口后,就可以手动引导系统进行启动了,比如:

手动引导系统/

当然,也可以在菜单选择界面使用 e 键,看系统默认的 Grub 是如何引导的,选中相关命令再按 e 键,就可以直接编辑默认的引导信息。

编辑系统引导/

使用

进入 Grub 命令行之后,可以输入 helphelp command 查看可使用的命令及相关命令的信息,常用的手动引导过程如下:

grub> root (hd0,0) # 设置 Grub 的根(不是文件系统的根);
grub> kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sda1 # 设定本次启动时用到的内核文件及以只读模式加载的文件系统根设备;额外还可以添加许多内核支持使用的 cmdline 参数,比如: init=/path/to/init , selinux=0 ;
grub> initrd /initramfs-2.6.32-431.el6.x86_64.img # 设定为选定的内核提供额外文件的 ramdisk ;(版本号必须与内核版本号完全一致);
grub> boot # 引导启动选定的内核;

配置文件

Grub 的配置文件置于 /boot/grub/grub.conf ,其内容为:

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/vg_magedu-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
        root (hd0,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_magedu-lv_root rd_NO_LUKS rd_LVM_LV=vg_magedu/lv_swap rd_NO_MD rd_LVM_LV=vg_magedu/lv_root crashkernel=auto LANG=zh_CN.UTF-8  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
        initrd /initramfs-2.6.32-431.el6.x86_64.img

通过最后几行不难看到,其实配置文件就是命令行的一个包装。将命令行接口启动所需要命令和参数,写到一个文件里面,再加上标题,默认选项,超时时间等配置项。相关配置项解析如下:

default=0 # 设定默认启动的菜单项;落单项( title )编号从 0 开始;
timeout=5 # 指定菜单项等待选项选择的时长;
splashimage=(hd0,0)/grub/splash.xpm.gz # 指明菜单背景图片文件路径;
hiddenmenu # 隐藏菜单;
password [--md5] STRING: 菜单编辑认证(可使用 grub-md5-crypt 命令生成密码串);
title CentOS (2.6.32-431.el6.x86_64) # 定义菜单项标题, 可出现多次;
        root (hd0,0) # grub 查找 stage2 及 kernel 文件所在设备分区,为 grub 的根路径;
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_magedu-lv_root rd_NO_LUKS rd_LVM_LV=vg_magedu/lv_swap rd_NO_MD rd_LVM_LV=vg_magedu/lv_root crashkernel=auto LANG=zh_CN.UTF-8  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet # 启动的内核及参数
        initrd /initramfs-2.6.32-431.el6.x86_64.img # 内核匹配的 ramfs 文件;
        password --md5 STRING # 启动选定的内核或操作系统时进行认证(可使用 grub-md5-crypt 命令生成密码串);

单用户模式

单用户模式属于维护模式,无需认证就可以使用 root 用户登录,用来处理一些普通模式下无法处理的问题,比如忘记 root 密码之类的。通过 Grub 命令行模式,我们可以直接以单用户模式启动,步骤如下:

  1. 在菜单选择界面,选择要启动系统的 title ,按 e 键进行编辑;
  2. 选择 kernel 行,按 e 键进行编辑;
  3. 在编辑行最后加上 1sSsingle 都可以,回车保存;
  4. 在 kernel 行,按 b 键启动系统,启动完后就是单用户模式了;
单用户模式/

可以看到,启动完成直接以 root 用户登录,无需验证。

当然,也可以在菜单选择界面按 c 键,进入命令行模式,手动输入命令来启动,只要在 kernel 调用的最后加上 1sSsingle 就可以了。

Grub 修复

在平时使用过程中,有可能无意中将 MBR 或者 Grub 搞坏,比如装双系统或者自定义启动流程删掉了 /boot/grub 中的某些配置。

未重启

如果没有进行重启,可以有以下两种方式修复:

方式一:重装 Grub (配置文件需自行创建)

[hzz@magedu ~]$ sudo grub-install /dev/sda                                
Installation finished. No error reported.
This is the contents of the device map /boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(fd0)   /dev/fd0
(hd0)   /dev/sda
[hzz@magedu ~]$ 

方式二:修复 MBR (仅限于 /boot/grub 未被破坏的前提下)

[hzz@magedu ~]$ sudo grub
Probing devices to guess BIOS drives. This may take a long time.

    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]
grub> root (hd0,0)
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0)
setup (hd0)
 Checking if "/boot/grub/stage1" exists... no
 Checking if "/grub/stage1" exists... yes
 Checking if "/grub/stage2" exists... yes
 Checking if "/grub/e2fs_stage1_5" exists... yes
 Running "embed /grub/e2fs_stage1_5 (hd0)"...  27 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit
[hzz@magedu ~]$

已重启

如果系统已经重启,无法进入 Grub 菜单界面(这种情况居多),可通过下面两种方式进行修复。

方式一:通过光盘,进入救援模式(怎么进就不说了),然后执行 重装 Grub修复 MBR (请看上边)进行修复;

方式二:拆下硬盘,挂载到其他机器,然后 mount 到一个指定目录(怎么 mount 也不说了,假设 mount 路径为 /mnt/sysimage ,挂载的设备为 /dev/sdb1

# 执行 Grub 重装(配置文件需自行创建)

[hzz@magedu ~]$ sudo grub-install --root-directory=/mnt/sysimage /dev/sdb 
Installation finished. No error reported.
This is the contents of the device map /mnt/sysimage/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(fd0)   /dev/fd0
(hd0)   /dev/sda
(hd1)   /dev/sdb
[hzz@magedu ~]$

# 执行 MBR 修复

[hzz@magedu ~]$ sudo grub
Probing devices to guess BIOS drives. This may take a long time.

    GNU GRUB  version 0.97  (640K lower / 3072K upper memory)

 [ Minimal BASH-like line editing is supported.  For the first word, TAB
   lists possible command completions.  Anywhere else TAB lists the possible
   completions of a device/filename.]
grub> root (hd1,0)
root (hd1,0)
 Filesystem type is ext2fs, partition type 0x83
grub> setup (hd1)
setup (hd1)
 Checking if "/boot/grub/stage1" exists... yes
 Checking if "/boot/grub/stage2" exists... yes
 Checking if "/boot/grub/e2fs_stage1_5" exists... yes
 Running "embed /boot/grub/e2fs_stage1_5 (hd1)"...  27 sectors are embedded.
succeeded
 Running "install /boot/grub/stage1 (hd1) (hd1)1+27 p (hd1,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded
Done.
grub> quit
quit
[hzz@magedu ~]$
消息盒子

# 暂无消息 #

只显示最新10条未读和已读信息