在我们学习过程中,往往会遇到以下问题:如果要安装多个操作系统,系统将如何实现多重引导?如果忘记root密码,要如何找回?如果/etc/fstab设置错误,无法顺利挂载根目录,在不重新安装系统的前提下,如何进行恢复?这些都需要了解系统的启动流程。因此学习系统的启动流程对我们解决现实中存在的一些问题有很大的帮助。
通过网上查找到了一个比较好理解的流程图,在这里借用一下:
操作系统的启动流程可以及合成下面的流程:
- 按下电源开机键,进行开机自检
在按下电源键后,服务器主板上面的BIOS芯片会对服务器的CPU、内存、硬盘等信息进行健康检查,在通过检查之后才能继续进行下一步;如果没有通过,那么服务器会返回相应的告警信息。 - 加载MBR引导菜单
MBR(main boot record)位于磁盘的0柱面0磁道的1扇区,总共有512字节。它是由bootloader、dpt、结束标志三部分组成。
bootloader
前446字节,包含有主引导程序等信息。
dpt
分区表的信息,共64字节。因为每个分区表需要2个字符位,也就是16字节,因此一块磁盘最多拥有4块主分区。
结束标志
最后两个字节,它的值是55 00
查看相应的MBR分区表信息如下:
1,先将/dev/sda磁盘的前512字节另存到/data目录下面,名字为mbr.bak:
[18:29:15 root@centos6 ~]#dd if=/dev/sda of=/data/mbr.bak bs=512 count=1
2,通过hexdump对mbr.bak打开进行查看:
[19:44:28 root@centos6 data]#hexdump -C mbr.bak
00000000 eb 48 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.H..............|
00000010 fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..|
00000020 00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u........u|
00000030 f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 03 02 |.........|...t..|
00000040 80 00 00 80 80 0c 05 00 00 08 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 a0 4f 0b 00 00 00 80 20 |.........O..... |
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 1a 06 00 fe |........U.......|
000001f0 ff ff 05 fe ff ff 00 88 6f 12 00 78 90 06 55 aa |........o..x..U.|
00000200
4,加载GRUB菜单;
GRUB菜单里面包含了需要加载的系统内核信息,具体内容可以在系统中的/etc/grub.conf文件中查看。
[19:53:40 root@centos6 ~]#cat /etc/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 #菜单项0表示第一项,如果是多系统可以修改此选项
timeout=5 #在菜单到自动启动系统前的停留时间,单位为sec,可按需分配
splashimage=(hd0,0)/grub/splash.xpm.gz #启动菜单的背景图标
hiddenmenu
title CentOS 6 (2.6.32-754.el6.x86_64) #启动项目的名称,可以修改
root (hd0,0) #root所在的位置
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=UUID=1fe65405-7b2e-412b-b698-187128c88ae2 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-754.el6.x86_64.img #内核的参数配置
5,加载Linux内核系统到内存当中,运行超级进程/sbin/init进程。
在Linux系统中,init进程的进程号永远是1,也就是说,系统中的所有进程的父进程都是init进程。可以使用命令pstree进行查看。
[20:04:53 root@centos6 ~]#pstree -p
init(1)─┬─NetworkManager(1441)─┬─dhclient(1513) #init进程号为1
│ └─{NetworkManager}(1514)
├─abrtd(1887)
├─acpid(1572)
├─atd(1929)
├─auditd(1322)───{auditd}(1323)
├─automount(1684)─┬─{automount}(1685)
│ ├─{automount}(1686)
│ ├─{automount}(1741)
│ └─{automount}(1744)
├─bluetoothd(1755)
├─bonobo-activati(2425)───{bonobo-activat}(2426)
├─certmonger(1945)
├─clock-applet(2524)
├─console-kit-dae(2034)─┬─{console-kit-da}(2035)
│ ├─{console-kit-da}(2036)
│ ├─{console-kit-da}(2037)
│ ├─{console-kit-da}(2038)
│ ├─{console-kit-da}(2039)
6,加载配置文件:
init程序启动之后,会读取/etc/inittab文件,来决定系统的运行级别,Linux系统的运行级别有7级,分别为0-6,通过/etc/inittab查看可知:
[20:07:40 root@centos6 ~]#cat /etc/inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
# 0 - halt (Do NOT set initdefault to this) #模式0.代表关机,默认不要设置为此模式
# 1 - Single user mode #模式1.单用户模式 (此模式相当于救援模式,内核损坏,root忘记密 等可以使用此模式进行恢复 。想当于windows下的安全模式)
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking) #模式2。代表多用户,但是没有网络。
# 3 - Full multiuser mode #模式3.完全多用户模式 (文本命令行界面,一般服务器都是此模式)
# 4 - unused #模式4。保留未使用。
# 5 - X11 #模式5. 图形桌面(系统启动之后会进入到图形化桌面系统中)
# 6 - reboot (Do NOT set initdefault to this) #模式6.重新启动(千万不要把initdefault设置为6 )
#
id:5:initdefault: #系统目前的默认启动模式为5.
7,运行/etc/rc.d/rc.sysinit脚本:
INIT进程首先会执行/etc/rc.d/rc.sysinit这个脚本,这个脚本会在开机的时候运行一次。这里面包含了开机的时候所需要做的一系列操作,例如磁盘挂载,selinux等等。
8,运行/etc/rc.d/rc脚本:
INIT进程接下来会执行/etc/rc.d/rc这个脚本,这个脚本除了一些基本的信息外,最主要的是去执行对应启动级别的启动脚本。
9,启动mingetty进程:
在所有的启动脚本运行完成之后,INIT进程会去启动mingetty这个进程,这个进程就会启动一个终端,也就是我们所熟悉的命令行了,系统启动过程也就此结束!