CentOS7 Grub引导故障恢复全过程
作者:ICT董老师
文章介绍了当CentOS7的/boot/grub2/grub.cfg文件丢失时的恢复步骤,包括手动引导进入系统、永久修复GRUB配置以及使用救援模式进行修复
当CentOS 7的/boot/grub2/grub.cfg文件丢失,重启后进入grub>模式时,可以按照以下步骤恢复系统。
第一步:手动引导进入系统
1. 查找系统分区
grub> ls
你会看到类似输出:(hd0) (hd0,msdos1) (hd0,msdos2)等
2. 找到/boot分区
逐个检查分区,寻找包含内核文件的分区:
grub> ls (hd0,1)/ grub> ls (hd0,2)/
当你看到包含以下文件的分区时,就找到了/boot分区:
- vmlinuz-3.10.0-xxx.el7.x86_64
- initramfs-3.10.0-xxx.el7.x86_64.img
- grub2/目录
记下这个分区位置,比如(hd0,1)
3. 设置根设备并加载内核
# 设置根设备为/boot分区 grub> set root=(hd0,1) # 加载Linux内核 grub> linux /vmlinuz-3.10.0-1160.el7.x86_64 ro root=/dev/mapper/centos-root # 加载initramfs grub> initrd /initramfs-3.10.0-1160.el7.x86_64.img # 启动系统 grub> boot
重要提示:
- 如果根分区不是LVM,可能是:root=/dev/sda2
- 如果不确定根分区,可以尝试:root=/dev/sda2 或 root=/dev/sda3
- 内核版本号用Tab键自动补全
第二步:永久修复GRUB配置
成功进入系统后,执行以下命令:
1. 重新挂载根分区为读写
# mount -o remount,rw /
2. 重新生成GRUB配置文件
grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-1160.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-1160.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-xxxx Found initrd image: /boot/initramfs-0-rescue-xxxx.img done
3. 可选:重新安装GRUB到磁盘(如果必要)
# grub2-install /dev/sda
4. 验证修复
# ls -la /boot/grub2/grub.cfg -rw-r--r--. 1 root root 6538 Dec 1 10:30 /boot/grub2/grub.cfg # cat /boot/grub2/grub.cfg | grep menuentry
备选方案:使用救援模式
如果手动引导失败,可以使用CentOS 7安装盘进入救援模式:
1. 启动救援模式
- 用CentOS 7安装U盘或光盘启动
- 选择"Troubleshooting" → “Rescue a CentOS system”
- 选择"1"继续
2. 挂载并修复系统
# 检查磁盘分区 # fdisk -l # 挂载根分区 # mkdir /mnt/sysimage # mount /dev/mapper/centos-root /mnt/sysimage # 挂载boot分区(如果是单独分区) # mount /dev/sda1 /mnt/sysimage/boot # 挂载必要的文件系统 # mount --bind /dev /mnt/sysimage/dev # mount --bind /proc /mnt/sysimage/proc # mount --bind /sys /mnt/sysimage/sys # 切换到原系统环境 # chroot /mnt/sysimage
3. 在chroot环境中修复
# 重新生成GRUB配置 # grub2-mkconfig -o /boot/grub2/grub.cfg # 重新安装GRUB # grub2-install /dev/sda # 退出chroot并重启 # exit # reboot
预防措施
修复完成后,建议:
# 备份GRUB配置 # cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg.backup # 定期检查系统完整性 # rpm -Va | grep grub2
总结
关键步骤:
- 手动引导:在grub>提示符下设置root、加载内核和initrd
- 永久修复:进入系统后执行grub2-mkconfig -o /boot/grub2/grub.cfg
- 备选方案:使用安装盘的救援模式进行修复
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
