六:测试

 1. boot为独立分区

该测试boot分区为独立分区。分区情况如下:
[root@RHEL5 ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14          78      522112+  82  Linux swap / Solaris
/dev/sda3              79        3916    30828735   83  Linux
[root@RHEL5 ~]#

 1) grub-install命令测试

首先使用grub-install命令对已安装好的系统进行重新安装grub(因为系统默认的安装方式是用install安装的)。
[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 ~]#
执行完成之后,使用rm命令删除/boot/grub下的stage2文件,重启系统。会看到有如下的错误出现:
grub学习笔记(二)_install
   这个时候我们只能进入linux rescue模式对系统进行修复了,而且只可以使用grub-install命令对系统进行修复。因为/boot/grub下的stage2文件已经被我们删掉了。原因是的安装源已被破坏。这个时候插入光盘,设置为cdrom启动,输入linux rescue回车进入rescue模式中。挂起你的系统到/mnt/sysp_w_picpath目录下。执行命令grub-install hd0命令来重新安装grub系统可以正常启动。具体操作如下:
grub学习笔记(二)_grub-install_02
 回车
grub学习笔记(二)_install_03
    选配置继续。(几步的语言选择和网络设置省略)
grub学习笔记(二)_linux_04
   选择ok后,出现sh-3.2#后,执行以下操作。
grub学习笔记(二)_linux_05
   完成后重新启动系统。可以看到系统正常启动成功了。
grub学习笔记(二)_linux_06
 备注:
那么在什么情况下使用grub-install命令来重新安装grub呢?
1grub被擦掉的时候。2/boot/grub目录下的所有镜像文件都给删除时。 2:使用grub-install命令安装的grubboot/grub目录下stage2文件被误删除时(有人会考虑到把/usr/share/grub/i386-redhat/下的stage2拷贝到/boot/grub目录中,此方法是不成功的)。

 2) setup命令测试

使用setup命令重新安装grubMBR中。
[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,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> quit
[root@RHEL5 ~]#
使用rm命令删除/boot/grub/目录中的stage2文件重启系统。系统仍然可以启动,而且重新生成了stage2文件。如下:
[root@RHEL5 grub]# cd
[root@RHEL5 ~]# cd /boot//grub/
[root@RHEL5 grub]# ls stage2
stage2
[root@RHEL5 grub]#
现在使用shred命令删除stage2文件,再使用rm命令把stage2文件删除。重新启动系统。
[root@RHEL5 grub]# shred -v stage2
shred: stage2: pass 1/25 (random)...
shred: stage2: pass 2/25 (ffffff)...
shred: stage2: pass 3/25 (492492)...
shred: stage2: pass 4/25 (888888)...
shred: stage2: pass 5/25 (924924)...
shred: stage2: pass 6/25 (249249)...
shred: stage2: pass 7/25 (cccccc)...
shred: stage2: pass 8/25 (222222)...
shred: stage2: pass 9/25 (db6db6)...
shred: stage2: pass 10/25 (777777)...
shred: stage2: pass 11/25 (999999)...
shred: stage2: pass 12/25 (eeeeee)...
shred: stage2: pass 13/25 (random)...
shred: stage2: pass 14/25 (dddddd)...
shred: stage2: pass 15/25 (000000)...
shred: stage2: pass 16/25 (555555)...
shred: stage2: pass 17/25 (111111)...
shred: stage2: pass 18/25 (444444)...
shred: stage2: pass 19/25 (aaaaaa)...
shred: stage2: pass 20/25 (333333)...
shred: stage2: pass 21/25 (bbbbbb)...
shred: stage2: pass 22/25 (b6db6d)...
shred: stage2: pass 23/25 (666666)...
shred: stage2: pass 24/25 (6db6db)...
shred: stage2: pass 25/25 (random)...
[root@RHEL5 grub]#rm –rf stage2
[root@RHEL5 grub]#reboot
这个时候系统不能再启动了,出现如下显示:
grub学习笔记(二)_setup_07
现在要恢复系统的话,只能是使用grub-install命令对系统进行恢复。修复方法与上面相同。

 3) install命令测试

使用install命令重新写MBR。如下:
[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,0)
 Filesystem type is ext2fs, partition type 0x83
grub> install /grub/stage1 (hd0) /grub/stage2 p /grub/grub.conf
grub> quit
[root@RHEL5 ~]#
使用rm命令删除/boot/grub/目录中的stage2文件重启系统。系统仍然可以启动,而且重新生成了stage2文件。如下:
[root@RHEL5 grub]# cd
[root@RHEL5 ~]# cd /boot//grub/
[root@RHEL5 grub]# ls stage2
stage2
[root@RHEL5 grub]#
现在使用shred命令删除stage2文件,再使用rm命令把stage2文件删除。重新启动系统。
[root@RHEL5 grub]# shred -v stage2
shred: stage2: pass 1/25 (random)...
shred: stage2: pass 2/25 (ffffff)...
shred: stage2: pass 3/25 (492492)...
shred: stage2: pass 4/25 (888888)...
shred: stage2: pass 5/25 (924924)...
shred: stage2: pass 6/25 (249249)...
shred: stage2: pass 7/25 (cccccc)...
shred: stage2: pass 8/25 (222222)...
shred: stage2: pass 9/25 (db6db6)...
shred: stage2: pass 10/25 (777777)...
shred: stage2: pass 11/25 (999999)...
shred: stage2: pass 12/25 (eeeeee)...
shred: stage2: pass 13/25 (random)...
shred: stage2: pass 14/25 (dddddd)...
shred: stage2: pass 15/25 (000000)...
shred: stage2: pass 16/25 (555555)...
shred: stage2: pass 17/25 (111111)...
shred: stage2: pass 18/25 (444444)...
shred: stage2: pass 19/25 (aaaaaa)...
shred: stage2: pass 20/25 (333333)...
shred: stage2: pass 21/25 (bbbbbb)...
shred: stage2: pass 22/25 (b6db6d)...
shred: stage2: pass 23/25 (666666)...
shred: stage2: pass 24/25 (6db6db)...
shred: stage2: pass 25/25 (random)...
[root@RHEL5 grub]#rm –rf stage2
[root@RHEL5 grub]#reboot
这个时候系统不能再启动了,出现如下显示:
grub学习笔记(二)_install_08

 2.boot为非独立分区

