Linux system analysis Startup Process

Source: Internet
Author: User


Part 1: Kernel boot (kernel boot)

Start the device to boot the Linux system using Boot programs such as LILO or grub. After the boot program successfully completes the boot task, Linux takes control of the CPU from them, then the CPU starts to execute the Linux core image code and starts the Linux Startup Process. Here we use several assembler programs to guide Linux. This step involves the files under "arch/i386/Boot" in the Linux source code tree: bootsect. s, setup. s, video. s.


Bootsect. S is the source code for generating the Boot Sector. After loading, it directly jumps to the program entry of setup. S. The main function of setup. S is to copy system parameters (including memory and disk, which are returned by the BIOS) to the special memory so that the codes in the protected mode of these parameters can be read in the future. In addition, setup. s includes the code in video. s to detect and set the display and display modes. Finally, setup. s converts the system to the protection mode and jumps to 0x100000.


The memory address 0x100000 stores the decompressed kernel. Because the kernel provided by RedHat contains a large number of drivers and functions, the "makebzimage" method is used in kernel compilation, to generate a compressed kernel, in RedHat the kernel is often named vmlinuz (there are also names like initrd- ). During the initial Linux boot process, the headers in "arch/i386/boot/compressed/" are used. s uses Misc. run the decompress_kernel () function defined in C to decompress the kernel vmlinuz to 0x100000.


When the CPU jumps to 0x100000, startup_32 in "arch/i386/kernel/head. s" will be executed. It is also the entrance of vmlinux, and then jumps to start_kernel. Start_kernel () is a function defined in "init/Main. c". start_kernel () calls a series of initialization functions to complete the setting of the kernel itself. If start_kernel () is successfully executed, the basic Linux core environment has been established.


At the end of start_kernel (), by calling the kernelthread () function, the system creates the first core thread Init () and calls cpu_idle (null) to rest. The Core Thread Init () is mainly used for peripheral initialization, including cache zone management initialization (FS/buffer. c), virtual memory management initialization (mm/vmscan. c), file system initialization (FS/filesystems. c) and root file system installation, call do_basic_setup () to complete the initialization of peripherals and their drivers.


When the do_basic_setup () function returns Init (), INIT () opens the/dev/console device again and redirects the three standard input/output files stdin, stdout, and stderr to the console. Finally, search for the INIT program in the file system (or the program specified by the init = command line parameter), and use the execve () System Call to load and execute the INIT program. By the end of the init () function, the INIT process switches from the kernel state to the user State (process number 1), and the kernel boot part ends.


Part 2: run init

The process Number of init is 1, which is the starting point of all processes in the system. After Linux completes the kernel boot, it starts to run the INIT program (the last line of Init ). The INIT program reads and executes the configuration file/etc/inittab.


Part 3: system initialization

There is such a line in the init configuration file:

Si: sysinit:/etc/rc. d/rc. sysinit

It calls/etc/rc. d/RC. sysinit, while RC. sysinit is a bash shell script mainly used to complete system initialization, RC. sysinit is an important script to run at every running level. It mainly includes activating swap partitions, checking disks, loading hardware modules, and other tasks that require priority.


Part 4: Start the running daemon

After RC. sysinit is executed, init will be returned to continue other actions. Normally, the/etc/rc. d/RC program will be executed next. Taking runtime Level 3 as an example, init will execute the following line in the configuration file inittab:

L5: 5: Wait:/etc/rc. d/RC 5


This line indicates running/etc/rc with 5 as the parameter. d/RC,/etc/rc. d/RC is a shell script that accepts number 5 as the parameter and executes/etc/rc. all RC startup scripts in the D/rc5.d/directory. Of course,/etc/rc. in the D/rc5.d/directory, these startup scripts are actually some link files, rather than the real RC startup scripts. The real RC startup scripts are actually stored in/etc/rc. d/init. d/directory.


Part 5: Establish a terminal

After the RC is executed, init is returned. At this time, the basic system environment has been set and various daemon processes have been started. Init will open six terminals (defined in/etc/inittab) to log on to the system. You can switch between the six terminals by pressing Alt + FN (N corresponds to 1-6.


Part 6: log on to the system and start the system

The login program receives and analyzes the user name sent from the terminal program as the user name parameter. If the user name is not root and the/etc/nologin file exists, login outputs the content of the nologin file and then exits. This is usually used to prevent non-Root User Logon during system maintenance.


Only terminals registered in/etc/securetty allow the root user to log on. If this file does not exist, the root user can log on to any terminal. The/etc/usertty file is used to add access restrictions to users. If this file does not exist, there are no other restrictions.


After the username is analyzed, login searches for/etc/passwd and/etc/shadow to verify the password and set other information about the account, such as what is the main directory and what shell is used. If no main directory is specified, the root directory is used by default. If no shell is specified,/bin/bash is used by default.


After the login program is successful, the last logon information (recorded in/var/log/lastlog) is output to the corresponding terminal ), check whether the user has any new emails (in the directory of the corresponding user name of/usr/spool/mail ). Then, set various environment variables: For Bash, the system first looks for the/etc/profile script file and runs it. Then, if the user's main directory contains the. bash_profile file, execute it.


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: 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.