使用Busybox製作根文件系統

來自:http://blog.csdn.net/xiaozhi_su/archive/2009/05/21/4206678.aspx

 

選定 busybox-1.9.2.tar.bz2 這個版本以靜態方式編譯即生成的 busybox 不需要共享庫的支持就能運行。這樣做我們就不需要佈署程序庫了。缺點是自己寫的 arm-linux 程序在這個根文件系統中是不能運行的,因爲缺少共享程序庫的支持。不過不用擔心,通過在目標機裏以掛接 NFS 的方式將宿主機的 arm-linux-gcc 編譯器的庫文件掛到 arm-linux  /lib 就可完美的運行我們自己的程序了。

現在開始製作靜態鏈接庫的根文件系統。

 

1、準備根文件系統

首先準備製作工具BusyBox1.9.2

準備交叉編譯工具arm-linux-gcc 3.3.2

在機器上建立rootfs的文件夾

#mkdir rootfs

rootfs中建立linux系統中典型的文件夾

#cd rootfs

#mkdir root home bin sbin etc dev usr lib tmp mnt sys proc

#mkdir usr/lib usr/bin

#pwd

/home/su/rootfs

2解壓源碼包
    #tar xjf busybox-1.9.2.tar.bz2
    #cd busybox-1.9.2
3
、修改 Makefile,

#vi Makefile
   
Makefile中的

CROSS_COMPILE ?=
    
改爲
    CROSS_COMPILE ?= /usr/local/arm/3.3.2/bin/arm-linux-

:這個版本的 busybox  3.4.1  arm-linux-gcc 編譯有些問題 3.3.2 版則可順利編譯。
4
、定製 busybox
    
選擇busybox下全部的可執行程序

#make defconfig

進到配置選項

#make menuconfig

設置靜態編譯方式
    Busybox Settings ---> Build Options ---> [*] Build BusyBox as a static binary (no shared libs)

Busybox Settings ---> Install Options ---> 中輸入建立根文件系統的文件所在的路徑/home/su/rootfs

其它的默認。
    
確保 [*] Build BusyBox as a static binary (no shared libs) 被選中,保存退出
 5
、執行 make 編譯
    #make

    編譯出錯信息如下:
    applets/applets.c:15:2: warning: #warning Static linking against glibc produces buggy executables
    applets/applets.c:16:2: warning: #warning (glibc does not cope well with ld --gc-sections).
    applets/applets.c:17:2: warning: #warning See sources.redhat.com/bugzilla/show_bug.cgi?id=3400
    applets/applets.c:18:2: warning: #warning Note that glibc is unsuitable for static linking anyway.
    applets/applets.c:19:2: warning: #warning If you still want to do it, remove -Wl,--gc-sections
    applets/applets.c:20:2: warning: #warning from scripts/trylink and remove this warning.
    applets/applets.c:21:2: error: #error Aborting compilation.
    make[1]: *** [applets/applets.o] Error 1

    按照提示,修改文件 applets/applets.c  21 
    #error Aborting compilation.
    
註釋掉:

    執行 make 重新編譯
    #make

    編譯通過, busybox 被生成了然後執行
    #make install

busybox 就被安裝到指定的路徑下了/home/su/rootfs,這時可發現rootfs下多了個liunxrc的文件,binsbin下也多了很多文件。用ls –l命令查看其中的一個文件,可發現其是鏈接到busybox的一個連接符,所以我們之後在目標機上運行的命令大多都會調用busybox這個文件的。

若之前忘了指定路徑,默認生成到臨時目錄busybox-1.9.2/_install 下了。

6、編寫配置/etc下的初始化程序(可省略)

    最簡單的做法是把busybox-1.9.2/examples/bootfloppy/etc下的全部文件拷到目標文件的etc目錄下

    #cd /home/su/busybox-1.9.2/examples/bootfloppy/etc

#cp –rf * /home/su/rootfs/etc

也可自己寫這些文件。

