busybox学习笔记之二 -- /sbin/init

1. busybox init
采用busybox的init的情况下,/sbin/init实际上是指向/bin/init的软链接
#root@e4:/sbin# ls -l init  
#lrwxrwxrwx    1 311263   named          14 Aug 31 17:12 init -> ../bin/busybox  
#/sbin/init实际上是一个指向/bin/busybox的软链接  

int init_main(int argc UNUSED_PARAM, char **argv)
+-- sa.sa_sigaction = handle_sigsegv;
+-- sa.sa_flags = SA_SIGINFO;
+-- sigaction(SIGSEGV, &sa, NULL);
+-- sigaction(SIGILL, &sa, NULL);
+-- sigaction(SIGFPE, &sa, NULL);
+-- sigaction(SIGBUS, &sa, NULL);

+-- console_init();
+-- set_sane_term();
+-- xchdir("/");
+-- setsid();

+-- new_init_action(SYSINIT, "mount -t proc proc /proc", "");
+-- new_init_action(SYSINIT, "swapon -a", "");
+-- run_actions(SYSINIT);  

+-- parse_inittab(); //解析/etc/inittab配置文件

2. /etc/inittab配置文件的解析
1. /etc/inittab

@/etc/inittab
#
# Create all essential device nodes
#
::sysinit:/bin/mknod /dev/console c 5 1
::sysinit:/bin/mknod /dev/dsp c 14 3
::sysinit:/bin/mknod /dev/full c 1 7
::sysinit:/bin/mknod /dev/kmem c 1 2
::sysinit:/bin/mknod /dev/mem c 1 1
::sysinit:/bin/mknod /dev/mixer c 14 0
::sysinit:/bin/mknod /dev/null c 1 3
::sysinit:/bin/mknod /dev/port c 1 4
::sysinit:/bin/mknod /dev/ptmx c 5 2
::sysinit:/bin/mknod /dev/ram b 1 1
::sysinit:/bin/mknod /dev/ram0 b 1 0
::sysinit:/bin/mknod /dev/ram1 b 1 1
::sysinit:/bin/mknod /dev/random c 1 8
::sysinit:/bin/mknod /dev/tty c 5 0
::sysinit:/bin/mknod /dev/urandom c 1 9
::sysinit:/bin/mknod /dev/zero c 1 5
::sysinit:/bin/mknod /dev/ttyS0 c 4 64
::sysinit:/bin/mknod /dev/mtdblock0 b 31 0
::sysinit:/bin/mknod /dev/mtdblock1 b 31 1
::sysinit:/bin/mknod /dev/mtdblock2 b 31 2
::sysinit:/bin/mknod /dev/mtdblock3 b 31 3

#
# Remount all including proc.
#
# ::sysinit:/bin/mkdir -p /obfl
# ::sysinit:/bin/mount -a

