The Linux boot process

The Linux boot process

1, the boot BIOS self-test -> check the CPU, hard disk and other hardware information

2, MBR boot] [Major Boot Record -> [primary partition boot read the first 446 bytes 0 track 0 of the cylindrical sector 1]

                                                                      -> OK [server startup mode is usually the hard disk, sometimes there are CD_ROM start]

3, GRUB GRand Unified Bootloader [boot] -> OK to load a GRUB system [GUN project is a multi-operating system startup program]

4, load the kernel kernal Information -> [cat / proc / version or kernel version uname -a to see information]

5, run the INIT process -> Linux process first started [ps -ef | grep / sbin / init]

6, read cat / etc / inittab -> determine the level Linux boot loader and self-starting device

                                                                      -> init process execution (information setting the host name, network, etc.) /etc/rc.d/rc.sysinit

7, boot the kernel module, perform different levels of scripts -> The self-loading start level since the launch of the program at that level /etc/rc.d/rc3.d/*

8, execution /etc/rc.d/rc.local - initialization information> recording system since the start of the non-system software, commands, environment variables, etc.

9, execute / bin / login login program, start mingetty (terminal login process), enter the system login screen

image

 

Graphic explanation

0, boot meaning

boot boots mean "start" and the boots have to do with it? The original, boot here is an abbreviation bootstrap (lace), and it comes from a proverb:

 “pull oneself up by one’s bootstraps”

Literally means "pulling the laces to pull himself up," which of course is impossible. The first time, engineers use it to describe, the computer starts is a very contradictory process: You must run the program, and then to start the computer, but the computer does not start will not be able to run the program!

Early that is the case, we must try every means, the short program loaded into memory, and then the computer to run properly. So engineers have put this process is called "pull the laces," the passage of time referred to as the boot.

Start the computer the whole process is divided into four stages.

A first phase: BIOS

In the early 1970s, "read only memory" (read-only memory, abbreviated as ROM) to the invention, the boot ROM program is brushed into the chip, the computer powered on, the first thing read it.

How the computer is started?

1.1 hardware self-test

The main program stored in the BIOS comprising: a self-diagnostic program (identified by reading the contents of the CMOS RAM hardware configuration, and subjected to self-test and initialization), CMOS setup program (the boot process is initiated by a special hot key is set after stored in the CMOS RAM), system automatic loader (after a successful self-test system, the boot program on disk 0 0 opposing sector into memory to run) and the main I / O drivers and interrupt service (BIOS and deal directly with the hardware, you need to load I / O driver).

First check the BIOS program, the basic conditions for running the computer hardware can meet, this is called "Self-test" (Power-On Self-Test), abbreviated as POST.

If a hardware problem, the motherboard beeps different meanings, start aborted. If not, the screen will show information about CPU, memory, hard disk and so on.

How the computer is started?

1.2 boot sequence

硬件自检完成后,BIOS把控制权转交给下一阶段的启动程序。

这时,BIOS需要知道,”下一阶段的启动程序”具体存放在哪一个设备。也就是说,BIOS需要有一个外部储存设备的排序,排在前面的设备就是优先转交控制权的设备。这种排序叫做”启动顺序”(Boot Sequence)。

打开BIOS的操作界面,里面有一项就是”设定启动顺序”。

How the computer is started?

二、第二阶段:主引导记录

BIOS按照”启动顺序”,把控制权转交给排在第一位的储存设备。即根据用户指定的引导顺序从软盘、硬盘或是可移动设备中读取启动设备的MBR,并放入指定的位置(0x7c000)内存中。

这时,计算机读取该设备的第一个扇区,也就是读取最前面的512个字节。如果这512个字节的最后两个字节是0x55和0xAA,表明这个设备可以用于启动;如果不是,表明设备不能用于启动,控制权于是被转交给”启动顺序”中的下一个设备。

这最前面的512个字节,就叫做”主引导记录”(Master boot record,缩写为MBR)。

2.1 主引导记录的结构

“主引导记录”只有512个字节,放不了太多东西。它的主要作用是,告诉计算机到硬盘的哪一个位置去找操作系统

主引导记录由三个部分组成

(1) 第1-446字节:调用操作系统的机器码。

(2) 第447-510字节:分区表(Partition table)。

(3) 第511-512字节:主引导记录签名(0x55和0xAA)。

其中,第二部分”分区表”的作用,是将硬盘分成若干个区。

2.2 分区表

硬盘分区有很多好处。考虑到每个区可以安装不同的操作系统,”主引导记录”因此必须知道将控制权转交给哪个区。

分区表的长度只有64个字节,里面又分成四项,每项16个字节。所以,一个硬盘最多只能分四个一级分区,又叫做”主分区”。

每个主分区的16个字节,由6个部分组成:

(1) 第1个字节:如果为0x80,就表示该主分区是激活分区,控制权要转交给这个分区。四个主分区里面只能有一个是激活的。

(2) 第2-4个字节:主分区第一个扇区的物理位置(柱面、磁头、扇区号等等)。

(3) 第5个字节:主分区类型。

(4) 第6-8个字节:主分区最后一个扇区的物理位置。

(5) 第9-12字节:该主分区第一个扇区的逻辑地址。

(6) 第13-16字节:主分区的扇区总数。

最后的四个字节(”主分区的扇区总数”),决定了这个主分区的长度。也就是说,一个主分区的扇区总数最多不超过2的32次方。

如果每个扇区为512个字节,就意味着单个分区最大不超过2TB。再考虑到扇区的逻辑地址也是32位,所以单个硬盘可利用的空间最大也不超过2TB。如果想使用更大的硬盘,只有2个方法:一是提高每个扇区的字节数,二是增加扇区总数。

三、第三阶段:硬盘启动

这时,计算机的控制权就要转交给硬盘的某个分区了,这里又分成三种情况。

3.1 情况A:卷引导记录

上一节提到,四个主分区里面,只有一个是激活的。计算机会读取激活分区的第一个扇区,叫做”卷引导记录”(Volume boot record,缩写为VBR)。

“卷引导记录”的主要作用是,告诉计算机,操作系统在这个分区里的位置。然后,计算机就会加载操作系统了。

3.2 情况B:扩展分区和逻辑分区

随着硬盘越来越大,四个主分区已经不够了,需要更多的分区。但是,分区表只有四项,因此规定有且仅有一个区可以被定义成”扩展分区”(Extended partition)。

所谓”扩展分区”,就是指这个区里面又分成多个区。这种分区里面的分区,就叫做”逻辑分区”(logical partition)。

计算机先读取扩展分区的第一个扇区,叫做”扩展引导记录”(Extended boot record,缩写为EBR)。它里面也包含一张64字节的分区表,但是最多只有两项(也就是两个逻辑分区)。

计算机接着读取第二个逻辑分区的第一个扇区,再从里面的分区表中找到第三个逻辑分区的位置,以此类推,直到某个逻辑分区的分区表只包含它自身为止(即只有一个分区项)。因此,扩展分区可以包含无数个逻辑分区。

但是,似乎很少通过这种方式启动操作系统。如果操作系统确实安装在扩展分区,一般采用下一种方式启动。

3.3 情况C:启动管理器

在这种情况下,计算机读取”主引导记录”前面446字节的机器码之后,不再把控制权转交给某一个分区,而是运行事先安装的”启动管理器”(boot loader),由用户选择启动哪一个操作系统。

Linux环境中,目前最流行的启动管理器是Grub。

How the computer is started?

四、第四阶段:操作系统

控制权转交给操作系统后,操作系统的内核首先被载入内存。

以Linux系统为例,先载入/boot目录下面的kernel。内核加载成功后,第一个运行的程序是/sbin/init。它根据配置文件(Debian系统是/etc/initab)产生init进程。这是Linux启动后的第一个进程,pid进程编号为1,其他进程都是它的后代。

然后,init线程加载系统的各个模块,比如窗口程序和网络程序,直至执行/bin/login程序,跳出登录界面,等待用户输入用户名和密码。

至此,全部启动过程完成。

常用的命令展示         

cat /etc/inittab

Linux共有7【0-6】个自启动级别,默认是3级

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
# 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 :3:initdefault:

Other documents show:

1
[root@localhost ~] # cd /etc/rc.d/

image

1
[root@localhost rc3.d] # tail /etc/rc.local

image

Guess you like

Origin www.cnblogs.com/canflyfish/p/11489753.html