2010年4月16日星期五

OpenWrt岁月(一)

由于鄙人不想只停留在交换机的开发路上而忘记摘采盛开的路由器野花,面对OpenWrt的诱惑,女朋友走后我和她亲密接触并放纵了几个夜晚(昨天从晚上8点奋战到了今天凌晨1点)。本想透过svn
co svn://svn.openwrt.org/openwrt/trunk/去抓取ING的她,不过却始终无法成功,不知道是否北G*.F$#W所为。无奈选择了http://downloads.openwrt.org/kamikaze/8.09.2/kamikaze_8.09.2_source.tar.bz2

#tar xvfj kamikaze_8.09.2_source.tar.bz2 ./*
#make menuconfig (Target System选择BCM947XX/953XX [2.6])
这个配置方案以下简称"她"
#make

基本上就是上面这几步骤,就可以把方案需要的bin档案生成了,档案放在./bin目录下。插一句:为了更了解她,我加入了一个她的QQ群,进去后我就出来了,里面很像华强北的商家,天天讨论的话题都是刷机刷机刷JJJJ,我当然要的不是刷JJ这么简单,我要了解她的灵魂。

在与她亲密接触的途中,我带着疑惑考察了下面几个要点。

一 Flash分区
嵌入式系统的Flash就像PC的硬盘。硬盘有分区,Flash也有分区。她给Flash分了4个区块,分别是bootloader(cfe),kernel(linux),root
file system(rootfs),configuration(nvram),详细见下面结构:

/build_dir/linux-brcm47xx/linux-2.6.25.20/drivers/mtd/maps/bcm47xx-flash.c
static struct mtd_partition bcm47xx_parts[] = {
{ name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
{ name: "linux", offset: 0, size: 0, },
{ name: "rootfs", offset: 0, size: 0, },
{ name: "nvram", offset: 0, size: 0, },
{ name: NULL, },
};

看到上面是否觉得奇怪,为何offset和size都为0,my god~
我迷糊了,到底每个区块从哪里开始,多大尺寸啊!不过又仔细看了看bcm47xx-flash.c,原来里面有动态分析Flash的代码,然后填充了bcm47xx_parts[]
的 size 和 offset 变量。那么问题又来了:用什么方法分析的呢?原来她的Firmware文件采用TRX文件格式,动态解析TRX
Head就可以分析出个个区块的offset和size了,具体TRX结构的详细信息请参阅
https://forum.openwrt.org/viewtopic.php?id=6938

二 Root File System
在Kernel目录下的.config文件中找到了CONFIG_CMDLINE的定义:
CONFIG_CMDLINE="root=/dev/mtdblock2 rootfstype=squashfs,jffs2
init=/etc/preinit noinitrd console=ttyS0,115200"
所以她采用squashfs或jffs2来作为Root File
System,squashfs是read-only的文件系统,jffs2是r/w都OK的系统,如果你想在Router
Shell下用ipkg(轻量级包管理工具)来动态安装Package,那么推荐使用jffs2作为Root
FS,不过忧着点,Flash不是硬盘,没多少空间,动态df看看有多少结余,别纵欲过度。

三 启动流程
由于CONFIG_CMDLINE把init配置成了/etc/preinit,所以内核启动后会执行/etc/preinit,
preinit会做根文件系统的加载动作,然后执行/sbin/init。

/build_dir/mipsel/root-brcm47xx/etc/inittab
::sysinit:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K stop
tts/0::askfirst:/bin/ash --login
ttyS0::askfirst:/bin/ash --login
tty1::askfirst:/bin/ash --login

/build_dir/mipsel/root-brcm47xx/etc/init.d/rcS
#!/bin/sh
# Copyright (C) 2006 OpenWrt.org

run_scripts() {
for i in /etc/rc.d/$1*; do
[ -x $i ] && $i $2 2>&1
done | $LOGGER
}

LOGGER="cat"
[ -x /usr/bin/logger ] && LOGGER="logger -s -p 6 -t sysinit"

if [ "$1" = "S" ]; then
run_scripts "$1" "$2" &
else
run_scripts "$1" "$2"
fi

看了inittab和rcS就很清楚了,接下来就是执行/etc/rc.d/S*,基本上就是初始化各种各样应用程序的脚本。

四 让我苦闷一晚的Web
在/etc/rd.d/下有一个文件叫S50httpd,是负责开启http server的脚本,她采用busybox内潜的httpd做http server
anders@ubuntu:~/share/openwrt/8.09.2/build_dir/mipsel/root-brcm47xx/usr/sbin$
ls -l
lrwxrwxrwx 1 anders anders 17 2010-04-16 14:21 httpd -> ../../bin/busybox
httpd是用www作为web目录的,于是我进入了www目录,发现里面竟然空空如野,WHY?她没有Web?那还玩个P :-(
不过,这么有名气的她,怎么可能没Web呢?上google搜索来搜索去都有web啊,为何我没有?(先讲讲五再回头说这个)

五 IPKG的包管理方式
In an OpenWrt firmware, almost everything is an .ipk, a software
package which can be added to the firmware to provide new features or
removed to save space. Note that packages are also maintained outside
of the main trunk and can be obtained from subversion using the
package feeds system:
$ ./scripts/feeds update
Those packages can be used to extend the functionality of the build
system and need to be symlinked into the main trunk. Once you do that,
the packages will show up in the menu for configuration. From kamikaze
you would do something like this:
$ ./scripts/feeds search nmap
Search results in feed 'packages':
nmap Network exploration and/or security auditing utility
$ ./scripts/feeds install nmap
To include all packages, issue the following command:
$ make package/symlinks

原来她有几个仓库,仓库里面有N多软件,web只是仓库的软件之一了,于是我用下面的方法安装了她的web:
anders@ubuntu:~/share/openwrt/8.09.2$./scripts/feeds update packages luci
anders@ubuntu:~/share/openwrt/8.09.2$./scripts/feeds install -a -p luci
anders@ubuntu:~/share/openwrt/8.09.2$make menuconfig
Administration
- LuCI Components
- luci-admin-full: M
anders@ubuntu:~/share/openwrt/8.09.2$make
anders@ubuntu:~/share/openwrt/8.09.2/build_dir/mipsel/root-brcm47xx/www$ ls
cgi-bin index.html luci-static resources


(To be continued..)