Computer/linux startup process

Source: Internet
Author: User
Tags dmesg

The boot process refers to the entire process from powering on the computer until Linux displays the user login screen.

Analyzing the Linux boot process is also a great way to get an insight into the workings of the Linux core.

The computer boot process is a very complex process and it is not easy to really understand it.

Computer startup is a very contradictory process: you must run the program before the computer can start, but the computer does not start to run the program!

In the early days, it was true that all sorts of things had to be done to put a small piece of the program into memory before the computer could run properly.

First understand what the BIOS is?

The BIOS is the abbreviation for the English "basic Input Output system", and the Chinese name is the "basic inputs and outputs" when translated. In fact, it's a set of programs that solidify onto a ROM chip on the motherboard of a computer

The BIOS system files are present on the ROM chip, but the BIOS setup information is stored in RAM and the power loss is lost. This RAM usually has a rectangular photo of about one centimeter long on the edge of the motherboard, which can be drawn poorly.

1, the BIOS self-test, according to the hard drive boot sequence, enters the operating system the hard disk

After the computer is powered on, first read the BIOS from the motherboard.

BIOS program first check that the computer hardware can meet the basic conditions of operation, this is called "power-on self-Test" (power-on self-test), abbreviated to post.
If there is a problem with the hardware, the motherboard will beep with a different meaning and start the abort. If there is no problem, the screen will display the CPU, memory, hard disk and other information.

After the self-test is complete, the BIOS transfers control to the next stage of the startup process.
At this point, the BIOS needs to know, "the next stage of the startup program," where is the specific storage? In the end is the optical drive, ordinary hard disk, solid-state drive, u disk inside which kind?
Open the BIOS operating interface, there is a "set the boot sequence."

2. Read MBR (master boot record), find operating system

After the first hard drive is found, the computer begins to read the MBR master boot record on the hard disk. He will tell the computer where to go to the hard drive to find the operating system

MBR refers to the first sector of the No. 0 track on a hard disk, which is 512 bytes in size and consists of three parts:
(1) 第1-446 byte: Boot (pre-boot) area, call the operating system's machine code. Locate the partition marked Active (active) and read the boot area of the active partition into memory.
(2) 第447-510 bytes: Partitioned Tables (Partition table). Recording partition information for a hard disk
(3) 第511-512 bytes: Master boot Record signature (0x55 and 0xAA). The last two bytes are 0x55 and 0xAA, indicating that the device can be used for booting, or, if not, that the device cannot be used for boot, and that control is then forwarded to the next device in the boot sequence.

Partition Table :
Hard disk partitioning has many benefits. Given that each zone can have a different operating system installed, the master boot record must therefore know which zone to transfer control to.
The partition table is only 64 bytes long, and it is divided into four items, each 16 bytes. Therefore, a hard disk can only be divided into four primary partitions, also known as the "main partition."
16 bytes per primary partition, consisting of 6 parts:
(1) 1th byte: If 0x80, it means that the primary partition is an active partition and control is transferred to this partition. Only one of the four primary partitions is active.
(2) 第2-4个 bytes: The physical location of the first sector of the primary partition (cylinder, head, sector area code, and so on).
(3) 5th byte: Primary partition type.
(4) 第6-8个 bytes: The physical location of the last sector of the primary partition.
(5) 第9-12 bytes: The logical address of the first sector of the primary partition.
(6) 第13-16 Bytes: The total number of sectors in the primary partition.
The last four bytes (the total number of sectors in the primary partition) determine the length of the primary partition. That is, the total number of sectors in a primary partition is not more than 2 32.
If each sector is 512 bytes, it means that the maximum number of individual partitions is not more than 2TB. Considering that the logical address of the sector is also 32 bits, the maximum space available for a single hard drive is no more than 2TB. If you want to use a larger hard disk, there are only 2 methods: one is to increase the number of bytes per sector, and the second is to increase the total number of sectors.

3. Load the Boot Manager bootloader and choose which operating system to enter

When the system locates the MBR of the hard disk specified by the BIOS, it is copied to the physical memory where the 0X7C00 address resides. In fact, the content that is copied to physical memory is boot manager boot Loader, and specific to different operating systems, take Linux, common is grub.

