Analysis of the init function of busybox

Source: Internet
Author: User
In addition to basic commands, the init function of busybox also supports the init function. Like other init functions, the init function of BusyBox 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 then finds the root Init of busybox
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 execute it. the init process of BusyBox will perform the following tasks in sequence: (refer to> 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 Vinit, 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 SytemVinit 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 pressEnter 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-tproc/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.

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.