OpenWRT下使用的软件包管理技术叫做OPKG,它是一个轻量级的软件包技术,和apt-get差不多可以下载和安装程序。
语法格式:opkg [参数...] 子命令 [子参数...],参数有:
update 下载服务器上可用的软件包列表
upgrade <包名> 升级软件包
install <包名> 安装软件包
configure <包名> 配置某一个软件包
remove <包名> 卸载软件包
info [pkg|regexp] 显示出指定软件包的信息
这是几个常用的还有别的不在一一讲解
接下来介绍包的编译安装
1. 首选配置编译出SDK包如图
SDK包生成在改目录下linux64@linux:~/openwrt/openwrt/bin/ar71xx$
(1)是对应路由器的固件
(2)是生成的SDK包
2.解压SDK包
$ mkdir helloworld
$ cd helloworld
helloworld$ mkdir srchelloworld$ touch Makefile
helloworld$ cd srcsrc$ touch helloword.c Makefile
在helloworld 中输入如下代码
1 #include<stdio.h>23 int main(int argc, const char *argv[])4 {567 printf("hello world\n");8 return 0;9 }
在helloworld.c当前文件夹的Makfile输入
helloworld : helloworld.o $(CC) $(LDFLAGS) helloworld.o -o helloworld helloworld.o : helloworld.c $(CC) $(CFLAGS) -c helloworld.c clean : rm *.o helloworld
整个过程下来,package目录结构如下:
package |-- helloworld | |-- Makefile | `-- src | |-- helloworld.c | `-- Makefile `-- Makefile
4.自建包的编译
我们要编写的是 package/helloworld/Makefile 这个文件。
在这个文件中,我们要描述 helloworld 包的信息,比如:如何配置、如何编译、如何打包、安装等等信息。
这个文件与一般的 Makefile 格式还不一样,详见OpenWrt上的说明文档:OpenWrt官网Packages说明这里我就依照例子编写 helloworld/Makefile:
1 include $(TOPDIR)/rules.mk23 PKG_NAME:=helloworld4 PKG_NAME:=15 PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)67 include $(INCLUDE_DIR)/package.mk89 define Package/helloworld10 SECTICON:=utils11 CAYEGORY:=Utilities12 TITLE:=helloworld -- prints a snarky message13 endef1415 define Package/helloworld/description16 It's my frist package demo17 endef1819 define Build/Prepare20 echo "Here is Package/Prepare"21 mkdir -p $(PAK_BUILD_DIR)/22 endef2324 define Package/helloworld/install25 echo "Here is Package/install"26 $(INSTALL_DIR) $(1)/bin27 $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld &(1)/bin/28 endef2930 $(eval $(a=call BuildPackage, helloworld))
然后回到 OpenWrt-SDK 目录下,执行:make V=s,结果有以下错误提示:
$ make V=s ERROR: please fix package/helloworld/Makefile - see logs/package/helloworld/dump.txt for details
说是我写的这个Makefile有错,请查看 dump.txt 文件。无奈只好去看看到底错在哪里啰。
打开 OpenWrt-SDK/logs/package/helloworld/dump.txt 文件:
Package: helloworld Version: 1 Depends: +libc +SSP_SUPPORT:libssp +USE_GLIBC:librt +USE_GLIBC:libpthread Conflicts: Menu-Depends: Provides: Section: opt Category: Extra packages Title: Maintainer: Source: Type: ipkg Description: @@ Makefile:32: *** invalid syntax in conditional. Stop.
前面那么多行信息没什么用,最重要的是最后一行,好像是说32行条件语法错误。赶紧打开 package/helloworld/Makefile,定位到32行看看。结果是:
$(eval $(call BuildPackage, helloworld))
这个,我何错之有呢?
最后反复排查,原来是 "BuildPackage," 逗号后面与 "helloworld" 之间多了个空格。不会吧!多个空格少个空格都会导致语法错误?
好了,改正过来了。
$(eval $(call BuildPackage,helloworld)) #去掉空格
$ make V = vs
生成了 helloworld_1_ar71xx.ipk 。find 一下,看在哪里。
$ find -name helloworld*.ipk ./bin/ar71xx/packages/base/helloworld_1_ar71xx.ipk
5.安装helloworld
将刚生成的 helloworld_1_ar71xx.ipk 文件用 scp 传到目标路由上。本人的路由IP为:192.168.1.2
$ scp bin/ar71xx/packages/base/helloworld_1_ar71xx.ipk root@192.168.1.2: root@192.168.1.2's password: helloworld_1_ar71xx.ipk 100% 1993 2.0KB/s 00:00
-----------------------------------------------------
root@OpenWrt:~# ls helloworld_1_ar71xx.ipk root@OpenWrt:~# opkg install helloworld_1_ar71xx.ipk Installing helloworld (1) to root... Configuring helloworld. root@OpenWrt:~#
安装完成后,执行一下试试看。
root@OpenWrt:~# helloworld This is my hello word!
用which命令查看 helloworld 安装的路径:
root@OpenWrt:~# which helloworld /bin/helloworld
在 /bin/ 路径下。