Analysis of Linux System Launch Process (1)

xiaoxiao2021-03-06  59

This paper uses the RedHat9.0 and I386 platforms as an example, which analyzes the entire Linux boot process of the power supply from the user until the screen appears command line prompt. And introduces the various files involved in the startup.

Read Linux source code is undoubtedly the best way to learn Linux in-depth. In the introduction of the Linux boot process, we also try to analyze Linux's startup process from the perspective of the source code, so it is also simple to refer to some-related Linux source code, Linux launched the source code of this part main use. It is C language and also involves a small amount of compilation. A large number of shells and scripts are also performed during the startup process. In order to facilitate the reader to read, the author divides the entire Linux startup process into the following sections one by one, you can refer to the figure below:

When the user turns on the PC, the BIOS boot self-test, press the boot device (usually hard disk) set in the BIOS (usually the hard disk) to start the boot program LILO or GRUB installed on the startup device LILO or GRUB begins to boot Linux, Linux first, the kernel boot, next Execute the init program, the init program calls Rc.Sysinit and RC and other programs, Rc.sinit and RC returns to init; init; init; init; INIT launches MINGETTY, open the terminal for user login system, user After the login is successful, I entered the Shell so that the entire startup process from the boot to the login is completed.

-> Power on -> BIOS -> LILO / GRUB -> Kernerl Boot -> Init (rc.sinit, rc) -> MINGETTY (LOGIN) - -> shell ---->

Here, you will introduce several key parts one by one:

Part 1: Guidance of the kernel (nuclear guidance)

Red Hat9.0 can start booting the Linux system using the boot programs such as LILO or GRUB. After the boot program successfully completed the boot task, Linux takes over the CPU control from them, and then the CPU will start executing Linux core image code, start The Linux startup process. Here, several assemblers are used to boot Linux, which is more than these files under "Arch / I386 / boot" in the Linux source tree: bootsect.s, setup.s, video.s, etc..

Where bootsect.s is the assembly source code generated by the boot sector, which is directly jumped to the program entry of the setup.s after the loading action. The main feature of Setup.s is to copy system parameters (including memory, disk, etc.) to special memory in the special memory in order to be read in the code of these parameters. In addition, Setup.s also contains code in Video.s, detects and sets the display and display mode. Finally, setup.s converts the system to the protection mode and jumps to 0x100000.

So what code is stored in this memory address in the 0x100000? What is these code coming from?

0x100000 This memory address is stored by the decompressed kernel, because the root provided by the Red Hat contains a large number of drivers and functions, so use "makebzimage" mode in kernel compilation, generating compressed kernels, in Redhat The kernel is often named VMLinuz. During the initial boot process of Linux, it is to extract the kernel VMLinuz to the Dec.c's defined Dec.s in the first boot process of Linux. 0x100000.

When the CPU jumps to 0x100000, Startup_32 in "Arch / I386 / Kernel / Head.s" will be executed, and it is also the entrance of VMLinux, and then jumps to start_kernel (). Start_kernel () is a function in "init / main.c", and a series of initialization functions are called in Start_kernel () to complete the set of kernel itself. In the start_kernel () function, a large number of work is made to establish a basic Linux core environment. If start_kernel () is successfully implemented, 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 working for some peripheral initialization, including calling DO_BASIC_SETUP () to complete the peripheral and its driver load and initialization. And complete the file system initialization and the installation of the root file system.

When the do_basic_setup () function returns init (), init () opens the / dev / console device, redirects three standard input / output files stdin, stdout, and stderr to the console, finally, search the init program in the file system ( Or the program specified by the init = command line parameter) and loads the init program using the Execve () system call. At this INIT () function end, the kernel's boot section is over,

Part II: Running init

The process number of the init is 1. From this point, the init process is the starting point of all system all-system, and Linux will start running the init program after completing the nuclear boot. The init program needs to read the configuration file / etc / inittab. INITTAB is an unhaffably text file that has several row instructions. In the Redhat system, the content of the inittab is as follows (in "###", the inventory is increasing to the author):

# # Inittab This file describes how the INIT process should 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 range is 5 (Id: 5: InitDefault):

