Analyze the background process of Linux Startup

Source: Internet
Author: User
Tags syslog

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.

Read LinuxSource codeIs undoubtedly the best way to learn more about Linux. In this article, we will also tryCodeLinux
The startup process also involves some of the relevant Linux source code. The Linux Startup source code 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 device installed on the boot deviceProgramLilo or GRUB start booting
Linux and Linux first perform kernel boot, and then execute the INIT program. The INIT program calls RC. sysinit and RC programs, RC. sysinit and RC
After the system initialization and running of the service are completed, return Init; init after mingetty is started, the terminal is opened for the user to log on to the system, and the user logs on to the shell.
It completes the entire startup process from boot to login.

The following describes several key parts one by one:

Part 1: Kernel boot (kernel boot)

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 Linux source code tree.
These files under "arch/i386/Boot": bootsect. S, setup. s, and 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 set
Including memory, disks, etc., returned by the BIOS) is copied to the special memory so that the code in the protected mode of these parameters can be read in the future. In addition, setup. s also 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?

0x100000 memory address stores the decompressed kernel, because red
The kernel provided by 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, it is used by head. s in "arch/i386/boot/compressed /".
The decompress_kernel () function defined in Misc. c 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 to vmlinux, however
Then jump to start_kernel. Start_kernel () is a function defined in "init/Main. c", start_kernel

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
Some peripheral initialization work, including calling do_basic_setup () to load and initialize the 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
Three standard input and output files, stdin, stdout, and stderr, go to the console, and finally, search for the INIT program in the file system (or the program specified by the init = command line parameter)
And 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. INIT program needs
Read the configuration file/etc/inittab. Inittab is an unexecutable text file consisting of several lines of commands. In the RedHat system, the inittab content is as follows:
As shown in the following figure (comments starting with "###" are added to the author ):


# Inittab

This file describes how

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




(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

### When the running level is 5,

Run the/etc/rc. d/RC Script with the parameter 5,

Init will wait for its return (wait)

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

L6: 6: Wait:/etc/rc. d/RC 6

### Allow

CTRL-ALT-DELETE reboot system


CA: ctrlaltdel:/sbin/shutdown-T3-R now

# When our ups tells us power has failed,

Assume we have a few minutes

# Of power left. Schedule a shutdown

For 2 minutes from now.

# This does, of course, assume you have

Powerd installed and your

# Ups connected and working correctly.

PF: powerfail:/sbin/shutdown-f-h + 2

"Power failure; system shutting down"

# If power was restored before

Shutdown kicked in, cancel it.

PR: 12345: powerokwait:/sbin/Shutdown

-C "power restored; shutdown cancelled"

### Run the command with ttyx as the parameter at Level 2, 3, 4, and 5

/Sbin/mingetty program,

Open the ttyx terminal for user logon,

### If the process exits, run the mingetty Program (respawn) again)

# Run Gettys in standard runlevels

1: 2345: respawn:/sbin/mingetty tty1

2: 2345: respawn:/sbin/mingetty tty2

3: 2345: respawn:/sbin/mingetty tty3

4: 2345: respawn:/sbin/mingetty tty4

5: 2345: respawn:/sbin/mingetty tty5

6: 2345: respawn:/sbin/mingetty tty6

### Run the xdm program at level 5,

Provides an xdm graphical logon interface,

And re-Execute (respawn) When exiting)

# Run xdm in runlevel 5

X: 5: respawn:/etc/X11/preofdm-nodaemon

The preceding inittab file is used as an example to describe the inittab format. The row starting with # Is a comment row. Each row has the following format except the comment row:

ID: runlevel: Action: Process

The detailed explanations of the above items are as follows:

1. ID

Id refers to the entry identifier, which is a string. For other login program items such as Getty or mingetty, the ID must be the same as the TTY number; otherwise, the Getty program will not work properly.

2. runlevel

Runlevel is the identifier of the running level of init. It is generally 0-6 and S or S. The 0, 1, and 6 running levels are retained by the system, where 0 acts as the shutdown action, and 1 acts as
Restart to single-user mode. 6 indicates restart. S and S indicate single-user mode, and the inittab file is not required. Therefore, it does not appear in inittab.

