OpenWrt file/bin/config_generate network初始化分析

本文详细解析了嵌入式设备中网络初始化的过程,包括从源码到系统配置的生成,以及如何通过脚本和配置文件实现网络及DHCP服务的定制。深入探讨了/etc/board.json的作用,以及如何检测和生成网络配置。

network init

1、源码中:package/base-files/files/etc/init.d/boot
生成文件系统中的/etc/rc.d/S10boot
2、调用/bin/config_generate

CFG=/etc/board.json
[ -s $CFG ] || /bin/board_detect || exit 1
[ -s /etc/config/network -a -s /etc/config/system ] && exit 0
检测/etc/board.json是否为空,为空则执行/bin/board_detect调用/etc/board.d/*下的脚本生成/etc/board.json。
检测/etc/config/network 与 /etc/config/system是否为空
第一次的时候为空,执行以下生成它们,第二次就跳过了
touch /etc/config/network	//生成network配置
generate_static_network
......
touch /etc/config/system
generate_static_system
......

/# shell & json函数
shell教程:https://www.runoob.com/linux/linux-shell.html

local ifname macaddr protocol type ipaddr netmask
local 命令定义局部变量,作用域为函数内部

json_init	初始化环境变量
json_load "$(cat ${CFG})" 

json_add_object	添加一个对象
json_add_string	添加string变量
json_add_int	添加一个对象
json_add_boolean	添加布尔变量
json_add_array	添加数组
json_dump	显示当前的json串	
json_select	切换对象
json_get_var	获取某个变量值	
[ -n name] || return     判断变量的值,若空,则返回

uci -q 安静模式(不打印错误信息)
使用<<-EOF,告诉主shell,后续的输入,是其他命令或者子shell的输入,直到遇到EOF为止,再回到主shell

/etc/config/dhcp配置文件

1、package/network/services/odhcpd/files/odhcpd.defaults
该脚本新建/etc/config/dhcp配置文件,读取/etc/board.json,重新生成该文件。
注意:删除该文件后无法编译
将该文件内容注释后,将自定义的/etc/config/dhcp拷贝到
package/base-files/files/etc/config目录
编译后即生效自定义的DHCP配置

config dnsmasq
        option domainneeded '1'
        option boguspriv '1'
        option filterwin2k '0'
        option localise_queries '1'
        option rebind_protection '1'
        option rebind_localhost '1'
        option local '/lan/'
        option domain 'lan'
        option expandhosts '1'
        option nonegcache '0'
        option authoritative '1'
        option readethers '1'
        option leasefile '/tmp/dhcp.leases'
        option resolvfile '/tmp/resolv.conf.auto'
        option nonwildcard '1'
        option localservice '1'

config dhcp 'lan'
        option interface 'lan'
        option ignore '1'

config dhcp 'wan'
        option interface 'wan'
        option ignore '1'

config odhcpd 'odhcpd'
        option maindhcp '0'
        option leasefile '/tmp/hosts/odhcpd'
        option leasetrigger '/usr/sbin/odhcpd-update'
        option loglevel '4'

config dhcp 'wlan'
        option start '100'
        option leasetime '12h'
        option limit '150'
        option interface 'wlan'
dropbear的config配置文件如下:config dropbear option PasswordAuth 'on' option RootPasswordAuth 'on' option Port '22' # option BannerFile '/etc/banner' dropbear的启动init.d文件如下:#!/bin/sh /etc/rc.common # Copyright (C) 2006-2010 OpenWrt.org # Copyright (C) 2006 Carlos Sobrinho START=50 STOP=50 SERVICE_USE_PID=1 NAME=dropbear PROG=/usr/sbin/dropbear PIDCOUNT=0 EXTRA_COMMANDS="killclients" EXTRA_HELP=" killclients Kill ${NAME} processes except servers and yourself" PATH_SSH_ALGO_VERSION="/tmp/ssh_algo_version" dropbear_start() { append_ports() { local ifname="$1" local port="$2" grep -qs "^ *$ifname:" /proc/net/dev || { append args "-p $port" return } for addr in $( ifconfig "$ifname" | sed -ne ' /addr: *fe[89ab][0-9a-f]:/d s/.* addr: *\([0-9a-f:\.]*\).*/\1/p ' ); do append args "-p $addr:$port" done } local section="$1" # check if section is enabled (default) local enabled config_get_bool enabled "${section}" enable 1 [ "${enabled}" -eq 0 ] && return 1 # verbose parameter local verbosed config_get_bool verbosed "${section}" verbose 0 # increase pid file count to handle multiple instances correctly PIDCOUNT="$(( ${PIDCOUNT} + 1))" # prepare parameters (initialise with pid file) local pid_file="/var/run/${NAME}.${PIDCOUNT}.pid" local args="-P $pid_file" local val # A) password authentication config_get_bool val "${section}" PasswordAuth 1 [ "${val}" -eq 0 ] && append args "-s" # B) listen interface and port local port local interface config_get interface "${section}" Interface config_get interface "${interface}" ifname "$interface" config_get port "${section}" Port 22 append_ports "$interface" "$port" # C) banner file config_get val "${section}" BannerFile [ -f "${val}" ] && append args "-b ${val}" # D) gatewayports config_get_bool val "${section}" GatewayPorts 0 [ "${val}" -eq 1 ] && append args "-a" # E) root password authentication config_get_bool val "${section}" RootPasswordAuth 1 [ "${val}" -eq 0 ] && append args "-g" # F) root login config_get_bool val "${section}" RootLogin 1 [ "${val}" -eq 0 ] && append args "-w" # G) host keys config_get val "${section}" rsakeyfile [ -f "${val}" ] && append args "-r ${val}" config_get val "${section}" dsskeyfile [ -f "${val}" ] && append args "-d ${val}" # H) enable ssh session login #config_get_bool val "${section}" RemoteSSH 0 #[ "${val}" -eq 1 ] && append args "-L" # I) linux account login config_get_bool val "${section}" SysAccountLogin 1 [ "${val}" -eq 0 ] && append args "-C" # 6)Disable ipv6 socket config_get_bool val "${section}" DisableIpv6 0 [ "${val}" -eq 1 ] && append args "-6" #enable allowblankpass append args "-B" # execute program and return its exit code [ "${verbosed}" -ne 0 ] && echo "${initscript}: section ${section} starting ${PROG} ${args}" SERVICE_PID_FILE="$pid_file" service_start ${PROG} ${args} } keygen() { local rsa2048_enable=$(uci get system.system.rsa2048_enable) local bits=1024 if [ "$rsa2048_enable" == "true" ]; then bits=2048 fi for keytype in rsa; do # check for keys key=dropbear/dropbear_${keytype}_host_key [ -f /tmp/$key -o -s /etc/$key ] || { # generate missing keys # mkdir -p /tmp/dropbear mkdir -p /tmp/dropbear/succ_cli [ -x /usr/bin/dropbearkey ] && { /usr/bin/dropbearkey -t $keytype -f /tmp/$key -s $bits 2>&- >&- && exec /etc/rc.common "$initscript" start } & #exit 0 } done lock /tmp/.switch2jffs mkdir -p /etc/dropbear mv /tmp/dropbear/dropbear_rsa_host_key /etc/dropbear/ lock -u /tmp/.switch2jffs chown root /etc/dropbear chmod 0700 /etc/dropbear } guest_portal() { if [ ! -d "/tmp/guest_portal" ]; then mkdir -p /tmp/guest_portal mount -t tmpfs -o rw,noatime,size=2m,mode=755 none /tmp/guest_portal chown admin:1000 /tmp/guest_portal fi } keygen_ecdsa() { for keytype in ecdsa; do # check for keys key=dropbear/dropbear_${keytype}_host_key [ -f /tmp/$key -o -s /etc/$key ] || { # generate missing keys mkdir -p /tmp/dropbear [ -x /usr/bin/dropbearkey ] && { /usr/bin/dropbearkey -t $keytype -f /tmp/$key -s 521 2>&- >&- && exec /etc/rc.common "$initscript" start } & #exit 0 } done lock /tmp/.switch2jffs mkdir -p /etc/dropbear mv /tmp/dropbear/dropbear_ecdsa_host_key /etc/dropbear/ lock -u /tmp/.switch2jffs chown root /etc/dropbear chmod 0700 /etc/dropbear } start() { [ -s /etc/dropbear/dropbear_rsa_host_key ] || keygen extern_partition=$(uci get profile.@backup_restore[0].extern_partition -c "/etc/profile.d" -q) for img_type in $extern_partition; do if [ "$img_type" = "portal-logo" ] || [ "$img_type" = "portal-back" ]; then guest_portal break fi done [ -s /etc/dropbear/dropbear_ecdsa_host_key ] || keygen_ecdsa local algo_version=`uci get dropbear.dropbear.AlgoVersion` [ -z "$algo_version" ] && { algo_version=0 } if [ ! -f $PATH_SSH_ALGO_VERSION ]; then touch $PATH_SSH_ALGO_VERSION echo $algo_version > $PATH_SSH_ALGO_VERSION fi # disable all client access dropbear default [ -f "/sbin/knock_functions.sh" ] && /sbin/knock_functions.sh start include /lib/network scan_interfaces config_load "${NAME}" config_foreach dropbear_start dropbear } stop() { local pid_file pid_files pid_files=`ls /var/run/${NAME}.*.pid 2>/dev/null` [ -z "$pid_files" ] && return 1 for pid_file in $pid_files; do SERVICE_PID_FILE="$pid_file" service_stop ${PROG} && { rm -f ${pid_file} } done } killclients() { local ignore='' local server local pid # if this script is run from inside a client session, then ignore that session pid="$$" while [ "${pid}" -ne 0 ] do # get parent process id pid=`cut -d ' ' -f 4 "/proc/${pid}/stat"` [ "${pid}" -eq 0 ] && break # check if client connection grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" && { append ignore "${pid}" break } done # get all server pids that should be ignored for server in `cat /var/run/${NAME}.*.pid` do append ignore "${server}" done # get all running pids and kill client connections local skip for pid in `pidof "${NAME}"` do # check if correct program, otherwise process next pid grep -F -q -e "${PROG}" "/proc/${pid}/cmdline" || { continue } # check if pid should be ignored (servers, ourself) skip=0 for server in ${ignore} do if [ "${pid}" == "${server}" ] then skip=1 break fi done [ "${skip}" -ne 0 ] && continue # kill process echo "${initscript}: Killing ${pid}..." kill -KILL ${pid} done } 如何修改上述文件,让外部dropbear远程连接该样机时要在键盘区输入密码验证
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值