OpenWrt /etc/config/network 的生成过程

OpenWrt /etc/config/network 的生成过程

/etc/config/network 是由谁生成的?

是由 /bin/config_generate 自动生成的

/bin/config_generate 在 /etc/init.d/boot 中被调用

boot() {                                                                 
        ...
     
        /bin/config_generate
        uci_apply_defaults  
                          
        # temporary hack until configd exists

来看 /bin/config_generate 内容

CFG=/etc/board.json                         # 这里要关注/etc/board.json  是怎样生成的
...
json_init
json_load "$(cat ${CFG})"                   # 读取 /etc/board.json 
if [ ! -s /etc/config/network ]; then                                              
        touch /etc/config/network                                                                  
        generate_static_network                                                                    
                                                                                                        
        json_get_keys keys network          # 根据 /etc/board.json  中的 network 生成配置                                                              
        for key in $keys; do generate_network $key; done                                
                                                                                        
        json_get_keys keys switch                                                   
        for key in $keys; do generate_switch $key; done                                              
fi  

...
uci commit

可见当 /etc/config/network 不存在时,会自动创建/etc/config/network , 这里依赖于 /etc/board.json 文件

/etc/board.json 示例

{
        "model": {
                "id": "openvox,openvox-h3-db-v1",
                "name": "OpenVox H3-DB-V1"
        },
        "network": {
                "lan": {
                        "ifname": "eth0",
                        "protocol": "static"
                }
        }
}

/etc/board.json 是如何生成的?

./lib/preinit/10_indicate_preinit

preinit_config_board() {
	/bin/board_detect /tmp/board.json      # 调用 /bin/board_detect

	[ -f "/tmp/board.json" ] || return

	...
}
...
preinit_ip() {
	[ "$pi_preinit_no_failsafe" = "y" ] && return

	# if the preinit interface isn't specified and ifname is set in
	# preinit.arch use that interface
	if [ -z "$pi_ifname" ]; then
		pi_ifname=$ifname
	fi

	if [ -n "$pi_ifname" ]; then
		preinit_ip_config $pi_ifname
	elif [ -d "/etc/board.d/" ]; then
		preinit_config_board
	fi

	preinit_net_echo "Doing OpenWrt Preinit\n"
}
...
boot_hook_add preinit_main preinit_ip         # 将 preinit_ip 添加到 preinit_main 中,最后由/etc/preinit 调用

来看 /bin/board_detect

#!/bin/sh

CFG=$1

[ -n "$CFG" ] || CFG=/etc/board.json

[ -d "/etc/board.d/" -a ! -s "$CFG" ] && {
	for a in `ls /etc/board.d/*`; do        # 执行 /etc/board.d/下的文件
		[ -x $a ] || continue;
		$(. $a)
	done
}

[ -s "$CFG" ] || return 1
$ ls -1 /etc/board.d
02_network
99-default_network

在/etc/board.d/02_network 中

. /lib/functions/uci-defaults.sh

board_config_update

case "$(board_name)" in
"olimex,a20-olinuxino-micro")
        ucidef_set_interface_lan 'wlan0'
        ;;
"lamobo,lamobo-r1")
        ucidef_add_switch "switch0" \
                "4:lan:1" "0:lan:2" "1:lan:3" "2:lan:4" "3:wan" "8@eth0"
        ;;
"xunlong,orangepi-r1")
        ucidef_set_interfaces_lan_wan "eth0" "eth1"
        ;;
*)
        ucidef_set_interface_lan 'eth0'
        ;;
esac

board_config_flush     # board_config_flush 定义在 ./lib/functions/uci-defaults.sh 中
                       # board_config_flush() {
                       #  json_dump -i > /tmp/.board.json
                       #  mv /tmp/.board.json ${CFG}    # CFG=/etc/board.json
                       #  }                  
                       #

exit 0

调用链梳理

第一部分:生成 /etc/board.json
/etc/preinit
-> /lib/preinit/10_indicate_preinit    
   preinit_ip
   -> preinit_config_board
     -> /bin/board_detect /tmp/board.json
       -> /etc/board.d/02_network
         -> /lib/functions/uci-defaults.sh
         -> board_config_flush 
           -> mv /tmp/.board.json ${CFG}
-------------------------------------------------------          
第二部分,根据/etc/board.json 中的network节点 生成 /etc/config/network
/etc/init.d/boot 
-> /bin/config_generate
  -> touch /etc/config/network 
     generate_static_network
     uci commit

小结

  • network 配置文件的生成依赖于 /etc/board.json
  • /etc/board.json 内容又由 /etc/board.d/02_network 决定
  • 所以要修改默认配置,修改/etc/board.d/02_network 即可

你可能感兴趣的:(OpenWrt,config/network,openwrt,network)