gurb故障修复
文章目录
实验一 认识grub配置文件
前提:可用的centos 6系统
#查看grub配置文件
[root@centos6 ~]# cat /boot/grub/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/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-642.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=09037557-e45a-4ea6-9ab4-93338d27fb34 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-642.el6.x86_64.img
title CentOS 6 10
# root (hd0,0)
kernel (hd0,0)/vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=173b69dc-d449-4355-9571-908a1124a06b rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd (hd0,0)/initramfs-2.6.32-642.el6.x86_64.img
解释
[root@centos6 ~]# cat /boot/grub/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/sda2
# initrd /initrd-[generic-]version.img
#boot=/dev/sda
# 默认启动菜单项,默认为0,即菜单项编号title
default=0
# 指定菜单项等待选项选择时长,默认5s,则默认选择第一个
timeout=5
# 菜单的背景图片,默认大小为640*480ps,可自定义
splashimage=(hd0,0)/grub/splash.xpm.gz
# 默认隐藏菜单
hiddenmenu
# 菜单项的显示标题,可出现多个
title CentOS 6 (2.6.32-642.el6.x86_64)
# 系统程序的存放路径:第一个硬盘的第一个扇区
root (hd0,0)
# 内核存放路径及内核文件 /vmlinuz-2.6.32-642.el6.x86_64
# 根存放路径 root=UUID=09037557-e45a-4ea6-9ab4-93338d27fb34
# 后续其他皆为内核参数
# rhgb 图形界面,可在加载时临时删除该参数
# quiet 加载内核驱动时,取消显示内核信息加载过程,可在加载时临时删除该参数
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=09037557-e45a-4ea6-9ab4-93338d27fb34 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
# 根的驱动文件存放路径 /initramfs-2.6.32-642.el6.x86_64.img
# gzip 压缩格式(如需解压,需增加gz 后缀),解压后的文件是cpio 格式文件,存放的是一个精简版的小型Linux 文件系统
# 作用:辅助加载根,损坏则无法加载根
initrd /initramfs-2.6.32-642.el6.x86_64.img
# 新增一个新的内核启动
title CentOS 6 10
# root (hd0,0) #可省略,在路径中增加
kernel (hd0,0)/vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=173b69dc-d449-4355-9571-908a1124a06b rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd (hd0,0)/initramfs-2.6.32-642.el6.x86_64.img
内核参数是存放在/proc/cmdline中
[root@centos6 ~]$ cat /proc/cmdline
ro root=UUID=09037557-e45a-4ea6-9ab4-93338d27fb34 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=129M@0M KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
实验二 验证kernel和initrd加载顺序
故障模拟
# 将配置kernel 和initrd 调整前后位置
[root@centos6 ~]# vim /boot/grub/grub.conf
[root@centos6 ~]# cat /boot/grub/grub.conf
......省略部分......
title CentOS 6 10
initrd /initramfs-2.6.32-642.el6.x86_64.img
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=09037557-e45a-4ea6-9ab4-93338d27fb34 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
现象 -Error19
处理步骤
-
在经几秒后就直接启动操作系统的界面时,按任意键,进入菜单
1 |选项说明: 2 |enter 进入当前选中os加载boot 3 |e 编辑 4 |a 修改内核参数 5 |c 进入命令界面
-
选中当前内核系统,输入e,编辑加载的内容
显示grub.conf中的内核参数,其中initrd配置在kernel之前,调整这两个参数的位置
选项说明:
b 加载boot
e 编辑
c 进入命令行
o 创建新的参数行
d 删除参数行
- 选中initrd,输出d,删除initrd配置
-
输入o,增加新的配置行
输入e编辑该配置行,输入initrd/initramfs后,双Tab会自动匹配上根文件名
回车,配置文件加载完毕
- 输入b,重新启动,可正常启动
注意:该方式只是临时修改了配置文件,保证系统可以正常启动,需修改配置文件并保存
结论:kernel配置必须在initrd配置之前加载
实验三 修复grub第一阶段故障
模拟故障
# 设置第一阶段故障
[root@centos6 ~]$ dd if=/dev/zero of=/dev/sda bs=1 count=446
446+0 records in
446+0 records out
446 bytes (446 B) copied, 0.000320794 s, 1.4 MB/s
[root@centos6 ~]$ hexdump -C -n 512 /dev/sda
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 20 |............... |
000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...|
000001d0 29 82 83 fe ff ff 00 08 20 00 00 00 35 0c 00 fe |)....... ...5...|
000001e0 ff ff 83 fe ff ff 00 08 55 0c 00 80 a9 03 00 fe |........U.......|
000001f0 ff ff 05 fe ff ff 00 88 fe 0f 00 78 01 09 55 aa |...........x..U.|
00000200
[root@centos6 ~]$ reboot
现象
处理步骤
方式一 通过grub-install命令修复(不依赖备份文件)
# 机器重启了,则使用光盘启动,进入rescue 模式
chroot /mnt/sysimage # 切换到实际根目录下
grub-install /dev/sda # 安装grub stage1 和stage1_5 到/dev/sda
sync # 将数据同步到磁盘中
sync
sync
exit
exit
# 如果机器没有重启的,直接执行grub-install
[root@centos6 ~]$ grub-install /dev/sda
Probing devices to guess BIOS drives. This may take a long time.
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'.
(fd0) /dev/fd0
(hd0) /dev/sda
[root@centos6 ~]$ hexdump -C -n 512 /dev/sda
00000000 eb 48 90 00 00 00 00 00 00 00 00 00 00 00 00 00 |.H..............|
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 02 |................|
00000040 ff 00 00 20 01 00 00 00 00 02 fa 90 90 f6 c2 80 |... ............|
00000050 75 02 b2 80 ea 59 7c 00 00 31 c0 8e d8 8e d0 bc |u....Y|..1......|
00000060 00 20 fb a0 40 7c 3c ff 74 02 88 c2 52 f6 c2 80 |. ..@|<.t...R...|
00000070 74 54 b4 41 bb aa 55 cd 13 5a 52 72 49 81 fb 55 |tT.A..U..ZRrI..U|
00000080 aa 75 43 a0 41 7c 84 c0 75 05 83 e1 01 74 37 66 |.uC.A|..u....t7f|
00000090 8b 4c 10 be 05 7c c6 44 ff 01 66 8b 1e 44 7c c7 |.L...|.D..f..D|.|
000000a0 04 10 00 c7 44 02 01 00 66 89 5c 08 c7 44 06 00 |....D...f.\..D..|
000000b0 70 66 31 c0 89 44 04 66 89 44 0c b4 42 cd 13 72 |pf1..D.f.D..B..r|
000000c0 05 bb 00 70 eb 7d b4 08 cd 13 73 0a f6 c2 80 0f |...p.}....s.....|
000000d0 84 f0 00 e9 8d 00 be 05 7c c6 44 ff 00 66 31 c0 |........|.D..f1.|
000000e0 88 f0 40 66 89 44 04 31 d2 88 ca c1 e2 02 88 e8 |..@f.D.1........|
000000f0 88 f4 40 89 44 08 31 c0 88 d0 c0 e8 02 66 89 04 |..@.D.1......f..|
00000100 66 a1 44 7c 66 31 d2 66 f7 34 88 54 0a 66 31 d2 |f.D|f1.f.4.T.f1.|
00000110 66 f7 74 04 88 54 0b 89 44 0c 3b 44 08 7d 3c 8a |f.t..T..D.;D.}<.|
00000120 54 0d c0 e2 06 8a 4c 0a fe c1 08 d1 8a 6c 0c 5a |T.....L......l.Z|
00000130 8a 74 0b bb 00 70 8e c3 31 db b8 01 02 cd 13 72 |.t...p..1......r|
00000140 2a 8c c3 8e 06 48 7c 60 1e b9 00 01 8e db 31 f6 |*....H|`......1.|
00000150 31 ff fc f3 a5 1f 61 ff 26 42 7c be 7f 7d e8 40 |1.....a.&B|..}.@|
00000160 00 eb 0e be 84 7d e8 38 00 eb 06 be 8e 7d e8 30 |.....}.8.....}.0|
00000170 00 be 93 7d e8 2a 00 eb fe 47 52 55 42 20 00 47 |...}.*...GRUB .G|
00000180 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 65 |eom.Hard Disk.Re|
00000190 61 64 00 20 45 72 72 6f 72 00 bb 01 00 b4 0e cd |ad. Error.......|
000001a0 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 00 00 00 |..<.u...........|
000001b0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 20 |............... |
000001c0 21 00 83 aa 28 82 00 08 00 00 00 00 20 00 00 aa |!...(....... ...|
000001d0 29 82 83 fe ff ff 00 08 20 00 00 00 35 0c 00 fe |)....... ...5...|
000001e0 ff ff 83 fe ff ff 00 08 55 0c 00 80 a9 03 00 fe |........U.......|
000001f0 ff ff 05 fe ff ff 00 88 fe 0f 00 78 01 09 55 aa |...........x..U.|
00000200
方式二通过grub命令来修复(注意:该方式在依赖文件没有损坏的情况有效)
grub
grub> root (hd#,#)
grub> setup (hd#)
1.输入grub命令,进入交互式命令界面
2.输入root(hd0,0)
3.输入setup(hd0)
[root@centos6 ~]$ 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>
如果**/boot/grub/**下的依赖文件被破坏,则该方式无效
# 依赖文件损坏
[root@centos6 ~]$ mkdir /data/grub
[root@centos6 ~]$ mv /boot/grub/* /data/grub/
[root@centos6 ~]$ mv /data/grub/grub.conf /boot/grub/
[root@centos6 ~]$ ls /boot/grub/
grub.conf
[root@centos6 ~]$ 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,0)
setup (hd0,0)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... no
Error 15t: File not found
实验四 修复grub的第1.5阶段故障
故障准备
[root@centos6 ~]$ dd if=/dev/zero of=/dev/sda bs=512 count=25 seek=1
25+0 records in
25+0 records out
12800 bytes (13 kB) copied, 0.000299646 s, 42.7 MB/s
现象-光标闪烁在黑屏中
处理步骤
通过救援模式,使用grub-install 解决该问题
# 光盘启动,进入rescue 模式
chroot /mnt/sysimage
grub-install /dev/sda
sync
exit
exit
实验五 验证/boot/grub/grub.cong不会自动创建
故障模拟
[root@centos6 ~]$ ls /boot/grub/
device.map grub.conf reiserfs_stage1_5 vstafs_stage1_5
e2fs_stage1_5 iso9660_stage1_5 stage1 xfs_stage1_5
fat_stage1_5 jfs_stage1_5 stage2
ffs_stage1_5 minix_stage1_5 ufs2_stage1_5
[root@centos6 ~]$ mv /boot/grub/grub.conf /data/grub/
[root@centos6 ~]$ rm -rf /boot/grub/
[root@centos6 ~]$ grub-install /dev/sda
Probing devices to guess BIOS drives. This may take a long time.
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'.
(fd0) /dev/fd0
(hd0) /dev/sda
[root@centos6 ~]$ ll /boot/grub/
total 272
-rw-r--r-- 1 root root 30 Sep 1 16:05 device.map
-rw-r--r-- 1 root root 13428 Sep 1 16:05 e2fs_stage1_5
-rw-r--r-- 1 root root 12636 Sep 1 16:05 fat_stage1_5
-rw-r--r-- 1 root root 11780 Sep 1 16:05 ffs_stage1_5
-rw-r--r-- 1 root root 11772 Sep 1 16:05 iso9660_stage1_5
-rw-r--r-- 1 root root 13284 Sep 1 16:05 jfs_stage1_5
-rw-r--r-- 1 root root 11972 Sep 1 16:05 minix_stage1_5
-rw-r--r-- 1 root root 14428 Sep 1 16:05 reiserfs_stage1_5
-rw-r--r-- 1 root root 512 Sep 1 16:05 stage1
-rw-r--r-- 1 root root 126148 Sep 1 16:05 stage2
-rw-r--r-- 1 root root 12040 Sep 1 16:05 ufs2_stage1_5
-rw-r--r-- 1 root root 11380 Sep 1 16:05 vstafs_stage1_5
-rw-r--r-- 1 root root 13980 Sep 1 16:05 xfs_stage1_5
现象 -Error 15
处理步骤
# 光盘启动,进入rescue 模式
chroot /mnt/sysimage
grub-install /dev/sda
vim /boot/grub/grub.conf
cat /boot/grub/grub.conf
default=0
timeout=5
title Centos 6 10
kernel /vmlinuz-2.6.32-642.el6.x86_64 root=/dev/sda2
initrd /initramfs-2.6.32-642.el6.x86_64.img
实验六 单用户模式破解root密码
操作员步骤
-
进入内核加载菜单界面,选择内核后,输入e
-
进入编辑内核参数界面,选中kernel配置,输入e
-
在kernel配置参数后,输入1,s,S,single任意一项(表示进入单用户模)
-
回车,则进入单用户模式(注意:不用输入密码即可登录root账号)
在Centos 6 中最为显著的功能,就是轻松破解root密码
实验七 给grub添加密码
操作步骤
-
通过grub加密口令
grub-md5-crypt # md5 加密方式
grub-crypt # sha216 加密方式
2. 在grub配置文件/boot/grub.conf中添加密码认证
```bash
[root@centos6 ~]$ grub-crypt
Password:
Retype password:
$6$zRpR6NIsEMe6WrUt$NQ3iQUKEx7kosdyxpglunIlgIDN/L7O6PtfAwgmH33hx4nmXI31yJ6U3FKFFL1KlFQZZSsp0RKPC0Q.cf3d581
[root@centos6 ~]$ cat /boot/grub/grub.conf
default=0
timeout=5
password --encrypt # 也使用--md5,配合grub-md5-crypt 加密口令
$6$zRpR6NIsEMe6WrUt$NQ3iQUKEx7kosdyxpglunIlgIDN/L7O6PtfAwgmH33hx4nmXI31yJ6U3FKFFL1KlFQZZSsp0RKPC0Q.cf3d581
title Centos 6 10
kernel /vmlinuz-2.6.32-642.el6.x86_64 root=/dev/sda2
initrd /initramfs-2.6.32-642.el6.x86_64.img
-
重新登录,编辑grub参数等内容前,要求输入口令
-
除了加载,还可以通过输入p来输入grub口令执行下一步
-
grub口令正确后,出现可编辑的命令
当然没有绝对的安全,可通过光盘启动,进入救援模式,强制修改密码文件,破解密码
实验八 修复第二阶段文件故障
故障模拟
[root@centos6 ~]$ rm -rf /boot/grub
[root@centos6 ~]$ reboot
现象
操作步骤
-
进入grub交互式界面,生成kernel和initrd两个配置文件
kernel /vmlinuz-2.6.32-642.el6.x86_64 root=/dev/sda2 # 文件名称可通过双tab 实现 initrd /initramfs-2.6.32-642.el6.x86_64.img # 文件名称可通过双tab 实现
- 配置完成后,输入boot即可
总结
- 在加载过程中,只要没有显示出菜单,说明问题出现在grub阶段。
- grub加载过程中第1和1.5阶段出现的问题,都可通过grub-install 命令处理。
- grub加载过程中,可临时设置内核参数,runlevel;也可通过设置grub口令防止恶意破解root密码。