如何在SONiC上添加应用程序

首先

我认为很少有人只使用Windows、Mac、iOS、Android或其他各种操作系统上提供的标准应用程序。然而,在用于白盒交换机的操作系统SONiC上,大多数人估计会在其本身提供的功能范围内使用。

我认为还是有一些情况下想要集成这个功能。这次我会介绍一些方法。

启动后进行嵌入

由于SONiC是基于Debian构建的,因此可以使用apt-get和apt。如果您要使用Ansible等工具,只需要以下操作即可:sudo apt update。

admin@sonic:~$ sudo apt update
Get:1 https://download.docker.com/linux/debian bullseye InRelease [43.3 kB]
Get:2 http://debian-archive.trafficmanager.net/debian bullseye InRelease [116 kB]                 
Get:3 https://download.docker.com/linux/debian bullseye/stable amd64 Packages [18.3 kB]
Get:4 http://debian-archive.trafficmanager.net/debian bullseye-updates InRelease [44.1 kB]
Get:5 http://debian-archive.trafficmanager.net/debian bullseye-backports InRelease [49.0 kB]
Get:6 http://debian-archive.trafficmanager.net/debian-security bullseye-security InRelease [48.4 kB]
Get:7 http://debian-archive.trafficmanager.net/debian bullseye/contrib Sources [51.4 kB]
Get:8 http://debian-archive.trafficmanager.net/debian bullseye/non-free Sources [98.3 kB]
Get:9 http://debian-archive.trafficmanager.net/debian bullseye/main Sources [11.4 MB]
Get:10 http://debian-archive.trafficmanager.net/debian bullseye/non-free amd64 Packages [122 kB]
Get:11 http://debian-archive.trafficmanager.net/debian bullseye/contrib amd64 Packages [60.9 kB]
Get:12 http://debian-archive.trafficmanager.net/debian bullseye/main amd64 Packages [11.1 MB]
Get:13 http://debian-archive.trafficmanager.net/debian bullseye-updates/main Sources [4,812 B]
Get:14 http://debian-archive.trafficmanager.net/debian bullseye-updates/main amd64 Packages [14.6 kB]
Get:15 http://debian-archive.trafficmanager.net/debian bullseye-backports/contrib amd64 Packages [4,396 B]
Get:16 http://debian-archive.trafficmanager.net/debian bullseye-backports/non-free amd64 Packages [14.3 kB]
Get:17 http://debian-archive.trafficmanager.net/debian bullseye-backports/main amd64 Packages [371 kB]
Get:18 http://debian-archive.trafficmanager.net/debian-security bullseye-security/non-free Sources [558 B]
Get:19 http://debian-archive.trafficmanager.net/debian-security bullseye-security/main Sources [272 kB]
Get:20 http://debian-archive.trafficmanager.net/debian-security bullseye-security/main amd64 Packages [264 kB]
Get:21 http://debian-archive.trafficmanager.net/debian-security bullseye-security/non-free amd64 Packages [457 B]
Fetched 24.1 MB in 3s (8,513 kB/s)                    
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
10 packages can be upgraded. Run 'apt list --upgradable' to see them.
admin@sonic:~$ sudo apt install htop
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  lm-sensors lsof strace
The following NEW packages will be installed:
  htop
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 127 kB of archives.
After this operation, 328 kB of additional disk space will be used.
Get:1 http://debian-archive.trafficmanager.net/debian bullseye/main amd64 htop amd64 3.0.5-7 [127 kB]
Fetched 127 kB in 0s (283 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package htop.
(Reading database ... 38304 files and directories currently installed.)
Preparing to unpack .../htop_3.0.5-7_amd64.deb ...
Unpacking htop (3.0.5-7) ...
Setting up htop (3.0.5-7) ...
admin@sonic:~$ 

如果你安装了它,它就属于你了。无论是使用pip还是npm,都可以随意选择。

在构建过程中进行集成

二进制软件包模块。

刚才的 apt-get、以及 apt 安装可以在构建时执行。

要修改的文件是build_debian.sh。查看文件内部,可以看到以下部分。

## Pre-install the fundamental packages
## Note: gdisk is needed for sgdisk in install.sh
## Note: parted is needed for partprobe in install.sh
## Note: ca-certificates is needed for easy_install
## Note: don't install python-apt by pip, older than Debian repo one
## Note: fdisk and gpg are needed by fwutil
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install      \
    file                    \
    ifmetric                \
    iproute2                \
    bridge-utils            \
    isc-dhcp-client         \
    sudo                    \
    vim                     \
    tcpdump                 \

只需要在这里添加要安装的包的名称并构建即可。例如,如果要集成keepalived,可以按照以下方式进行。(虽然这里只是举例说明,但是只是简单地集成keepalived可能无法正常运行,请注意)

diff --git a/build_debian.sh b/build_debian.sh
index 01a10d78d..a68f7d7a2 100755
--- a/build_debian.sh
+++ b/build_debian.sh
@@ -330,6 +330,7 @@ fi
 ## Note: don't install python-apt by pip, older than Debian repo one
 ## Note: fdisk and gpg are needed by fwutil
 sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install      \
+    keepalived              \
     file                    \
     ifmetric                \
     iproute2                \

Python包编程

同样地,只需要在build_debian.sh中进行编写,但是需要一些咒语。以下是将j2cli(Jinja2模板引擎的CLI)集成进来的差异。

diff --git a/build_debian.sh b/build_debian.sh
index 01a10d78d..a68f7d7a2 100755
--- a/build_debian.sh
+++ b/build_debian.sh
@@ -526,6 +526,9 @@ sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install 'docke
 # Install scapy
 sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install 'scapy==2.4.4'
 
+# Install j2cli
+sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install 'j2cli'
+
 ## Note: keep pip installed for maintainance purpose
 
 # Install GCC, needed for building/installing some Python packages

本人开发的应用程序篇

在这里,您当然可以添加自己开发的应用程序。
这里举个例子,假设这是一个不存在的虚构应用程序,来阐述一个复杂的案例。

    • アプリケーション名 etcdconfig

機能: etcdからJSON形式の設定を読み込み、SONiCの設定に反映させる
使用言語: Differential Datalog (DDlog)

如果要说这个有什么地方复杂的话,

    • DDlogコンパイラはSONiCのビルド環境に用意されていない

 

    • DDlogコンパイラの出力はRust言語のソースコードで、これをコンパイルする必要がある

 

    つまりRustコンパイラも必要だが、これもSONiCのビルド環境に用意されていない

换句话说,就是需要对构建环境进行调整。

我希望能够像在SONiC中一样,以Debian包的形式制作出来,因为我觉得这样更加方便。

以下是必要的工作任务清单:

    1. 准备etcdconfig本体(源代码和构建规则)

 

    1. 添加etcdconfigDebian软件包的构建规则

 

    1. 将etcdconfigDebian软件包作为构建目标

 

    1. 将etcdconfigDebian软件包集成到SONiC中

 

    1. 将etcd-client集成到SONiC中

 

    1. 将DDlog编译器集成到构建环境中

 

    将Rust编译器集成到构建环境中

非常麻煩啊。如果是用簡單的C++編寫的程序,從5開始後面的都是不必要的。
那麼我們現在就試試看。

准备etcdconfig的本体(源代码和构建规则)。

本文假设将整套文件放在 src/sonic-etcdconfig/ 目录下。
文件的主体是 src/sonic-etcdconfig/sonic_config.dl。

以下是Makefile和debian/*的样例。

diff --git a/src/sonic-etcdconfig/Makefile b/src/sonic-etcdconfig/Makefile
new file mode 100644
index 000000000..84bfe8ea9
--- /dev/null
+++ b/src/sonic-etcdconfig/Makefile
@@ -0,0 +1,30 @@
+INSTALL := /usr/bin/install
+ETCDCONFIGDIR=$(abspath .)
+CONFIGCLIDIR := $(ETCDCONFIGDIR)/sonic_config_ddlog/target/release
+PROG := sonic_config_cli
+TARGET := $(CONFIGCLIDIR)/$(PROG)
+DDLOG := /usr/local/ddlog/bin/ddlog -L /usr/local/ddlog/lib
+
+ETCDCONFIG_VERSION = 1.0.0-0
+MAIN_TARGET = etcdconfig_$(ETCDCONFIG_VERSION)_$(CONFIGURED_ARCH).deb
+
+all: $(TARGET)
+
+$(TARGET): sonic_config.dl
+       $(DDLOG) -i $<
+       cd sonic_config_ddlog; $(HOME)/.cargo/bin/cargo build --release; cd -
+
+check:
+
+clean:
+       $(RM) -r $(ETCDCONFIGDIR)/sonic_config_ddlog
+
+install:
+       $(INSTALL) -D -m 0755 $(TARGET) $(DESTDIR)/usr/bin/$(TARGET)
+
+uninstall:
+       $(RM) $(DESTDIR)/usr/bin/$(PROG)
+
+$(addprefix $(DEST)/, $(MAIN_TARGET)): $(DEST)/% :
+       dpkg-buildpackage -us -uc -b -j$(SONIC_CONFIG_MAKE_JOBS) --admindir $(SONIC_DPKG_ADMINDIR)
+       mv $* $(DEST)/
diff --git a/src/sonic-etcdconfig/debian/changelog b/src/sonic-etcdconfig/debian/changelog
new file mode 100644
index 000000000..ea08a73b0
--- /dev/null
+++ b/src/sonic-etcdconfig/debian/changelog
@@ -0,0 +1,5 @@
+etcdconfig (1.0.0-0) unstable; urgency=low
+
+  * initial release
+
+ -- Masaru OKI <*****@*********>  Mon, 5 Jan 2021 18:49:00 +0900
diff --git a/src/sonic-etcdconfig/debian/compat b/src/sonic-etcdconfig/debian/compat
new file mode 100644
index 000000000..f599e28b8
--- /dev/null
+++ b/src/sonic-etcdconfig/debian/compat
@@ -0,0 +1 @@
+10
diff --git a/src/sonic-etcdconfig/debian/control b/src/sonic-etcdconfig/debian/control
new file mode 100644
index 000000000..34374fc69
--- /dev/null
+++ b/src/sonic-ecdconfig/debian/control
@@ -0,0 +1,10 @@
+Source: etcdconfig
+Maintainer: Masaru OKI <*****@*********>
+Section: net
+Priority: optional
+Build-Depends: dh-exec (>=0.3), debhelper (>= 9)
+Standards-Version: 1.0.0
+
+Package: etcdconfig
+Architecture: amd64
+Description: This package contains Distiributed Configuration Service for SONiC.
diff --git a/src/sonic-etcdconfig/debian/rules b/src/sonic-etcdconfig/debian/rules
new file mode 100755
index 000000000..dd72c9a7b
--- /dev/null
+++ b/src/sonic-etcdconfig/debian/rules
@@ -0,0 +1,7 @@
+#!/usr/bin/make -f
+# See debhelper(7) (uncomment to enable)
+# output every command that modifies files on the build system.
+#export DH_VERBOSE = 1
+
+%:
+       dh $@ 

添加etcdconfigDebian软件包的构建规则

diff --git a/rules/etcdconfig.mk b/rules/etcdconfig.mk
new file mode 100644
index 000000000..ef51a6a15
--- /dev/null
+++ b/rules/etcdconfig.mk
@@ -0,0 +1,10 @@
+# etcdconfig package
+
+ETCDCONFIG_VERSION = 1.0.0-0
+
+export ETCDCONFIG_VERSION
+export ETCDCONFIG
+
+ETCDCONFIG = etcdconfig_$(ETCDCONFIG_VERSION)_$(CONFIGURED_ARCH).deb
+$(ETCDCONFIG)_SRC_PATH = $(SRC_PATH)/sonic-etcdconfig
+SONIC_DPKG_DEBS += $(ETCDCONFIG)

构建目标为etcdconfigDebian软件包。

diff --git a/slave.mk b/slave.mk
index 54a01aaa1..296f15e67 100644
--- a/slave.mk
+++ b/slave.mk
@@ -1122,6 +1122,7 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
                 $(SONIC_DEVICE_DATA) \
                 $(IFUPDOWN2) \
                 $(KDUMP_TOOLS) \
+                $(ETCDCONFIG) \
                 $(NTP) \
                 $(LIBPAM_RADIUS) \
                 $(LIBNSS_RADIUS) \

将etcdconfigDebian软件包集成到SONiC中。

diff --git a/files/build_templates/sonic_debian_extension.j2 b/files/build_templates/sonic_debian_extension.j2
index c5e76d3c4..41b356365 100644
--- a/files/build_templates/sonic_debian_extension.j2
+++ b/files/build_templates/sonic_debian_extension.j2
@@ -914,6 +914,9 @@ sudo cp {{src}} $FILESYSTEM_ROOT/{{dst}}
 {% endif -%}
 {% endfor -%}
 
+# Install etcdconfig
+sudo dpkg --root=$FILESYSTEM_ROOT -i $debs_path/etcdconfig_*.deb
+
 {% if sonic_asic_platform == "mellanox" %}
 sudo mkdir -p $FILESYSTEM_ROOT/etc/mlnx/
 sudo cp $files_path/$MLNX_SPC_FW_FILE $FILESYSTEM_ROOT/etc/mlnx/fw-SPC.mfa

将etcd客户端集成到SONiC中

etcd的客户端将使用现有的版本。这是之前介绍的二进制软件包的内嵌。

diff --git a/build_debian.sh b/build_debian.sh
index 01a10d78d..705076017 100755
--- a/build_debian.sh
+++ b/build_debian.sh
@@ -355,6 +355,7 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
     iptables-persistent     \
     ebtables                \
     logrotate               \
+    etcd-client             \
     curl                    \
     kexec-tools             \
     less                    \

将DDlog编译器集成到构建环境中。

我从Github获取发布二进制文件。由于只有amd64架构可用,所以附加了条件,但是我刚才意识到其他部分没有任何条件。非常抱歉不完整。

diff --git a/sonic-slave-bullseye/Dockerfile.j2 b/sonic-slave-bullseye/Dockerfile.j2
index 296905787..7ae7bbf27 100644
--- a/sonic-slave-bullseye/Dockerfile.j2
+++ b/sonic-slave-bullseye/Dockerfile.j2
@@ -610,6 +610,15 @@ RUN apt-get install -y docker-ce=5:20.10.14~3-0~debian-bullseye docker-ce-cli=5:
 RUN echo "DOCKER_OPTS=\"--experimental --storage-driver=vfs {{ DOCKER_EXTRA_OPTS }}\"" >> /etc/default/docker
 RUN update-alternatives --set iptables /usr/sbin/iptables-legacy
 
+# Install differential datalog
+{% if CONFIGURED_ARCH == "amd64" -%}
+RUN wget https://github.com/vmware/differential-datalog/releases/download/v0.35.0/ddlog-v0.35.0-20210120055331-linux.tar.gz \
+ && tar -C /usr/local -xzf ddlog-v0.35.0-20210120055331-linux.tar.gz \
+ && echo 'export PATH=$PATH:/usr/local/ddlog/bin' >> /etc/bash.bashrc \
+ && echo 'export DDLOG_HOME=/usr/local/ddlog' >> /etc/bash.bashrc \
+ && rm ddlog-v0.35.0-20210120055331-linux.tar.gz
+{% endif -%}
+
 # Install m2crypto package, needed by SWI tools
 RUN pip3 install m2crypto==0.36.0
 

将 Rust 编译器集成到构建环境中

这基本上是推荐的官方方法,但有些特殊。
通常情况下,Rust编译器会安装在一般用户环境中,所以我们稍作调整,以适应执行构建的用户环境。

index 2eb7ef18b..02d80e1e7 100644
--- a/sonic-slave-bullseye/Dockerfile.user.j2
+++ b/sonic-slave-bullseye/Dockerfile.user.j2
@@ -31,4 +31,7 @@ RUN chmod go= /var/$user/.ssh -R
 # Add user to sudoers
 RUN echo "$user ALL=(ALL) NOPASSWD:ALL" >>/etc/sudoers
 
+# Install Rust
+RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | su $user -c "sh -s -- -y" && echo 'export PATH=$PATH:$HOME/.cargo/bin' >> /etc/bash.bashrc
+
 USER $user

这就是以上的内容。

总之

要启动嵌入的二进制文件作为服务,要将其嵌入到现有的docker容器中,要增加docker容器,还有很多需要补充的内容,但是也不能过分膨胀,所以我打算就这么多先。

如果您希望为SONiC添加功能,希望这可以作为参考。

广告
将在 10 秒后关闭
bannerAds