使用busybox-1.13.3 創建Linux根文件系統

  首先我使用的busybox是1.13.3版本!此製作過程也是參考網絡上各位高手的文章,加上自己的摸索與嘗試,在此留下必要的札記。

1:配置Busybox

進入解壓後的目錄,配置Busybox
[~busybox-1.13.3]#make menuconfig

###############################################################
 Busybox Settings >
 General Configuration >
 [*] Support for devfs
 Build Options >
 [*] Build BusyBox as a static binary (no shared libs)
/* 將busybox編譯爲靜態連接,少了啓動時找動態庫的麻煩 */
 [*] Do you want to build BusyBox with a Cross Compiler?
 (/usr/local/arm/3.4.1/bin/arm-linux-)
 Cross Compiler prefix
/* 指定交叉編譯工具路徑 */
 Init Utilities >
 [*] init
 [*] Support reading an inittab file
/* 支持init讀取/etc/inittab配置文件,一定要選上 */
 Shells >
 Choose your default shell (ash) >
/* (X) ash 選中ash,這樣生成的時候纔會生成bin/sh文件
* 看看我們前頭的linuxrc腳本的頭一句:
* #!/bin/sh 是由bin/sh來解釋執行的
*/
[*] ash
###################################################################################
另外,按照他的這種方法做出來的文件系統,運行的時候 shell 並不好有,沒有歷史記錄、自動補全、刪除字符的功能,下面介紹如何爲它添加這些功能: 
               Shells ---> 
---   Bourne Shell Options                                            
   [ ]   Hide message on interactive shell startup                    
   [ ]   Standalone shell                                             
   [*]   command line editing                                         
   [*]     vi-style line editing commands                             
      (15)    history size                                           
   [*]     history saving                                             
   [*]     tab completion                                             
   [*]       username completion                                     
   [ ]     Fancy shell prompts
###################################################################################
 Coreutils >
[*] cp
[*] cat
[*] ls
[*] mkdir
[*] echo (basic SuSv3 version taking no options)
[*] env
[*] mv
[*] pwd
[*] rm
[*] touch
Editors >
[*] vi
Linux System Utilities >
[*] mount
[*] umount
[*] Support loopback mounts
[*] Support for the old /etc/mtab file
Networking Utilities >
##########################################################
 Linux Module Utilities  ---> 
        
[*] insmod                  
        
[*] rmmod                                   
        
[*] lsmod                   
        
[*]   lsmod pretty output for 2.6.x Linux kernels                     
        
[*] modprobe                                             
       
[*]   Multiple options parsing               
                     ---   Options common to multiple modutils               
        
[*]   Support tainted module checking with new kernels             
       
[ ]   Support version 2.2.x to 2.4.x Linux kernels    //此項一定不要選!!!
       
[*]   Support version 2.6.x Linux kernels                                         
########################################################################
退出並保存config選項。然後:
[~busybox-1.13.3]#export PATH={交叉編譯器路徑}:$PATH
[~busybox-1.13.3]#make ARCH=arm CROSS_COMPILE=arm-linux-    install
此處的選項ARCH、CROSS_COMPILE是爲了明確目標平臺以及所使用的交叉編譯器。非此版本的Busybox有可能不是ARCH或CROSS_COMPILE,具體名稱可以從Makefile中可以找到。
 
編譯完成後,在_install目錄下會生成目標平臺的相關根文件。
 
以下說明並沒有真正確認過其可行性,在此不做詳細解釋。
如果你是使用的1.4以上的版本,交叉編譯同編譯內核一樣,需要修改Makefile中的arch=arm     CROSS_COMPILE=arm-linux-
然後#make xconfig
#make
#make install


2.用shell腳本創建根文件系統的目錄結構,並在想要建立根文件系統的地方運行此腳本

sh腳本文件build_fs.sh 內容:

##############################################
#!/bin/sh 
echo "makeing rootdir" 
mkdir rootfs 
cd rootfs 
 
echo "makeing dir: bin dev etc lib proc sbin sys usr" 
mkdir bin dev etc lib proc sbin sys usr #8 dirs 
mkdir usr/bin usr/lib usr/sbin lib/modules 
 