### Automatically execute the /etc/rc.d/rc.sysinit script (sysinit). Si :: sysinit: /etc/rc.d/rc.sinitl0: 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 run level is 5, run /tc/rc.d/rc script with 5 for the parameter, init will wait for its return (Wait) L5: 5 : Wait: /etc/rc.d/rc 5 L6: 6: Wait: /etc/rc.d/rc 6

### In the startup process, allow Press Ctrl-Alt-delete to restart the system # trap ctrl-alt-delete 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"

# I i i k k in,, i i i i

### In the 2, 3, 4, 5th, TTYX is executed with TTYX for the parameter, open the TTYX terminal for user login, ### If the process exits, run the mingetty program (Respawn) # Run GetTys in Standard Runlevels 1: 2345: Respawn: / sbin / mingetty TTY1 2: 2345: Respaw: / 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, provide the XDM graphical way to log in, and re-execute (Respawn) while exiting # Run XDM in Runlevel 5 x: 5: Respawn: / etc / x11 / prefdm -nodaemon

Take the inittab file above as an example to clear the format of INITTAB. Among them, the rows starting is a notes, except for the comment line, each line has the following format:

ID: Runlevel: Action: Process

The detailed explanation of the above is as follows:

Id

ID refers to the entry identifier, which is a string, for other Login program items such as getty or mingetty, require the ID to be the same as TTY, otherwise the Getty program will not work properly.

2. Runlevel

Runlevel is an identifier of the run level of init, usually 0-6 and S or S. 0, 1, 6 running levels are reserved by the system: where 0 is used as a shutdown action, 1 is used to restart to a single user mode, 6 is the same as the meaning, indicating the single user mode, and does not need the inittab file, so it is not in inittab When you appear, in fact, when entering a single user mode, init is running / Sbin / Sulogin on the console (/ dev / console). In the general system implementation, 2, 3, 4, 5 levels are used. In the redhat system, 2 indicates a multi-user mode supported by NFS, 3 indicates full multi-user mode (also the most common level), 4 Reserved to the user custom, 5 means the XDM graphic login method. The 7-9 level is also available, and the traditional UNIX system does not define these levels. Runlevel can be a plurality of values ​​in parallel to match multiple run levels, for most Actions, only when RunLvel is successful when RunLvel is successful. 3. Action

Action is a way to describe its subsequent process. The Action is available includes: initDefault, sysinit, boot, bootwait, etc .:

INitDefault is a special Action value for identifying the default start level; after init is activated by the core, it will read the initTDefault item in the initTab, and obtain the Runlevel and as the current run level. If there is no inittab file, or there is no initDefault item, init will request input Runlevel on the console.

Sysinit, boot, bootwait, etc. Action will run unconditionally when the system is started, and ignore the RunLvel.

The rest of the Action (excluding INITDEFAULT) is related to a RunLvel. The definition of each Action has a detailed description in the inittab's Manbook.

4. Process

Process is a specific execution program. The program can be used later.

Part III: System Initialization

There is such a line in the INIT's configuration file:

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

It calls executive /etc/rc.d/rc.system, and rc.sinit is a Bash Shell script, which is mainly working in some system initialization, rc.sinit is an important thing to run every run level. script. It works mainly to work with: activate the exchange partition, check the disk, load the hardware module, and some other need to perform tasks.

Rc.sinit has more than 850 rows, but each single function is still relatively simple, and with comments, it is recommended to read this file on your own machine to understand the system initialization. Since this file is longer, it is not listed in this article, and does not do a specific introduction.

When the rc.sysinit program is executed, the next step will be returned to INIT.

Part 4: Start the daemon of the corresponding operational level

After the rc.sysinit is executed, the init will return to other actions, usually the /etc/rc.d/rc program will be executed next. Take Run Level 3 as an example, init will perform the following lines in the configuration file inTtab:

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

