天天看點

搭建自己的OpenWrt開發環境

1、  安裝環境

Linux系統,如果在CentOS上操作,需安裝如下依賴包:

yum install binutils bzip2 gawk gcc gcc-c++ gettext makencurses-devel patch unzip wget zlib-devel

yum install subversion screen

2、  下載下傳OpenWRT源碼

mkdir /usr/src/test

cd /usr/src/test

svn co svn://svn.openwrt.org/openwrt/trunk (開發版本,不穩定,不建議下載下傳)

3、  檢查配置環境

必須使用非root使用者進行操作,添加一個普通使用者。(注意:如果使用root進行操作的話,會提示檢查失敗:Checking 'non-root'...failed.)

[root@localhost trunk]# adduser openwrt  

[root@localhost trunk]# su openwrt

為了防止檔案讀寫權限造成問題,需要将源碼copy到openwet的根目錄下進行所有操作。

[openwrt@localhost openwrt]$ cd ~

[openwrt@localhost ~]$ cp -r /usr/src/test/trunk ./

[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

4、  下載下傳feeds

Feeds是OpenWrt環境所需要的軟體包套件。最重要的feeds有:

‘packages’一些額外的基礎路由器特性軟體

‘LuCI’OpenWrt預設的GUI

‘Xwrt’另一種可選的GUI界面

需要能夠連接配接網際網路。

在下載下傳之前可以通過檢視’feeds.conf.default’檔案,來檢查哪些檔案需要包含在環境中。

開始下載下傳,使用:

[openwrt@localhost trunk]$ ./scripts/feeds update -a

安裝feeds包,隻有安裝之後,在後面的makemenuconfig時,才可以對相關配置進行勾選。

[openwrt@localhost trunk]$ ./scripts/feeds install -a

如果更新了feeds的配置檔案,需要添加新的軟體包用于生成系統。隻需進行重複操作:

5、  進行配置

編譯過程使用的交叉編譯,交叉編譯生成的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添加:其實LinksysWrt54gs v3.0是支援Linux 2.6版本的,TargetSystem 選擇 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進行了選擇;勾選了Advancedconfiguration option和Build the OpenWrt SDK選項。

6、  編譯

(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 |teebuild.log |grep -i error

說明:将編譯的所有輸出資訊儲存在build.log中,将error資訊列印在螢幕上。

(6)一個複雜指令的應用

[openwrt@localhost trunk]$ ionice -c 3 nice -n 20 make -j 2V=99 CONFIG_DEBUG_SECTION_MISMATCH=y 2>&1 \|tee build.log |egrep -i'(warn|error)'

說明:将編譯的所有輸出資訊儲存在build.log中,将error和warning資訊列印在螢幕上。編譯過程使用雙核CPU,占用背景資源。

7、  生成鏡像(Image)位置

新生成的鏡像會預設放在建立的一個bin目錄下。例如:/bin/brcm-2.4/packages

[openwrt@localhost trunk]$ls bin/*

将編譯好的鏡像做個備份,例如備份到/目錄下:

[openwrt@localhost trunk]$cp bin /

8、  清理工作

建議現在清理編譯産生的檔案,以免下次編譯時造成沖突,(檔案如果存在的話,将不會被替換),執行makeclean

注意:在執行clean指令,確定已經将編譯好的image進行了備份。清理工作會清楚bin目錄。

[openwrt@localhost trunk]$make clean  

除了清除生成的目錄,還想清除交叉編譯工具(以及工具鍊目錄)

[openwrt@localhost trunk]$make dirclean

清除所有相關的東西,包括下載下傳的軟體包,配置檔案,feed内容等:(不建議使用)

[openwrt@localhost trunk]$make distclean

對于更新feeds後出現的錯誤:

ERROR:please fixpackage/feeds/packages/mc/Makefile 等類似的問題,需要執行這條語句進行系統的清理

9、 安裝OpenWrt

找到對應的固件,進行固件更新。網上方法很多,這裡不再贅述。

後續會推出在OpenWrt上的開發文檔:)