用OpenWRT编译软件安装包

1.简介

  OpenWrt是一个用于嵌入式设备的GNU/Linux发行版,具有强大的扩展性。不同于其他许多用于路由器的发行版,OpenWrt是一个从零开始编写的、功能齐全的、容易修改的路由器操作系统。
  在本篇文章中,我将编译出三个ipk包,分别是用来测试的helloworld,以及port-mirroring,capsulator,并安装它们进行测试使用.

2.环境

  • Ubuntu 14.04
  • Netgear WNDR4300v1(已刷Openwrt 15.05)
  • 官方SDK

3.相关资料

  • SDK获取地址
    WNDR4300官方地址,当然也可以在make menucofnig中选中自己编译。文件名OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2。
    https://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/nand/
  • OpenWRT软件包详解
    https://wiki.openwrt.org/zh-cn/doc/packages
  • port-mirroring地址
    http://port-mirroring.googlecode.com/ 需要。
  • Capsulator地址
    https://github.com/peymank/Capsulator
  • OpenWRT Makefile详解
    https://wiki.openwrt.org/zh-cn/doc/devel/packages

4. 编译

1) SDK的使用

  将对应的SDK文件直接解压,会看到熟悉的结构,和OpenWRT源码基本一致。我们要关注的文件夹是package和build_dir,以及staging_dir。
  在package目录下创建相应的目录,名字为你要创建的软件包名。在软件包目录下创建一个Makefile文件,该文件就是OpenWRT用来解释生成ipk的文件。

2) 编译helloworld

  首先,自己编写一个C程序,输出一些字符串,如下helloworld.c。

#include 
#include 

int main(void) {
    printf("Success,You can do next step now.\n");
    return 0;
}

  我们为其编写linux的Makefile文件如下,注意区分这个Makefile和OpenWRT的Makefile。

# build helloworld executable when user executes "make"
helloworld: helloworld.o
    $(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
    $(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
    rm *.o helloworld

  为确保程序的正确性,我们先到这两个文件的目录下执行make,如果没出错会生成可执行文件helloworld。接下来我们运行./helloworld就可以看到输出的信息了。
  我们在SDK的package目录下创建一个目录,命名为helloworld,在其下分别创建一个名为src的文件夹和Makefile的文件。将之前测试的Makefile和helloworld.c都丢到src文件夹里。最后package/Makefile的内容如下。

##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################
include $(TOPDIR)/rules.mk
# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1

# This specifies the directory where we're going to build the program.
# The root build directory, $(BUILD_DIR), is by default the build_mipsel
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Helloworld -- prints a snarky message
endef

# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
If you can't figure out what this program does, you're probably
brain-dead and need immediate medical attention.
endef

# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default. The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef

# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one

# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist. Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef

# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

  相关变量的含义请参考官网。之后可以用以下命令编译,也可以在make menuconfig中选择再make。

sudo make package/helloworld/compile V=99

3)编译port-mirroring

  port-mirroring官方给出了一些ipk包,但是非常有限,都是针对很老的OpenWRT版本,因此我们需要自己编译。
  我们可以从官网上下到其源码及OpenWRT的Makefile,但是要对其做一定的修改,修改后的Makefile如下。
  

##############################################
# OpenWrt Makefile for port-mirroring program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################

include $(TOPDIR)/rules.mk

# Name and release number of this package
PKG_NAME:=port-mirroring
PKG_VERSION:=1.3
PKG_RELEASE:=1


# This specifies the directory where we're going to build the program. 
# The root build directory, $(BUILD_DIR), is by default the build_mipsel
# directory in your OpenWrt SDK directory
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://code.google.com/archive/p/port-mirroring/downloads 
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)


include $(INCLUDE_DIR)/package.mk



# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/port-mirroring
    SECTION:=net
    CATEGORY:=Network
    DEPENDS:=+libpcap +libpthread
    TITLE:=port-mirroring tool
    URL:=http://code.google.com/p/port-mirroring/
    MENU:=1
endef

