How the Linux system works when booting up

Source: Internet
Author: User
Tags dmesg

How Linux systems work at boot time is also a good way to get an insight into the workings of Linux's core system.

Start the first step-load the BIOS

When you turn on the computer, the computer loads the BIOS information first, and the BIOS information is so important that the computer must find it at the very beginning. This is because the BIOS contains information about the CPU, device boot sequence information, hard disk information, memory information, clock information, PNP features, and so on. After that, the computer has a spectrum and knows which hardware device to read. After the BIOS has given control of the system to the first sector of the hard disk, Linux has started to control the system.

Start the second step-read MBR

The first sector of the No. 0 track on the hard disk is called the MBR, which is the master boot record, which is the main boot recording, it is 512 bytes in size, but it contains pre-boot information and partition table information. Can be divided into two parts: the first part is the boot (pre-boot) area, accounting for 446 bytes, the second part is divided into partitioned table (PARTITION pable), a total of 66 bytes, recording the partition information of the hard disk. One of the functions of the Preboot zone is to find the partition marked Active (active) and read the boot area of the active partition into memory.

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. Actually the content that is copied to the physical memory is boot Loader, and specifically to your computer, that is LILO or grub.

Start the third step--boot Loader

Boot Loader is a small program that runs before the operating system kernel runs. Through this small program, we can initialize the hardware device, set up a map of the memory space, so as to bring the system's hardware and software environment to a suitable state, in order to finally call the operating system kernel ready to do everything. Typically, Bootl Oade: is implemented heavily on hardware, with different boot Loader for systems with different architectures.

Linux boot sector content is written in assembly language program, its source code in Arch/i386/boot (different system CPU has its own boot directory), there are 4 program files:

Bootsect. S, the main program of the boot sector, the compiled code does not exceed 512 bytes, that is, the size of a sector

Setup. S, Boot Assist program

Edd. S, part of the auxiliary program, to support the BIOS enhanced disk device service

Video. S, another part of the auxiliary program for the screen display at boot time

There are several Boot loader, where grub, Lilo, and spfdisk are common loader, which is explained in grub as an example.

The system reads the GRUB configuration information in memory (typically menu.lst or grub.lst) and launches a different operating system according to this configuration information.

Start step fourth-load the kernel

Based on the path of the kernel image set by grub, the system reads the memory image and does the decompression operation. 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.

Start_kenrel () is defined in INIT/MAIN.C, which is similar to the main () function in a generic executable program, and the system has done only a few initialization operations that allow the kernel program to perform minimally, and the real kernel initialization process starts here. The function start_kerenl () will invoke a series of initialization functions to complete various aspects of the kernel itself, in order to eventually build a basic and complete Linux core environment.

The following operations are primarily performed in Start_kernel ():

(1) Print out the current kernel version information on the screen.

(2) Execute Setup_arch () to set the system structure.

(3) Execute Sched_init () to initialize the scheduling mechanism of the system. Initialize the Runqueque on each available CPU first, and then initialize the No. 0 process (whose task struct and system empty m stack is already allocated in startup_32 ()) as the system Idle process, which is the process that occupies the CPU when the system is idle.

(4) Execute Parse_early_param () and Parsees_args () to parse the system startup parameters.

(5) Execute trap_in ItQ, first set up the system interrupt vector table. Trap Gate number 0-19 is used for CPU exception handling, then initializes the system call vector, and finally calls Cpu_init () to complete the initialization of the CPU to support the process scheduling mechanism, including setting the flag Register, Task register, initialization program debug related registers and so on.

(6) Executing rcu_init (), initializing the read-copy update mutex mechanism in the system.

(7) Execute the INIT_IRQ () function, initialize the interrupt for the peripheral, and complete the final initialization process for IDT.

(8) Execute init_timers (), Softirq_init () and Time_init () functions, respectively, the timer mechanism of the initial system, the soft interrupt mechanism, and the system date and time.

(9) Execute the Mem_init () function, initialize the page data structure descriptor of the physical memory pages, and complete the creation of the physical memory management mechanism.

(10) Execute Kmem_cache_init (), complete the initialization of the universal slab buffer management mechanism.

(11) Execute Fork_init () to calculate the number of processes (threads) that the physical memory capacity of the current system can allow to create.

(12) Executive Proc_caches_init (), Bufer_init (), Unnamed_dev_init (), Vfs_caches_init (), Signals_init () Such functions establish a dedicated slab buffer queue for various management mechanisms.

(13) perform proc_root_init () WL number, initialize the virtual file system/proc.

