Detailed analysis of the openwrt startup process

OpenWrt is an open Linux platform, mainly used for wireless routing with wifi. Similar to the Linux distributions like Ubuntu, Red Hat, and so on, it also has its own startup process. This article mainly introduces the openwrt startup process and detailed analysis.

1 Overview

In the development process of linux, the Linux startup program is also developing, from sysv init to the current upstart, systemd, usually the process is the process number 1 process, the program has a pivotal place in the Linux system. In openwrt, another startup program called procd is used. The focus of this article is not on introducing procd. This article mainly introduces and parses procd, preinit and various scripts to complete the initialization of the whole system.

2, the software environment

Linux distribution: ubuntu14.04 LTS

Openwrt version: barrier break 14.07 r42635 (linux kernel 3.10.49)

Hardware: MPR-A2 module (rt5350)

Before viewing the Linux kernel code and the script under the root file system, you need to configure openwrt, run make menuconfig, select Ralink RT288x/RT3xxx in Target System, select RT3x5x/RT5350based boards in Subtarget, and select HAME MPR-A2 in Target Profile. Then make completes the compilation of openwrt. After the patch kernel code is in the build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/linux-ramips_rt305x/ directory, the root file system directory is in build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/root-ramips/.

3, start analysis 3.1 kernel boot point

The boot entry for Linux is start_kernel() (init/main.c). At the end of the function, rest_init()(init/main.c) is called. After the two kernel threads init and kthreadd are created, the function enters an infinite loop. The so-called process number 0. The init thread executes the kenrel_init()(init/main.c) function. In the kernel_init function, it first checks whether the init parameter is set in the kernel startup parameters. If there is, the program specified by the parameter is used as the init program. Otherwise, otherwise Will try to start in the order shown in the following code (after patching), if not able to start will lead to kernel panic.

If (!run_init_process("/etc/preinit") ||

! Run_init_process("/sbin/init") ||

! Run_init_process("/etc/init") ||

! Run_init_process("/bin/init") ||

! Run_init_process("/bin/sh"))

Return 0;

In the current environment, the kernel startup parameter does not set the init parameter, so the /etc/preinit program will be used as the init program, and the preinit is actually the shell script.

3.2 Related documents 3.2.1 Script files

/etc/preinit

/lib/funcTIons.sh

/lib/funcTIons/preinit.sh

/lib/funcTIons/system.sh

All scripts under /lib/preinit/

3.2.2 init program

In the proc package, two executable programs are generated after compilation: init and procd, both in the directory /sbin, both programs will be used.

3.3 Process Analysis

This initialization process follows the main line as follows:

Detailed analysis of the openwrt startup process

Below we analyze this process step by step.

In the /etc/preinit script, the first command is as follows:

[ -z "$PREINIT" ] && exec /sbin/init

When executing this script from the kernel, the PREINIT variable is not defined, so /sbin/init will be executed directly. The /sbin/init program mainly does some initialization work, such as environment variable setting, file system mount, kernel module loading, etc. After that, two processes will be created, respectively executing /etc/preinit and /sbin/procd, executing /etc/ Before the preinit will set the variable PREINIT, /sbin/procd will take the -h parameter, when the prod exits, it will call exec to execute /sbin/proc to replace the current init process (for details, see the init and procd programs in the procd package). This is the process name of the process ID of 1 displayed by the ps command after the system is started. The name of the process is finally /sbin/procd. There are several changes in the middle.

Continue to look at the /etc/preinit script, out of the variable settings, and then execute three shell scripts:

/lib/funcTIons.sh

/lib/functions/preinit.sh

/lib/functions/system.sh

Note that there is a space between "." and "/". The point here is quite similar to the souce command, but the souce is unique to bash and is not in the POSIX standard, "."

It is a general usage. Using "." means running in the current shell environment and will not run in a subshell. These shell scripts mainly define shell functions.

In particular, in preinit.sh, a function that defines a hook-related operation.

After that, five hook nodes will be defined using boot_hook_init as follows:

Boot_hook_init preinit_essential

Boot_hook_init preinit_main

Boot_hook_init failsafe

Boot_hook_init initramfs

Boot_hook_init preinit_mount_root

A hook function is added to these nodes.

After that is a loop, in turn, execute the script in the /lib/preinit/ directory under the current shell.

For pi_source_file in /lib/preinit/*; do

$pi_source_file

Done

The script in the /lib/preinit/ directory has a similar format, defines the function to be added to the hook node, and then adds the function to the corresponding hook node via boot_hook_add.

Finally, /etc/preinit executes the boot_run_hook function to execute the function on the corresponding hook node. Only preinit_essential and only executed in the current environment

The function on the preinit_main node is as follows:

Boot_run_hook preinit_essential

Boot_run_hook preinit_main

At this point, /etc/preinit is executed and exits. If you need to trace these scripts, you can add a command set -x at the very beginning of /etc/preinit, which will print out the process of executing the command, and will not actually execute.

As for the startup and initialization of the system service program will be completed by procd, the function of procd is not only here, it also integrates more other functions, see the procd data.

12'' 7 Blades Stand Fan

Stand Table Fan ,12 Inch Oscillating Table Fan,Pedestal Table Fan,Height Adjustable 7 Blades Table Fan

Foshan Shunde Josintech Electrical Appliance Technology Co.,Ltd , https://www.josintech.com

This entry was posted in on