This line indicates that the parameter runs /etc/rc.d/rc/tc/rc.d/rc ia/etc/rc.d/rc is a shell script, which accepts 5 as a parameter, execute /etc/rc.d/rc5.d/ directory All RC startup scripts, / etc / rc.d / rc5.d / These boot scripts are actually some link files, not true RC startup scripts, the real RC startup script is actually Placed in the /etc/rc.d/init.d/ directory. These RC startup scripts have similar usage, which can generally accept parameters such as Start, STOP, Restart, Status. The RC start script in /etc/rc.d/rc5.d/ is typically a link file at the beginning of K or S, which will run in a start parameter for startup scripts starting with S. If there is a link to the K header, it is already in operation status (with the file as a flag under / var / lock / subs ", it will first stop these launched guards as the parameters of STOP. Process, then run again. This is to ensure that all relevant daemon will restart when init changes the run level.

As for which daemons will be run in each run level, the user can set it from the "System Services" in chkconfig or setup. Common daemons are:

AMD: Automatically install NFS daemon APMD: Advanced power management daemon ARPWATCH: Record the log and build an Ethernet address and IP address as seen on the LAN interface and the IP address of the database AutoFS: Automatic installation management process Automount, related to NFS, dependent on NIS Crond: The daemon of the planned task under Linux Named: DNS server Netfs: Install NFS, Samba, and NetWare Network File System Network: Activate script programs configured network interface NFS: Open NFS Service Portmap: RPC Portmap Manager, it management Based on RPC Services SESDMAIL: Samba Sendmail SMB: Samba File Shared / Print Service Syslog: A script XFS: X Window Drawing Server for Swing Syslog and Klogd System Requirements When System Boot Dictionary set xinetd: Support for a variety of network services, you can manage WUFTP, SSHD, TELNET, etc.

These daemons are also completed, and the RC program is also executed, and then the next step will be returned to INIT.

Part 5: Establish terminal

After the RC is executed, it returns to INIT. At this time, the basic system environment has been set, and the various daemons have been launched. INIT will open 6 terminals so that the user logs in the system. Switching in these 6 terminals by pressing Alt Fn (n corresponding to 1-6). The following 6 lines in inittab are defined by 6 terminals:

1: 2345: Respawn: / sbin / mingetty TTY1 2: 2345: Respawn: / sbin / mingetty Tty2 3: 2345: Respawn: / sbin / mingetty Tty3 4: 2345: Respaw: / sbin / mingetty TTY4 5: 2345: Respawn: / sbin / mingetty tty5 6: 2345: Respawn: / sbin / mingetty TTY6

From the above, it can be seen in the run level of 2, 3, 4, and 5, and the mingetty program will be run in a respawn, and the MINGETTY program can open the terminal and set the mode. At the same time, it will display a text login interface. This interface is the login interface we often see. In this login interface, you will prompt the user to enter the username, and the user entered by the user will pass to the login program as a parameter to verify the identity of the user. Section 6: Log in to the system, start the completion

For a graphical way user of the run level 5, their login is through a graphical login interface. After the login is successful, you can go directly to the KDE, GNOME and other window manager. And this article is mainly talking about whether the text is logged in:

When we see the MINGETTY login interface, we can enter the username and password to log in to the system.

Linux account verification programs are login, and login receives the username from MINGETTY as a username parameter. Then Login analyzes the username: If the username is not root, and there is / etc / nologin file, login will output the contents of the NOLOGIN file, and then exit. This is often used to prevent non-root user logins when maintaining. Only the terminals registered in / etc / securetty allow the root user to log in, if this file does not exist, root can log in on any terminal. The / etc / use of file is used to make additional access restrictions on the user. If this file does not exist, there is no other restriction.

After the user name is analyzed, login searches for / etc / passwd, and / etc / shadow to verify passwords and other information to set up accounts, such as what the main directory is, what shell is used. If you do not specify a primary directory, you will default an root directory; if you don't specify a shell, you will default to / bin / bash.

After the Login program is successful, the corresponding terminal will have the most recent login information (recorded in / var / log / lastlog), and check if the user has new mail (in / usr / spool / mail / corresponding username) Under contents). Then start setting various environment variables: For Bash, the system is first looking for / etc / profile script files, and executes it; then if there is a .bash_profile file in the user's home directory, execute it, and may call in these files Other profiles, after all the configuration files are executed, the various environment variables are also set, and the familiar command line prompt will appear, and the entire startup process is over.

It is hoped that the analysis of the Linux startup process can help those who want to learn Linux users to create a clear concept of the associated Linux boot process, and further studies how Linux will work next.

转载请注明原文地址:https://www.9cbs.com/read-121093.html

New Post(0)