7、把rootfs做成鏡像

    #mkcramfs rootfs rootfs.cramfs

8、把rootfs.cramfs燒寫到目標機中。

9、運行目標機

    這時會遇到一個錯誤信息:

    Can’t open tty2 

Can’t open tty3

Can’t open tty4

    解決辦法:把/rootfs/etc/ inittab 文件的第三行“tty2::askfirst:-bin/sh”刪除掉。

返回到第7步重做。

 

 

 

 

    現實中,動態編譯的方法更適合工程的需要,所以一般是採用動態的方法編譯根文件系統的。若選擇動態編譯的辦法,大體方法還是一樣的,存在一些不同之處是:

不同之處之一是:

    進到配置選項

#make menuconfig

選擇動態方式

Busybox Settings ---> Build Options ---> [*] Build Shared libbusybox

不同之處之二是:

上面靜態編譯出現的出錯信息不會出現了,所以不需對程序做任何修改,但還是必須用arm-linux-gcc 3.3.2編譯,否則還是會有麻煩。

不同之處之三是(最大的不同之處):

編譯完成後,需進到rootfs目錄的lib中,往裏面添加一些庫文件

#cd /home/su/rootfs/lib

這裏有點麻煩,我怎麼知道需要什麼庫文件的支持呢?

最簡單的辦法是把arm-linux-gcc 3.3.2下的整個lib庫拷進來,簡單省事。但是這麼做存在一個問題,做出的根文件系統非常大。

另一個辦法是:

#cd /home/su/rootfs/bin

#arm-linux-readelf busybox | grep shared

這樣就可以顯示出系統運行起來需要什麼庫文件,再把相應的庫文件拷到/home/su/rootfs/lib下。一般而言,系統庫用到兩個:動態鏈接器ld-linux.soc函數庫GlibcGlibc包括:

ld-linux:動態鏈接庫,必需

libc: 標準c函數庫,必需

libm: 數學庫,一般需要

libdl: 用於動態裝載共享庫,較少用到

libcrypt: 加密附加庫,需要認證的程序用到,較少用

libpthread: POSIX線程庫,一般需要

如果需要某個函數庫,我們可以將這些庫和對應的符號鏈接拷到目標根文件系統的/lib目錄下。簡單起見,應該使用-d選項或-a選項調用cp命令,這樣可保留完整的符號鏈接信息。

例:

#cp –a libc.so.6 /home/su/rootfs/lib/

爲了減少運行時庫的大小,我們應該使用交叉編譯版本即arm-linux-gcc 3.3.2strip工具來處理根文件系統的庫文件,把二進制文件中的包含的符號表和調試信息刪除掉。

例:

#arm-linux-strip /home/su/rootfs/lib/*.so

 

 

 

 

注意:

    使用busybox做文件系統時,運行make命令,系統會馬上顯示:

    沒有/dev/null這個文件

但是還是能最終編譯出根文件系統,問題出在重啓linux系統,機器進不去了。提示出錯,信息如下:

/etc/rc.d/rc.sysinit: line 173:/dev/null: read-only file system

 /etc/rc.d/rc.sysinit: line 173:/dev/null: read-only file system

 /etc/rc.d/rc.sysinit: line 184:/dev/null: read-only file system

 /etc/rc.d/rc.sysinit: line 184:/dev/null: read-only file system

 /etc/rc.d/rc.sysinit: line 200:/dev/null: read-only file system

  .

  .

  .

 ***An error occured during the file system check.

 ***Dropping you to a shell;the system will reboot

 ***when you leave the shell

 Give root password for maintenance

 (or type Control-D to continue):

解決辦法:

    按提示輸入root用戶的密碼,回車,可看到

    Repair filesystem1#:

    依次輸入命令:

Repair filesystem1# mount -n -o remount,rw /

    Repair filesystem1# rm -f /dev/null

    Repair filesystem1# mknod -m 0666 /dev/null c 1 3

    Repair filesystem1# reboot

問題解決。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章