Skip to content

Commit

Permalink
[OpenWrt] Build WasmEdge for OpenWrt (WasmEdge#1620)
Browse files Browse the repository at this point in the history
* [OpenWrt] Build WasmEdge for OpenWrt.
* [Docs] Fix the README-zh.md.
* [Docs] Add README-en.md.
* [CI] Add build_openwrt into build.yml
* [CI] Move the part of openwrt into build_for_openwrt.yml

Signed-off-by: wcy <[email protected]>
  • Loading branch information
O3Ol authored Jul 20, 2022
1 parent 4de15e9 commit 1f21045
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,4 @@ jobs:
with:
name: WasmEdge-${{ needs.get_version_v2.outputs.version }}-android_aarch64.tar.gz
path: build/WasmEdge-${{ needs.get_version_v2.outputs.version }}-Android.tar.gz

134 changes: 134 additions & 0 deletions .github/workflows/build_for_openwrt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
name: Build and Test WasmEdge on OpenWrt

concurrency:
group: build-openwrt-${{ github.head_ref }}
cancel-in-progress: true

on:
push:
branches:
- master
paths:
- ".github/workflows/build_for_openwrt.yml"
- "include/**"
- "lib/**"
- "plugins/**"
- "examples/**"
- "rpm/**"
- "test/**"
- "thirdparty/**"
- "tools/**"
- "CMakeLists.txt"
pull_request:
branches:
- master
- 'proposal/**'
paths:
- ".github/workflows/build_for_openwrt.yml"
- "include/**"
- "lib/**"
- "plugins/**"
- "examples/**"
- "rpm/**"
- "test/**"
- "thirdparty/**"
- "tools/**"
- "CMakeLists.txt"

jobs:
build_openwrt:
name: OpenWrt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install dependency
run: |
git clone https://github.com/openwrt/openwrt.git ../openwrt
cd ../openwrt
git checkout 9dc86d19628f55bfed2048c412c5b23bda328cff
- name: Space cleanup
env:
DEBIAN_FRONTEND: noninteractive
run: |
docker rmi `docker images -q`
sudo rm -rf /usr/share/dotnet /etc/mysql /etc/php /etc/apt/sources.list.d /usr/local/lib/android
sudo -E apt-get -y purge azure-cli ghc* zulu* hhvm llvm* firefox google* dotnet* powershell openjdk* adoptopenjdk* mysql* php* mongodb* dotnet* moby* snapd* || true
sudo -E apt-get update
sudo -E apt-get -y install build-essential asciidoc binutils bzip2 gawk gettext git libncurses5-dev libz-dev patch python3 unzip zlib1g-dev lib32gcc1 libc6-dev-i386 subversion flex uglifyjs gcc-multilib g++-multilib p7zip p7zip-full msmtp libssl-dev texinfo libglib2.0-dev xmlto qemu-utils upx libelf-dev autoconf automake libtool autopoint device-tree-compiler antlr3 gperf swig libtinfo5
df -h
- name: Update feeds
run: |
cd ../openwrt
./scripts/feeds update -a
./scripts/feeds install -a
- name: Make WasmEdge package
run: |
cd ../
mkdir -p ./openwrt/package/utils/WasmEdge
cp -r ./WasmEdge ./openwrt/package/utils/WasmEdge/src
cp ./WasmEdge/utils/openwrt/configuration/Makefile ./openwrt/package/utils/WasmEdge/
cd ./openwrt
ls ./
rm -f ./.config*
touch ./.config
cat >> .config <<EOF
CONFIG_TARGET_IMAGES_GZIP=y
CONFIG_TARGET_x86=y
CONFIG_TARGET_x86_64=y
CONFIG_TARGET_x86_64_DEVICE_generic=y
CONFIG_HAS_SUBTARGETS=y
CONFIG_HAS_DEVICES=y
CONFIG_TARGET_BOARD="x86"
CONFIG_TARGET_SUBTARGET="64"
CONFIG_TARGET_PROFILE="DEVICE_generic"
CONFIG_TARGET_ARCH_PACKAGES="x86_64"
CONFIG_PACKAGE_libpthread=y
CONFIG_PACKAGE_libstdcpp=y
CONFIG_PACKAGE_boost=y
CONFIG_PACKAGE_WasmEdge=y
EOF
sed -i 's/^[ \t]*//g' ./.config
make defconfig
make download -j8 || make download -j1 V=s
rm -rf $(find ./dl/ -size -1024c)
make -j$(nproc) || make -j1 V=s
- name: Pack OpenWrt images and WasmEdge ipk
run: |
cp -rf $(find ../openwrt/bin/ -type f -name "openwrt-x86-64-generic-rootfs.tar.gz") ./
- name: Upload artifact (v2)
uses: actions/upload-artifact@v2
with:
name: openwrt-rootfs
path: ./openwrt-x86-64-generic-rootfs.tar.gz

build_openwrt_docker:
name: OpenWrt-docker
runs-on: ubuntu-latest
needs: build_openwrt
steps:
- name: Download image
uses: actions/download-artifact@v3
with:
name: openwrt-rootfs
path: ./
- name: make_openwrt_docker
run: |
mkdir -p ./openwrt-docker
cp openwrt-x86-64-generic-rootfs.tar.gz ./openwrt-docker/
cd openwrt-docker
touch ./Dockerfile
cat >> ./Dockerfile <<EOF
FROM scratch
ADD openwrt-x86-64-generic-rootfs.tar.gz /
EXPOSE 22 80 443
ENTRYPOINT ["/sbin/init"]
EOF
ls ./
docker build -t openwrt .
docker run --name openwrt -d --privileged openwrt:latest /sbin/init
docker exec openwrt wasmedge -v
87 changes: 87 additions & 0 deletions docs/book/en/src/os/openwrt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Build and test WasmEdge on OpenWrt from source code

Please follow this tutorial to build and test WasmEdge in OpenWrt(x86_64) from source code.

> Currently, we only support the runtime for the interpreter mode.
## Prepare the Environment

### OpenWrt

First, we need to obtain the source code of OpenWrt and install the relevant tools to compile OpenWrt. The following commands take Debian / Ubuntu system as an example. For commands to install OpenWrt compilation tools in other host systems, see [Building OpenWrt System Settings](https://openwrt.org/docs/guide-developer/toolchain/install-buildsystem).

```bash
$ git clone https://github.com/openwrt/openwrt
$ sudo apt update
$ sudo apt install build-essential ccache ecj fastjar file g++ gawk \
gettext git java-propose-classpath libelf-dev libncurses5-dev \
libncursesw5-dev libssl-dev python python2.7-dev python3 unzip wget \
python-distutils-extra python3-setuptools python3-dev rsync subversion \
swig time xsltproc zlib1g-dev
```

Then, obtain all the latest package definitions of OpenWrt and install the symlinks for all obtained packages.

```bash
$ cd openwrt
$ ./scripts/feeds update -a
$ ./scripts/feeds install -a
```

## Build WasmEdge

### Get WasmEdge source code

```bash
$ git clone https://github.com/WasmEdge/WasmEdge.git
$ cd WasmEdge
```

### Run the build script

Run the build script `build_for_openwrt.sh` in WasmEdge source code, and input the path of the OpenWrt source code as parameter. This script will automatically add the WasmEdge into the packages list which will be built of OpenWrt, and build the OpenWrt firmware. The generated OpenWrt images are in the `openwrt/bin/targets/x86/64` folder.

```bash
$ ./utils/openwrt/build_for_openwrt.sh ~/openwrt
```

When running the build script, the OpenWrt configuration interface will appear. In this interface, we need to set `Target System` to x86, `Target Profile` to Generic x86/64, and find `WasmEdge` in the `Runtime` column and check it . Once set up, the script automatically builds WasmEdge and compiles the OpenWrt system.

## Test

### Deploy OpenWrt in VMware

In order to verify the availability of WasmEdge, we use a VMware virtual machine to install the compiled OpenWrt image. Before creating a virtual machine, we need to use the `QEMU` command to convert the OpenWrt image to vmdk format.

```
$ cd ~/openwrt/bin/targets/x86/64
$ sudo apt install qemu
$ gunzip openwrt-x86-64-generic-squashfs-combined.img.gz
$ qemu-img convert -f raw -O vmdk openwrt-x86-64-generic-squashfs-combined.img Openwrt.vmdk
```

After that, create a virtual machine in VMware and install the OpenWrt system.

### upload the test files

After setting the IP address of OpenWrt according to the gateway of the host, use `scp` to transfer the wasm file on the host to the OpenWrt system.

For example, we set the ip address of OpenWrt as 192.168.0.111, then we use the following commands to upload [hello.wasm](https://github.com/WasmEdge/WasmEdge/raw/master/examples/wasm/hello.wasm) and [add.wasm](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/add.wasm) these two test files to OpenWrt.

```
$ scp hello.wasm [email protected]:/
$ scp add.wasm [email protected]:/
```

### Test the wasmedge program

```
$ wasmedge hello.wasm second state
hello
second
state
$ wasmedge --reactor add.wasm add 2 2
4
```


87 changes: 87 additions & 0 deletions docs/book/zh/src/os/openwrt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 从源码在 OpenWrt 上构建和测试 WasmEdge

请参考这个教程来从源码在 OpenWrt(x86_64) 中构建和测试 WasmEdge.

> 目前,我们仅支持解释器模式的运行时。
## 环境准备

### OpenWrt 系统

首先,我们需要获取 OpenWrt 的源码,并安装相关的编译工具。以下命令以 Debian / Ubuntu 系统为例,其他系统安装 OpenWrt 编译工具的命令具体见 [构建 OpenWrt 系统设置](https://openwrt.org/docs/guide-developer/toolchain/install-buildsystem).

```bash
$ git clone https://github.com/openwrt/openwrt
$ sudo apt update
$ sudo apt install build-essential ccache ecj fastjar file g++ gawk \
gettext git java-propose-classpath libelf-dev libncurses5-dev \
libncursesw5-dev libssl-dev python python2.7-dev python3 unzip wget \
python-distutils-extra python3-setuptools python3-dev rsync subversion \
swig time xsltproc zlib1g-dev
```

之后,获取 OpenWrt 最新的软件包定义,将所有软件包的安装符号链接下载到本地。

```bash
$ cd openwrt
$ ./scripts/feeds update -a
$ ./scripts/feeds install -a
```

## 构建 WasmEdge

### 获取 WasmEdge 源码

```bash
$ git clone https://github.com/WasmEdge/WasmEdge.git
$ cd WasmEdge
```

### 执行构建脚本

在 WasmEdge 源代码中运行构建脚本 `build_for_openwrt.sh`,传入 OpenWrt 源码的路径作为参数。此脚本将自动将 WasmEdge 加入 OpenWrt 的软件包列表,并编译 OpenWrt 固件,生成的 OpenWrt 镜像文件位于 `openwrt/bin/targets/x86/64` 文件夹中。

```bash
$ ./utils/openwrt/build_for_openwrt.sh ~/openwrt
```

运行构建脚本时,会出现 OpenWrt 配置界面,在该界面中,我们需要将 `Target System` 设置为 x86,将 `Target Profile` 设置为 Generic x86/64,并在 `Runtime` 一栏中找到 `WasmEdge` 勾选上。设置完成后,脚本会自动构建 WasmEdge 并编译 OpenWrt 系统。

## 测试 WasmEdge

### 在 Vmware 中部署 OpenWrt

为了验证 WasmEdge 的可用性,我们使用 VMware 虚拟机来安装经过编译得到的 OpenWrt 镜像,创建虚拟机之前需要使用 `QEMU` 的命令将 OpenWrt 的镜像转换成 vmdk 格式。

```
$ cd ~/openwrt/bin/targets/x86/64
$ sudo apt install qemu
$ gunzip openwrt-x86-64-generic-squashfs-combined.img.gz
$ qemu-img convert -f raw -O vmdk openwrt-x86-64-generic-squashfs-combined.img Openwrt.vmdk
```

之后,在 VMware 中创建虚拟机,安装 OpenWrt 系统。

### 上传测试文件

根据主机的网关来设置 OpenWrt 的 ip 地址后,使用 `scp` 传输主机上的 wasm 文件到 OpenWrt 系统上。

例如,设置 OpenWrt 的 ip 地址为 192.168.0.111,则使用以下命令来上传 [hello.wasm](https://github.com/WasmEdge/WasmEdge/raw/master/examples/wasm/hello.wasm)[add.wasm](https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/examples/wasm/add.wasm) 测试文件到 OpenWrt。

```
$ scp hello.wasm [email protected]:/
$ scp add.wasm [email protected]:/
```

### 测试 wasmedge 程序

```
$ wasmedge hello.wasm second state
hello
second
state
$ wasmedge --reactor add.wasm add 2 2
4
```


16 changes: 16 additions & 0 deletions utils/openwrt/build_for_openwrt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2019-2022 Second State INC

OPENWRT_DIR_PATH=$1
WASMEDGE_FATHER_PATH=$(dirname $(dirname $(dirname $(pwd))))

cd ${OPENWRT_DIR_PATH}/package/utils
mkdir WasmEdge && cd WasmEdge
cp -r ${WASMEDGE_FATHER_PATH}/WasmEdge ${OPENWRT_DIR_PATH}/package/utils/WasmEdge/src
cp ${WASMEDGE_FATHER_PATH}/WasmEdge/utils/openwrt/configuration/Makefile ${OPENWRT_DIR_PATH}/package/utils/WasmEdge/
cd ${OPENWRT_DIR_PATH}

make menuconfig

make -j1 V=s
40 changes: 40 additions & 0 deletions utils/openwrt/configuration/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=WasmEdge
PKG_VERSION:=0.10.0
PKG_BUILD_DIR:= $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/$(PKG_NAME)
SECTION:=base
CATEGORY:=Runtime
TITLE:=$(PKG_NAME) -prints a $(PKG_NAME) message
DEPENDS:=+libpthread +libstdcpp
endef

define Package/$(PKG_NAME)/description
WasmEdge is a lightweight, high-performance, and extensible
WebAssembly runtime. It is the fastest Wasm VM today.
endef

define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Package/$(PKG_NAME)/install
mkdir -p $(1)/usr/include/wasmedge
$(INSTALL_DIR) $(1)/usr/bin $(1)/usr/lib $(1)/usr/include/wasmedge
$(CP) $(PKG_INSTALL_DIR)/usr/bin/wasmedge $(1)/usr/bin/
$(CP) $(PKG_INSTALL_DIR)/usr/include/wasmedge/* $(1)/usr/include/wasmedge/
$(CP) $(PKG_INSTALL_DIR)/usr/lib/libwasmedge_c.so $(1)/usr/lib/
$(CP) $(PKG_INSTALL_DIR)/usr/lib/wasmedge/libwasmedgePluginWasmEdgeProcess.so $(1)/usr/lib/
endef

CMAKE_OPTIONS += \
-DWASMEDGE_BUILD_AOT_RUNTIME=OFF \

$(eval $(call BuildPackage,$(PKG_NAME)))

0 comments on commit 1f21045

Please sign in to comment.