参考文档:
下面是我的理解:
总的流程是:1.CFE->2.linux->3./etc/preinit->4./sbin/init ->5./etc/inittab ->6./etc/init.d/rcS->7./etc/rc.d/S*
1.按照http://wiki.openwrt.org/doc/techref/process.boot中的描述,初始化从bootloader开始,会调用/etc/preinit脚本。 至于bootloader又是如何启动的,以及bootloader如何调用/etc/preinit的,这个以后再分析
2./etc/preinit脚本的分析:
//注释:如下是初始化5种hook,这5种hook用来处理/lib/preinit/*中的各类脚本文件
boot_hook_init preinit_essential
boot_hook_init preinit_mainboot_hook_init failsafeboot_hook_init initramfsboot_hook_init preinit_mount_root//注释:按开头数字的顺序,依次执行/lib/preinit/中的各个脚本
for pi_source_file in /lib/preinit/*; do
. $pi_source_filedone#boot_hook_add preinit_main 某个脚本中自定义的函数
#boot_hook_add preinit_mount_root 某个脚本中自定义的函数 #boot_hook_add preinit_essential 某个脚本中自定义的函数 #boot_hook_add failsafe 某个脚本中自定义的函数#boot_hook_add initramfs 某个脚本中自定义的函数
#可以想见,这些脚本都是将各自脚本定义的函数,通过boot_hook_add挂接到了某个函数链上。而这些函数又分为了几类:preinit_main/preinit_mount_root/preinit_essential/failsafe/initramfs
boot_run_hook preinit_essential //注释:等上述脚本中定义的函数,都按照分类挂接好之后,则这里统一处理所有类型为:preinit_essential的函数
pi_mount_skip_next=falsepi_jffs2_mount_success=falsepi_failsafe_net_message=falseboot_run_hook preinit_main//注释:再统一处理所有类型为:preinit_main的函数
3. 下面分析:/etc/inittab 其内容如下:
//注释:
4.下面分析 /etc/init.d/rcS 其内容如下:
//注释:由于在inittab中有:::sysinit:/etc/init.d/rcS S boot,故这里进入if分支
if [ "$1" = "S" ]; then
run_scripts "$1" "$2" & //注释:运行/etc/rc.d/目录下所有以S开头的脚本,而且还是后台运行(&表示后台运行)else run_scripts "$1" "$2"fi
5. /etc/rc.d/目录中有啥呢? 进入设备中的此目录,ls看一下,会发现:
这么多以S开头的脚本,都是初始化时启动的。下面就选择其中的 S60samba 来分析吧。
6./etc/rc.d/S60samba --- 实际上,S60samba -> ../init.d/samba , 其内容为:
//注释:引用/etc/rc.common脚本的处理,这里面会引用脚本:/etc/functions.sh,并且还有针对boot函数的定义,如下:
# Copyright (C) 2008-2011 OpenWrt.org
START=60smb_header() { local interface config_get interface $1 interface "loopback lan" # resolve interfaces local interfaces=$( include /lib/network scan_interfaces local net for net in $interface; do local ifname config_get ifname "$net" ifname [ -n "$ifname" ] && { local ipaddr netmask config_get ipaddr "$net" ipaddr config_get netmask "$net" netmask [ -n "$ipaddr" ] && echo -n "$ipaddr/${netmask:-255.255.255.255} " local ip6addr config_get ip6addr "$net" ip6addr [ -n "$ip6addr" ] && echo -n "$ip6addr " } echo -n "${ifname:-$net} " done ) local name workgroup description charset local hostname="$(uci_get system.@system[0].hostname)" config_get name $1 name "${hostname:-OpenWrt}" config_get workgroup $1 workgroup "${hostname:-OpenWrt}" config_get description $1 description "Samba on ${hostname:-OpenWrt}" config_get charset $1 charset "UTF-8" mkdir -p /var/etc sed -e "s#|NAME|#$name#g" \ -e "s#|WORKGROUP|#$workgroup#g" \ -e "s#|DESCRIPTION|#$description#g" \ -e "s#|INTERFACES|#$interfaces#g" \ -e "s#|CHARSET|#$charset#g" \ /etc/samba/smb.conf.template > /var/etc/smb.conf local homes config_get_bool homes $1 homes 0 [ $homes -gt 0 ] && { cat <<EOT >> /var/etc/smb.conf[homes] comment = Home Directories browsable = no read only = no create mode = 0750EOT } [ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf}smb_add_share() { local name local path local users local read_only local guest_ok local create_mask local dir_mask config_get name $1 name config_get path $1 path config_get users $1 users config_get read_only $1 read_only config_get guest_ok $1 guest_ok config_get create_mask $1 create_mask config_get dir_mask $1 dir_mask [ -z "$name" -o -z "$path" ] && return echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf}start() { //注释:这是启动时实际调用的地方 config_load samba //注释:config_load在/etc/functions.sh中定义,其主体是:uci_load "$@" 。没找到进一步的函数定义,我猜测这里实际就是要加载配置文件:/etc/config/samba --- 如果我猜错,请纠正我。特别提出的是:/etc/config/samba就是符合openWRT的uci要求的配置文件了。openWRT本身就是通过调整此配置文件,来控制samba进程的service_start /usr/sbin/smbd -D //注释:启动服务进程。这个进程是要读取文件/var/etc/smb.conf的。即:samba进程本身是不识别/etc/config/samba的,而只识别/var/etc/smb.conf文件
}stop() { service_stop /usr/sbin/smbd}