Grub(GRand Unified Bootloader)是统一资源引导器,也就是引导加载器;它的工作是提供一个菜单,允许用户选择要启动的系统或不同的内核版本;把用户选定的内核装载到RAM中的特定空间中,然后解压、展开,而后把系统控制权移交给内核。

    它目前有两个版本:1、GRUB 0.X:Grub Legacy

                2、GRUB 1.X:Grub2

一、Grub Legacy概述

本文主要讲解Grub Legacy,也就是最经典的Grub程序。它分为三个部分stage1、stage1_5和stage2。

1、stage1:即写入mbr中存储的bootloader程序。它的任务就是将stage1_5(此时并不能算是磁盘分区/boot/grub/下的stage1_5,因为stage1无法识别文件系统)载入内存执行。

2、stage1_5:位于mbr之后的扇区,通过提供基本文件系统驱动让stage1中的bootloader程序能识别磁盘分区/boot/grub/上的stage2文件并载入内存执行。

    CentOS系统grub详解_grub

    注意:此处若是boot分区的文件系统类型不属于stage1_5的中的一个,则会借助ramdisk来加载在/lib64/moudles/下额外的文件系统驱动。

3、stage2:这个程序主要给用户提供一个比较友好的启动菜单,而后去加载位于同一个磁盘分区/boot/的内核文件(例如 vmlinuz-2.6.32-573.el6.x86_64)

二、Grub的配置文件和功用   

1、配置文件/boot/grub/grub.conf,保证grub和内核等在一个目录;此外它创建了一个链接文件/etc/grub.conf指向配置文件来保证用户使用配置文件一致性;

  配置项:

    default=#:设定默认启动的菜单项:菜单项(title)编号从0开始

    timeout=#:指定菜单项等待选项选择的时长;

    splashp_w_picpath=(hd#,#)/PATH/TO/XPM_PIC_FILE:指明菜单背景图片文件路径;

    hiddemenu:隐藏菜单

    password [--md5] STRING:菜单编辑认证;

    title TITLE:定义菜单项“标题”,可出现多次;

        roothd##):grub查找stage2kernel文件所在设备分区:为grub的“根”

        kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:启动的内核

        initrd /PATH/TO/INITRAMFS_FILE:内核匹配的ramfs文件

        password [--md5] STRING:启动选定的内核或者操作系统进行认证;

    注意:password在不同的位置所起效果不同

password 配置示例

#可以利用grub-md5-crypt命令来生成密码,如下
~]# grub-md5-crypt 
Password: 
Retype password: 
$1$PG8va$eD0LgRT7UcU035hedUszc/
#之后将生成的密码添加在grub.conf配置文件的password字段后面如下
password --md5 $1$PG8va$eD0LgRT7UcU035hedUszc/

示例:

]# cat 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/sda3
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-573.el6.x86_64)
	root (hd0,0)
	kernel /vmlinuz-2.6.32-573.el6.x86_64 ro root=UUID=c0fe6dd3-fcf4-41ce-a951-75a4e472cb2a rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
	initrd /initramfs-2.6.32-573.el6.x86_64.img	

2、功用

    (1)提供菜单、并提供交互式接口

        e:编辑模式,用于编辑菜单

        c:命令模式,交互式接口

    (2)加载用户选择的内核或操作系统

        允许传递参数给内核;可隐藏此菜单(通过grub.conf中的hiddenmenu)

    (3)为菜单提供了保护机制

        为编辑菜单进行认证(title上定义的password)

        为启用内核或者操作系统进行认证(title下的password字段)

三、Grub的命令行接口和编辑功能

1、进入grub的命令行界面和编辑界面

 (1)启动机器时,按ESC键可进入如下界面

CentOS系统grub详解_linux_02

(2)按c可进入命令行界面如下
CentOS系统grub详解_centos_03

(3)进入编辑界面,在界面(1)下输入e可进入编辑界面如下

 CentOS系统grub详解_centos_04   2、grub的命令行命令和编辑命令

   (1)grub的命令行命令

help:获取帮助信息

help KEYWORD:获取某一具体命令的帮助信息

find (hd#,#)/PATH/TO/SOMEFFILE:查找文件(内核文件等)

root (hd#,#):设定那个磁盘是根,执行后,fnd不需指磁盘了;

kernel /PATH/TO/KERNEL_FILE:设定本次启动时用到的内核文件;

initrd /PATH/TO/OMOTRAMFS_FILE:设定为选定的initrd文件

boot:引导启动选定的内核;

示例:手动启动系统

CentOS系统grub详解_linux_05

   (2)grub的编辑命令

        b:boot,启动

        e:edit,编辑选中的项

         o:在选中行的下一行新加一行

          O:在选中行的上一行添加一行

          d:删除选中行

          ESC:可以退出编辑界面进入主界面

示例:如何进入单用户模式 

1)编辑grub菜单(选定要编辑的title,而后使用e命令)           

CentOS系统grub详解_linux_06

2)在选定的kernel后附加 1sSsingle 都可以

CentOS系统grub详解_centos_07

CentOS系统grub详解_grub_08

3)ESC退出后在编辑界面,选中kernel所在行,键入"b"命令;


四、grub的安装

1grub-install

使用格式:grub-install --root-directory=/PATH/TO/rootDIR DEVICE

~]# grub-install --root-directory=/ /dev/sda3
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'.

# this device map was generated by anaconda
(hd0)     /dev/sda

2grub命令shell中输入,quit可退出)

grub> root (hd0,0)

grub> setup (hd0)

~]# 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

五、一种损坏状况及修复解决方式。

mbr中的bootloader程序损坏,注意分区表不可被破坏,此时系统无法启动

解决办法:插入一个系统引导光盘,进入救援模式重新安装grub

CentOS系统grub详解_centos_09

总结:linux的启动引导过程主要由grub完成,所以配置以及损坏之后的修复十分重要。