#
# Setup init.d
#
::sysinit:/bin/cp -a /etc/init.d/* /etc/rc.d/init.d/
::sysinit:/bin/rm -rf /etc/init.d
::sysinit:/bin/ln -s /etc/rc.d/init.d /etc/init.d
::sysinit:/bin/rm -rf /bin/sh
::sysinit:/bin/ln -s /bin/bash /bin/sh
::sysinit:/bin/ln -s /bin/printenv /usr/bin/printenv

#
# Execute single user and rc3 explicitly
# Busybox does not support run levels
#

echo "*****INITTAB***** calling rcS and rc 3 NOW **** "

::wait:/etc/init.d/rcS
::wait:/etc/init.d/rc 3

# Handle reboot request from CTRL-ALT-DEL
# ::ctrlaltdel:/bin/umount -a -r
# ::ctrlaltdel:/sbin/reboot

#
# Login prompt for the serial console
# Note: requires /dev/ttyS0 to be present during boot
# Created above in this script
#
::respawn:/sbin/getty -L ttyS0 9600 xterm

#
# Use this for debugging, drops to shell without prompt
#
#::respawn:/bin/sh

# Stuff to do when restarting the init process
# ::restart:/sbin/init


/etc/inittab
+-- ::wait:/etc/init.d/rcS 
    +-- /sbin/unconfigured.sh
    +-- /etc/default/rcS
    +-- /etc/rc.d/rcS.d/S??*
        +-- /etc/rc.d/rcS.d/S03mountvirtfs
        +-- /etc/rc.d/rcS.d/S10checkroot.sh
        +-- /etc/rc.d/rcS.d/S11syslog
        +-- /etc/rc.d/rcS.d/S15mountall.sh
        +-- /etc/rc.d/rcS.d/S29module-init-tools
        +-- /etc/rc.d/rcS.d/S30diag                 //diag...
        +-- /etc/rc.d/rcS.d/S30procps
        +-- /etc/rc.d/rcS.d/S40networking
    +-- [ -x /etc/rc.d/rc.local ]&&/etc/rc.d/rc.local 
    +-- /sbin/setup.sh
+-- ::wait:/etc/init.d/rc 3  
    +-- /etc/rc.d/rc$runlevel.d/K[0-9][0-9]*
    +-- /etc/rc.d/rc$runlevel.d/S*
        +-- /etc/rc.d/rc3.d/S11syslog
        +-- /etc/rc.d/rc3.d/S19nfs-common
        +-- /etc/rc.d/rc3.d/S30sshd
        +-- /etc/rc.d/rc3.d/S56xinetd
        +-- /etc/rc.d/rc3.d/S70diag_eng             //diageng...

2. /etc/inittab语法
inittab的man手册:


INITTAB(5)                                  Linux System Administrator‘s Manual                                  INITTAB(5)

NAME
       inittab - format of the inittab file used by the sysv-compatible init process

DESCRIPTION
       The inittab file describes which processes are started at bootup and during normal operation (e.g. /etc/init.d/boot,
       /etc/init.d/rc, gettys...).  Init(8) distinguishes multiple runlevels, each of which can have its own  set  of  pro-
       cesses  that  are  started.  Valid runlevels are 0-6 plus A, B, and C for ondemand entries.  An entry in the inittab
       file has the following format:

              id:runlevels:action:process

       Lines beginning with '#' are ignored.

       id     is a unique sequence of 1-4 characters which identifies an entry in inittab (for versions  of  sysvinit  com-
              piled with the old libc5 (< 5.2.18) or a.out libraries the limit is 2 characters).

              Note:  traditionally,  for getty and other login processes, the value of the id field is kept the same as the
              suffix of the corresponding tty, e.g. 1 for tty1. Some ancient login accounting programs might  expect  this,
              though I can't think of any.

       runlevels
              lists the runlevels for which the specified action should be taken.

       action 
              describes which action should be taken.

       process
              specifies  the  process  to  be executed.  If the process field starts with a ‘+’ character, init will not do
              utmp and wtmp accounting for that process.  This is  needed  for  gettys  that  insist  on  doing  their  own
              utmp/wtmp housekeeping.  This is also a historic bug.

       The  runlevels  field  may contain multiple characters for different runlevels.  For example, 123 specifies that the
       process should be started in runlevels 1, 2, and 3.  The runlevels for ondemand entries may contain an A, B,  or  C.
       The runlevels field of "sysinit", "boot", and "bootwait" entries are ignored.

       When  the  system runlevel is changed, any running processes that are not specified for the new runlevel are killed,
       first with SIGTERM, then with SIGKILL.

       Valid actions for the action field are:

       respawn
              The process will be restarted whenever it terminates (e.g. getty).

       wait   The process will be started once when the specified runlevel is entered and init will wait for  its  termina-
              tion.

       once   The process will be executed once when the specified runlevel is entered.

       boot   The process will be executed during system boot.  The runlevels field is ignored.

       bootwait
              The  process  will  be executed during system boot, while init waits for its termination (e.g. /etc/rc).  The
              runlevels field is ignored.

       off    This does nothing.

       ondemand
              A process marked with an ondemand runlevel will be executed  whenever  the  specified  ondemand  runlevel  is
              called.  However, no runlevel change will occur (ondemand runlevels are \u2018a\u2019, \u2018b\u2019, and \u2018c\u2019).

       initdefault
              An  initdefault entry specifies the runlevel which should be entered after system boot.  If none exists, init
              will ask for a runlevel on the console. The process field is ignored.

       sysinit
              The process will be executed during system boot. It will be executed before any boot  or   bootwait  entries.
              The runlevels field is ignored.

       powerwait
              The process will be executed when the power goes down. Init is usually informed about this by a process talk-
              ing to a UPS connected to the computer.  Init will wait for the process to finish before continuing.

       powerfail
              As for powerwait, except that init does not wait for the process\u2019s completion.

       powerokwait
              This process will be executed as soon as init is informormed that the power has been restored.

       powerfailnow
              This process will be executed when init is told that the battery of the external UPS is almost empty and  the
              power  is  failing  (provided that the external UPS and the monitoring process are able to detect this condi-
              tion).

       ctrlaltdel
              The process will be executed when init receives the SIGINT signal.  This means that  someone  on  the  system
              console  has  pressed  the CTRL-ALT-DEL key combination. Typically one wants to execute some sort of shutdown
              either to get into single-user level or to reboot the machine.

       kbrequest
              The process will be executed when init receives a signal from the keyboard handler that a special key  combi-
              nation was pressed on the console keyboard.

              The  documentation  for  this  function  is not complete yet; more documentation can be found in the kbd-x.xx
              packages (most recent was kbd-0.94 at the time of this writing). Basically you want to map some keyboard com-
              bination  to  the "KeyboardSignal" action. For example, to map Alt-Uparrow for this purpose use the following
              in your keymaps file:

              alt keycode 103 = KeyboardSignal

EXAMPLES
       This is an example of a inittab which resembles the old Linux inittab:

              # inittab for linux
              id:1:initdefault:
              rc::bootwait:/etc/rc
              1:1:respawn:/etc/getty 9600 tty1
              2:1:respawn:/etc/getty 9600 tty2
              3:1:respawn:/etc/getty 9600 tty3
              4:1:respawn:/etc/getty 9600 tty4

       This inittab file executes /etc/rc during boot and starts gettys on tty1-tty4.

       A more elaborate inittab with different runlevels (see the comments inside):

              # Level to run in
              id:2:initdefault:

              # Boot-time system configuration/initialization script.
              si::sysinit:/etc/init.d/rcS

              # What to do in single-user mode.
              ~:S:wait:/sbin/sulogin

              # /etc/init.d executes the S and K scripts upon change
              # of runlevel.
              #
              # Runlevel 0 is halt.
              # Runlevel 1 is single-user.
              # Runlevels 2-5 are multi-user.
              # Runlevel 6 is reboot.

              l0:0:wait:/etc/init.d/rc 0
              l1:1:wait:/etc/init.d/rc 1
              l2:2:wait:/etc/init.d/rc 2
              l3:3:wait:/etc/init.d/rc 3
              l4:4:wait:/etc/init.d/rc 4
              l5:5:wait:/etc/init.d/rc 5
              l6:6:wait:/etc/init.d/rc 6

              # What to do at the "3 finger salute".
              ca::ctrlaltdel:/sbin/shutdown -t1 -h now

              # Runlevel 2,3: getty on virtual consoles
              # Runlevel   3: getty on terminal (ttyS0) and modem (ttyS1)
              1:23:respawn:/sbin/getty tty1 VC linux
              2:23:respawn:/sbin/getty tty2 VC linux
              3:23:respawn:/sbin/getty tty3 VC linux
              4:23:respawn:/sbin/getty tty4 VC linux
              S0:3:respawn:/sbin/getty -L 9600 ttyS0 vt320
              S1:3:respawn:/sbin/mgetty -x0 -D ttyS1

FILES
       /etc/inittab

AUTHOR
       Init was written by Miquel van Smoorenburg (miquels@cistron.nl).  This manual page was written by Sebastian  Lederer
       (lederer@francium.informatik.uni-bonn.de) and modified by Michael Haardt (u31b3hs@pool.informatik.rwth-aachen.de).

SEE ALSO
       init(8), telinit(8)

                                                        Dec 4, 2001                                              INITTAB(5)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值