In fact, when you enter the single-user mode, init runs/sbin/sulogin directly on the console (/dev/console. In general system implementation, 2, 3,
4. 5 levels. In the RedHat system, 2 indicates the multi-user mode not supported by NFS, 3 indicates the full multi-user mode (also the most common level), and 4 indicates the user-defined mode, 5 indicates xdm
Graphical logon mode.

The 7-9 level can also be used. Traditional UNIX systems do not define these levels. Runlevel can be multiple parallel values to match multiple running levels. For most actions, runlevel is executed only when it matches the current running level.

3. Action

Action describes the running mode of the subsequent process. Optional values of action include initdefault, sysinit, boot, and bootwait:

Initdefault is a special action value used to identify the default startup level. When init is activated by the core, it reads
Initdefault, which gets the runlevel and serves as the current running level. If there is no inittab file or there is no initdefault item,
Init will request to enter runlevel on the console.

Actions such as sysinit, boot, and bootwait will run unconditionally when the system is started, ignoring runlevel. Other actions (excluding
Initdefault) is related to a runlevel. The definitions of each action are described in detail in the man manual of inittab.

4. Process

Process is the specific execution program. The program can be followed by parameters.

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 scripts are mainly used to initialize the system. Rc. sysinit is an important script that must be run first at every running level. It mainly performs the following tasks: Activation of swap points
Check disks, load hardware modules, and perform other tasks first.

RC. sysinit has more than 850 lines, but each single function is relatively simple and annotated. It is recommended that interested users read the file on their own machines to understand the initial state of the system.
Details. This file is long, so it is not listed in this article, and will not be described in detail. After the RC. sysinit program is executed, the system returns init to continue the next step.

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. d/RC with 5 as the parameter./etc/rc. d/RC is a shell script, which accepts 5 as the parameter and executes
All the RC startup scripts in the/etc/rc. d/rc5.d/directory, and these startup scripts in the/etc/rc. d/rc5.d/directory are actually some link files, not real
The real rc startup script is put in the/etc/rc. d/init. d/directory. These RC boot scripts have similar usage and are generally acceptable.
Start, stop, restart, status, and other parameters.

The rc startup script in/etc/rc. d/rc5.d/is usually a link file starting with K or S. For a STARTUP script starting with S, it will run with the start parameter. However
The corresponding script also has a K-header link and is in the running state (marked by a file under/var/lock/subsys ), first, stop
And then re-run the daemon. This ensures that all related daemon will be restarted when init changes the running level.

You can use chkconfig or "system services" in setup to set which daemon will run at each running level. Common daemon processes include:

AMD: automatically installs the NFS daemon.

Apmd: Advanced Power Management daemon

Arpwatch: records logs and constructs an ethernet address and IP address pair database that is visible on the LAN interface.

Autofs: automatically installs the management process automount, which is related to NFS and relies on NIS.

Crond: the daemon of scheduled tasks in Linux

Named: DNS Server

Netfs: Install NFS, Samba, and Netware Network File Systems

Network: Activate the script program with configured network interfaces

NFS: Enable the NFS service

Portmap: RPC Portmap manager, which manages RPC-based connections

Sendmail: Sendmail

SMB: Samba file sharing/printing service

Syslog: a script that enables Syslog and klogd system logs to wait for processes during system boot.

XFS: X Window server, which provides a font set for local and remote X Servers

Xinetd: supports core daemon processes of multiple network services and manages services such as wuftp, sshd, and telnet.

These daemon processes are also started, the RC program is executed, and the init is returned to continue the next step.

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 then open six terminals so that users can log on to the system. You can switch between the six terminals by pressing Alt + FN (N corresponds to 1-6. The following six lines in the inittab define six terminals:

1: 2345: respawn:/sbin/mingetty tty1

2: 2345: respawn:/sbin/mingetty tty2

3: 2345: respawn:/sbin/mingetty tty3

4: 2345: respawn:/sbin/mingetty tty4

5: 2345: respawn:/sbin/mingetty tty5

6: 2345: respawn:/sbin/mingetty tty6

From the above, we can see that the mingetty program will be run in the respawn mode in the 2, 3, 4, 4, and 5 running levels. The mingetty program can open the terminal and set the mode. At the same time, it will display
Shows a text logon interface, which is a logon interface we often see. In this logon interface, a user is prompted to enter the user name, and the user input will be passed as a parameter to the login program for verification.
User identity.

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

For graphical users with a running level of 5, their logon is through a graphical logon interface. After successful logon, you can directly go to the KDE, gnome, and other Window managers. This article focuses on text-based Logon:

When we see the mingetty logon interface, we can enter the user name and password to log on to the system.

In Linux, the Account Verification Program is login. login receives the username sent from mingetty as the username parameter. Login then analyzes the User name: if the user name is not
Is 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, and
Check whether the user has a new email (in the directory of the corresponding user name of/usr/spool/mail ). Then start to set various environment variables: For Bash, the system first looks
Run the/etc/profile script file and run it. If the. bash_profile file exists in the user's home directory, run it.

Other configuration files may be called in these files. After all the configuration files are executed, various environment variables are also set. A familiar command line prompt will appear, the entire startup process ends.

We hope that the analysis of the Linux Startup Process will help those who want to learn more about the Linux Startup Process, and further study how Linux will work next.

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