关于openwrt启动过程preinit

openwrt启动过程中,kernel启动后执行的第一个脚本是/etc/preinit,其中有一个PREINIT参数,不知道是从哪里传递进来的(我grep了内核代码,但是没有找到"PREINIT"。按理说这个值不应该为空,否则export那句及后面的语句就没用了?),求高手给分析下。
下面是/etc/preinit的一部分#!/bin/sh
# Copyright (C) 2006 OpenWrt.org
# Copyright (C) 2010 Vertical Communications

[ -z "$PREINIT" ] && exec /sbin/init

export PATH=/bin:/sbin:/usr/bin:/usr/sbin

pi_ifname=
pi_ip=192.168.1.1
pi_broadcast=192.168.1.255
pi_netmask=255.255.255.0
...
已邀请:

HanDsoMes

赞同来自: noel

通过用x86的虚拟机做实验,终于弄清楚了。在[ -z "$PREINIT" ] && exec /sbin/init之前插入debug的语句,发现有两次输出,第一次PREINIT确实为空,于是执行/sbin/init,这个程序来源于procd包,然后在procd中搜索PREINIT查找到相关代码,基本验证猜想。
结论就是:第一次PREINIT没有值,执行/sbin/init,后来在其中设置了PREINIT变量,接着又执行了一次/etc/preinit。
[    1.451217] sd 0:0:0:0: [sda] Attached SCSI disk
[ 1.470854] EXT4-fs (sda2): mounted filesystem without journal. Opts: (null)
[ 1.485756] VFS: Mounted root (ext4 filesystem) readonly on device 8:2.
[ 1.486811] Freeing unused kernel memory: 348K (c1562000 - c15b9000)
......han...debug: PREINIT=
[ 1.627101] init: Console is alive
[ 1.678668] pps_core: LinuxPPS API ver. 1 registered
[ 1.702742] pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
[ 1.714512] PTP clock support registered
[ 2.446062] Switched to clocksource tsc
[ 2.645414] init: - preinit -
......han...debug: PREINIT=1
Press the [f] key and hit [enter] to enter failsafe mode
Press the [1], [2], [3] or [4] key and hit [enter] to select the debug level
[ 18.744130] mount_root: mounting /dev/root
[ 18.763733] EXT4-fs (sda2): warning: mounting unchecked fs, running e2fsck is recommended
[ 18.791712] EXT4-fs (sda2): re-mounted. Opts: (null)
[ 18.802927] EXT4-fs (sda1): warning: mounting unchecked fs, running e2fsck is recommended
[ 18.826347] EXT4-fs (sda1): mounted filesystem without journal. Opts: (null)

要回复问题请先登录注册