linux启动流程grub管理及故障排错


grub介绍

grub: GRand Unified Bootloader
grub 0.97: grub legacy
grub 2.x: grub2

grub legacy:
stage1: mbr  硬盘的前446个字节为第一个阶段mbr,中间64个字节为分区表信息,最后两个字节为55AA标记位
stage1_5: mbr之后的扇区,让stage1中的bootloader能识别stage2所在的分区上的文件系统
stage2:磁盘分区(/boot/grub/)

  

grub.conf详解

[root@zhou grub]# cat 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   #默认从哪个title启动,一个title代表一个内核
timeout=5  #启动菜单界面停留的超时时长
splashimage=(hd0,0)/grub/splash.xpm.gz   #xpm.gz是背景图片 
hiddenmenu
title CentOS 6 (2.6.32-696.el6.x86_64)
	root (hd0,0)    #hd0,0表示第一块硬盘的第一个分区
	kernel /vmlinuz-2.6.32-696.el6.x86_64 ro root=UUID=11efc218-6676-4741-94c2-d8aaab0abd1a 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
#  /vmlinuz-2.6.32-696.el6.x86_64  前面的/表白/boot
#ro root 表示操作系统的根;
#rhgb表示图像,quiet表示安静,启动的时候不显示内核文件启动的内容
	initrd /initramfs-2.6.32-696.el6.x86_64.img 

 centos6启动时需第二次挂载根,在系统启动后需执行第一个启动脚本/etc/rc.d/rc.sysinit,下图可以看到一些挂着的信息:

[root@zhou grub]# cat /etc/rc.d/rc.sysinit |grep mount
if [ ! -e /proc/mounts ]; then
	mount -n -t proc /proc /proc
	mount -n -t sysfs /sys /sys >/dev/null 2>&1
	modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb
	mount -n -t usbfs /proc/bus/usb /proc/bus/usb
#remount /dev/shm to set attributes from fstab #669700
mount -n -o remount /dev/shm >/dev/null 2>&1
#remount /proc to set attributes from fstab #984003
mount -n -o remount /proc >/dev/null 2>&1
if [ -n "$SELINUX_STATE" -a -x /sbin/restorecon ] && __fgrep " /dev " /proc/mounts >/dev/null 2>&1 ; then
    echo $"Unmounting file systems"
    umount -a
    mount -n -o remount,ro /