At the end of Start_kenrel (), the kernel creates the first system kernel thread (that is, process 1th) through Kenrel_thread (), which executes the init () function in the kernel and is responsible for the next phase of the startup task. The last call to the Cpues_idle () function: Entering the main loop of the system will always execute the instruction in the Default_idle () function, the halt instruction of the CPU, until other processes in the ready queue need to be dispatched to perform other functions. At this point, the only process in the system that is in a ready state is the init process (kernel thread) created by Kerne_hread (), so the kernel does not enter the Default_idle () function, but instead moves to the init () function to continue the boot process.

Start the fifth Step-user layer Init sets the operating level according to the Inittab file

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 is set in the form of ": Id:5:initdefault:", which indicates that Linux needs to run on level 5. Linux runs at the following levels:

0: Turn off the machine

1: Single-user mode

2: Multi-user mode with no network support

3: Multi-user mode with network support

4: reserved, not used

5: Multi-user mode with network support with X-window support

6: Reboot the system, that is, restart

Start the sixth step--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:

(1) Executes the function Do_basic_setup (), which initializes the external device completely.

(2) Build the system's virtual file system directory tree, hook up the system as the root directory of the device (its specific file system has been registered in the previous step).

(3) Turn on the device/dev/console and copy two times through the connection of the function sys_dup (), so that the file number 0,1, 2 all point to the console. These three file connections are commonly referred to as "standard input" stdin, "standard output" stdout and "standard error message" StdErr These three standard I/O channels.

(4) After all the above is ready, the system begins to enter the initialization stage of the user layer. The kernel uses the system call EXECVE () to load the corresponding user-level initialization program of the T-Sub, and then tries to load the program "/SBIN/INITL", "/etc/init", "/bin/init", and "/bin/sh" in turn. As long as one of the program loads succeeds, the system starts the initialization of the user layer without going back to the init () function segment. At this point, the init () function ends and the boot portion of the Linux kernel ends.

Start the seventh step--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.

Start step eighth-execute scripts with different runlevel

Depending on the runlevel, the system will run the appropriate script from RC0.D to RC6.D to perform the appropriate initialization and start the appropriate service.

Start Nineth Step--Execute/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.

Start Tenth Step--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 starting the power supply, the first step of the host is to query the BIOS (full name: basic input/output system input and output systems) information. Understand the hardware status of the whole system, such as CPU, memory, video card, network card, etc. Well, this step windows calculates and it's a. equals

2: Next, the host reads the boot loader in the MBR (the first sector of the hard disk). This is the point oh, it is said that troubleshooting in the exam this, to a broken loader, called you to Amend. Windows does not support the Linux partition format. So, with the boot of Windows. INI is not a system that does not find Linux. In general, I installed the system to install Windows and then install Linux, and then use grub to do boot loader. Two words: Worry! Because Linux is not as stingy as windows. GRUB is supported in Windows partition format.

3: After a step, the host read the boot loader, will read the information inside, know who and who is staying where, if the host wants to enter the Linux system, read to the Linux core is in the/boot file directory, the core is loaded into memory. Started the next analysis of the start-up journey.

4:ok, who is the first running program? is the/sbin/init program. Do not believe, with the top program to see, is not the PID 1 is this east, it, but the ancestors of all things, I short it is nu wa Empress (do not like Adam).

· 5:init first looks for the boot level (run-level). Because the boot level is different, its running scripts (that is, services) are different. The default level has the following:

0-halt (System direct shutdown)

1-single user mode (single-use for system maintenance)

2-multi-user, without NFS (similar to 3 mode, but with less NFS service)

3-full Multi-User mode (full mode, however, is text mode)

4-unused (System hold function)

5-x11 (similar to 3 mode, however, is X terminal display)

6-reboot (re-boot)

(Do not choose 0 or 4, 6 otherwise, progressive system)

· 6:ok. After the system knows its boot level, the next step is not to start the service, but to set up the host running environment first. The file read is/ETC/RC. D/RC. Sysinit file. What kind of environment do you want to set up?

· 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.)

· 7:ok, Next, is to start the system service, different run-level will have different service startup. To/ETC/RC. In the D directory, different level will have different directories. If you start 3 mode, there will be a RC3. D directory, where the service is stored. 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.

Note that they are all connected to the ETC/RC. D/init. The relevant files in the D/directory. So, to start a service manually, you can use the/ETC/RC. D/init. D/a service start "Oh. Instead, we can also put a service ln (a link command) into a directory of different run-level. Remember to hit the s or K + numbers.

· 8: The main opportunity reads the/ETC/RC after the service is read. D/RC. Local file. So, if you need something to boot up, you can write a script or command to come in here. It doesn't have to be as troublesome as that. Later removal is also convenient.

OK, after a long trek, the system can finally open the shell with peace of mind.

How the Linux system works when booting up

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.