openwrt 插件 编译教程

openwrt 插件 编译教程

OpenWrt 插件编译教程

OpenWrt 是一个高度可定制化的嵌入式 Linux 发行版,广泛用于路由器和其他网络设备。通过编译和安装插件,你可以扩展 OpenWrt 的功能以满足特定需求。以下是一个详细的 OpenWrt 插件编译教程,帮助你从头开始编译并安装自定义插件。

一、准备工作

  1. 环境要求

    • 操作系统:Linux(推荐使用 Ubuntu 或 Debian)
    • 工具链:GCC、Make 等编译工具
    • SDK:OpenWrt SDK 或者从源码编译的完整 OpenWrt 构建环境
  2. 获取 OpenWrt 源码

    • 可以从 OpenWrt 官方网站下载最新的源码包,或者使用 git 克隆 OpenWrt 的仓库。
git clone https://github.com/openwrt/openwrt.git cd openwrt ./scripts/feeds update -a ./scripts/feeds install -a
  1. 设置编译目标
    • 根据你的硬件平台选择正确的目标配置文件(如 target/linux/x86/64)。
make target/linux/{your_platform}/clean V=s QUILT=1 make target/linux/{your_platform}/image V=s QUILT=1

二、创建插件项目

  1. 插件目录结构

    • 在 OpenWrt 源码的根目录下创建一个新的插件目录,例如 package/myplugin。
    • 目录结构如下:package/ └── myplugin/ ├── Makefile ├── files/ └── src/ └── Makefile (可选,如果插件包含源代码)
  2. 编写 Makefile

    • package/myplugin/Makefile 是插件的主要构建文件,内容示例如下:
include $(TOPDIR)/rules.mk PKG_NAME:=myplugin PKG_VERSION:=1.0 PKG_RELEASE:=1 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE_URL:=http://example.com/source PKG_HASH:=<hash_value> # 使用 sha256sum 生成哈希值 PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION) PKG_INSTALL_DIR:=$(INSTALL_DIR)/$(PKG_NAME)-$(PKG_VERSION) include $(INCLUDE_DIR)/package.mk define Package/$(PKG_NAME) SECTION:=utils CATEGORY:=Utilities TITLE:=My Custom Plugin DEPENDS:=+libuci +libubox # 根据需要添加依赖 endef define Package/$(PKG_NAME)/install $(INSTALL_DIR) $(1)/usr/bin $(INSTALL_BIN) $(PKG_INSTALL_DIR)/usr/bin/myplugin $(1)/usr/bin/ endef $(eval $(call BuildPackage,$(PKG_NAME)))
  1. 准备插件文件
    • 将插件的可执行文件或脚本放在 package/myplugin/files/ 目录中。
    • 如果插件包含源代码,需要在 src/ 目录中提供 Makefile 和源文件,并在主 Makefile 中进行相应的配置。

三、编译插件

  1. 更新 feeds 并安装依赖
./scripts/feeds update -a ./scripts/feeds install <any_additional_dependencies>
  1. 编译插件
    • 进入 OpenWrt 根目录并执行编译命令。
make package/$(PKG_NAME)/compile V=s
  1. 查找生成的 IPK 文件
    • 编译成功后,IPK 包将位于 bin/packages/<architecture>/ 目录下。

四、安装插件到路由器

  1. 上传 IPK 文件
    • 使用 SCP 或其他方法将 IPK 文件上传到路由器的 /tmp/ 目录。
scp bin/packages/<architecture>/myplugin_*.ipk root@<router_ip>:/tmp/
  1. 在路由器上安装插件
    • 通过 SSH 登录到路由器并使用 opkg 命令安装插件。
opkg update opkg install /tmp/myplugin_*.ipk
  1. 验证安装
    • 检查插件是否已成功安装并运行。
myplugin --version

五、总结