Centos6系统启动流程
POST --> Boot Sequence(BIOS) --> Boot Loader --> Kernel(ramdisk) --> rootfs --> switchroot --> /sbin/init -->(/etc/inittab, /etc/init/*.conf) --> 设定默认运行级别 --> 系统初始化脚本rc.sysinit (/etc/rc.d/rc.sysinit)–> 关闭或启动对应级别的服务(/etc/rc.d/rc*.d/) --> 启动终端
一 硬件启动流程
1.1 BIOS
1.1.1POST 初始化硬件设备,检查系统外围主要设备(CPU、内存、IO)
1.1.2确定启动设备
根据BIOS设置的启动顺序,检查驱动器(硬盘、光盘、U盘、网络)
如果硬盘是启动项,读取硬盘第一个扇区(MBR、512bit)到内存
控制区转给MBR中的bootloader
1.2 MBR
MBR由512字节组成,前446字节为bootloader,中间64字节为分区表,最后2字节55aa为分区标记
执行MBR中的bootloader
MBR由主引导程序、硬盘分区表、有效标记组成
MBR里面安装有/boot/grub/的一部分,在安装时写入
根据grub能够加载内核及文件驱动
[root@centos6 ~]#cat /boot/grub/grub.conf
default=0 #当有多个title时可以选择默认进入内核default=1
timeout=5 #开机间隔5秒载入内核
splashimage=(hd0,0)/grub/splash.xpm.gz #背景 640*480 (hd0,0)表示第一个硬盘第一个分区
hiddenmenu
title CentOS 6 (2.6.32-754.el6.x86_64) #命名,启动菜单显示,可以有多个
root (hd0,0)
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=32b998eb-6eb0-41e3-86a3-d3ceaefdae2d rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=128M KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
#Kernel 内核参数配置 rugb 背景 quiet 不输出明细
initrd /initramfs-2.6.32-754.el6.x86_64.img
#vmlinuz initramfs在/boot中
实验:64字节分区表丢失(备份修复)
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446
[root@localhost ~]#fdisk -l /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ba3f3
Device Boot Start End Blocks Id System
[root@localhost ~]#dd if=/data/part.txt of=/dev/sda bs=1 count=64 skip=446 seek=446
实验:分区标记55aa丢失(备份修复)
[root@localhost ~]#dd if=/dev/sda of=/data/part.txt bs=1 count=512
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=2 seek=510
[root@localhost ~]#fdisk -l /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@localhost ~]#dd if=/data/part.txt of=/dev/sda bs=1 count=2 skip=510 seek=510
2+0 records in
2+0 records out
2 bytes (2 B) copied, 0.000120493 s, 16.6 kB/s
[root@localhost ~]#fdisk -l /dev/sda
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000ba3f3
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 211814399 104857600 83 Linux
/dev/sda3 211814400 216008703 2097152 82 Linux swap / Solaris
/dev/sda4 216008704 419430399 101710848 5 Extended
/dev/sda5 216010752 320868351 52428800 83 Linux
实验:前446字节bootloader丢失(备份修复)
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=446
[root@localhost ~]#dd if=/data/part.txt of=/dev/sda bs=1 count=512
以上实验可以通过光盘进入拯救系统拷贝文件修复
二 GRUB启动引导阶段
2.1 Stage1阶段
stage1是安装时写入MBR中的
因为MBR空间有限,因此,MBR中安装bootloader的最小程序
主要作用是系统启动装在stage2
实验:stage1阶段bootloader破坏
[root@localhost ~]#dd if=/dev/zero of=/dev/sda bs=1 count=446
[root@localhost ~]#reboot
开机进入光盘启动(硬盘启动不能识别)
一、/boot/grub/存在
二、/boot/grub/删除
chroot /mnt/sysimage
grub-install /dev/sda
由于/boot/grub/grub.conf没有,不能进入系统,开机后手工输入(临时)以下内容,进入系统后修改文件
编写/boot/grub/grub.conf
[root@CentOS6 ~]# cat /boot/grub/grub.conf
default=0
timeout=5
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=UUID=54cfd3b3-28e0-43ce-813a-2957c26d429d
initrd /initramfs-2.6.32-754.el6.x86_64.img
2.2 Stage1.5阶段
stage1.5是在MBR后面的分区,
stage1.5能够识别文件系统,
stage1.5是stage1和2的桥梁;
grub访问/boot/grub/stage2,将stage2载入内存并执行
实验:stage1.5阶段MBR后面部分破坏
[root@CentOS6 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=10240 seek=512
/boot文件并没有破坏,只需写入
grub-install /dev/sda
2.3 Stage2阶段
解析grub的配置文件/boot/grub/grub.conf,
显示操作系统启动菜单,
加载内核镜像到内存,
通过/boot/initramfs-2.6.32-754.el6.x86_64.img加载虚拟文件系统,
转交给内核
stage2阶段/boot/grub/grub.conf破坏
实验:破解用户口令
在启动阶段按ESC进入以下界面
a:内核参数
e: 编辑模式,用于编辑菜单
c: 命令模式,交互式接口
输入a后输入1(s,S,single)进入单用户模式(维护模式),不要密码直接进入系统,可以修改root口令
实验:增加密码进入单用户模式
由于单用户模式可以直接进入系统,因此系统变得不安全,在此之前添加口令进入维护模式,可以增加系统安全
[root@CentOS6 ~]# cat /boot/grub/grub.conf
default=0
timeout=5
password mage #增加明码
title test linux
kernel /vmlinuz-2.6.32-754.el6.x86_64 root=UUID=54cfd3b3-28e0-43ce-813a-2957c26d429d
initrd /initramfs-2.6.32-754.el6.x