[转载]OpenWRT UCI API的使用

本文介绍 OpenWRT 中 UCI (通用配置接口) 的使用方法,包括如何加载和解析配置文件、遍历配置项及修改配置值等关键步骤。
 

OpenWRT UCI API的使用

分类: OpenWRT C   3551人阅读  评论(4)  收藏  举报

目录(?)[+]


原文:http://blog.youkuaiyun.com/bywayboy/article/details/20866287


OpenWRT UCI API的使用

UCI 是 OpenWRT为实现配置集中化而引入的一个软件包, 通过修改UCI,可以实现对OpenWRT的绝对部分配置的修改.LUCI(OpenWRT 的WEB配置界面)也是通过读UCI配置文件的操作来实现用户对路由的配置的。通过掌握UCI的API的使用,可以方便地将您的软件的配置接口集成到LUCI中.


LUCI配置文件简介



LUCI的配置文件一般存储在 /etc/config目录下。比如网络配置文件则是 /etc/config/network 无线的配置文件是 /etc/config/wireless. 跟多配置文件的含义参考官方 WIKI


基本概念



UCI上下文: struct uci_context *
包(Package): 一个包对应一个UCI格式的文件.类型是 struct uci_package *
节(Section): 一个配置文件的节点. 类型是 struct uci_list *
值(Value):一个节下面可能包含多个值 一个值具有一个名字.
UCI配置文件的基本操作.


首先您需要引入头文件



