一、GRUB 简介
系统启动引导管理器,是在计算机启动后运行的第一个程序,他是用来负责加载、传输控制到操作系统的内核,一旦把内核挂载,系统引导管理器的任务就算完成退出,系统引导的其它部份,比如系统的初始化及启动过程则完全由内核来控制完成。
二、GRUB软件包版安装
linux系统中查看有没有安装grub-0.97-13.2包,如过没有安装此包,那么可以在光盘中找到grub-0.97-13.2进行安装即可。一般在安装完系统后该包已经被安装到系统中。如果没有rpm包可以下载tar包进行编译安装。(grub-0.97-13.2是安装grubmbr中的工具包)
[root@rhel5 ~]# rpm -qa grub
grub-0.97-13.2
[root@rhel5 ~]#
三、GRUB的配置文件grub.conf的写法

 1. 设备命名命名习惯

首先GRUB要求设备名被括在一个括号中。fd表示软盘,hd 表示硬盘(不区分IDE还是SCSI)。其次设备是从0开始编号。分区也是如此,分区和设备之间用一个逗号分开。
下面给出几个例子 :
(fd0) :表示整个软盘
(hd0,1)表示系统的第一个硬盘的第2个分区
(hd0,0):表示系统中的第一个硬盘的第一个分区。
如果没有指定某个分区,则表示使用整个设备,否则只使用指定的分区。

 2. grub.conf的写法

default:定义默认引导的操作系统。0 表示第一个操作系统,1表示第2个,依此类推。
hiddenmenu:用于启动时隐藏菜单,除非在timeout之前按下 ESC 才能看到菜单。
timeout:定义多少秒内如果用户没有按下键盘上的某个按键,就自动引导 default 所指定的操作系统。
splashp_w_picpath:指定启动的背景图片,一般为压缩文件。路径为绝对路径。
title:定义引导项目的名称。
root:指定boot分区所在磁盘及分区,如:root (hd0,0)
kernel:指定kernel文件所在绝对目录地址,如:kernel /boot/vmlinuz-2.6.18-92.el5 ro root=LABEL=/ rhgb quiet
initrd:指定ramdisk盘所在绝对目录地址,如:initrd /boot/initrd-2.6.18-92.el5.img
 
注意:
kernelinitrd这两个设置项中,指定的路径都是绝对路径。因为这两个文件都存放在/boot目录。而且/boot所在的分区已经在root (hd[0-n],[0-n])中指定,所以就无需再指明kernelinitrd在哪个分区了。如果boot分区为独立分区,那么前面的/boot省略掉。如果boot分区为非独立分区,那么必须加上/boot

 3.boot分区为独立分区

[root@RHEL5 grub]# cat grub.conf
default=0
#启动第一个操作系统。
timeout=5
#超时时间为5秒。
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-92.el5) #设置项目标题。
        root (hd0,0)
#设置boot分区所在位置,第一个硬盘的第一分区。
        kernel /vmlinuz-2.6.18-92.el5 ro root=LABEL=/ rhgb quiet
#设置kernel所在位置。注意这里的root指的系统根所在位置,与上面的root不同。
        initrd /initrd-2.6.18-92.el5.img
        #设置initrd所在位置。
[root@RHEL5 grub]#

 4.boot分区非独立分区

[root@rhel5 ~]# cat /boot/grub/grub.conf
default=0
#启动第一个操作系统。
timeout=5
#超时时间为5秒。
splashp_w_picpath=(hd0,0)/boot/grub/splash.xpm.gz
hiddenmenu
title Red Hat Enterprise Linux Server (2.6.18-92.el5) #设置项目标题。
        root (hd0,0)
#设置boot分区所在位置,第一个硬盘的第一分区。
        kernel /boot/vmlinuz-2.6.18-92.el5 ro root=LABEL=/ rhgb quiet
#设置kernel所在位置。注意这里的root指的系统根所在位置,与上面的root不同。
        initrd /boot/initrd-2.6.18-92.el5.img
#设置initrd所在位置。
[root@rhel5 ~]#
四、GRUBstage文件
 

 1. stage文件

GRUB含有几个p_w_picpaths文件,两个必需的文件stage1 stage2,可选的为stage1_5和两个网络引导的文件nxgrubpxegrubstage1 是用于引导GURB的一个必须的映象文件。通常它是被嵌入到 MBR。或者一个分区的引导扇区之中。因为 PC 的引导扇区是 512 字节,所以 stage1也是512字节。stage1的作用就是从一个本地磁盘加载stage 2 或者stage1_5 。因为大小的限制,stage1 stage2 或者stage1_5的位置进行编码。
stage1stage2文件一般位于/boot/grub/目录下,在这个目录下还有很多stage1_5的文件,而且都是以文件系统格式命名的。它们的目的是在stage1stage2之间搭建一个桥梁,也就是stage1加载stage1_5, 然后stage1_5加载stage2stage1 stage1_5的不同之处是 stage1无法识别文件系统,stage1_5可以。因为 Stage2 太大了,无法被嵌入到某个固定的区域,而stage1_5可以安装在 MBR 之后的位置。

 2.grub启动方式

 1)stage1_5 -> stage2 方式

首先监测是否是合适的文件系统的驱动,如果存在就使用文件系统逻辑的方式stage2 ;如果找不到就用blocklist的方式再找stage2,找到了挂起来,找不到报错。

 2)stage1 -> stage2方式

