One minute
UCI 和 ubus 学习笔记
UCI 和 ubus 都是 Openwrt 项目提供的软件,UCI 提供了统一的配置文件格式和读写工具,ubus 提供了基于 Unix Socket 的进程间通信框架。两个软件都可以方便的移植到其他 Linux 系统,以 Ubuntu 系统为例,首先要安装一些依赖的软件:
apt-get install pkg-config
apt-get install lua5.1
apt-get install liblua5.1-dev
apt-get install cmake
apt-get install libjson-c-dev
然后要编译安装 libubox :
git clone https://git.openwrt.org/project/libubox.git
cd libubox
cmake .
make
make install
编译时可能出现找不到头文件的错误:
/root/libubox/lua/uloop.c:21:17: fatal error: lua.h: No such file or directory
这是路径问题,因为 lua 的头文件在 /usr/include/lua5.1
目录下,所有修改 uloop.c 文件,为 lua.h 等头文件前面加上 lua5.1/
路径:
#include <lua5.1/lua.h>
#include <lua5.1/lualib.h>
#include <lua5.1/lauxlib.h>
1. UCI
编译安装 uci
git clone https://git.openwrt.org/project/uci.git
cd uci
cmake .
make
make install
安装完成后执行一次 sudo ldconfig -v
,否则可能找不到刚安装的共享库。
UCI 的配置文件格式是 config -> section -> option
这样的层级关系,它提供了 uci 命令读写配置文件,语法可以直接用 uci -h
获得。我们可以新建 /etc/config/system
文件,然后做一次配置测试:
/etc/config$ uci add system info
cfg0184d1
/etc/config$ uci show
system.@info[0]=info
/etc/config$ uci rename system.@info[0]=time
/etc/config$ uci show
system.time=info
/etc/config$ uci set system.time.zone=As^C
/etc/config$ cat /etc/timezone
Asia/Chongqing
/etc/config$ uci set system.time.zone=Asia/Chongqing
/etc/config$ uci show
system.time=info
system.time.zone='Asia/Chongqing'
/etc/config$ uci commit system
/etc/config$ cat system
config info 'time'
option zone 'Asia/Chongqing'
2. ubus
ubus 是基于 unix socket 的进程间通信框架,包含守护进程、命令行工具和链接库,守护进程 ubusd 作为 socket server ,用户可用 lua 或者 C 语言的 API 实现 socket client ,client 和 server 之间用 json 格式进行通信,client 端的消息处理抽象处理对象(object)和方法(method)。ubus 通过对 socket 的封装,简化了进程间通信的步骤,只需按照固定模式调用 ubus 提供的API即可。有两种常见的应用场景:
- 服务器-客户端的形式,进程 A 注册一系列服务,进程 B 调用这些服务
- 订阅-通知的形式,进程 A 提供订阅服务,其他进程可用订阅或者退订这些服务,进程 A 可用向所有订阅者发布消息
编译安装 ubus
git clone https://git.openwrt.org/project/ubus.git
cd ubus
cmake .
make
make install
204 Words
2019-12-24 00:00 +0000
comments powered by Disqus