[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <unistd.h>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <uci.h>  
  5.   
  6.   
  7. static struct uci_context * ctx = NULL; //定义一个UCI上下文的静态变量.  
  8. /********************************************* 
  9. *   载入配置文件,并遍历Section. 
  10. */  
  11. bool load_config()  
  12. {  
  13.     struct uci_package * pkg = NULL;  
  14.     struct uci_element *e;  
  15.   
  16.   
  17.     ctx = uci_alloc_context(); // 申请一个UCI上下文.  
  18.     if (UCI_OK != uci_load(ctx, UCI_CONFIG_FILE, &pkg))  
  19.         goto cleanup; //如果打开UCI文件失败,则跳到末尾 清理 UCI 上下文.  
  20.   
  21.   
  22.     /*遍历UCI的每一个节*/  
  23.     uci_foreach_element(&pkg->sections, e)  
  24.     {  
  25.         struct uci_section *s = uci_to_section(e);  
  26.         // 将一个 element 转换为 section类型, 如果节点有名字,则 s->anonymous 为 false.  
  27.         // 此时通过 s->e->name 来获取.  
  28.         // 此时 您可以通过 uci_lookup_option()来获取 当前节下的一个值.  
  29.         if (NULL != (value = uci_lookup_option_string(ctx, s, "ipaddr")))  
  30.         {  
  31.             ip = strdup(value) //如果您想持有该变量值,一定要拷贝一份。当 pkg销毁后value的内存会被释放。  
  32.         }  
  33.         // 如果您不确定是 string类型 可以先使用 uci_lookup_option() 函数得到Option 然后再判断.  
  34.         // Option 的类型有 UCI_TYPE_STRING 和 UCI_TYPE_LIST 两种.  
  35.   
  36.   
  37.     }  
  38.     uci_unload(ctx, pkg); // 释放 pkg   
  39. cleanup:  
  40.     uci_free_context(ctx);  
  41.     ctx = NULL;  
  42. }  


遍历一个UCI_TYPE_LIST 类型.



加入现在有一个如下的配置文件:


[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. config  "server" "webserver"  
  2.     list    "index" "index.html"  
  3.     list    "index" "index.php"  
  4.     list    "index" "default.html"  


代码片:


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // s 为 section.  
  2. struct uci_option * o = uci_lookup_option(ctx, s, "index");  
  3. if ((NULL != o) && (UCI_TYPE_LIST == o->type)) //o存在 且 类型是 UCI_TYPE_LIST则可以继续.  
  4. {  
  5.     struct uci_element *e;  
  6.     uci_foreach_element(&o->v.list, e)  
  7.     {  
  8.         //这里会循环遍历 list  
  9.         // e->name 的值依次是 index.html, index.php, default.html  
  10.     }  
  11. }  


写配置



UCI提供了一个简洁的办法来操作配置信息,例如有一个配置文件


[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #文件名: testconfig  
  2. config  'servver'  
  3.     option  'value' '123' # 我们想修改 'value' 的值为 '456'  


代码如下:


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. struct uci_context * ctx = uci_alloc_context(); //申请上下文  
  2. struct uci_ptr ptr ={  
  3.     .package = "config",  
  4.     .section = "servver",  
  5.     .option = "value",  
  6.     .value = "256",  
  7. };  
  8. uci_set(_ctx,&ptr); //写入配置  
  9. uci_commit(_ctx, &ptr.p, false); //提交保存更改  
  10. uci_unload(_ctx,ptr.p); //卸载包  
  11.   
  12.   
  13. uci_free_context(ctx); //释放上下文  


依照上面的例子,我们可以举一反三, uci_ptr 用来指定信息.而是用uci_set则是写入信息.同类的函数有如下几个: 针对list的操作:
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. uci_add_list()  // 添加一个list 值  
  2. uci_del_list()  // 删除一个list 值  
  3. uci_delete()    // 删除一个option值  
OpenWrt 系统中,**UCI(Unified Configuration Interface)** 是用于管理系统配置的核心组件。它提供了一种统一、简洁的方式来配置网络、无线、系统服务等各项参数。 --- ### 一、UCI 的基本概念 1. **作用**: - 统一管理配置文件。 - 提供命令行工具 `uci` 供用户操作配置。 - 配置更改后可立即生效或重启后生效。 2. **配置文件位置**: - 主配置文件存放在 `/etc/config/` 目录下。 - 常见配置文件包括: - `/etc/config/network`:网络配置 - `/etc/config/wireless`:无线配置 - `/etc/config/dhcp`:DHCP 服务配置 - `/etc/config/system`:系统设置 - `/etc/config/firewall`:防火墙规则 --- ### 二、UCI 命令详解 `uci` 是操作配置的核心命令,支持增删改查等操作。 #### 常用命令格式: ```bash uci [操作] [配置项路径] [值] ``` #### 操作类型: | 操作 | 说明 | |------------|------------------------------| | `get` | 获取配置项的值 | | `set` | 设置配置项的值 | | `add` | 添加一个新节(section) | | `delete` | 删除一个节或配置项 | | `commit` | 提交更改,使配置永久生效 | | `revert` | 撤销未提交的更改 | | `show` | 显示配置项内容 | #### 示例: ```bash # 查看无线配置 uci show wireless # 获取无线设备 radio0 的类型 uci get wireless.radio0.type # 设置无线设备 radio0 的通道为 6 uci set wireless.radio0.channel=6 # 添加一个新的无线网络配置 uci add wireless wifi-iface # 提交更改 uci commit wireless # 重启无线服务使配置生效 wifi reload ``` --- ### 三、UCI 配置结构说明 UCI 配置由 **配置文件 → 节(section)→ 选项(option)** 组成。 #### 示例:`/etc/config/network` ```bash config interface 'loopback' option ifname 'lo' option proto 'static' option ipaddr '127.0.0.1' option netmask '255.0.0.0' config interface 'lan' option ifname 'eth0' option proto 'dhcp' ``` - `config interface 'loopback'`:定义一个名为 `loopback` 的接口节。 - `option ifname 'lo'`:设置该节的选项。 --- ### 四、常用配置文件示例 #### 1. 网络配置(network) ```bash config interface 'lan' option proto 'static' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option gateway '192.168.1.254' option dns '8.8.8.8' ``` #### 2. 无线配置(wireless) ```bash config wifi-device 'radio0' option type 'mac80211' option channel '6' option hwmode '11g' option path 'platform/ar934x/wmac' option htmode 'HT20' option disabled 0 config wifi-iface option device 'radio0' option network 'lan' option mode 'ap' option ssid 'OpenWrt' option encryption 'psk2' option key 'password123' ``` --- ### 五、注意事项 1. **修改后需提交**:`uci commit` 2. **服务需重启**:如网络、无线等服务需手动重启(如 `wifi reload`、`ifdown lan && ifup lan`) 3. **备份配置**:可使用 `uci export > config_backup.txt` 备份配置 4. **恢复配置**:使用 `uci import < config_backup.txt` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值