大师网-带你快速走向大师之路 解决你在学习过程中的疑惑,带你快速进入大师之门。节省时间,提升效率

7-Openwrt uci system

openwrt的很多配置文件都是通过uci接口来进行查看修改的,虽然我们可以通过直接打开config/下面的配置文件进行修改,但是有个统一的接口为何不用呢,又方便又不会错。

https://wiki.openwrt.org/doc/uci

config_load system
通过. /lib/functions.sh进行包含内容

config_load() {
    [ -n "$IPKG_INSTROOT" ] && return 0
    uci_load "$@"
}

调用uci_load函数,该函数位于lib/config/uci.sh中

uci_load() {
    local PACKAGE="$1"
    local DATA
    local RET
    local VAR

    _C=0
    if [ -z "$CONFIG_APPEND" ]; then
        for VAR in $CONFIG_LIST_STATE; do
            export ${NO_EXPORT:+-n} CONFIG_${VAR}=
            export ${NO_EXPORT:+-n} CONFIG_${VAR}_LENGTH=
        done
        export ${NO_EXPORT:+-n} CONFIG_LIST_STATE=
        export ${NO_EXPORT:+-n} CONFIG_SECTIONS=
        export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=0
        export ${NO_EXPORT:+-n} CONFIG_SECTION=
    fi

    DATA="$(/sbin/uci ${UCI_CONFIG_DIR:+-c $UCI_CONFIG_DIR} ${LOAD_STATE:+-P /var/state} -S -n export "$PACKAGE" 2>/dev/null)"
    RET="$?"
    [ "$RET" != 0 -o -z "$DATA" ] || eval "$DATA"
    unset DATA

    ${CONFIG_SECTION:+config_cb}
    return "$RET"
}

uci.sh里面都是uci的处理函数,如:
uci_set_default(),uci_set(),uci_get()等

最底层调用的都是sbin/uci接口

uci set

uci set network.ppp2=interface
uci set network.ppp2.ifname=ppp2
uci set network.ppp2.proto=3g
uci set network.ppp2.username=$user
uci set network.ppp2.password=$password
uci set network.ppp2.apn=$apn
uci set network.ppp2.auto=1
uci set network.ppp2.device=/dev/ttyUSB3
uci commit

ifup ppp2
uci set wireless.@wifi-device[0].disabled=0
uci commit wireless 
wifi down && wifi up