現在開始制作靜態鏈接庫的根文件系統。
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的文件,bin、sbin下也多了很多文件。用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.so和c函數庫Glibc,Glibc包括:
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.2的strip工具來處理根文件系統的庫文件,把二進制文件中的包含的符號表和調試信息刪除掉。
例:
#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 filesystem)1#:
依次輸入命令:
(Repair filesystem)1# mount -n -o remount,rw /
(Repair filesystem)1# rm -f /dev/null
(Repair filesystem)1# mknod -m 0666 /dev/null c 1 3
(Repair filesystem)1# reboot
問題解決。