OpenWrt是一个高度模块化、高度自动化的嵌入式linux发行版,其编译和安装过程比普通的linux发行版而言,要简单太多了。若是您是新手,您那恐惧的心大可放到肚子里,呵呵。对于新手来讲最麻烦的恐怕是安装开发环境须要的软件了。
最基本条件:php
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1.PC一台,性能强点,性能好编译得就快。linux推荐安装ubuntu 8.04中文版(或者suse)和fedora 11(对于初学者而言,最好是装在虚拟机如VM里,可省去不少麻烦。如何在VM里安装linux,您可多google或者baidu)
2.宽带一条,须要速度好点,由于有不少软件包是网络下载后安装。
3.耐心和时间。
编译环境准备(安装linux发行版和软件包):html
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1.安装好ubuntu8.04中文版本或者fedora 9或者11,搞定网络链接。
以非root用户登录,打开“应用程序—-附件—-终端”。若是您已经安装好linux, 那么此步骤请略去。
2.若是您选择ubuntu, 那么ubuntu开发环境须要的软件有sudo apt-get install gcc g++ binutils patch bzip2 flex bison make autoconf gettext texinfo unzip sharutils subversion libncurses5-dev ncurses-term zlib1g-dev
而后
sudo apt-get update
如您选择安装fedora11或者fedora9
Fedora 11 or 9:
# yum install autoconf binutils bison bzip2 flex gawk gcc gcc-c++ gettext make ncurses-devel patch unzip wget zlib-devel
因为fedora 安装包原本就很全面,因此需另外安装的东西较少。因此通常状况下,推荐安装fedora 11。对于初学者而言,最方便的方法是在VM上安装linux。
因为ubuntu原本就是基于suse, 因此若是您要以suse为开发环境,那么您参考ubuntu的安装步骤便可。
若是你能阅读英文,那么请参考
http://wiki.openwrt.org/doc/howto/buildroot,
OpenWrt英文论坛2006年文章:Building OpenWrt Kamikaze from source
https://forum.openwrt.org/viewtopic.php?id=9180
这里有更详细权威的说明。
4.获取OpenWrt源代码和安装包,更新
1. svn checkout svn://svn.openwrt.org/openwrt/trunk (开发版本,不稳定,不建议下载)linux
PS:wgethttp://downloads.openwrt.org/backfire/10.03/backfire_10.03_source.tar.bz2(目前稳定版本,建议)
2. 检查配置环境c++
必须使用非root用户进行操做,添加一个普通用户。(注意:若是使用root进行操做的话,会提示检查失败:Checking 'non-root'... failed.)web
[root@localhost trunk]# adduser openwrt ubuntu
[root@localhost trunk]# su openwrtvim
为了防止文件读写权限形成问题,须要将源码copy到openwet的根目录下进行全部操做。windows
[openwrt@localhost openwrt]$ cd ~网络
[openwrt@localhost ~]$ cp -r /usr/src/test/trunk ./app
[openwrt@localhost ~]$ cd trunk/
[openwrt@localhost trunk]$ ls
BSDmakefile LICENSE README feeds.conf.default package scripts toolchain
Config.in Makefile docs include rules.mk target tools
3.下载feeds
Feeds是OpenWrt环境所须要的软件包套件。最重要的feeds有:
‘packages’一些额外的基础路由器特性软件
‘LuCI’OpenWrt默认的GUI
‘Xwrt’另外一种可选的GUI界面
须要可以链接互联网。
在下载以前能够经过查看’feeds.conf.default’文件,来检查哪些文件须要包含在环境中。
开始下载,使用:
[openwrt@localhost trunk]$ ./scripts/feeds update -a
安装feeds包,只有安装以后,在后面的make menuconfig时,才能够对相关配置进行勾选。
[openwrt@localhost trunk]$ ./scripts/feeds install -a
若是更新了feeds的配置文件,须要添加新的软件包用于生成系统。只需进行重复操做:
[openwrt@localhost trunk]$ ./scripts/feeds update -a
[openwrt@localhost trunk]$ ./scripts/feeds install -a
4.进行配置
编译过程使用的交叉编译,交叉编译生成的SDK以及image等文件的类型取决于开发环境、应用硬件、以及源码版本。因此要对本身的环境进行了解,才能进行正确的配置。我在配置过程当中,就遇到了这个问题,个人硬件是brcm47xx,在第一次编译的时候,选择地是Target System (Broadcom BCM947xx/953xx),最后生成的包没法在router上安装,版本不匹配。第二次安装时,选择了Target System (Broadcom BCM947xx/953xx[2.4]),安装成功,个人板子可能只支持linux2.4的内核。(设备型号是Linksys Wrt54gs v3.0) (2011.05.01添加:其实Linksys Wrt54gs v3.0是支持Linux 2.6版本的,Target System 选择 Broadcom BCM947xx/953xx,编译后也是能够用的,这周末因为项目须要改某个软件,每次修改代码后,都得从新编译一下固件,通过来回编译了十来次,均可以使用。另外。内核版本升级以后,无线驱动改为了mac802.11,而非之前的wl,觉得wl.o这个专门的库只在linux2.4中才用到。)
[openwrt@localhost trunk]$ make defconfig
[openwrt@localhost trunk]$ make prereq
[openwrt@localhost trunk]$ make menuconfig
经过文本对话框进行选项配置,最主要的配置项有:
Target system(目标系统类型)
Package selection(软件包选择)
Build system settings (编译系统设置)
Kernel modules (内核模块)
[*]表示:这个包裹选中编译,并安装在firmware中;
[M]表示:这个软件包选中编译,但并不安装在firmware中。
在退出Menuconfig的时,会提示是否保存配置。
在此我只对target system进行了选择;勾选了Advanced configuration option和Build the OpenWrt SDK选项。
5. 编译
(1)通常状况,使用一个简单的命令:
[openwrt@localhost trunk]$ make
(2)在多核处理器系统上为提升速度,可以使用(例如用3核处理器):
[openwrt@localhost trunk]$ make –j 3
(3)在后台进行编译,使用空闲的I/O资源和CPU性能,可以使用(例如使用双核处理器)
[openwrt@localhost trunk]$ onice -c 3 nice -n 20 make -j 2
(4)编译一个单独的软件包(例如在cups软件包):
[openwrt@localhost trunk]$ make package/cups/compile V=99
(5)若是特殊缘由须要分析编译报错信息:
[openwrt@localhost trunk]$ make V=99 2>&1 |tee build.log |grep -i error
说明:将编译的全部输出信息保存在build.log中,将error信息打印在屏幕上。
(6)一个复杂指令的应用
[openwrt@localhost trunk]$ ionice -c 3 nice -n 20 make -j 2 V=99 CONFIG_DEBUG_SECTION_MISMATCH=y 2>&1 \|tee build.log |egrep -i '(warn|error)'
说明:将编译的全部输出信息保存在build.log中,将error和warning信息打印在屏幕上。编译过程使用双核CPU,占用后台资源。
6.生成镜像(Image)位置
新生成的镜像会默认放在新建的一个bin目录下。例如:/bin/brcm-2.4/packages
[openwrt@localhost trunk]$ ls bin/*
将编译好的镜像作个备份,例如备份到/目录下:
[openwrt@localhost trunk]$ cp bin /
7.清理工做
建议如今清理编译产生的文件,以避免下次编译时形成冲突,(文件若是存在的话,将不会被替换),执行make clean
注意:在执行clean命令,确保已经将编译好的image进行了备份。清理工做会清楚bin目录。
[openwrt@localhost trunk]$ make clean
除了清除生成的目录,还想清除交叉编译工具(以及工具链目录)
[openwrt@localhost trunk]$ make dirclean
清除全部相关的东西,包括下载的软件包,配置文件,feed内容等:(不建议使用)
[openwrt@localhost trunk]$ make distclean
对于更新feeds后出现的错误:
ERROR:please fix package/feeds/packages/mc/Makefile 等相似的问题,须要执行这条语句进行系统的清理
更详细的信息建议您参考
http://downloads.openwrt.org/kamikaze/docs/openwrt.html
应该是当前最新最权威的文档
编译过程当中须要经过官网下载不少相关的软件包,因此必须保证可以顺利连上外网。因为下载速度的限制,编译过程大概须要数小时。编译结束后,全部的产品都会放在编译根目录下的bin/yourtarget/. 例如:我所编译的产物都放在./bin/brcm47xx/下,其中文件主要有几类:
(1). bin/.trx 文件: 这些都是在咱们所选的target-system的类别之下,针对不一样路由器型号、版本编译的路由器固件。这些不一样路由器的型号和版本是openwrt预先设置好的,咱们不须要更改。至于.bin和.trx的区别,一种说法是,第一次刷路由器的时候,须要用.bin文件,若是须要再升级,则不能再使用. bin文件,而须要用.trx文件。缘由是,.bin是将路由器的相关配置信息和.trx封装在一块儿而生成的封包,也就是说是包含路由器版本信息的. trx。在第一次刷固件的时候,咱们须要提供这样的信息,而在后续升级时,则再也不须要,用.trx文件便可。
(2)packages文件夹: 里面包含了咱们在配置文件里设定的全部编译好的软件包。默认状况下,会有默认选择的软件包。
(3) OpenWrt-SDK.**.tar.bz2: 这个也就是咱们定制编译好的OpenWRT SDK环境。咱们将用这个来进行OpenWrt软件包的开发。例如,我所编译好的SDK环境包为:/bin/brcm47xx/OpenWrt-SDK- brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2
能够从名称上看出,target system是brcm47xx,host system是Linux-x86_64,使用的编译工具以及库是4.3.3+cs_uClibc-0.9.30.1。
(4)md5sums 文件: 这个文件记录了全部咱们编译好的文件的MD5值,来保证文件的完整性。由于文件的不完整,很容易将路由器变成“砖头”。
须要主要的是,编译完成后,必定要将编译好的bin目录进行备份(若是里面东西对你很重要的话),由于在下次编译以前,执行make clean 会将bin目录下的全部文件给清除掉!!
二、 更改原有packages
在编译根目录下会有一个dl的目录,这个目录实际上是“download”的简写,在编译前期,须要从网络下载的数据包都会放在这个目录下,这些软件包的一个特色就是,会自动安装在所编译的固件中,也就是咱们make menuconfig的时候,为固件配置的一些软件包。若是咱们须要更改这些源码包,只须要将更改好的源码包打包成相同的名字放在这个目录下,而后开始编译便可。编译时,会将软件包解压到build_dir目录下。
固然,你也能够本身在dl里面建立本身的软件包,而后更改相关的配置文件,让openwrt能够识别这个文件包。
因为个人项目更改的内容是底层的,须要跟固件一块儿安装。因此,我使用的方法就是直接更改dl目录下软件包,而后从新进行固件编译。感受相似于Linux的内核编译。反复编过十屡次,没有任何问题。
第一次编译因为要下载大量的软件包,慢慢的等吧,快则半小时,慢则2,3小时. 若是下载过慢,建议您中断编译过程,而后用迅雷去主动下载,放到./dl目录下。一个加快速度的小技巧: 本站小编已经将将一些经常使用的软件包打包成dl.tar,放在http://ul.to/2y8a2w, 下载后用tar xvf dl.tar(在windows下用winrar打开便可,而后你用ftp或者samba之类传到Linux PC上),将解包出来的东东直接放到./dl目录下,相必会大大加快初次安装、编译的速度。一样在http://ul.to/fasgus上可下载linux-2.36.30.10.tar.bz2,固然您也能够用迅雷之类的工具来下。
8. 编译好的文件在当前目录的bin文件夹下。
若是是brcm-2.4
ls bin/brcm-2.4/
就能够看到不少熟悉的文件了。
九、 安装OpenWrt
找到对应的固件,进行固件升级。网上方法不少,这里再也不赘述。
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
下面咱们编写本身的package:
新建本身的packages
对于本身新建的package,而这个package又不须要随固件一块儿安装,换句话说,就是能够当作一个可选软件包的话。咱们能够利用咱们的SDK环境来单独编译,编译后会生成一个ipk的文件包。而后利用 opkg install xxx.ipk 来安装这个软件。
下面具体说下,如何编译一个helloword的软件包。
(1)首先,编写helloworld程序
编写helloworld.c
/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/
#include <stdio.h>
#include <unistd.h>
int main(void)
{
printf("Hell! O' world, why won't my code compile?\n\n");
return 0;
}
编写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后,可以打印出“Hell! O' world, why won't my code compile?”。这一步,主要保证咱们的源程序是能够正常编译的。下面咱们将其移植到OpenWRT上。
(2)将OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2解压
tar –xvf OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1.tar.bz2
(3)进入SDK
cd OpenWrt-SDK-brcm47xx-for-Linux-x86_64-gcc-4.3.3+cs_uClibc-0.9.30.1
能够看到里面的目录结构跟咱们以前source的目录结构基本相同,所须要编译的软件包,须要放置在package目录下
(4)在package目录下建立helloworld目录
cd package
mkdir helloworld
cd helloworld
(5)建立src目录,拷贝 helloworld文件
mkdir src
cp /home/wrt/test/helloworld.c src
cp /home/wrt/test/Makefile src
(6)在helloworld目录下建立Makefile文件
这个Makefile文件是给OpenWRT读的,而以前写的那个Makefile文件是针对helloworld给编译其读的。两个Makefile不在同一层目录下。
touch Makefile
vim Makefile
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))
(7)返回到SDK的根目录执行make进行编译编译过程会在build_dir目录下完成编译结果会放在 bin/[yourtarget]/package目录下helloworld_1_bcm47xx.ipk(8)上传helloworld_1_bcm47xx.ipk使用sftp软件上传helloworld_1_bcm47xx.ipk至路由器执行 opkg install helloworld_1_bcm47xx.ipk输入hello而后按Tab键,发现openwrt中已经有helloworld可执行命令。执行 helloworld 查看程序的效果。Hell! O' world, why won't my code compile?