OpenWrt是一个高度模块化、高度自动化的嵌入式Linux系统,拥有强大的网络组件,常常被用于工控设备、电话、小型机器人、智能家 居、路由器以及VOIP设备中。OpenWrt支持各种处理器架构,无论是对ARM,X86,PowerPC或者MIPS都有很好的支持。 其多达3000多种软件包,囊括从工具链(toolchain),到内核(linux kernel),到软件包(packages),再到根文件系统(rootfs)整个体系,使得用户只需简单的一个make命令即可方便快速地定制一个具 有特定功能的嵌入式系统来制作固件。其模块化设计也可以方便的移植各类功能到OpenWrt下,加快开发速度。
对于开发人员,OpenWrt 是使用框架来构建应用程序,而无需建立一个完整的固件来支持;对于用户来说,这意味着其拥有完全定制的能力,可以用前所未有的方式使用该设备。
2013年12月19日小米路由器公测版正式发售,也意味着OpenWrt进入国内主流科技企业的眼球。然而OpenWrt到底是一款什么样的操作系统呢?对于创客来讲,怎么才能融入创客的设计,下面就从零介绍如果在pcDuino上开发OpenWrt。
OpenWrt项目始于2004年1月. 最早的OpenWrt版本基于Linksys为遵守GPL而放出的、为WRT54G所编写的代码,以及uclibc项目的buildroot。 这个版本以OpenWrt “stable release”(“稳定版”)之名为人所知,使用广泛。仍有许多OpenWrt应用程序是基于这一版的,例如Freifunk-Firmware或 Sip@Home。
2005年初,一些新的开发者进入了团队。在封闭开发了数月之后,团队决定发布OpenWrt的第一个“实验”(experimental)版本。 这个实验版本使用的build系统是基于buildroot2大改而成的,而buildroot2来自于uclibc项目。 OpenWrt使用官方版GNU/Linux内核代码,只是额外添加了片上系统(SoC,System on Chip)的补丁和网络接口的驱动。开发团队尝试重新实现GPL tarball中不同开发商的绝大多数专有代码。其中有:将新固件镜像文件直接写入闪存的自由工具(mtd)、配置无线局域网 (wlcompat/wificonf)、通过proc文件系统对支持VLAN的switch(交换机?)进行编程。最初发布的OpenWrt的代号是 “White Russian”,来自于著名鸡尾酒的名称。在OpenWrt发布0.9版的时候,White Russian的生命周期结束。
下一个版本的开发正在我们的subversion(SVN) repository(仓库)中进行。下面一张图将很清晰的反应OpenWrt的版本史。从图上可以看出最新的稳定版本代号为Backfire,早期的稳 定版本为Kamikaze,开发版本一直都是trunk。各个版本的官方下载地址为
https://dev.openwrt.org/wiki/GetSource
还存在另一种版本最新测试版代号为Attitude_Adjustment
硬件:pcDuino V2 1块
代码指令:
$ 表示在pcDuino执行
# 表示在x86 linux上执行
> 表示在u-boot状态执行
1,下载依赖包
#sudo apt-get install libncurses5-dev zlib1g-dev gawk flex patch git-core g++ subversion
2,OpenWrt的源代码管理默认用的是SVN,当然你还可以用Git,本教程中使用最新的trunk版本,用SVN工具下载源码
#svn co svn://svn.openwrt.org/openwrt/trunk/ openwrt-pcDuino
你还可以用Git下载
#git clone git://git.openwrt.org/openwrt.git
#git clone git://git.openwrt.org/packages.git
支持pcDuino的OpenWrt源码下载:
#https://github.com/Pillar1989/OpenWrt-pcDuino OpenWrt
3,扩展软件包package feeds,feeds即为包含到你的OpenWrt环境中的额外软件包的索引之类的。目前常用的feeds有:
src-git packages git://git.openwrt.org/packages.git
src-svn xwrt http://x-wrt.googlecode.com/svn/trunk/package
src-git luci git://nbd.name/luci.git
src-git routing git://github.com/openwrt-routing/packages.git
src-git telephony http://feeds.openwrt.nanl.de/openwrt/telephony.git
src-svn phone svn://svn.openwrt.org/openwrt/feeds/phone
src-svn efl svn://svn.openwrt.org/openwrt/feeds/efl
src-svn xorg svn://svn.openwrt.org/openwrt/feeds/xorg
src-svn desktop svn://svn.openwrt.org/openwrt/feeds/desktop
src-svn xfce svn://svn.openwrt.org/openwrt/feeds/xfce
src-svn lxde svn://svn.openwrt.org/openwrt/feeds/lxde
src-link custom /usr/src/openwrt/custom-feed
一般情况,你至少需要含packages feeds,其他可根据需求下载、安装feeds。
* packages – 提供众多库、工具等基本功能;也是其他feed所依赖的软件源,因此在安装其他feed前一定要先安装packages!
* luci – OpenWrt默认的GUI(WEB管理界面)
* xwrt – 另一种可替换LuCI的GUI
* qpe – DreamBox维护的基于Qt的图形界面,包含Qt2,Qt4,Qtopia,OPIE,SMPlayer等众多图形界面。
* device – DreamBox维护与硬件密切相关的软件,如uboot,qemu等。
* dreambox_packages – DreamBox维护的国内常用网络工具,如oh3c,njit8021xclient等
* desktop -
* xfce -基于Xorg的著名轻量级桌面环境。Xfce建基在GTK+2.x之上,它使用Xfwm作为窗口管理器。
* efl -针对enlightenment
* phone -针对fso, paroli
Trunk中默认的feeds下载有packages、xwrt、luci、routing、telephony。如果你需要其他的软件包,你只需要打开 源码根目录下面的feeds.conf.default文去掉你需要的软件包前面的#号,本教程中使用默认的软件,确定了软件源之后,更新源:
#./scripts/feeds update –a
安装下载好的包:
#./scripts/feeds install -a
4,OpenWrt源码目录结构,执行上面命令之后你就可以得到全部的Openwrt源码。
pillar@monster:~/openwrt/trunk$ ls
bin config docs include Makefile rules.mk target tools BSDmakefile Config.in feeds LICENSE package scripts tmp build_dir dl feeds.conf.default logs README staging_dir toolchain
tools和toolchain包含了一些通用命令,用来生成固件、编译器、和C库;
build dir/host是一个临时目录,用来储存不依赖于目标平台的工具;
build dir/toolchain-<arch>*用来储存依赖于指定平台的编译链,只是编译文件存放目录无需修改;
build dir/ target-<arch>*用来储存依赖于指定平台的软件包的编译文件,其中包括linux内核,u-boot,packages,只是编译文件存放目录无需修改;
staging dir是编译目标的最终安装位置,其中包括rootfs,package, toolchain;
package——软件包的下载编译规则,在OpenWrt固件中,几乎所有东西都是.ipk,这样就可以很方便的安装和卸载;
target目标系统指嵌入式设备,针对不同的平台有不同的特性,针对这些特性,”target/linux”目录下按照平台<arch>进行目录划分,里面包括了针对标准内核的补丁,特殊配置等。
bin 编译完OpenWrt的二进制文件生成目录,其中包括sdk,uImage,u-boot,dts,rootfs构建一个嵌入式系统完整的二进制文件;
config存放着整个系统的的配置文件;
docs里面不断包含了整个宿主机的文件源码的介绍,里面还有Makefile为目标系统生成docs
include里面包括了整个系统的编译需要的头文件,但是是以Make进行连接的;
feeds 扩展软件包索引目录;
scripts组织编译整个OpenWrt的规则;
tmp编译文件夹,一般情况为空;
dl所有软件的下载目录,包括u-boot,kernel;
logs如果编译出错,可以在这里找到编译出错的log。
5,配置OpenWrt系统
#make menuconfig
这时候会弹出和linux一样的配置界面。
在官方trunk版本中已经支持pcDuino硬件了。这里只需要配置OpenWrt系统,使系统支持pcDuino,具体配置如下:
A,配置目标系统(Target System)
Target System (Allwinner A1x/A20/A3x) —>
B,配置目标硬件(Target Profile)
Target Profile (pcDuino) —>
C,配置编译出来的image,配置rootfs文件系统的格式这里选择ext4,rootfs文件系统的大小这里设置(48M)。
Target Images —>
[ ] ramdisk —>
*** Root filesystem archives ***
[ ] cpio.gz
[*] tar.gz
*** Root filesystem images ***
[*] ext4
[ ] jffs2
[ ] squashfs
[*] GZip images
*** Image Options ***
(48) Root filesystem partition size (in MB)
(6000) Maximum number of inodes in root filesystem
(0) Percentage of reserved blocks in root filesystem
[ ] Include kernel in root filesystem —>
[ ] Include DTB in root filesystem
D,选择编译交叉编译器,还有开发SDK
[*] Build the OpenWrt Image Builder
[*] Build the OpenWrt SDK
E,配置无线网卡,V2/V3都是用的rtl8188cus无线网卡
Kernel modules —>
Wireless Drivers —>
-*- kmod-cfg80211…………………. cfg80211 – wireless configuration API
<*> kmod-lib80211……………………………… 802.11 Networking stack
{M} kmod-mac80211………………… Linux 802.11 Wireless Networking Stack
<M> kmod-rtl8192cu………………….. Realtek RTL8192CU/RTL8188CU support
{M} kmod-rtlwifi……………………………. Realtek common driver part
F, LucI系统快速配置接口
LuCI —>
- Collections —>
{*} luci
<M> luci-ssl……………………. Standard OpenWrt set with HTTPS
4. Themes —>
-*- luci-theme-base…………………………. Common base for all
-*- luci-theme-bootstrap……………………… Bootstrap Theme
<*> luci-theme-freifunk-bno……………….. Freifunk Berlin Nordost Theme
<*> luci-theme-freifunk-generic………………….. Freifunk Generic Theme
<*> luci-theme-openwrt……………………………………. OpenWrt.org
5. Translations —>
<*> luci-i18n-chinese………………….. Chinese (by Chinese Translators)
-*- luci-i18n-english………………………………………… English
6,编译OpenWrt系统
#make –j 8 V=s
由于OpenWrt整个系统非常庞大,编译很慢。“-j 8 “表示用8线程进行编译,”V=s”编译的时候显示编译信息。如果你的电脑是4核建议你用8线程进行编译,双核建议你使用4线程。这里测试8线程编译需要一个小时才能编译完成。
由于OpenWrt的u-boot用的是u-boot-2013的版本,目前只支持SD卡启动,而且内核用的是3.12.5版本。另外我们的 3.4.29的内核用的是全志fex,而且3.12.5用的是linux官方的kernel使用的是dts设备树。这样的话我们就不能用之前的BSP方 案,我们要自己做一个从SD卡启动的系统。
pcDuino从SD卡启动顺序是A10—>u-boot–>uImage–>OpenWrt.
根据全志官网的说明,这些软件都必须放在SD卡固定的地址。那么首先要对A10进行分区。根据全志芯片的说明,需要对SD卡进行下表固定分区。
1, 先格式化TF卡前面的1M空间,这里是将TF卡通过读卡器插入到PC的虚拟机。可以看出TF卡的设备是sdb
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 195G 60G 126G 33% /
udev 989M 4.0K 989M 1% /dev
tmpfs 400M 940K 399M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 998M 76K 998M 1% /run/shm
/dev/sdb1 3.8G 12K 3.8G 1% /media/0005-559B
sudo dd if=/dev/zero of=/dev/sdb bs=1M count=1
2, 写入u-boot-spl.bin和u-boot.bin。OpenWrt的生成的二进制文件都在openwrt/trunk/bin/sunxi目录 下,这里OpenWrt做了一些工作将u-boot-spl.bin和u-boot.bin合在了一起,只需要把openwrt-sunxi- pcDuino-sunxi-with-spl.bin写到
#cd uboot-sunxi-pcDuino
pillar@monster:~/openwrt/trunk/bin/sunxi/uboot-sunxi-pcDuino$ ls
openwrt-sunxi-pcDuino-sunxi-spl.bin openwrt-sunxi-pcDuino-u-boot.bin
openwrt-sunxi-pcDuino-sunxi-with-spl.bin
sudo dd if=openwrt-sunxi-pcDuino-sunxi-with-spl.bin of=/dev/sdb bs=1024 seek=8
这时候把SD卡插到板子上,重新上电就会看到下面打印信息
U-Boot 2013.10-rc2 (Jan 15 2014 – 17:48:38) Allwinner Technology
CPU: Allwinner A10 (SUN4I)
Board: pcDuino
I2C: ready
DRAM: 1 GiB
MMC: SUNXI SD/MMC: 0
*** Warning – bad CRC, using default environment
In: serial
Out: serial
Err: serial
Net: emac
Hit any key to stop autoboot: 0
sun4i#
从上面可以看到u-boot已经完全启动了,上面的时间是编译的时间。上面的信息还可以看到我们的环境变量没有设置,它使用的是默认的环境变量。前面介绍,系统建立在分区表不同的地方,但是我们现在SD卡还没有分区表,我们需要先建立分区表再做环境变量。
3, 建立分区表。重新把SD卡插回到电脑的虚拟机里面,使用fdisk创建分区表。具体的分区见下操作,步骤的说明请看# 后面的注释。
pillar@monster:~/openwrt/trunk/bin/sunxi$ sudo fdisk /dev/sdb
[sudo] password for pillar:
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x97bf3019.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)
Command (m for help): m #帮助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu