Init of busybox

Source: Internet
Author: User

BusyboxOfInit

In addition to basic commands, busybox also supports the init function. Like other init functions, busybox init also completes system initialization and pre-shutdown, we know that after the Linux kernel is loaded, the machine transfers the control to the kernel. After the Linux kernel is started, it does some work and finds the INIT program in the root file system, and run it. The INIT process of busybox will perform the following tasks in sequence: (refer to <build an embedded Linux system> P201)

1. Set the signal processing process for init

2. initialize the Console

3. Analyze the/etc/inittab File

4. Execute the system initialization command line. By default,/etc/init. d/RCS will be used.

5. Execute all inittab commands that cause init to be paused (Action type: Wait)

6. Execute all inittab statements that are executed only once (Action type: Once)

Once the above work is completed, the INIT process cyclically executes the following processes:

1. Execute all inittab commands that must be restarted upon termination (Action type: Once)

2. Execute all the inittab commands (Action type: askfirst) That must be restarted at the time of termination)

After the console is initialized, busybox checks whether the/etc/inittab file exists. If the file does not exist, busybox uses the default inittab configuration, which is mainly used for system reboot, the default actions of system suspension and init restart settings. In addition, it also sets shell startup actions for four virtual consoles (tty1 to tty4. If these device files are not created, busybox reports an error.

The format of each line in the inittab file is as follows: (the example folder under the root directory of busybox contains a detailed example of the inittab file)

ID: runlevel: Action: Process

Although this format is similar to the traditional sytem v init, ID has different meanings in the init of busybox. For busybox, ID is used to specify the control tty of the startup process. If the started process is not an interactive shell, such as the SH (Ash) of busybox, there should be a control TTY. If the control tty does not exist, the SH of busybox will report an error. Busybox will ignore the runlevel field completely, so leave it empty. You may ask why it is useless, I suppose it is to be consistent with the traditional sytem v init format. The process field is used to specify the path of the program to be executed, including the command line option. The action field is used to specify one of the eight actions that can be applied to the process in the following table.

Action

Result

Sysinit

Provide the path for init to initialize the command line

Respawn

Every time the corresponding process is terminated, it restarts.

Askfirst

Similar to respawn, but it is mainly used to reduce the number of terminal applications executed on the system. It will prompt init to display "Please press enter to active this console" Information on the console, and wait for the user to press enter before restarting

Wait

Instruct init to continue execution after the corresponding process is completed

Once

Only execute the corresponding process once, and will not wait for it to complete

Ctratldel

When you press CTRL + ALT + Delete, execute the corresponding process

Shutdown

When the system is shut down, execute the corresponding process

Restart

When init restarts, execute the corresponding process. Generally, the process executed here is INIT itself.

The following is the inittab file of My USB Linux

: Sysinit:/etc/init. d/RCS

: Respawn:/sbin/Getty 38400 tty1

Tty2: askfirst:-/bin/sh

Tty3: askfirst:-/bin/sh

: Restart:/sbin/init

: Ctrlaltdel:/bin/umount-a-r

This inittab performs the following actions

1. Set/etc/init. d/RCs to the system initialization file.

2. Start a login session on the virtual terminal tty1 of 38400 BPS (note the usage of Getty)

3. Start the shell of askfirst action on the virtual terminal tty2 and tty3

4. If init restarts, set/sbin/init to the program it will execute.

5. Tell init to uninstall all file systems by executing the umount command when the system is shut down, and use read-only mode to refresh the new installation to protect the file system when the uninstallation fails.

5.6 System Configuration File

5.6.1./etc/init. d/RCS File

The configuration file is usually stored in the/etc/directory. In section 5.5.3, the file for initializing the command line through init is/etc/init. d/RCs. below is my rfsfile.

#! /Bin/sh

/Bin/Mount-n-t proc/proc

/Bin/Mount-n-o remount, RW/

/Bin/Mount-AV

/Bin/hostname usblinux

/Etc/init. d/rc. Nic

/Etc/init. d/rc. Network

/Etc/init. d/rc. USB

/Etc/init. d/rc. Local

1. Install the proc file system, but do not write the/etc/mtab file (the mtab file is the file read by the DF command)

2. Remount the root file system in read/write mode (/etc/fstab file is required)

3. Mount-AV mount all the file systems listed in/etc/fstab

4. Name the host name USB Linux

5. Execute RC. Nic (automatic Nic Detection), RC. Network (network configuration), RC. USB (attach USB Module), RC. Local (execute some local services) in sequence)

