全部博文(51)
分类: LINUX
2016-08-18 16:01:17
脚本内容如下:
root@OpenWrt:/# cat /etc/preinit #!/bin/sh # Copyright (C) 2006 OpenWrt.org # Copyright (C) 2010 Vertical Communications
[ -z "$PREINIT" ] && exec /sbin/init
export PATH=/usr/sbin:/usr/bin:/sbin:/bin
pi_ifname= pi_ip=192.168.1.1 pi_broadcast=192.168.1.255 pi_netmask=255.255.255.0
fs_failsafe_ifname= fs_failsafe_ip=192.168.1.1 fs_failsafe_broadcast=192.168.1.255 fs_failsafe_netmask=255.255.255.0
fs_failsafe_wait_timeout=2
pi_suppress_stderr="y" pi_init_suppress_stderr="y" pi_init_path="/usr/sbin:/usr/bin:/sbin:/bin" pi_init_cmd="/sbin/init"
. /lib/functions.sh . /lib/functions/preinit.sh . /lib/functions/system.sh
boot_hook_init preinit_essential boot_hook_init preinit_main boot_hook_init failsafe boot_hook_init initramfs boot_hook_init preinit_mount_root
for pi_source_file in /lib/preinit/*; do . $pi_source_file done
boot_run_hook preinit_essential
pi_mount_skip_next=false pi_jffs2_mount_success=false pi_failsafe_net_message=false
boot_run_hook preinit_main |
第二次执行该脚本时,$PREINIT已赋值为1,所以跳过/sbin/init,继续执行;
该脚本内核包含三个其它脚本:
. /lib/functions.sh
. /lib/functions/preinit.sh
. /lib/functions/system.sh
l 调用boot_hook_init对hook点就行初始化
其实现是清除当前shell的环境变量:preinit_essential_hook、preinit_main_hook、failsafe_hook、initramfs_hook、preinit_mount_root_hook
l 依次执行/lib/preinit/*目录下的各个脚本文件
root@OpenWrt:/# ls /lib/preinit/ 02_default_set_state 50_indicate_regular_preinit 03_preinit_do_ipq806x.sh 70_initramfs_test 10_indicate_failsafe 80_mount_root 10_indicate_preinit 81_load_wifi_board_bin 10_sysinfo 99_10_failsafe_login 30_failsafe_wait 99_10_run_init 40_run_failsafe_hook |
root@OpenWrt:/# cat /lib/preinit/03_preinit_do_ipq806x.sh do_ipq806x() { . /lib/ipq806x.sh ipq806x_board_detect }
boot_hook_add preinit_main do_ipq806x |
这些脚本的格式大致都一样也就是将需要执行的脚本函数添加到对应的hook点内;
boot_hook_add() { local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}" local func="${2}"
[ -n "$func" ] && { local v; eval "v=\$$hook" export -n "$hook=${v:+$v }$func" #v已被定义且非空时,用$V替换;并且追加$func函数名 } } |
l 执行preinit_essential、和preinit_main两个hook点上的脚本函数;
我的环境中这两个hook点内容为:
preinit_essential_hook为空
export -n preinit_main_hook=define_default_set_state do_ipq806x preinit_ip pi_indicate_preinit do_sysinfo_generic failsafe_wait run_failsafe_hook indicate_regular_preinit initramfs_test do_mount_root do_load_ipq4019_board_bin run_init
boot_hook_shift() { local hook="${1}_hook" local rvar="${2}"
local v; eval "v=\$$hook" [ -n "$v" ] && { local first="${v%% *}" #截取首个字符串
[ "$v" != "${v#* }" ] && \ #去掉首个字符串后与源字符串不同 export -n "$hook=${v#* }" || \ #$hook为去除首个字符串 export -n "$hook="
export -n "$rvar=$first" #获取一个shell函数 return 0 }
return 1 }
boot_run_hook() { local hook="$1" local func
while boot_hook_shift "$hook" func; do #调用boot_hook_shift,func中为存放提取的shell函数 local ran; eval "ran=\$PI_RAN_$func" [ -n "$ran" ] || { export -n "PI_RAN_$func=1" $func "$1" "$2" #函数执行 } done } |