1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
1、POST加电自检
   按下电源后ROM芯片中的CMOS程序执行并检测CPU、内存等设备是否存在并正常运行,CMOS中的程序叫BIOS,可以设置硬盘接口,网卡声卡开关之类的简单设置。一般PC机主板上有一个纽扣电池,这个电池就是给ROM供电的,可以保证主板断电后BIOS的一些基本设置不会重置。
   下面是一个比较新的主板的BIOS,在老主板上一般只有英文不会有中文界面,老式主板只支持键盘操作不支持鼠标,现在很多新主板已经可以支持鼠标操作了。
2、引导加载次序Boot Sequence
   在系统启动前计算机是不知道你系统在哪里的,需要按照设备启动顺序一次查找引导加载器器bootloader,这个查找次序可以自己在BIOS中设定,一般在boot选项中设置,【注意】不同厂商不同版本的主板设置方法是不同的,请以厂商的官方说明为准。
3、引导加载器bootloader
   前面说了系计算机是不知道系统在哪里的,所以需要程序进行引导,这个引导的程序就叫bootloader,不同操作系统的bootloader是不同的,windows使用的bootloader程序是ntloader,只能对windows进行引导不能对其他系统进行引导,而Linux的bootloader叫GRUB,可以对其他操作系统进行引导(包括windows),正是因为这样,所以在装既有Windows又有Linux的双系统的时候,需要先装Windows,再装Linux。
功能:提供一个菜单,允许用户选择要启动系统或不同的内核版本;把用户选定的内核装载到内存中的特定空间中,解压、展开,并把系统控制权移交给内核。
  bootloader的引导程序GRUB放在MBR中。
4、引导加载器程序GRUB
     1、grub legacy
     前面提到GRUB是bootloader阶段的一个程序,这是Centos中最常用的引导程序,CentOS 6安装的是grub legacy
     下面是CentOS 6的grub legacy
  
按e:进入编辑模式,用于编辑菜单;
  然后按c: 进入命令模式,交互式接口;
常用的命令有
#help: 获取帮助列表
#help KEYWORD: 详细帮助信息
#find (hd#,#)/PATH/TO/SOMEFILE:
#root (hd#,#)设定grub的根设备
#kernel /PATH/TO/KERNEL_FILE: 设定本次启动时用到的内核文件;额外还可以添加许多内核支持使用的cmdline参数;
#例如:init=/path/to/init, selinux=0
#initrd /PATH/TO/INITRAMFS_FILE: 设定为选定的内核提供额外文件的ramdisk;
#boot: 引导启动选定的内核;
CentOS 6中grub的配置文件保存在/boot/grub/grub.conf中
vim 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/mapper/VolGroup-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
         root (hd0,0)
         kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/VolGroup-lv_root rd_NO_LUKS rd_NO_MD rd_LVM_LV=VolGroup/lv_swap crashkernel=auto LANG=zh_CN.UTF-8 rd_LVM_LV=VolGroup/lv_root  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
         initrd /initramfs-2.6.32-431.el6.x86_64.img
   其中常用的配置
