环境
Ubuntu 20.04.1 (内核版本5.13.0-39-generic)
更新并安装必要的软件
# apt update
# apt upgrade
# apt install vim cmake
编译安装libyang
libyang依赖于libpcre2,因此我们需要先安装libpcre2
安装依赖libpcre2
# apt install libpcre2-dev
下载libyang并解压
# cd ~
# wget https://github.com/CESNET/libyang/archive/refs/tags/v2.0.164.tar.gz -O libyang-2.0.164.tar.gz
# tar -xvzf libyang-2.0.164.tar.gz
编译安装
# cd libyang-2.0.164
# mkdir build; cd build
# cmake ..
# make
# make install
P.S. 如果cmake的过程中出现错误,解决问题后要清空build文件夹再重新cmake
编译安装Sysrepo
下载Sysrepo并解压
# cd ~
# wget https://github.com/sysrepo/sysrepo/archive/refs/tags/v2.1.42.tar.gz -O sysrepo-2.1.42.tar.gz
# tar -xvzf sysrepo-2.1.42.tar.gz
编译安装
# cd sysrepo-2.1.42
# mkdir build; cd build
# cmake ..
# make
# make install
测试
首先需要修改.bashrc文件,指定环境变量LD_LIBRARY_PATH的值
# vim ~/.bashrc
在该文件最后加上一行export LD_LIBRARY_PATH=/usr/local/lib,完成之后重启一下系统
export LD_LIBRARY_PATH=/usr/local/lib
往Sysrepo数据库中导入测试需要用到的YANG Module(对应examples.yang文件)
# cd ~/sysrepo-2.1.42/examples
# sysrepoctl -i examples.yang
如果出现以下找不到libsysrepo.so.7的错误
sysrepoctl: error while loading shared libraries: libsysrepo.so.7: cannot open shared object file: No such file or directory
运行以下命令可以解决
# export LD_LIBRARY_PATH=/usr/local/lib
运行程序application_changes_example,该程序可以监听examples整个Module的变化
# cd ~/sysrepo-2.1.42/build/examples
# ./application_changes_example examples
此时可以看到以下输出,说明已经监听成功
Application will watch for changes in "examples".
========== READING RUNNING CONFIG: ==========
/examples:cont (container)
========== LISTENING FOR CHANGES ==========
打开一个新的Terminal(不妨称为Terminal2,原来的称为Terminal1),运行程序sr_set_items_example,该程序可以修改Model中指定路径(即xpath)对应的配置值
我们修改配置/examples:cont/l的值为hello
# ./sr_set_item_example /examples:cont/l hello
Application will set "/examples:cont/l" to "hello".
此时可以看到Terminal1中有以下输出,说明程序捕获到了这个配置值的变化
========== EVENT change CHANGES: ====================================
CREATED: /examples:cont/l = hello
========== END OF CHANGES =======================================
========== EVENT done CHANGES: ====================================
CREATED: /examples:cont/l = hello
========== END OF CHANGES =======================================
========== CONFIG HAS CHANGED, CURRENT RUNNING CONFIG: ==========
/examples:cont (container)
/examples:cont/l = hello
在Terminal2中运行sr_get_items_example程序也能查看该配置的值
# ./sr_get_items_example /examples:cont/l
Application will get "/examples:cont/l" from "running" datastore.
/examples:cont/l = hello
更多的测试用例可以参考:
https://github.com/sysrepo/sysrepo/tree/master/examples
参考链接
sysrepo: Introduction - Sysrepo官方文档
What is the difference between apt-get update and upgrade? - Ask Ubuntu
How to rename the downloaded file with wget? - Stack Overflow