该测试系统分区如下:
[root@rhel5 grub]# Last login: Tue Apr 28 08:02:03 2009 from 192.168.88.81
[root@rhel5 ~]# fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2544    20434648+  83  Linux
/dev/sda2            2545        2609      522112+  82  Linux swap / Solaris
[root@rhel5 ~]#
boot分区与根在同一分区中。
grub-install命令与setup命令与boot为独立分区时使用方法一样。而install命令略有不同。就是在指定路径的时候不能省略boot目录。如下:
grub>root (hd0,0)
grub>install /boot/grub/stage1 (hd0) /boot/grub/stage2 p /boot/grub/grub.conf
 
以下为测试结果:
使用install命令重新写MBR。如下:
[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,0)
 Filesystem type is ext2fs, partition type 0x83
grub>install /boot/grub/stage1 (hd0) /boot/grub/stage2 p /boot/grub/grub.conf
grub> quit
[root@RHEL5 ~]#
使用rm命令删除/boot/grub/目录中的stage2文件重启系统。系统仍然可以启动。
现在使用shred命令删除stage2文件,再使用rm命令把stage2文件删除。重新启动系统。
[root@RHEL5 grub]# shred -v stage2
shred: stage2: pass 1/25 (random)...
shred: stage2: pass 2/25 (ffffff)...
shred: stage2: pass 3/25 (492492)...
shred: stage2: pass 4/25 (888888)...
shred: stage2: pass 5/25 (924924)...
shred: stage2: pass 6/25 (249249)...
shred: stage2: pass 7/25 (cccccc)...
shred: stage2: pass 8/25 (222222)...
shred: stage2: pass 9/25 (db6db6)...
shred: stage2: pass 10/25 (777777)...
shred: stage2: pass 11/25 (999999)...
shred: stage2: pass 12/25 (eeeeee)...
shred: stage2: pass 13/25 (random)...
shred: stage2: pass 14/25 (dddddd)...
shred: stage2: pass 15/25 (000000)...
shred: stage2: pass 16/25 (555555)...
shred: stage2: pass 17/25 (111111)...
shred: stage2: pass 18/25 (444444)...
shred: stage2: pass 19/25 (aaaaaa)...
shred: stage2: pass 20/25 (333333)...
shred: stage2: pass 21/25 (bbbbbb)...
shred: stage2: pass 22/25 (b6db6d)...
shred: stage2: pass 23/25 (666666)...
shred: stage2: pass 24/25 (6db6db)...
shred: stage2: pass 25/25 (random)...
[root@RHEL5 grub]#rm –rf stage2
[root@RHEL5 grub]#reboot
这个时候系统不能再启动了,出现如下显示:
grub学习笔记(二)_linux_09
同样现在要恢复系统的话,只能是使用grub-install命令对系统进行恢复。恢复后系统就可以正常启动了。
七、修复grub.conf
当系统中的grub.conf文件被删除掉后。在没有备份的情况下我们只能用grub交互模式来写grub.conf文件让系统重新启动起来。正如下图所示:
grub学习笔记(二)_install_10
这台测试机的分区如下:
 [root@rhel5 ~]# fdisk -l
 Disk /dev/sda: 21.4 GB, 21474836480 bytes
 255 heads, 63 sectors/track, 2610 cylinders
 Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
 /dev/sda1   *           1        2544    20434648+  83  Linux
 /dev/sda2            2545        2609      522112+  82  Linux     swap / Solaris
[root@rhel5 ~]#
boot分区为非独立分区。grub.conf文件内容如下:
[root@rhel5 grub]# cat grub.conf
default=0
timeout=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)
        kernel /boot/vmlinuz-2.6.18-92.el5 ro root=LABEL=/ rhgb quiet
        initrd /boot/initrd-2.6.18-92.el5.img
现在尝试用如下操作修复grub.conf文件来启动系统。
grub学习笔记(二)_linux_11
输入完成后按回车开始正常启动系统。也可以写成下面这样使系统可以正常启动。不使用卷标指定根,直接使用设备名。
grub学习笔记(二)_setup_12
正常进入系统之后在重新编写grub.conf文件。
八、附录
grub的详细使用说明参考info grub的帮助内容或登陆http://www.gnu.org/software/grub/grub.html参考官方说明对grub更深了解。