default=#: 设定默认启动的菜单项;落单项(title)编号从0开始;
timeout=#:指定菜单项等待选项选择的时长;
splashimage=(hd#,#)/PATH/TO/XPM_PIC_FILE:指明菜单背景图片文件路径;
hiddenmenu:隐藏菜单;
password [--md5] STRING: 菜单编辑认证;
title TITLE:定义菜单项“标题”, 可出现多次;
root (hd#,#):grub查找stage2及kernel文件所在设备分区;为grub的“根”; 
kernel /PATH/TO/VMLINUZ_FILE [PARAMETERS]:启动的内核
initrd /PATH/TO/INITRAMFS_FILE: 内核匹配的ramfs文件;
password --md5 STRING: 启动选定的内核或操作系统时进行认证;
  
   2、GRUB2
  CentOS 7上安装的是gurb2
[root@localhost ~]# rpm -q grub2
grub2-2.02-0.34.el7.centos.x86_64
下面是centos7的GRUB,如果什么都不选,在3秒后系统会进入默认设置的那一项系统。
按e进入GRUB编辑
按ctrl+c进入命令行模式
常用的命令行命令有下面这些:
1. help
     查看命令用法,显示所有可用命令
     help search 
     search 命令用法
2. ls
     列出当前的所有设备。如 (hd0) (hd0,1) (hd0,5) (hd1) (hd1,1) (hd1,2) .......
     ls -l
     详细列出当前的所有设备。对于分区,会显示其label及uuid。
     ls /
     列出当前设为root的分区下的文件
     ls (hd1,1)/
     列出(hd1,1)分区下文件
3. search
     search -f /ntldr
     列出根目录里包含ntldr文件的分区,返回为分区号
     search -l LINUX
     搜索label是LINUX的分区。
     search --set -f /ntldr
     搜索根目录包含ntldr文件的分区并设为root,注意如果多外分区含有ntldr文件,set 失去作用
4. loopback
     loopback命令可用于建立回放设备,如
     loopback lo0 (hd1,1)/abc.iso
     可以使用lo0设备来访问abc.iso里的内容,比如说,可以从abc.iso里的软盘映像中启动
     loopback lo0 (hd1,1)/aa.iso
     linux (lo0)/memdisk
     initrd (lo0)/abc.img
     要删除某一回放设备,可以使用-d参数:
     loopback -d lo0
5. set
     使用set可以设置变量的值
     set root=
     set timeout=
     需要调用变量的值时,使用${AA},如set root=(hd1,1)
     则${root}=(hd1,1)
6. pager
     分页显示。
     set pager=1
     满页时暂停,按space继续
     set pager=0
     取消分页
7. linux
     linux取代grub中的kernel
  CentOS 7的/boot/grub2/grub.conf官方建议是不要随便修改的,一般都是在grub.d文件下生成新的启动目录,再重新生成
默认启动的修改可通过/boot/grub2/grubenv文件修改
[root@localhost grub2]# vim grubenv 
  
# GRUB Environment Block
saved_entry=CentOS Linux (3.10.0-327.el7.x86_64) 7 (Core)
#########################################################
可以进入/etc/grub.d下查看
[root@localhost grub.d]# ls
00_header  01_users  20_linux_xen     30_os-prober  41_custom
00_tuned   10_linux  20_ppc_terminfo  40_custom     README
进入README可以查看grub.d中文件的说明
[root@localhost grub.d]# cat README 
  
All executable files in this directory are processed in shell expansion order.
  
   00_*: Reserved for 00_header.
   10_*: Native boot entries.
   20_*: Third party apps (e.g. memtest86+).
  
The number namespace in-between is configurable by system installer and/or
administrator.  For example, you can add an entry to boot another OS as
01_otheros, 11_otheros, etc, depending on the position you want it to occupy in
the menu; and then adjust the default setting via /etc/default/grub.
#00 _ *:为00 _header保留
#10 _ *:本机引导条目
#20 _ *:第三方应用程序(例如memtest86)
5、进入kernel
     在GRUB中选择对应的kernel进入,然后kernel会对自身进行初始化
     探测可识别到的所有硬件设备。
     加载硬件驱动程序。
     以只读方式挂载根文件系统。
     运行用户空间的第一个应用程序,在CentOS 6上是init,在CentOS 7上是Systemd
     内核组成部分:
         kernel: 内核核心,一般为bzImage,通常在/boot目录下,名称为vmlinuz-VERSION-RELEASE;
         kernel object: 内核对象,一般放置于/lib/modules/VERSION-RELEASE/
6、运行init
     CentOS 6上init所在的位置是在/sbin/init,init共分为7个级别
#0:关机
#1:单用户模式(root, 无须登录), single, 维护模式;
#2: 多用户模式,会启动网络功能,但不会启动NFS;维护模式;
#3:多用户模式,正常模式;文本界面;
#4:预留级别;可同3级别;
#5:多用户模式,正常模式;图形界面;
#6:重启
例如你要关机可直接输入init 0进行关机操作
[root@localhost sbin]# init 0
如果要查看当前系统运行的init级别可使用runlevel或者who -r
[root@localhost sbin]# runlevel 
N 5
[root@localhost sbin]# who -r
          运行级别 5 2016-04-10 18:25
init的配置文件在/etc/inittab
[root@localhost sbin]# vim /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)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)
id:5:initdefault:
#这里定义的默认运行级别为5
  配置文件中的格式是:id:runlevel:action:process
     此配置文件每行定义一个级别
id:指入口标识符,它是一个字符串,对于getty或mingetty等其他login程序项,要求id与tty的编号相同,否则getty程序将不能正常工作。
runlevel:init的系统运行级别
action:是用来定义后面process的运行方式,常用的有下面几种
         wait: 切换至此级别运行一次;
         respawn:此process终止,就重新启动之;
         initdefault:设定默认运行级别;process省略;
         sysinit:设定系统初始化方式,此处一般为指定/etc/rc.d/rc.sysinit
process:为具体的执行程序,
  例如设置为l0:5:wait:/etc/rc.d/rc 5
     /etc/rc.d/rc是一个Shell脚本,它接受5作为参数,去执行/etc /rc.d/rc5.d/目录下的所有的rc启动脚本,/etc/rc.d/rc5.d/目录中的这些启动脚本实际上都是一些链接文件,而不是真正的rc 启动脚本,真正的rc启动脚本实际上都是放在/etc/rc.d/init.d/目录下。而这些rc启动脚本有着类似的用法,它们一般能接受start、stop、restart、status等参数。
     /etc/rc.d/rc5.d/中的rc启动脚本通常是K或S开头的链接文件。
#K开头的文件:运行次序是数字越小,越先运行;数字越小的服务,通常为依赖到别的服务;关闭服务。
#S开头的文件:运行次序是数字越小,越先运行;数字越小的服务,通常为被依赖到的服务;启动服务。
[root@localhost rc5.d]# pwd
/etc/rc.d/rc5.d
7、运行系统初始化脚本
     系统初始化脚本对应的文件是/etc/rc.d/rc.sysinit
     大概就是这个样子,因为太长,省略了很多
     
     
     
     
8、启动系统服务
     centos6下的系统服务脚本都放在/etc/rc.d/init.d或者/etc/init.d中,可以看到两个文件中的内容是一样的,因为/etc/init.d链接到了/etc/rc.d/init.d中
通过chkconfig命令可以查看什么系统级别下开启了哪些服务,以及添加服务到系统中并在指定级别下启动
     查看chkconfig --list
添加chkconfig --add 服务名,在添加的时候需要将对应的服务放入/etc/init.d中,并在服务脚本中加入chkconfig: LLLL nn nn
     写到LLLL的init级别就是S的,没写得就是K的,第一个nn代表S的数字,第二个nn代表K的数字
[root@localhost init.d]# chkconfig --add httpd
删除chkconfig --del 服务名
[root@localhost init.d]# chkconfig --del httpd
【注意】:正常级别下,最后启动一个服务S99local没有链接至/etc/rc.d/init.d一个服务脚本,而是指向了/etc/rc.d/rc.local脚本;因此,不便或不需写为服务脚本放置于/etc/rc.d/init.d/目录,且又想开机时自动运行的命令,可直接放置于/etc/rc.d/rc.local文件中。
9、打印登录提示符
     系统在启动完服务后会打印登录提示符,然后输入账号密码,进入对应的命令行界面或图形界面。
10、总结
     系统的启动流程总的来说分成以下几个步骤:
     1、加电自检
     2、按照BIOS中设置的Boot Sequence查找有bootloader程序的设备
     3、加载MBR中的bootloader程序GRUB
     4、进入GRUB设置的默认kernel
     5、kernel初始化
     6、运行init程序
     7、运行系统初始化脚本
     8、启动对应服务
     9、打印登录提示符