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": "my,myboard-v1",
"name": "my board"
},
"network": {
"lan": {
"ifname": "eth0",
"protocol": "static"
}
}
}
/etc/board.json 是如何生成的?
bin/config_generate 中:
#!/bin/sh
CFG=/etc/board.json
. /usr/share/libubox/jshn.sh
[ -s $CFG ] || /bin/board_detect || exit 1
来看 /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/init.d/boot
-> /bin/config_generate
-> /bin/board_detect
-> /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 即可
-
/bin/config_generate 是生成 /etc/config/network 的关键实现