At this point, the control of the computer will be transferred to a partition of the hard disk, which is divided into three kinds of situations.
3.1 Situation A: Volume boot record
As mentioned in the previous section, only one of the four primary partitions is active. The computer reads the first sector of the active partition, called the volume Boot Record, which is abbreviated to VBR, Volume.
The primary function of the "Volume Boot Record" is to tell the computer where the operating system is located in this partition. The computer will then load the operating system.
3.2 Scenario B: Extended partitions and logical partitions
With the hard drive getting bigger, four primary partitions are not enough and require more partitions. However, there are only four partition tables, so there is only one area that can be defined as an "extended Partition" (Extended partition).
The so-called "extended partition" means that the area is divided into multiple zones. The partitions inside this partition are called "Logical Partitions" (logical partition).
The computer reads the first sector of the extended partition, called the "Extended Boot Record" (Extended Boot recording, abbreviated to EBR). It also contains a 64-byte partition table, but there are at most two items (that is, two logical partitions).
The computer then reads the first sector of the second logical partition, finds the location of the third logical partition from the partition table inside, and so on, until the partition table of a logical partition contains only its own (that is, only one partition entry). Therefore, an extended partition can contain countless logical partitions.
However, it seems that the operating system is rarely started this way. If the operating system is indeed installed on an extended partition, it is typically started the next way.
3.3 Scenario C: Boot Manager
In this case, after the computer reads the 446-byte machine code in front of the master boot record, it no longer transfers control to a partition, but instead runs the pre-installed Boot Manager (boot loader), which is the user's choice to start the operating system.
In the Linux environment, the most popular boot manager currently is grub.

4. Load the kernel

After entering the operating system, the kernel is loaded into memory first.
Take the Linux system as an example and load the kernel under the/boot directory first.

After the kernel is loaded successfully, the system reads the memory image and does the decompression according to the path of the kernel image set by grub. At this point, the screen will generally output "uncompressing Linux" prompt. When the decompression core is complete, the screen output "OK, booting the kernel".

The system places the extracted kernel in memory and calls the Start_kernel () function to start a series of initialization functions and initialize the various devices to complete the Linux core environment. At this point, the Linux kernel has been established, Linux-based programs should be able to run properly.

 

5, user layer init according to the Inittab file to set the operating level

After the kernel is loaded, the first program to run is/sbin/init, which reads the/etc/inittab file and initializes it based on the file.

In fact, the main function of the/etc/inittab file is to set the Linux operating level, which indicates that Linux needs to run on level 3.

cat /etc/ID:3: Initdefault:

6. Init Process Execution Rc.sysinit

After setting the operating level, the Linux system executes the first user layer file is the/etc/rc.d/rc.sysinit script, it does a lot of work, including setting path, setting the network configuration (/etc/sysconfig/network), Start swap partitions, set/proc, and so on. If you are interested, you can check the Rc.sysinit file in/etc/rc.d.

The final completion state of the thread init is to enable the normal user program to be executed normally, so as to really complete the system environment for the application to run. Its main operations are:

· Set up network environment/etc/sysconfig/network, such as hostname, Gateway, Ip,dns and so on.

· Mount the/proc. This file is a special file with a size of 0 because it is in memory. It is best not to delete the inside.

· Based on the/proc/sys/kernel/modprobe result of the kernel at boot time. Start detection of peripheral devices.

· Load user-defined module/etc/sysconfig/modules/*. Modules

· Read/etc/sysctl. The conf file sets the kernel.

· Set the time, terminal font, hard disk LVM or RAID function to perform disk detection with FSCK.

· Logs the boot status to/VAR/LOG/DMESG. (You can view the results with the command DMESG.)

At this point, the init () function ends and the boot portion of the Linux kernel ends.

7. Start the kernel module

The kernel modules are loaded according to the files in the/etc/modules.conf file or the/ETC/MODULES.D directory.

8. Execute scripts with different runlevel

Depending on the runlevel, different service launches are available. To the/ETC/RC.D directory, different level will have different directories. Where the start of S (start) indicates that the boot starts, K (kill) begins to indicate that the boot does not start. Numbers indicate the boot order. The smaller the number, the sooner it starts.

9, the implementation of/etc/rc.d/rc.local

If you open this file, there is a word, read it, you will be the role of this command at a glance:

# This script is executed *after* all and the other init scripts.

# can put your own initialization stuff in here if you don ' t

# want to does the full Sys V style init stuff.

Rc.local is the place where Linux is left to the user to personalize after all initialization work. You can put the things you want to set up and start up here.

10. Execute/bin/login program, enter login status

At this time, the system has entered the waiting for the user input username and password, you can already use your own account login system.

 

 

1. After power-on, read the BIOS program from the motherboard and locate the boot hard drive.

2. read MBR master boot record from hard disk (first sector of hard disk)

3. Read boot loader boot manager (GRUB) from MBR master boot record

4, load the kernel, execute the first program/sbin/init

5. Initialization of the operating environment.

6. Start the system service

  

 

Reference

http://blog.csdn.net/bailyzheng/article/details/7485105

http://blog.csdn.net/bailyzheng/article/details/7485105

Http://www.ruanyifeng.com/blog/2013/02/booting.html

Computer/linux startup process

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.