GRUB笔记:grub.conf解析 stage介绍 grub故障及安装(2)

三、grub与stage文件
1、stage文件
[root@client ~]# ll /boot/grub/
total 259
-rw-r--r--. 1 root root     63 Mar 27 16:43 device.map
-rw-r--r--. 1 root root  13204 Mar 27 16:43 e2fs_stage1_5
-rw-r--r--. 1 root root  12516 Mar 27 16:43 fat_stage1_5
-rw-r--r--. 1 root root  11748 Mar 27 16:43 ffs_stage1_5
-rw-------. 1 root root    783 Mar 27 16:43 grub.conf
-rw-r--r--. 1 root root  11756 Mar 27 16:43 iso9660_stage1_5
-rw-r--r--. 1 root root  13220 Mar 27 16:43 jfs_stage1_5
lrwxrwxrwx. 1 root root     11 Mar 27 16:43 menu.lst -> ./grub.conf
-rw-r--r--. 1 root root  11940 Mar 27 16:43 minix_stage1_5
-rw-r--r--. 1 root root  14300 Mar 27 16:43 reiserfs_stage1_5
-rw-r--r--. 1 root root   1341 May  7  2010 splash.xpm.gz
-rw-r--r--. 1 root root    512 Mar 27 16:43 stage1
-rw-r--r--. 1 root root 113044 Mar 27 16:43 stage2
-rw-r--r--. 1 root root  12008 Mar 27 16:43 ufs2_stage1_5
-rw-r--r--. 1 root root  11364 Mar 27 16:43 vstafs_stage1_5
-rw-r--r--. 1 root root  13932 Mar 27 16:43 xfs_stage1_5

***stage1 是用于引导GURB的一个必须的映象文件。通常它是被嵌入到 MBR。或者一个分区的引导扇区之中。因为 PC 的引导扇区是 512 字节,所以 stage1也是512字节。stage1的作用就是从一个本地磁盘加载stage 2 或者stage1_5 。因为大小的限制,stage1 对stage2 或者stage1_5的位置进行编码。
***在这个目录下还有很多stage1_5的文件,而且都是以文件系统格式命名的。它们的目的是在stage1和stage2之间搭建一个桥梁,也就是stage1加载stage1_5, 然后stage1_5加载stage2。stage1 和stage1_5的不同之处是 stage1无法识别文件系统,stage1_5可以。因为 Stage2 太大了,无法被嵌入到某个固定的区域,而stage1_5可以安装在 MBR 之后的位置。
2、grub启动方式
a、stage1-> stage1_5 -> stage2
首先监测是否有合适的文件系统的驱动,如果有就使用文件系统逻辑的方式加载stage2 ;如果找不到就用blocklist的方式再找stage2,找到了挂起来,找不到报错。
b、stage1 -> stage2
如果确认stage1_5没有被安装在MBR之后, stage1就会使用记录的stage2的blocklist寻找stage2,找到后挂起来,找不到报错。

四、grub的三种安装方式
在linux系统中可以执行grub命令。进入grub的命令界面,在这里面可以使用一些常用的命令。
可以输入命令help查看帮助
[root@client ~]# 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
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_
grub will attempt to avoid printing an terminal [--dumb] [--no-echo] [--no-ed
terminfo [--name=NAME --cursor-address testvbe MODE                           
unhide PARTITION                       uppermem KBYTES                        
vbeprobe [MODE]                        
grub> help root
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.

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)
root (hd0,0)
 Filesystem type is ext2fs, partition type 0x83
grub> install /grub/stage1 (hd0) /grub/stage2 p /grub/grub.conf
install /grub/stage1 (hd0) /grub/stage2 p /grub/grub.conf
boot为非独立分区时:
grub>root (hd0,0)
grub>install /boot/grub/stage1 (hd0) /boot/grub/stage2 p /boot/grub/grub.conf

2、setup命令
setup [--prefix=DIR] [--stage2=STAGE2_FILE] [--force-lba] INSTALL_DEVICE [IMAGE_DEVICE]
INSTALL_DEVICE:指定安装grub的位置���
IMAGE_DEVICE:指定要安装的镜象文件。
例如:
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)"...  26 sectors are embedded.
succeeded
 Running "install /grub/stage1 (hd0) (hd0)1+26 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.

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

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

注意:
grub-install命令安装grub到MBR中的时候利用的安装源是/usr/share/grub/x86_64-RedHat/下的stage1、stage2和各种stage1_5。它利用stage1_5 -> stage2 方式的文件系统方式去查找,如果stage2不存在,那么系统就不能启动。
install、setup命令安装grub的安装源是/boot/grub目录中的stage1、stage2和各种stage1_5。所以这两种安装方法来安装grub到MBR中的时候,确保/boot/grub目录中的stage1、stage2和各种stage1_5必须存在。setup命令利用stage1_5 -> stage2方式去查找stage2,首先监测是否有合适的文件系统驱动,存在就使用文件系统逻辑的方式查找stage2 ;如果不存在就用blocklist的方式再找stage2。Install命令利用stage1 -> stage2方式去查找。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/21629.html