基于LFS-6.3的 GRUB-0.97 实用教程

  • 前言
x86-linux机器启动流程一般是
	BIOS->MBR->BOOTLOADER(LILO/GRUB)->kernel
	对于使用 GRUB-0.97 的 x86机器来说,也是一样的,不过MBR被 GRUB-0.97 接管了.


GRUB-0.97 (网站:http://www.gnu.org/software/grub/,下载地址 https://ftp.gnu.org/gnu/grub/) 的主题有 
	1.安装GRUB-0.97
	2.配置GRUB-0.97配置文件
	3.GRUB-0.97如何启动

// 比较全的版本 在 http://ftp.octave.org/gnu/grub/
3. 基于GRUB-0.97的x86机器启动流程
  • 第一种启动流程(无stage1.5)
BIOS->MBR位置的stage1(属于GRUB-0.97)->/boot/grub/stage2(属于GRUB-0.97)->kernel
// stage1 索引 stage2 靠的是 硬盘绝对位置
  • 第二种启动流程(有stage1.5)
BIOS->MBR位置的stage1(属于GRUB-0.97)->MBR后的xxx_stage_1_5(GRUB-0.97)->/boot/grub/stage2(属于GRUB-0.97)->kernel
// stage1   索引 stage1_5 靠的是 硬盘绝对位置
// stage1_5 索引 stage2   靠的是 文件系统挂载,文件名索引
1. 安装GRUB-0.97
  • 针对第一种启动流程的安装(无stage1.5)
tar xvf $LFS/grub-0.97.tar.gz
cd grub-0.97
patch -Np1 -i $LFS/grub-0.97-disk_geometry-1.patch
./configure --prefix=/usr
make
make check

make install
mkdir -v /boot/grub
cp -v /usr/lib/grub/i386-pc/stage{1,2} /boot/grub
cd ..
rm -rf grub-0.97
输入grub
grub命令行下输入
root (hd0,1)
setup (hd0)
	实际上执行 install (hd0,1)/boot/grub/stage1 (hd0) (hd0,1)/boot/grub/stage2 p /boot/grub/menu.lst 
quit
  • 针对第二种启动流程的安装(有stage1.5)
tar xvf $LFS/grub-0.97.tar.gz
cd grub-0.97
patch -Np1 -i $LFS/grub-0.97-disk_geometry-1.patch
./configure --prefix=/usr
make
make check

make install
mkdir -v /boot/grub
cp -v /usr/lib/grub/i386-pc/stage{1,2} /boot/grub
cp -v /usr/lib/grub/i386-pc/xfs_stage1_5 /boot/grub
	// /usr/lib/grub/i386-pc 下面有很多 *_stage1_5 ,具体拷贝什么要看 /boot/grub 所在分区的文件系统类型.
cd ..
rm -rf grub-0.97
输入grub
grub命令行下输入
root (hd0,1)
setup (hd0)
	实际上执行install /boot/grub/stage1 (hd0) (hd0)1+0 p (hd0,1)/boot/grub/stage2 /boot/grub/menu.lst
quit
2.配置GRUB-0.97配置文件
  • 针对第一二种启动流程的安装
cat > /boot/grub/menu.lst << "EOF"
# Begin /boot/grub/menu.lst
# By default boot the first menu entry.
default 0
# Allow 30 seconds before booting the default.
timeout 30
# Use prettier colors.
color green/black light-green/black
# The first entry is for LFS.
title LFS 6.3
root (hd0,1)
kernel /boot/lfskernel-2.6.22.5 root=/dev/hda2
EOF
mkdir -v /etc/grub
ln -sv /boot/grub/menu.lst /etc/grub
# 做这个动作,应该只是为了修改 /boot/grub/menu.lst 方便
综述
  • 针对第一种启动流程
(只有stage1 和 stage2时) :
	其实是包括两个 磁盘 
		1.
			输入磁盘: (2个grub镜像(stage1 stage2)及menu.lst所在磁盘分区)
		2.
			输出磁盘: 待写入MBR的磁盘

	写入过程:
		1.检测输入磁盘是否存在
			存在则挂载输入磁盘(通过root 指令指定或者install中直接写入磁盘及分区)
			不存在则直接返回Error 17: Cannot mount selected partition,并终止写入过程
		2.检测输出磁盘是否存在
			存在则什么都不做
			不存在则直接返回Error 21: Selected disk does not exist,并终止写入过程
		3.检测 /boot/grub/stage1 /boot/grub/stage2 是否存在
			存在则什么都不做
			不存在则直接返回Error 15: File not found,并终止写入过程
		4.检测计算stage2的绝对地址
		5.将挂载后的磁盘中的 /boot/grub/stage1(512字节)recipe(替换跳转地址为stage2的绝对地址)写入 磁盘的MBR(512字节)
	启动过程
		1. BIOS启动,选择输出磁盘作为启动介质,将控制权交给输出磁盘的MBR
		2. 输出磁盘的MBR启动,并通过 stage2的绝对地址 ,跳转到 stage2(/boot/grub/stage2) ,(跳转时打印GRUB Loading stage2..) 将控制权交给 stage2 
		3. stage2 启动, 读取 /boot/grub/menu.lst,根据/boot/grub/menu.lst 中的脚本启动
		4. 启动脚本
			4.1 root (hd0,1)
				通过 root 指令指定下面的指令所读取的文件位于hda磁盘的2分区
			4.2 kernel /boot/lfskernel-2.6.22.5 root=/dev/hda2 
				通过 kernel 指令
					1.加载 /boot/lfskernel-2.6.22.5(内核) 到内存
					2.设置bootargs为root=/dev/hda2
					3.将控制权移交给加载到内存中的内核
  • 针对第二种启动流程
综上(只有stage1 和 stage2 和 xxx_stage_1_5 (xxx为xfs,xfs为/boot/grub/stage1所在分区的文件系统格式)) :
	其实是包括两个 磁盘 
		1.
			输入磁盘: (2个grub镜像(stage1 stage2 xxx_stage_1_5)及menu.lst所在磁盘分区)
		2.
			输出磁盘: 待写入MBR的磁盘

	写入过程:
		1.检测输入磁盘是否存在
			存在则挂载输入磁盘(通过root 指令指定或者install中直接写入磁盘及分区)
			不存在则直接返回Error 17: Cannot mount selected partition,并终止写入过程
		2.检测输出磁盘是否存在
			存在则什么都不做
			不存在则直接返回Error 21: Selected disk does not exist,并终止写入过程
		3.检测 /boot/grub/stage1 /boot/grub/stage2 是否存在
			存在则什么都不做
			不存在则直接返回Error 15: File not found,并终止写入过程
		4.计算即将写入的 xxx_stage_1_5 的绝对地址(MBR 后面),将挂载后的磁盘中的 /boot/grub/xxx_stage_1_5 写到该绝对地址 (具体是哪里,TODO)
		5.将挂载后的磁盘中的 /boot/grub/stage1(512字节)recipe(替换跳转地址为xxx_stage_1_5的绝对地址)写入 磁盘的MBR(512字节)

	启动过程
		1. BIOS启动,选择输出磁盘作为启动介质,将控制权交给输出磁盘的MBR
		2. 输出磁盘的MBR启动,并通过 stage1_5 的绝对地址 ,跳转到 stage1_5(/boot/grub/xxx_stage_1_5) ,(跳转时打印GURB Loading stage1.5)将控制权交给 stage1.5
		3. stage1.5启动
			1.挂载 安装grub时写入的(install /boot/grub/stage1 (hd0) (hd0)1+18 p (hd0,1)/boot/grub/stage2 /boot/grub/menu.lst)中的 (hd0),挂在格式为xfs文件系统
			2.读取 /boot/grub/stage2到内存,如果没有stage2,则会打印 Error 15,在grub-0.97中 Error 15: File not found
			3.打印GRUB loading, please wait...
			4.将控制权交给 /boot/grub/stage2

		4. stage2 启动, 读取 /boot/grub/menu.lst,根据/boot/grub/menu.lst 中的脚本启动
		5. 启动脚本
			4.1 root (hd0,1)
				通过 root 指令指定下面的指令所读取的文件位于hda磁盘的2分区
			4.2 kernel /boot/lfskernel-2.6.22.5 root=/dev/hda2 
				通过 kernel 指令
					1.加载 /boot/lfskernel-2.6.22.5(内核) 到内存
					2.设置bootargs为root=/dev/hda2
					3.将控制权移交给加载到内存中的内核
问题
    1. grub 启动读取的文件顺序是哪些
只读取 /boot/grub/menu.lst
    1. 针对第一二种启动流程,删掉 /boot/grub/stage1 有无影响
无影响,lfs正常启动
    1. 针对第二种启动流程,删掉 /boot/grub/xxx_stage1_5 有无影响
无影响,lfs正常启动
    1. 针对第一二种启动流程,删掉 /boot/grub/stage2有无影响
第一种,暂时无影响,lfs正常启动.因为stage1到stage2 不是读的文件系统,而是硬盘绝对位置.(rm的时候没有删除文件内容).但是后期如果覆盖该 stage2 所在位置,则 grub的命令行无法启动
第二种,grub的命令行无法启动
    1. grub-0.97 make install 后生成了什么文件
/usr # ls -R
.:
bin  info  lib  man  sbin

./bin:
mbchk

./info:
dir  grub.info  multiboot.info

./lib:
grub

./lib/grub:
i386-pc

./lib/grub/i386-pc:
e2fs_stage1_5  ffs_stage1_5      jfs_stage1_5    reiserfs_stage1_5  stage2           ufs2_stage1_5    xfs_stage1_5
fat_stage1_5   iso9660_stage1_5  minix_stage1_5  stage1             stage2_eltorito  vstafs_stage1_5

./man:
man1  man8

./man/man1:
mbchk.1

./man/man8:
grub.8  grub-install.8  grub-md5-crypt.8  grub-terminfo.8

./sbin:
grub  grub-install  grub-md5-crypt  grub-set-default  grub-terminfo
  • 安装文件类型
# find . -type f   | xargs  file
./lib/grub/i386-pc/stage2:            GRand Unified Bootloader stage2 version 3.2, identifier 0x0, GRUB version 0.97, configuration file /boot/grub/menu.lst
./lib/grub/i386-pc/stage2_eltorito:   data
./lib/grub/i386-pc/e2fs_stage1_5:     GRand Unified Bootloader stage1_5 version 3.2, identifier 0x2, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/fat_stage1_5:      GRand Unified Bootloader stage1_5 version 3.2, identifier 0x3, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/ffs_stage1_5:      GRand Unified Bootloader stage1_5 version 3.2, identifier 0x1, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/iso9660_stage1_5:  data
./lib/grub/i386-pc/jfs_stage1_5:      GRand Unified Bootloader stage1_5 version 3.2, identifier 0x7, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/minix_stage1_5:    GRand Unified Bootloader stage1_5 version 3.2, identifier 0x4, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/reiserfs_stage1_5: GRand Unified Bootloader stage1_5 version 3.2, identifier 0x5, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/ufs2_stage1_5:     GRand Unified Bootloader stage1_5 version 3.2, identifier 0xa, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/vstafs_stage1_5:   GRand Unified Bootloader stage1_5 version 3.2, identifier 0x6, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/xfs_stage1_5:      GRand Unified Bootloader stage1_5 version 3.2, identifier 0x8, GRUB version 0.97, configuration file /boot/grub/stage2
./lib/grub/i386-pc/stage1:            x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, code offset 0x48
./sbin/grub:                          ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.0, dynamically linked (uses shared libs), not stripped
./sbin/grub-install:                  Bourne shell script text executable
./sbin/grub-md5-crypt:                Bourne shell script text executable
./sbin/grub-terminfo:                 Bourne shell script text executable
./sbin/grub-set-default:              Bourne shell script text executable
./bin/mbchk:                          ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.0, dynamically linked (uses shared libs), not stripped
./info/dir:                           data
./info/grub.info:                     data
./info/multiboot.info:                data
./man/man1/mbchk.1:                   troff or preprocessor input text
./man/man8/grub.8:                    troff or preprocessor input text
./man/man8/grub-install.8:            troff or preprocessor input text
./man/man8/grub-md5-crypt.8:          troff or preprocessor input text
./man/man8/grub-terminfo.8:           troff or preprocessor input text
  • 安装的可执行文件
二进制可执行文件 /usr/bin/mbchk
root:/sources/grub-0.97# mbchk  /boot/grub/stage1
/boot/grub/stage1: No Multiboot header.
# mbchk  /usr/lib/grub/i386-pc/xfs_stage1_5 
/usr/lib/grub/i386-pc/xfs_stage1_5: No Multiboot header.
root:/sources/grub-0.97# mbchk  /boot/grub/stage2
/boot/grub/stage2: The Multiboot header is found at the offset 5630.
/boot/grub/stage2: Bad checksum (0x2818d04).
二进制可执行文件 /usr/sbin/grub
在 linux 的shell界面 下面 提供一个 grub shell,该 grub shell 供 安装 stage1 和 stage1_5 到 MBR
以下皆为sh脚本文件
/usr/sbin/grub-install
/usr/sbin/grub-md5-crypt
/usr/sbin/grub-terminfo
/usr/sbin/grub-set-default
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值