Analyze the Linux Startup Process

Source: Internet
Author: User
Article Title: analyzes the Linux Startup Process. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Taking RedHat9.0 and i386 as examples, this article analyzes the Linux Startup Process from user power on to command line prompt on the screen. It also introduces various files involved in startup.

Reading the Linux source code is undoubtedly the best way to learn more about Linux. In this article, we also try to further analyze the Linux Startup Process from the source code perspective, so it also involves some of the relevant Linux source code, the source code for Linux Startup mainly uses the C language and involves a small amount of compilation. A large number of scripts written by shell (mainly bash shell) are also executed during the startup process. To facilitate reading, I will introduce the entire Linux Startup Process in the following parts one by one. For details, refer:

When the user powers on the PC, BIOS boot self-check, start according to the boot device set in BIOS (usually hard disk), then start the boot program lilo or grub installed on the device to boot Linux, linux first performs kernel boot, and then executes the init program. The init program calls rc. sysinit and rc programs, rc. after completing system initialization and running service tasks, sysinit and rc return init; init starts mingetty and opens the terminal for users to log on to the system. After the user logs on to the system, the user enters Shell, this completes the entire startup process from boot to login.

The following describes several key parts one by one:

Part 1: Kernel boot (kernel boot)

Red Hat9.0 can use boot programs such as lilo or grub to start guiding the Linux system. 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 is generic to 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.

So what code is stored in the memory address 0x100000? Where did these codes come from?

The memory address 0x100000 stores the decompressed kernel, because the kernel provided by Red Hat contains a large number of drivers and functions, therefore, the "makebzImage" method is used in kernel compilation to generate a compressed kernel. in RedHat, the kernel is often named vmlinuz. During the initial Linux boot process, is passed through the head in "arch/i386/boot/compressed. 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. The start_kernel () function does a lot of work to build a basic Linux core environment. If start_kernel () is successfully executed, the basic Linux core environment has been established.

At the end of start_kernel (), by calling the init () function, the system creates the first core thread and starts the init process. The Core Thread init () is mainly used to initialize peripherals, including calling do_basic_setup () to load and initialize peripherals and their drivers. Complete file system initialization and root file system installation.

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. The init () function ends, and the kernel boot part ends,

Part 2: run init

The init process number is 1. From this point, we can see that the init process is the starting point of all processes in the system. After Linux completes the kernel boot, it starts to run the init program ,. The init program needs to read the configuration file/etc/inittab. Inittab is an unexecutable text file consisting of several lines of commands. In the Redhat system, the content of the inittab is as follows (comments starting with "###" are added to the author ):

#

# Inittab This file describes how the INIT process shocould set up

# The system in a certain run-level.

#

# Author: Miquel van Smoorenburg,

# Modified for RHS Linux by Marc Ewing and Donnie Barnes

#

# Default runlevel. The runlevels used by RHS 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 havenetworking)

#3-Full multiuser mode

#4-unused

#5-X11

#6-reboot (Do NOT set initdefault to this)

#

### Indicates that the current default running level is 5 (initdefault );

Id: 5: initdefault:

### Automatically execute the/etc/rc. d/rc. sysinit script (sysinit) at startup)

# System initialization.

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

L0: 0: wait:/etc/rc. d/rc 0

L1: 1: wait:/etc/rc. d/rc 1

L2: 2: wait:/etc/rc. d/rc 2

L3: 3: wait:/etc/rc. d/rc 3

L4: 4: wait:/etc/rc. d/rc 4

[1] [2] [3] Next page

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.