如果确认stage1_5没有被安装在MBR之后, stage1就会使用记录的stage2blocklist寻找stage2,找到后挂起来,找不到报错。
五、GRUB三种安装方式
linux系统中可以执行grub命令。进入grub的命令界面,在这里面可以使用一些常用的命令。也可以对不太了解的命令进行帮助查看。可以使用TAB键对命令进行补全操作,系统会给予提示信息。如下:
[root@RHEL5 ~]# 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> help
blocklist FILE                         boot
cat FILE                               chainloader [--force] FILE
clear                                  color NORMAL [HIGHLIGHT]
configfile FILE                        device DRIVE DEVICE
displayapm                             displaymem
find FILENAME                          geometry DRIVE [CYLINDER HEAD SECTOR [
halt [--no-apm]                        help [--all] [PATTERN ...]
hide PARTITION                         initrd FILE [ARG ...]
kernel [--no-mem-option] [--type=TYPE] makeactive
map TO_DRIVE FROM_DRIVE                md5crypt
module FILE [ARG ...]                  modulenounzip FILE [ARG ...]
pager [FLAG]                           partnew PART TYPE START LEN
parttype PART TYPE                     quit
reboot                                 root [DEVICE [HDBIAS]]
rootnoverify [DEVICE [HDBIAS]]         serial [--unit=UNIT] [--port=PORT] [--
setkey [TO_KEY FROM_KEY]               setup [--prefix=DIR] [--stage2=STAGE2_
terminal [--dumb] [--no-echo] [--no-ed terminfo [--name=NAME --cursor-address
testvbe MODE                           unhide PARTITION
uppermem KBYTES                        vbeprobe [MODE]
grub> help root
root: root [DEVICE [HDBIAS]]
    Set the current "root device" to the device DEVICE, then attempt
    to mount it to get the partition size (for passing the partition
    descriptor in `ES:ESI', used by some chain-loaded bootloaders),
    the BSD drive-type (for booting BSD kernels using their native
    boot format), and correctly determine  the PC partition where a
    BSD sub-partition is located. The optional HDBIAS parameter is a
    number to tell a BSD kernel how many BIOS drive numbers are on
    controllers before the current one. For example, if there is an
    IDE disk and a SCSI disk, and your FreeBSD root partition is on
    the SCSI disk, then use a `1' for HDBIAS.
rootnoverify: rootnoverify [DEVICE [HDBIAS]]
    Similar to `root', but don't attempt to mount the partition. This
    is useful for when an OS is outside of the area of the disk that
    GRUB can read, but setting the correct root device is still
    desired. Note that the items mentioned in `root' which derived
    from attempting the mount will NOT work correctly.
grub>

 1.install命令

命令格式:
install: install [--stage2=STAGE2_FILE] [--force-lba] STAGE1 [d] DEVICE STAGE2 [ADDR] [p] [CONFIG_FILE] [REAL_CONFIG_FILE]
STAGE1:指定stage1文件所在绝对路径。
DEVICE:指定vstage1装载的位置。
STAGGE2:指定stage2文件所在绝对路径。
ADDR:指定装载stage2的位置,如果不写,系统自动决定。
例如:
boot为独立分区时:
grub>root (hd0,0)
grub>install /grub/stage1 (hd0) /grub/stage2 p /grub/grub.conf
 
boot为非独立分区时:
grub>root (hd0,0)
grub>install (hd0,0)/boot/grub/stage1 (hd0) (hd0,0)/boot/grub/stage2 p (hd0,0)/boot/grub/grub.conf
该句可以写成如下这样:
grub>root (hd0,0)
grub>install /boot/grub/stage1 (hd0) /boot/grub/stage2 p /boot/grub/grub.conf
注意:
boot分区为独立分区与非独立分区时,安装方法有区别。

 2.setup命令

命令格式:
setup [--prefix=DIR] [--stage2=STAGE2_FILE] [--force-lba] INSTALL_DEVICE [IMAGE_DEVICE]
INSTALL_DEVICE:指定安装grub的位置。
IMAGE_DEVICE:指定要安装的镜象文件。
例如:
[root@RHEL5 ~]# 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,)
 Possible partitions are:
   Partition num: 0,  Filesystem type is ext2fs, partition type 0x83
   Partition num: 1,  Filesystem type unknown, partition type 0x82
   Partition num: 2,  Filesystem type is ext2fs, partition type 0x83
grub> root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83
grub> 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)"...  15 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+15 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub>

 3.grub-install命令

grub-install拷贝grub镜象文件到DIR/boot目录中(可以通过参数--root-directory指定目录),并用grub shell安装grubMBR中。
命令格式:
grub-install [OPTION] install_device
例如:
[root@RHEL5 ~]# grub-install hd0
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
[root@RHEL5 ~]#

 4.区别

3种方式中,最常用的是grub-install,其中install是最底层的方式。setup是更高一层的方式,而grub-install是最高级最简单的方式。grub-install命令安装grubMBR中的时候利用的安装源是/usr/share/grub/i386-redhat/下的stage1stage2和各种stage1_5。而install命令与setup命令安装grub的安装源是/boot/grub目录中的stage1stage2和各种stage1_5。所以要使用这两种安装方法来安装grubMBR中的时候,确保/boot/grub目录中的stage1stage2和各种stage1_5必须存在。同时grub-install命令安装的grub只是利用stage1_5 -> stage2 方式的文件系统方式去查找,如果stage2不存在,那么系统就不能启动。setup命令安装的grubstage1_5 -> stage2 方式去查找stage2,首先监测是否是合适的文件系统的驱动,如果存在就使用文件系统逻辑的方式查找stage2 ;如果找不到就用blocklist的方式再找stage2install命令安装的grub使用stage1 -> stage2方式去查找。