5.6.2./etc/fstab

The above fstab file is involved when the mount command is used. below is my fstab file

/Dev/ram0/ext2 defaults 0 0

Proc/proc defaults 0 0

The syntax of the fstab configuration file can be man fstab.

In fact, there is no need to add Mount-AV in the RCS.

5.6.3./etc/mtab

The content in this file changes dynamically. When a file system is mounted, if the-n parameter is not added to the Mount, the installation information will be written to the mtab file, the DF command reads this file.

5.6.4./etc/profile

This file is used by SH. After the user obtains a shell, SH will configure the user's login environment based on this file. below is my profile file.

Path =/bin:/sbin:/usr/bin:/usr/sbin

PS1 = '[/u @/h/W]/$'

Hostname = '/bin/hostname'

Export path hostname PS1

Alias L. = 'LS-D. [A-Za-Z] * -- color = tty'

Alias Rm = 'rm-I'

Alias CP = 'cp-I'

Alias mv = 'mv-I'

Export Path =/usr/local/bin: $ path

Export Path =/usr/local/sbin: $ path

The Path Environment Variable specifies that sh searches for the command path when you type a command.

PS1 specifies the sh prompt format

For other export commands, do not need to mention the alias command. The Ash in busybox is very similar to bash, so it is not a problem if you are familiar with Bash.

Each record in the inittab file starts from a new line. Each record item can contain a maximum of 512 characters. The format of each record is usually as follows: ID: rstate: Action: process.
1. The ID field is a string of up to four characters, used to uniquely identify a table item.
2. the rstate (run state) field defines the running level of the record item when it is called. The rstate can be composed of one or more running levels, or it can be empty. If it is null, it indicates the running level 0 ~ 6. When the init is requested to change the running level, processes that do not include the new running level in the rstate field will receive the sigterm warning signal and finally be killed; only commands started by A, B, and C (A, B, and C are not real running levels)
3. The action field tells init how to process the process specified by the process field. The values and corresponding actions of the action field are as follows:
1) respawn: if the process specified by the process field does not exist, start the process. init continues to scan the subsequent processes in the inittab file. When the process ends, init restarts it. If such a process already exists, nothing will be done.
2) Wait: start the process specified by the Process Field and wait until the processing is completed to process the next record in the inittab.
3) Once: start the process specified by the Process Field and process the next record item before the process ends. When such a process is terminated, it will not be restarted. When it enters the new running level, if such a process is still running, init will not restart it.
4) boot: init processes such records only when the system is started. It starts the corresponding process and processes the next record item without waiting for the processing to complete. When such a process is terminated, the system does not restart it.
5) bootwait: After the system is started, it processes such records when it enters multi-user mode for the first time from single-user mode. init starts such a process and waits until its processing ends, then the next record item is processed. When such a process is terminated, the system does not restart it.
6) powerfail: When init receives a power-off signal (sigpwr), IT processes the specified process.
7) powerwait: When init receives a power-off signal (sigpwr), IT processes the specified process and checks other record items until the processing ends.
8) off: if the specified process is running, init sends a sigterm warning signal to it, waiting for 5 seconds before the sigkill command is executed. If the process does not exist, ignore this item.
9) OnDemand: the respawn function is used only for record items whose rstate field is A, B, and C.
10) sysinit: the specified process is executed before accessing the console. Such record items are only used for initialization of certain devices, the purpose is to enable init to ask the user about the running level on such a device. init needs to wait until the process finishes running.
11) initdefault: Specifies a default running level. This item is scanned only when init is called at the beginning. If the rstate field specifies multiple running levels, the maximum number is the default running level. If the rstate field is empty, init considers the field to be 0123456, and then enters Level 6, which leads to a loop, if the inittab file does not contain the initdefault record item, you are requested to specify an initial running level for it at system startup.
4. The process in the process field can be any waiting process, executable script or program.
In addition, you can add new record items in the file inittab at any time. Level Q/Q does not change the current running level, and re-check the inittab file, you can run init Q or init Q to re-read the INIT process and process the file inittab.

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.