#Don't use mknod, unless you run this Script as 
mknod -m 600 dev/console c 5 1 
mknod -m 666 dev/null c 1 3 
 
echo "making dir: mnt tmp var" 
mkdir mnt tmp var 
chmod 1777 tmp 
mkdir mnt/etc mnt/jiffs2 mnt/yaffs mnt/data mnt/temp 
mkdir var/lib var/lock var/log var/run var/tmp 
chmod 1777 var/tmp 
 
echo "making dir: home root boot" 
mkdir home root boot 
echo "done" 
####################################################

執行這個sh: 
[~busybox-1.13.3]#./build_fs.sh 
makeing rootdir 
makeing dir: bin dev etc lib proc sbin sys usr 
making dir: mnt tmp var 
making dir: home root boot 
done 
創建出一個主文件夾rootfs,裏面有一批文件: 
[~busybox-1.13.3]# cd rootfs/ 
[~rootfs]# ls 
bin boot dev etc home lib mnt proc root sbin sys tmp usr var 
 
4、把busybox源碼目錄下的etc的內容拷貝到這裏的etc下 
[~rootfs]# cd etc/ 
[~etc]# ls 
[~etc]# cp -a /opt/develop/lyj/common/porting/rootfs/busybox-1.12.2/examples/bootfloppy/etc/* ./ 
[~etc]# ls 
fstab init.d inittab profile 
 
5、修改拷貝過來的profile文件 
[~etc]# vi profile 
# /etc/profile: system-wide .profile file for the Bourne shells 
echo "Processing /etc/profile" 
# no-op 
# Set search library path 
echo " Set search library path" 
export LD_LIBRARY_PATH=/lib:/usr/lib 
# Set user path 
echo " Set user path" 
PATH=/bin:/sbin:/usr/bin:/usr/sbin 
export PATH 
# Set PS1 
echo " Set PS1" 
HOSTNAME=`/bin/hostname` 
# 此處讓shell提示符顯示host名稱的。是`,不是’,要注意!
# 會在進入根系統後顯示Jacky 
export PS1="//e[32m[$USER@$HOSTNAME //w//a]//$//e[00;37m " 
# 此處//e[32m是讓後面的“[$USER@$HOSTNAME //w//a]”顯示爲綠色 
//e[00是關閉效果 
//e[05是閃爍 
# 37m是讓後面的顯示爲白色 
# 多個命令可以;號隔開

echo "All done!" 
echo 
 
6、修改初始化文件inittab和fstab 
Inittab 
[~etc]# vi inittab 
::sysinit:/etc/init.d/rcS 
::respawn:-/bin/sh 
::restart:/sbin/init 
tty2::askfirst:-/bin/sh 
::ctrlaltdel:/bin/umount -a -r 
::shutdown:/bin/umount -a -r 
::shutdown:/sbin/swapoff –a 
Fstab 
[~etc]# vim fstab 
proc /proc proc defaults 0 0 
none /tmp ramfs defaults 0 0 
mdev /dev ramfs defaults 0 0 
sysfs /sys sysfs defaults 0 0 
 
 
7、修改初始化的腳本文件init.d/rcS 
 
[~etc]# vi init.d/rcS 
#! /bin/sh 
echo "Processing etc/init.d/rc.S" 
#hostname ${HOSTNAME} 
hostname quickengine-tech
echo " Mount all" 
/bin/mount -a 
echo " Start mdev...." 
/bin/echo /sbin/mdev > proc/sys/kernel/hotplug 
mdev -s 
echo "****************************************************" 
echo " rootfs by NFS, s3c2440" 
echo " Created by Anson Liu@ 2009.11.05" 
echo " Good Luck" 
echo " www.quickengine-tech.com
echo "****************************************************" 
echo 
 

8、創建一個空的mdev.conf文件,在掛載根文件系統時會用到的 
[~etc]# touch mdev.conf

9、從本機拷貝passwd、shadow、group文件。 
[~ etc]# cp /etc/passwd . 
[~etc]# cp /etc/shadow . 
[~etc]# cp /etc/group . 
修改passwd文件,把第一行和最後一行的bash修改成ash。 
 
10、把busybox默認安裝目錄中的文件全部複製到這裏的rootfs中。