mount -n /dev/pts >/dev/null 2>&1
	mount_empty() {
			mount -n --bind "$RW_MOUNT$1" "$1"
	mount_dirs() {
			mount -n --bind "$RW_MOUNT$1" "$1"
	mount_files() {
			mount -n --bind "$RW_MOUNT$1" "$1"
	# Common mount options for scratch space regardless of
	mountopts=
	rw_mount_dev=$(blkid -t LABEL="$RW_LABEL" -l -o device)
	# First try to mount scratch storage from /etc/fstab, then any
	# to wipe the scratch storage clean.  If both fail, then mount
	if mount $mountopts "$RW_MOUNT" > /dev/null 2>&1 ; then
	elif [ x$rw_mount_dev != x ] && mount $rw_mount_dev $mountopts "$RW_MOUNT" > /dev/null 2>&1; then
		mount -n -t tmpfs $RW_OPTIONS $mountopts none "$RW_MOUNT"
					mount_empty $path
					mount_files $path
					mount_dirs $path
	# place where they can place minimal amounts of persistent
	# create the bind mounts.  However, until that's all ready this
	# First try to mount persistent data from /etc/fstab, then any
	state_mount_dev=$(blkid -t LABEL="$STATE_LABEL" -l -o device)
	if mount $mountopts $STATE_OPTIONS "$STATE_MOUNT" > /dev/null 2>&1 ; then
	elif [ x$state_mount_dev != x ] && mount $state_mount_dev $mountopts "$STATE_MOUNT" > /dev/null 2>&1;  then
		mount -t nfs $CLIENTSTATE/$HOSTNAME $STATE_MOUNT -o rw,nolock
		mount_state() {
				mount -n --bind "$STATE_MOUNT$1" "$1"
				mount -n --bind "$STATE_MOUNT/$file" "$file"
				mount_state "$path"
				mount_state "$path"
        if mount | grep -q /var/lib/nfs/rpc_pipefs ; then
                mount -t rpc_pipefs sunrpc /var/lib/nfs/rpc_pipefs && service rpcidmapd restart
		echo $"Unmounting file systems"
		umount -a
		mount -n -o remount,ro /
		echo $"Unmounting file systems"
		umount -a
		mount -n -o remount,ro /
remount_needed() {
  state=$(LC_ALL=C awk '/ \/ / && ($3 !~ /rootfs/) { print $4 }' /proc/mounts)
# Remount the root filesystem read-write.
update_boot_stage RCmountfs
if remount_needed ; then
  action $"Remounting root filesystem in read-write mode: " mount -n -o remount,rw /
# If relabeling, relabel mount points.
	# Enter mounted filesystems into /etc/mtab
	mount -f /
	mount -f /proc >/dev/null 2>&1
	mount -f /sys >/dev/null 2>&1
	mount -f /dev/pts >/dev/null 2>&1
	mount -f /dev/shm >/dev/null 2>&1
	mount -f /proc/bus/usb >/dev/null 2>&1
# mounted). Contrary to standard usage,
# filesystems are NOT unmounted in single user mode.
# The 'no' applies to all listed filesystem types. See mount(8).
	action $"Mounting local filesystems: " mount -a -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2,glusterfs -O no_netdev
	action $"Mounting local filesystems: " mount -a -n -t nonfs,nfs4,smbfs,ncpfs,cifs,gfs,gfs2,glusterfs -O no_netdev
/bin/mount -t binfmt_misc none /proc/sys/fs/binfmt_misc > /dev/null 2>&1

 实验一:构建第一阶段错误,使用dd命令清除mbr

 

 

 

mbr清0之后查看,如下图:

 

 

mbr清零后,reboot的状态界面,如下图:

 

 

实验一:grub第一个阶段破坏,修复之:

安装grub:
方法一: grub-install
安装grub stage1和stage1_5到/dev/DISK磁盘上,并复制GRUB相关文件
到 DIR/boot目录下
grub-install --root-directory=DIR /dev/DISK  #DIR代表操作系统的根 /dev/DISK是boot所在的硬盘
方法二: grub
grub> root (hd#,#) 
grub> setup (hd#)

  

通过光盘进入救援模式

 

 进入救援模式后,发现mbr确实被破坏了,如下图:

 

 使用第二种方法恢复:

grub> root (hd0,0) 
grub> setup (hd0)

 

 执行修复命令quit退出shell,reboot就可以正常进入系统里,至此grub第一阶段修复完成。

 注意:第二种方法需要/boot/grub目录下的相关文件都需要存在,否则恢复不成功。

 

实验二:将/boot/grub下的文件移除,修复之:

 

清除后,使用第一种方法,显然报错,如下图

 

 使用第二种方法修复,就能成功,如下图:

 

 

 

 

实验三:破坏grub的1.5阶段,使用dd命令跳过前512个字节,清楚后面20个扇区,如何恢复。

 

 发现第1.5阶段确实清零了,如下图:

 

 破坏1.5阶段后reboot,发现阶段的启动功能还是有,可以进入grub界面,只是加载不了文件系统驱动导致起不来,出现下面状态界面:

 

 

恢复之:使用方法二恢复

 

 

恢复完grub1.5阶段后,因为/boot/grub目录下没有grub.conf文件,需要执行下面命令指定内核文件和initramfs文件。

 

 

上图只是对本次启动有效,进入系统后需要手动写grub.conf文件,或者从别处拷贝一个到/boot/grub目录下,下图是我自己手工写的grub.conf文件:

 

 

 实验四:自定义启动菜单背景图:

 

 使用concert工具将自己的图片转换成xpm格式:

 

 

将转换后的图片压缩,并移动到/boot/grub 目录下。

 

 reboot就可以看到启动菜单自己定义的背景图片。

 

 实验五:清空/boot目录下所有文件,如何修复:

 

 

清空/boot目录后,重启界面如下图:

 

 

 修复,救援模式进行恢复,如下图:

 

 

恢复grub第1和1.5阶段以后还确实grub.conf文件,下面我自己手写。如下图

 

 恢复grub后,发现/boot目录下只有grub目录,还缺少内核文件和initramfs文件,需要执行下面命令生成,也可以直接使用rpm从光盘安装,下面我使用mkinitrd命令生成。如下图:

 

 

 

 实验五:操作系统的根基于lvm的,grub被破坏,并且/etc/fstab文件也被删除,如何修复。

下图可以看到目前/ 是以lvm的方式挂载的,并且内核文件制定的操作系统的根路径也是lvm。

 

 

 下图不光破坏/boot,这时候/etc/fstab文件也破坏了

 

 

 

 重启进入救援模式,发现挂载关系也找不着了,没有任何的分区:

 

 

 进入shell,查看挂载关系,发下硬盘没有任何挂载关系,使用blkid查看,没有任何lvm文件系统的挂载关系。

 

 使用lvdisply查看,发现LV Status 是not available

 

 

 

 

 

 

 使用vgdisply,找到VG名字,然后激活VG

 

 

使用vgchange -ay VolGroup 激活VG卷组

 

 

 

 激活卷组后发现lv的状态是活跃的:

 

 

 使用blkid命令,发现文件系统都激活了

 

 

 

修复/etc/fstab文件

 

 

 修复/boot

 

 

 

 手写grub.conf文件:

 

 reboot,就可以正常进入系统,至此修复完毕。

 

 

 上面都是关于centos6的启动修复,下面是centos7的grub修复:

实验一:删除/boot/grub目录,如何修复:

 

 重启后状态界面如下图:

 

 进入救援模式,恢复grub

 

 

 

 实验二:删除/boot

 

 

进入救援模式:

 

 

 

 

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM