"Reprint" Linux boot process

Source: Internet
Author: User

Transfer from: http://cizixs.com/2015/01/18/linux-boot-process introduction

We all know: The operating system runs the code on the hard disk, eventually to run to the memory and CPU, in order to be used by us.

That from the power button to see the system interface, the operating system is how to occupy all the hardware resources, load themselves into the memory to start running it? You can think of two possibilities: the operating system has its own implementation, or there are other noble help. If the operating system itself started, there is a "chicken eggs, eggs, chickens," the problem; if the latter, there must be a power before the operating system starts to work, the sleep on the hard disk operating system to the working state.

In fact, the start of a Linux system is the second of the above. The program that helps load the Linux kernel (Kernel) into memory is the Boot Loader, which is the arrow that represents the load relationship.

Boot loader--> Linux Kernel

Now all you need to know is Boot Loader the first sector in the Startup disk, the function is to boot the Linux system, as a more detailed explanation will be mentioned later. So the question is, Boot Loader how does it work? And how does the system know which bootable device to use? Sometimes your computer may start with multiple devices: network, hard disk, USB drive, CD, etc. This requires something else to do this, which is the BIOS.

Bios--> Boot Loader

Along the lines above, the question now is: Who started the BIOS? Well, it seems to be an endless process. Fortunately, this is the end of the puzzle. BIOS is embedded in the motherboard of the firmware, the computer when the start of the Convention is to start the BIOS execution.

Finally, summarize the Linux startup process:

    1. By pressing the Power key, the BIOS (Basic input/output System) initiates the work of initializing the hardware, including the screen and keyboard, memory detection, and this process is also the POST (Power on Self Test), followed by CMOS RAM To find the bootable device by setting the boot device lookup order in the Note: The BIOS program is embedded on the motherboard's ROM chip.
    2. At the end of the post process, control of the system is transferred from Biso to boot loader. The Boot loader is typically stored on the system's hard disk (the traditional BIOS/MBR system), or on the EFI partition (the nearest EFI system). At this time the machine cannot acquire external storage or network information, and some important values (date, time, other external values) are read from the CMOS. CMOS devices that can work after a computer loses power, Boot Loader will be explained later.
    3. Boot Loader Select the operating system to start, load the kernel image, and initialize the RAM disk to memory. The kernel of the system starts running until it shuts down.

Bios

When the power button is pressed, some registers of the computer are set to the initial value, and the instruction register CS:IP the first instruction of the BIOS. The BIOS grasps the control to perform the hardware detection program, and the BIOS will look for the bootable device before ending its life. So, how does the BIOS know which equipment rooms can be started? If the computer is going to load the system from a setting that cannot be started, a serious error occurs. The two bytes at the end of the first sector of the boot device must be: 0x55 and 0xAA , the two magic numbers are the key to distinguishing between bootable devices and non-bootable devices. With sudo head -c 512 /dev/sda | hd the contents of the first sector you can see, note the last two characters.

BIOS If the bootable device cannot be found, it will be reported No Bootable Device Error .

Boot Loader

For systems using BIOS/MBR mode, Boot Loader is located on the first sector of the hard disk, also known as the MBR (Master Boot Record). MBR only 512 bytes, the main task is to check the partition table, and find the partition can be started, once the boot partition is found, in the partition to find the back boot loader– such as grub, loading it into memory (RAM).

MBR in this limited byte space, mainly includes three parts of the content:

    1. Bootstrap code: Starting the operating system
    2. Partition table: Indicates the location of the system disk
    3. Magic Number: 0X55AA

For systems using Efi/uefi mode, the UEFI firmware reads the boot Manager's data to determine which UEFI application to start, and where it has been found. The firmware then launches the UEFI application, such as GRUB.

Now the control to the Grub launcher in the hand, grub according to the system you choose (multi-system situation will have the interface to let the user choose, only one system will directly select the system), the system's kernel loaded into memory to start running, but also initialize the RAM disk File System (INITRAMFS) to memory, which is used by the kernel, and gives control over to the kernel.

Linux Kernel

The kernel is generally compressed, so its first task is to decompress, then examine and analyze the system's hardware and initialize the hardware drivers in the kernel. The file system is not yet available when the kernel is loaded into memory, and it uses the Initramfs of the Boot Loader to load the gold memory. After the kernel is loaded into memory, the primary task is to initialize and configure the machine's memory, processor, storage device, etc., and the kernel will start some user-state programs.

Initramfs

As mentioned earlier, the boot loader RAM disk, which is Initramfs, is loaded into memory, so let's talk about it in detail now. INITRAMFS contains programs and binaries that perform a series of actions to ensure that the root file system is mount to the system. The actions here include providing kernel functionality for the required file system, and using the Udev (User Device) tool to discover and load the hard drive driver. When the root file system is found, it checks for errors and then mount to the system.

The Mount program tells the operating system that a file system can be used and loads it into a path on the file system (mount point). If these actions are successful, the INITRAMFS is removed from memory and the INIT program (located at/sbin/init) starts executing.

/sbin/init

Init handles mount (mount) work, which is the hub for the entire file system to run properly. When you need to be aware of the need for hardware drivers when accessing storage devices, you must load them in the INITRAMFS phase.

So far, the kernel program has prepared all the hardware resources and the file system has been mounted. It runs the/sbin/init program, which is the first system process (the previously running program is not OS-level, not the OS's jurisdiction), and its process number (PID) is 1. Here are the results I run on my own system ps aux | grep init , and the second column is the process number:

root         1  0.0  0.0  24320   864 ? Ss Jan15 0:00 /sbin/init

The first program to start up, of course, also bears a more important responsibility: to start up the system needs other programs. In the traditional System V UNIX operating mode, this process is a program script that iterates through the runlevels sequence to start or stop a pre-defined service. In addition to the main task above, Init is responsible for keeping the system running and cleaning up the shutdown system, as well as user login and logout work.

Login and use

As already mentioned, the INIT program is responsible for user login and logout. In the case of server Linux or other text-mode Linux,init, the Getty program is launched to accept user-entered user names and passwords to authenticate users.

In the case of Linux with a graphical interface, the display manager's service is responsible for detecting displays and starting x-server. Display Manager is also responsible for user login on the graphical interface, as well as launching the correct desktop environment.

Resources
    1. EdX on Introduction to Linux boot Chapter
    2. Nanyi Introduction to Computer-initiated articles
    3. Wikipedia on related articles

"Reprint" Linux boot process

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.