會發現多了linuxrc -> bin/busybox文件,這是掛載文件系。 
[~etc]# cd .. 
[~rootfs]# cp -Rfv /opt/develop/lyj/common/porting/rootfs/busybox-1.12.2/_install/* ./

OK,以上用busybox創建了一個基本的文件系統。 
PS: 
如果編譯busybox時選擇動態庫方式編譯,則需要查看生成的busybox使用哪些動態庫,然後把它們拷貝到rootfs/lib目錄下。 
[~lib]# arm-linux-readelf -d ../bin/busybox 
Dynamic section at offset 0xc1014 contains 21 entries: 
Tag Type Name/Value 
0x00000001 (NEEDED) Shared library: [libm.so.6] 
0x00000001 (NEEDED) Shared library: [libc.so.6] 
0x0000000c (INIT) 0xc2ec 
0x0000000d (FINI) 0xa96b8 
0x00000004 (HASH) 0x80e8 
0x00000005 (STRTAB) 0xa4c4 
0x00000006 (SYMTAB) 0x8b64 
0x0000000a (STRSZ) 3505 (bytes) 
0x0000000b (SYMENT) 16 (bytes) 
0x00000015 (DEBUG) 0x0 
0x00000003 (PLTGOT) 0xd10e4 
0x00000002 (PLTRELSZ) 3112 (bytes) 
0x00000014 (PLTREL) REL 
0x00000017 (JMPREL) 0xb6c4 
0x00000011 (REL) 0xb674 
0x00000012 (RELSZ) 80 (bytes) 
0x00000013 (RELENT) 8 (bytes) 
0x6ffffffe (VERNEED) 0xb5a4 
0x6fffffff (VERNEEDNUM) 2 
0x6ffffff0 (VERSYM) 0xb276 
0x00000000 (NULL) 0x0 
可以看出,使用了libm.so.6和libc.so.6兩個庫。發現只拷貝這兩個庫還不夠,還需要ld-linux.so.2和libgcc_s.so.1,也就是我編譯出來的這個busybox需要4個動態庫文件。 
# cp /opt/crosstools/gcc-3.4.6-glibc-2.3.6/lib/libm.so.6 . 
# cp /opt/crosstools/gcc-3.4.6-glibc-2.3.6/lib/libc.so.6 . 
# cp /opt/crosstools/gcc-3.4.6-glibc-2.3.6/lib/ld-linux.so.2 . 
# cp /opt/crosstools/gcc-3.4.6-glibc-2.3.6/lib/libgcc_s.so.1 . 
這樣,使用動態庫可以節省一半左右的空間,不過效率有所降低。

準備鏈接庫 
#cd ${OBJ_LIB}/lib (${OBJ_LIB}是交叉編譯環境的目錄) 
#for file in libc libcrypt libdl libm / 
>libpthread libresolv libutil 
>do 
>cp $file-*.so /home/fortis/rootfs/lib
>cp -d $file.so.[*0-9] /home/fortis/rootfs/lib
>done 
#cp -d ld*.so* /home/fortis/rootfs/lib

11.測試

1)、在本機修改/etc/export文件,重啓NFS服務: 
[root@vm-dev root]# vi /etc/exports
/opt/lyj             192.168.1.*(rw,insecure,sync,no_root_squash)
/opt/develop/lyj/common/porting/rootfs/root_stand/reset/rootfs             192.168.1.*(rw,insecure,sync,no_root_squash)
[root@vm-dev root]# service nfs restart
 
2)、在Bootloader中傳遞以下參數給Kernel:
root=/dev/nfs rw nfsroot=192.168.1.152:/opt/develop/lyj/common/porting/rootfs/root_stand/reset/rootfs ip=192.168.1.155:192.168.1.152:192.168.1.254:255.255.255.0:Jacky:eth0:off console=ttySAC0,115200 init=/linuxrc noinitrd
 
3)、啓動目標板,串口輸出顯示根文件系統已經加載成功: 
U-Boot 1.3.2 (Nov 27 2016 - 17:43:03)
 
DRAM:  64 MB
Flash: 512 kB
NAND:  64 MiB
In:    serial
Out:   serial
Err:   serial
Found DM9000 ID:90000a46 at address 10000000 !
DM9000 work in 16 bus width
bd->bi_entaddr: 08:00:3e:26:0a:5b
[eth_init]MAC:8:0:3e:26:a:5b:
Hit any key to stop autoboot:  0 
 
