讲init进程的启动过程从/etc/inittab出发。
内核启动init进程时已经打开/dev/console设备作为控制台,一般情况下Busybox init程序就使用/dev/console,
如果内核启动init进程的同时设置了环境变量CONSOLE或console,则使用环境变量所指定的设备。
1、/etc/inittab文件的相关文档和示例代码都在Busybox 的examples/inittab文件中。
vi examples/inittab
内容如下(红色字为格式重要部分):
# /etc/inittab init(8) configuration for BusyBox
#
# Copyright (C)
1999-2004 by Erik Andersen <andersen@codepoet.org>
#
#
# Note,
BusyBox init doesn't support runlevels. The runlevels field is
# completely
ignored by BusyBox init. If you want runlevels, use sysvinit.
#
#
#
Format for each entry:
<id>:<runlevels>:<action>:<process>
#
#
<id>: WARNING: This field has a non-traditional meaning for BusyBox
init!
#
# The id field is used by BusyBox init
to specify the controlling tty for
# the specified process to run
on. The contents of this field are
# appended to "/dev/" and
used as-is. There is no need for this field to
# be unique, although
if it isn't you may have strange results. If
this
# field is left blank, it is completely ignored. Also note
that if
# BusyBox detects that a serial console is in use, then all
entries
# containing non-empty id fields will be ignored. BusyBox init
does
# nothing with utmp. We don't need no stinkin'
utmp.
<runlevels>: The runlevels field is
completely ignored.
#
# <action>: Valid actions include:
sysinit, respawn, askfirst, wait,
once,
# restart, ctrlaltdel, and
shutdown.
#
# Note: askfirst acts
just like respawn, but before running the specified
# process it
displays the line "Please press Enter to activate this
# console." and
then waits for the user to press enter before starting
# the specified
process.
#
# Note: unrecognised actions (like initdefault) will
cause init to emit
# an error message, and then go along with its
business.
#
# <process>: Specifies the process
to be executed and it's command line.
#
# Note: BusyBox init works
just fine without an inittab. If no inittab is
# found, it has the following
default behavior:
# ::sysinit:/etc/init.d/rcS
#
::askfirst:/bin/sh
# ::ctrlaltdel:/sbin/reboot
#
::shutdown:/sbin/swapoff -a
# ::shutdown:/bin/umount -a
-r
# ::restart:/sbin/init
#
# if it detects that /dev/console
is _not_ a serial console, it will
# also run:
#
tty2::askfirst:/bin/sh
# tty3::askfirst:/bin/sh
#
tty4::askfirst:/bin/sh
#
# Boot-time system configuration/initialization
script.
# This is run first except when booting in single-user
mode.
#
::sysinit:/etc/init.d/rcS
# /bin/sh invocations on selected
ttys
#
# Note below that we prefix the shell commands with a "-" to
indicate to the
# shell that it is supposed to be a login shell. Normally
this is handled by
# login, but since we are bypassing login in this case,
BusyBox lets you do
# this yourself...
#
# Start an "askfirst" shell on
the console (whatever that may be)
::askfirst:-/bin/sh
# Start an "askfirst" shell on
/dev/tty2-4
tty2::askfirst:-/bin/sh
tty3::askfirst:-/bin/sh
tty4::askfirst:-/bin/sh
#
/sbin/getty invocations for selected ttys
tty4::respawn:/sbin/getty 38400
tty5
tty5::respawn:/sbin/getty 38400 tty6
# Example of how to put a
getty on a serial line (for a terminal)
#::respawn:/sbin/getty -L ttyS0 9600
vt100
#::respawn:/sbin/getty -L ttyS1 9600 vt100
#
# Example how to put
a getty on a modem line.
#::respawn:/sbin/getty 57600 ttyS2
# Stuff to
do when restarting the init process
::restart:/sbin/init
# Stuff to do
before rebooting
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a
-r
::shutdown:/sbin/swapoff -a
2、仿照Busybox 的examples/inittab 文件,创建一个inittab文件,内容如下:
#/etc/inittab
#init进程启动的第一个子进程,它是一个脚本,可以在里面指定用户想执行的操作
#比较挂接其他文件系统、配置网络等
::sysinit:/etc/init.d/rcS
#启动shell,以/dev/ttySAC0作为控制台
ttySAC0::askfirst:-/bin/sh
#按下Ctrl+Alt+Del之后执行的程序,不过在串口控制台中无法输入Ctrl+Alt+Del组合键
::ctrlaltdel:/sbin/reboot
#重启、关机前执行的程序
::shutdown:/bin/umount -a -r