# Specify where and how to install the program. Since we only have one file,
# the port-mirroring, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/port-mirroring/install
    $(INSTALL_DIR) $(1)/usr/sbin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/port-mirroring $(1)/usr/sbin/
    $(INSTALL_DIR) $(1)/etc/config
    $(INSTALL_CONF) $(PKG_BUILD_DIR)/port-mirroring.conf $(1)/etc/config/port-mirroring
    $(INSTALL_DIR) $(1)/etc/init.d
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/port-mirroringd $(1)/etc/init.d/port_mirroring
endef

define Package/port-mirroring/postinst
#!/bin/sh
if [ -z "$${IPKG_INSTROOT}" ]; then
    echo "Enabling rc.d symlink for port-mirroring"
    /etc/init.d/port_mirroring enable
fi
exit 0
endef

define Package/port-mirroring/prerm
#!/bin/sh
# check if we are on real system
if [ -z "$${IPKG_INSTROOT}" ]; then
    echo "Removing rc.d symlink for port-mirroring"
    /etc/init.d/port_mirroring disable
fi
exit 0
endef

# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,port-mirroring))

  同样在package目录下创建port-mirroring目录,将Makefile丢进里面。源码不用解压,放到SDK的dl目录下即可。
  运行下面语句进行编译。

sudo make package/port-mirroring/compile V=99

4)编译Capsulator

  Capsulator是斯坦福大学用在SDN的一款Mac in Ip隧道软件,我们先用git工具将它拷下来。

git clone https://github.com/peymank/Capsulator

  进入该目录下make,出错,报gmake没有找到,在Makefile里将Make=gmake改成Make =make,再重新make,成功生成可执行文件。
  接下来同样在package为其创建一个目录capsulator,模仿着helloworld的Makefile捣鼓了好久才写出来,Makefile如下。

include $(TOPDIR)/rules.mk

PKG_NAME:=capsulator
PKG_RELEASE:=1

PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)


include $(INCLUDE_DIR)/package.mk

define Package/capsulator
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:=this is capsulator_test
    DEPENDS:=+libpthread 
endef

define Package/capsulator/description
    this is a test capsulator!
endef

#CONFIGURE_VARS+= \
    CC="$(TOOLCHAIN_DIR)/bin/$(TARGET_CC)"
#MAKE_FLAGS += \
    CXXFLAGS="$(TARGET_CXXFLAGS) -fno-builtin -fno-rtti -nostdinc++" \
    CPPFLAGS="$(TARGET_CPPFLAGS) -I$(STAGING_DIR)/usr/include/uClibc++ -I$(LINUX_DIR)/include" \
    LDFLAGS="$(TARGET_LDFLAGS) $(LDFLAGS)" \
    LIBS="$(TARGET_LIBS) -nodefaultlibs -luClibc++ -lm" \
    DESTDIR="$(PKG_INSTALL_DIR)"


define Build/Prepare
    touch $(PKG_BUILD_DIR)/capsulator
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

#define Build/Compile
#   $(MAKE) -C $(PKG_BUILD_DIR) $(MAKE_FLAGS);
#endef

define Package/capsulator/install
    $(INSTALL_DIR) $(1)/bin
    #$(CP) /home/wucanrui/Desktop/libpthread.so.0 $(1)/bin/
    $(CP) $(TOPDIR)/staging_dir/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/lib/libpthread.so.0 $(1)/bin/
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/capsulator $(1)/bin
endef

$(eval $(call BuildPackage,capsulator))

  最后,在capsulator目录下同样创建src文件夹,将源程序丢进去,注意别把之前测试make生成的.o文件和可执行文件也丢进去了。编译成功生成。
  

sudo make package/capsulator/compile V=99

5.测试

  编译出来的ipk包都在SDK的bin目录下,将其取出用下面命令传到路由器中,并用opkg命令进行安装。

scp xxxx.ipk root@192.168.1.1:/tmp
ssh root@192.168.1.1
cd ..
cd tmp
opkg install xxxx.ipk

  三个软件包安装后都成功正常运行。
用OpenWRT编译软件安装包_第1张图片

6. 结语

  编译出来的东西急着使用,所以关于OpenWRT Makefile很多东西没有过深的研究。后续还有很多事情要做,等有空再更新一篇关于OpenWRT Makefile的文章。

你可能感兴趣的:(Openwrt,SDN,openwrt,路由器)