NAND read: device 0 offset 0x80000, size 0x1b0000
 1769472 bytes read: OK
## Booting image at 30008000 ...
   Image Name:   Linux-2.6.24.4
   Created:      2016-11-27   7:24:11 UTC
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    1697964 Bytes =  1.6 MB
   Load Address: 30008000
   Entry Point:  30008040
   Verifying Checksum ... OK
test:hdr->ih_type:2
test:hdr->ih_comp:0
   XIP Kernel Image ... OK
test:hdr->ih_type:2
test:hdr->ih_os:5
 
Starting kernel ...
 
test:machid:805306624
test: bi_boot_params:0x33f5bfb8
test:starting 1
Uncompressing Linux............................................................................................................ done, booting the kernel.
Linux version 2.6.24.4 (root@vm-dev) (gcc version 3.4.6) #94 Thu Nov 27 10:02:26 CST 2008
CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=00007177
Machine: SMDK2410
Memory policy: ECC disabled, Data cache writeback
CPU S3C2410A (id 0x32410002)
S3C2410: core 202.800 MHz, memory 101.400 MHz, peripheral 50.700 MHz
S3C24XX Clocks, (c) 2004 Simtec Electronics
CLOCK: Slow mode (1.500 MHz), fast, MPLL on, UPLL on
CPU0: D VIVT write-back cache
CPU0: I cache: 16384 bytes, associativity 64, 32 byte lines, 8 sets
CPU0: D cache: 16384 bytes, associativity 64, 32 byte lines, 8 sets
Built 1 zonelists in Zone order, mobility grouping on.  Total pages: 16256
Kernel command line: root=/dev/nfs rw nfsroot=192.168.1.152:/opt/develop/lyj/common/porting/rootfs/root_stand/reset/rootfs ip=192.168.1.155:192.168.1.152:192.168.1.254:255.255.255.0:Jacky:eth0:off console=ttySAC0,115200 init=/linuxrc noinitrd
irq: clearing subpending status 00000002
PID hash table entries: 256 (order: 8, 1024 bytes)
timer tcon=00500000, tcnt a509, tcfg 00000200,00000000, usec 00001e4c
Console: colour dummy device 80x30
console [ttySAC0] enabled
Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)
Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)
Memory: 64MB = 64MB total
Memory: 61324KB available (3088K code, 316K data, 132K init)
Mount-cache hash table entries: 512
CPU: Testing write buffer coherency: ok
net_namespace: 64 bytes
NET: Registered protocol family 16
S3C2410 Power Management, (c) 2004 Simtec Electronics
S3C2410: Initialising architecture
S3C24XX DMA Driver, (c) 2003-2004,2006 Simtec Electronics
DMA channel 0 at c4800000, irq 33
DMA channel 1 at c4800040, irq 34
DMA channel 2 at c4800080, irq 35
DMA channel 3 at c48000c0, irq 36
SCSI subsystem initialized
usbcore: registered new interface driver usbfs
usbcore: registered new interface driver hub
usbcore: registered new device driver usb
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 2048 (order: 2, 16384 bytes)
TCP bind hash table entries: 2048 (order: 1, 8192 bytes)
TCP: Hash tables configured (established 2048 bind 2048)
TCP reno registered
NetWinder Floating Point Emulator V0.97 (double precision)
yaffs Nov 27 2008 09:58:27 Installing. 
io scheduler noop registered
io scheduler anticipatory registered (default)
io scheduler deadline registered
io scheduler cfq registered
Console: switching to colour frame buffer device 80x30
fb0: s3c2410fb frame buffer device
lp: driver loaded but no devices found
ppdev: user-space parallel port driver
Serial: 8250/16550 driver $Revision: 1.90 $ 4 ports, IRQ sharing enabled
s3c2410-uart.0: s3c2410_serial0 at MMIO 0x50000000 (irq = 70) is a S3C2410
s3c2410-uart.1: s3c2410_serial1 at MMIO 0x50004000 (irq = 73) is a S3C2410
s3c2410-uart.2: s3c2410_serial2 at MMIO 0x50008000 (irq = 76) is a S3C2410
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
loop: module loaded
DM9000: dm9k_init_module
Board init for dm9000a finished!
<DM9KS> I/O: c480e000, VID: 90000a46 
eth0: at 0xc480e000 IRQ 18
eth0: Ethernet addr: 08:00:3e:26:0a:5b
Uniform Multi-Platform E-IDE driver Revision: 7.00alpha2
ide: Assuming 50MHz system bus speed for PIO modes; override with idebus=xx
Driver 'sd' needs updating - please use bus_type methods
S3C24XX NAND Driver, (c) 2004 Simtec Electronics
s3c2410-nand s3c2410-nand: Tacls=3, 29ns Twrph0=7 69ns, Twrph1=3 29ns
NAND device: Manufacturer ID: 0xec, Chip ID: 0x76 (Samsung NAND 64MiB 3,3V 8-bit)
NAND_ECC_NONE selected by board driver. This is not recommended !!
Scanning device for bad blocks
Creating 4 MTD partitions on "NAND 64MiB 3,3V 8-bit":
0x00000000-0x00080000 : "Boot Agent"
0x00080000-0x00280000 : "S3C2410 kernel"
0x00280000-0x00680000 : "S3C2410 rootfs"
0x00680000-0x04000000 : "user"
usbmon: debugfs is not available
116x: driver isp116x-hcd, 03 Nov 2005
s3c2410-ohci s3c2410-ohci: S3C24XX OHCI
s3c2410-ohci s3c2410-ohci: new USB bus registered, assigned bus number 1
s3c2410-ohci s3c2410-ohci: irq 42, io mem 0x49000000
usb usb1: configuration #1 chosen from 1 choice
hub 1-0:1.0: USB hub found
hub 1-0:1.0: 2 ports detected
Initializing USB Mass Storage driver...
usbcore: registered new interface driver usb-storage
USB Mass Storage support registered.
mice: PS/2 mouse device common for all mice
s3c2410 TouchScreen successfully loaded
input: s3c2410 TouchScreen as /class/input/input0
S3C24XX RTC, (c) 2004,2006 Simtec Electronics
s3c2410-rtc s3c2410-rtc: rtc disabled, re-enabling
s3c2410-rtc s3c2410-rtc: rtc core: registered s3c as rtc0
i2c /dev entries driver
s3c2410-i2c s3c2410-i2c: slave address 0x10
s3c2410-i2c s3c2410-i2c: bus frequency set to 99 KHz
s3c2410-i2c s3c2410-i2c: i2c-0: S3C I2C adapter
S3C2410 Watchdog Timer, (c) 2004 Simtec Electronics
s3c2410-wdt s3c2410-wdt: watchdog inactive, reset disabled, irq enabled
TCP cubic registered
NET: Registered protocol family 1
RPC: Registered udp transport module.
RPC: Registered tcp transport module.
s3c2410-rtc s3c2410-rtc: hctosys: invalid date/time
IP-Config: Complete:
      device=eth0, addr=192.168.1.155, mask=255.255.255.0, gw=192.168.1.254,
     host=Jacky, domain=, nis-domain=(none),
     bootserver=192.168.1.152, rootserver=192.168.1.152, rootpath=
Looking up port of RPC 100003/2 on 192.168.1.152
Looking up port of RPC 100005/1 on 192.168.1.152
VFS: Mounted root (nfs filesystem).
Freeing init memory: 132K
init started: BusyBox v1.12.2 (2016-11-27 14:55:55 CST)
starting pid 785, tty '': '/etc/init.d/rcS'
Processing etc/init.d/rc.S
 Mount all
 Start mdev....
****************************************************
 rootfs by NFS, s3c2410
 Created by lyj_uptech @ 2008.11.28
 Good Luck
 www.up-tech.com
****************************************************
 
 
Please press Enter to activate this console. 
starting pid 790, tty '': '-/bin/sh'
 
Processing /etc/profile... Done
 
Processing /etc/profile
 Set search library path
 Set user path
 Set PS1
All done!
 
[root@up-tech /]# ls
bin      dev      home     linuxrc  proc     sbin     tmp      var
boot     etc      lib      mnt      root     sys      usr
[root@up-tech /]#
 
已經直接進入了nfs文件系統!

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