2013年8月17日 星期六

OpenWrt Unified Configuration Interface

Unified Configuration Interface (UCI) 是 OpenWrt 的設定環境,大部分的設定都使用 UCI,然而還有一些設定並不在 UCI

使用 UCI 的設定檔是文字檔,放在 /etc/config/ 目錄下,可以用文字編輯器編譯,也可以透過 uci 指令存取,或者其它程式 API 存取,例如 shell script 的 /lib/config/uci.sh、C library /lib/libuci.so、lua library /usr/lib/lua/uci.so 等,適合用於資源有限不好執行一個資料庫的環境。

uci 設定檔有三層架構
  1. <config>:/etc/config/ 下的設定檔檔名
  2. <section>、<section-type>:每個設定檔內容分成許多 config 開始的 section,section 有 <section-type>。格式是:
    config <section-type> [<section>]
    <section> 是 section 的名稱,可有可無。沒有 section 名稱時用 @<section-type>[index] 的方式存取。
  3. <option>、<value>:每個 section 有 option 或 list。
uci 設定檔格式
config section-type section-name
config section-type
option option-name option-value
list option-name option-value
# 開始為註解註界
config 表示一個 section 開始,type 是必要的,讓程式決定如何應用。可以沒有 name,稱為 anonymous section。
option 提供選項 name 到 value 的對應,list 單一 name 可以有多個 value。

檔案範例 /etc/config/foo
# cat /etc/config/foo

config bar first
        option name 'Mr. First'
config bar
        option name 'Mr. Second'
config bar third
        option name 'Mr. Third'
        list kid aaa
        list kid bbb
首先這裡存在 <config> 檔 foo,內容分成許多 config 開頭的 section,每個 section 有 option 或 list。範例裡 <section-type> 都是 bar。每個 option 定義一對<option> 及 <value>,而 list 讓一個 <option> 有許多 <value>。

<config>、<section>、<section-type>、<option>、<value>

uci 指令:
# uci
Usage: uci [<options>] <command> [<arguments>]

Commands:
        batch  
        export     [<config>]
        import     [<config>]
        changes    [<config>]
        commit     [<config>]
        add        <config> <section-type>
        add_list   <config>.<section>.<option>=<string>
        show       [<config>[.<section>[.<option>]]]
        get        <config>.<section>[.<option>]
        set        <config>.<section>[.<option>]=<value>
        delete     <config>[.<section[.<option>]]
        rename     <config>.<section>[.<option>]=<name>
        revert     <config>[.<section>[.<option>]]
        reorder    <config>.<section>=<position>

Options:
        -c <path>  set the search path for config files (default: /etc/config)
        -d <str>   set the delimiter for list values in uci show
         -f <file>  use <file> as input instead of stdin
        -L         do not load any plugins
        -m         when importing, merge data into an existing package
        -n         name unnamed sections on export (default)
        -N         don't name unnamed sections
        -p <path>  add a search path for config change files
        -P <path>  add a search path for config change files and use as default
        -q         quiet mode (don't print error messages)
        -s         force strict mode (stop on parser errors, default)
        -S         disable strict mode
        -X         do not use extended syntax on 'show'

透過 <config>.<section>.<option> 存取 <value>,而透過 <config>.<section> 取到的是 <section-type>。<section> 可用特定 <section-type> 的 index 表示法取代,index 從 0 開始,第一個是 @<section-type>[0]。也可以從後面數,此時 index 是負的,從 -1 開始,最後一個是 @<section-type>[-1]。
# uci get foo.first
bar
# uci get foo.first.name
Mr. First
# uci get foo.@bar[0].name
Mr. First
# uci get foo.@bar[1].name
Mr. Second
# uci get foo.@bar[-1].name
Mr. Third
# uci get foo.third.kid
aaa bbb
要注意的是 uci show [<config>] 時,會顯示所有的 <section-type> 及 <value>,<section-type> 的 index 不計有 <section> 的,所以可能會有所不同。
# uci show foo
foo.first=bar
foo.first.name=Mr. First
foo.@bar[0]=bar
foo.@bar[0].name=Mr. Second
foo.third=bar
foo.third.name=Mr. Third
foo.third.kid=aaa bbb


新增
uci add <config> <section-type>
uci set <config>.<section>=<section-type>

touch /etc/config/pbx
uci set pbx.201=user
uci set pbx.201.CFUC=202
uci commit pbx

參考:
  1. http://wiki.openwrt.org/doc/uci
  2. http://wiki.openwrt.org/doc/techref/uci
  3. Using UCI as stand alone (使用範例)
  4.  https://openwrt.org/docs/guide-user/base-system/uci
待續:
  • 設定/新增
    • uci  set <config>.<section>[.<option>]=<value>
    • uci add <config> <section-type>
    • uci add_list <config>.<section>.<option>=<string>
    • uci commit [<config>]
    • uci revert     <config>[.<section>[.<option>]]
  • 其它
    • uci batch
    • uci export [<config>]
    • import     [<config>]
    • changes    [<config>]
    • delete     <config>[.<section[.<option>]]
    • rename     <config>.<section>[.<option>]=<name>
    • reorder    <config>.<section>=<position>

沒有留言:

張貼留言

SIP header Via

所有 SIP 訊息 都要有 Via,縮寫 v。一開始的 UAC 和後續途經的每個 proxy 都會疊加一個 Via 放傳送的位址,依序作為回應的路徑。 格式 sent-protocol sent-by [ ;branch= branch ][ ; 參數 ...] s...