-
Kernel 首先运行/usr/sbin/init,然后init 程序会读取/etc/inittab 这个文件
-
分析etc/inittab 文件
# /etc/inittab: init(8) configuration. # $Id: inittab,v 1.91 2002/01/25 13:35:21 miquels Exp $ # The default runlevel. id:5:initdefault: //默认启动水平 # Boot-time system configuration/initialization script. //初始化脚本 # This is run first except when booting in emergency (-b) mode. 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 # Normally not reached, but fallthrough in case of emergency. z6:6:respawn:/sbin/sulogin mxc::respawn:/etc/init.d/rc_mxc.S #mxc0:12345:respawn:/sbin/getty 115200 ttymxc0 # /sbin/getty invocations for the runlevels. # # The "id" field MUST be the same as the last # characters of the device (after "tty"). # # Format: # <id>:<runlevels>:<action>:<process> # 1:2345:respawn:/sbin/getty 38400 tty1 //设置tty1 ::boot:echo "hello wrold" //启动时执行 gpu::sysinit:/etc/init.d/rc_gpu.S //设置gpu的配置
etc/inittab 文件描述
:每个指令的标识符,不能重复。但是对于 busybox 的 init 来说,有着特殊意义。
对于 busybox 而言用来指定启动进程的控制 tty,一般我们将串口或者 LCD 屏幕设置为控
制 tty。
:对 busybox 来说此项完全没用,所以空着。
:动作,用于指定可能用到的动作。busybox 支持的动作如表 38.4.3.1 所
示:
行为 | 描述 |
---|---|
sysinit | 在系统初始化的时候 process 才会执行一次。 |
respawn | 一旦第4项指定的process命令中止,便重新运行该命令。 |
restart | 当 init 重启的时候才会执行 procee。 |
shutdown | 关机的时候执行 process。 |
wait | 表执行第4项指定的process,并等其结束后再运行其它命令。 |
once | 执行第4项指定的process,不等待它执行完成,继续运行其它命令。 |
boot | 不论在哪个执行等级,系统启动时都会运行第4项指定的process。 |
bootwait | 不论在哪个执行等级,系统启动时都会运行第4项指定的process,且一直等它执行完备。 |
off | 关闭任何动作,相当于忽略该配置行。 |
askfirst | 和 respawn 类似,在运行 process 之前在控制台上显示“Please press Enter to activate ,this console.”。只要用户按下“Enter”键以后才会执行 process |
ondemand | 进入ondemand执行等级时,执行第4项指定的process。 |
initdefault | 系统启动后进入的执行等级,该行不需要指定process。 |
powerwait | 当系统的供电不足时执行第4项指定的 process,且一直等它执行完毕。 |
powerokwait | 当系统的供电恢复正常时执行第4项指定的process,且一直等它执行完毕。 |
powerfailnow | 当系统的供电严重不足时执行第4项指定的process。 |
powerfail | 当出现电源错误时执行第4项指定的process命令,不等待其结束。 |
ctrlaltdel | 当用户按下【Ctrl+Alt+Del】时执行第4项指定的 process。 |
kbrequest | 当用户按下特殊的组合键时执行第4项指定的process,此组合键需在keymaps文件定义。 |
3.分析/etc/init.d/rcS
#!/bin/sh
#
# rcS Call all S??* scripts in /etc/rcS.d in
# numerical/alphabetical order.
#
# Version: @(#)/etc/init.d/rcS 2.76 19-Apr-1999 miquels@cistron.nl
#
PATH=/sbin:/bin:/usr/sbin:/usr/bin
runlevel=S
prevlevel=N
umask 022 //umask决定目录和文件被创建时得到的初始权限,“022”时初始权限为“rwxr-xr-x”,即“755”
//umask为屏蔽,即数值取反,二进制取反值为“111101101”,八进制取反值为“755
export PATH runlevel prevlevel
# Make sure proc is mounted
#
[ -d "/proc/1" ] || mount /proc //有/proc/1则挂载proc目录
#
# Source defaults.
#
. /etc/default/rcS //rcS.d启动脚本默认值
#
# Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
#
trap ":" INT QUIT TSTP
#
# Call all parts in order.
#
exec /etc/init.d/rc S //执行/etc/init.d/rcS.d中的脚本
3.1分析/etc/default/rcS
#
# Defaults for the boot scripts in /etc/rcS.d ///etc/rcS.d启动脚本 默认值
#
# Time files in /tmp are kept in days. //tmp下的文件保存时间
TMPTIME=0
# Set to yes if you want sulogin to be spawned on bootup //启动时生成sulogin选择yes
SULOGIN=no
# Set to no if you want to be able to login over telnet/rlogin
# before system startup is complete (as soon as inetd is started)
DELAYLOGIN=no
# Assume that the BIOS clock is set to UTC time (recommended)
UTC=yes
# Set VERBOSE to "no" if you would like a more quiet bootup. //安静启动,选择no
VERBOSE=no
# Set EDITMOTD to "no" if you don't want /etc/motd to be edited automatically
EDITMOTD=no
# Whether to fsck root on boot //是否在启动时检查root
ENABLE_ROOTFS_FSCK=no
# Set FSCKFIX to "yes" if you want to add "-y" to the fsck at startup.
FSCKFIX=yes
# Set TICKADJ to the correct tick value for this specific machine //https://www.eecis.udel.edu/~mills/ntp/html/tickadj.html
#TICKADJ=10000 //设置与时间相关的内核变量,即为时钟中断添加到系统时间的微秒数,ntpd 网络时间协议 (NTP) 守护程序会用到
# Enable caching in populate-volatile.sh
VOLATILE_ENABLE_CACHE=yes
# Indicate whether the rootfs is intended to be read-only or not.
# Setting ROOTFS_READ_ONLY to yes and rebooting will give you a read-only rootfs.
# Normally you should not change this value.
ROOTFS_READ_ONLY=no
3.2分析/etc/init.d/rc //该文件负责在运行级别更改时启动,停止服务
使用方法:脚本后面第一个参数是5 则运行/etc/rc5.d里面的脚本,第二个参数可以是start restart stop
4.分析etc/init.d/rc_mxc.S
#!/bin/bash
#
if grep -sq ttymxc0 /proc/cmdline; then //从uboot命令行取到ttymxc0的话,将mxc0设置为终端
/sbin/getty -L ttymxc0 115200 vt100
elif grep -sq ttymxc1 /proc/cmdline; then
/sbin/getty -L ttymxc1 115200 vt100
elif grep -sq ttymxc2 /proc/cmdline; then
/sbin/getty -L ttymxc2 115200 vt100
elif grep -sq ttymxc3 /proc/cmdline; then
/sbin/getty -L ttymxc3 115200 vt100
else
sleep 100000 //如果没有找到终端,等待27个小时
fi
linux的串口登录主要是由两个文件在控制,/sbin/getty来获得用户名,并进行检查用户名是否存在,然后将用户名传递给/bin/login来获取用户输入密码和检查密码是否正确
getty 是一个服务程序
所以要实现linux的自动登录,就要改动这两个文件.
1.getty实现的主要功能是:
1)打开指定的tty;
2)提示用户登录(login:);
3)获得登录用户名;
4)把用户名传递给login命令
2.login实现的主要功能是:
1)先检车是不是超级用户;
2)提示用户输入密码(通过getpass()实现);
3)检查密码并检查是否quiet登录;
4)设置登录的用户的ID和组ID,并设置相应的环境变量.