構建嵌入式Linux根文件系統

移植好了UBOOT和內核以後,即是構建linux根文件系統了。以 http://blog.chinaunix.net/u1/34474/showart_485837.html文章爲基礎進行修改。
1)建立根文件系統的基本目錄結構。
我把這個過程作成了shell腳本(文件名爲 mkroot) ,很方便!

#! /bin/sh
    echo "creatint rootfs dir......"
    mkdir rootfs
    cd rootfs

    echo "making dir : bin dev etc lib proc sbin sys usr"
    mkdir bin dev etc lib proc sbin sys usr #必備的8個目錄
    mkdir usr/bin usr/lib usr/sbin lib/modules
html


# Don't use mknod ,unless you run this Script as root !
# mknod -m 600 dev/console c 5 1
# mknod -m 666 dev/null c 1 3node

 

    echo "making dir : mnt tmp var"
    mkdir mnt tmp var
    chmod 1777 tmp
    mkdir mnt/etc mnt/jffs2 mnt/yaffs mnt/data mnt/temp
    mkdir var/lib var/lock var/log var/run var/tmp 
    chmod 1777 var/tmp
linux

 

    echo "making dir : home root boot"
    mkdir home root boot
shell

    echo "done"less

 

在你想要創建根文件系統的地方,運行:

[HongGe@Hzh1024n]$ ./mkroot
creatint rootfs dir......
making dir : bin dev etc lib proc sbin sys usr
making dir : mnt tmp var
making dir : home root boot
done
[HongGe@Hzh1024n]$ cd rootfs/dev/
[HongGe@Hzh1024n dev]$ su
口令:
[root@Hzh1024n dev]# mknod -m 600 console c 5 1;mknod -m 666 null c 1 3;exit
exit
[HongGe@Hzh1024n dev]$
tcp

2)配置、編譯和安裝Busybox-1.9.0ide

[HongGe@Hzh1024n source]$ tar -xjvf busybox-1.9.0.tar.bz2
工具

修改Makefile文件: oop

[HongGe@Hzh1024n busybox-1.9.0]$ pwd
/home/work/hzh1024n/source/busybox-1.9.0
[HongGe@Hzh1024n busybox-1.9.0]$ kwrite Makefile

......(第174行附近)
#ARCH        ?= $(SUBARCH)
#CROSS_COMPILE    ?=
ARCH         = arm
CROSS_COMPILE =
/usr/local/arm/3.3.2/bin/arm-linux-

[HongGe@Hzh1024n busybox-1.9.0]$ make menuconfig
ui

 

在原有的基礎上修改以下:

 

Busybox Settings --->
      Installation Options --->
           [*] Don't use /usr

           (/home/work/hzh1024n/rootfs) BusyBox installation prefix

        Busybox Library Tuning  --->

           [*] Support for /etc/networks

           [*]   Additional editing keys     

           [*]   vi-style line editing commands   

           (15)  History size  

           [*]   History saving  

           [*]   Tab completion 

           [*]     Username completion  

           [*]   Fancy shell prompts

 

Linux Module Utilities  ---> 

      [N] Support version 2.2.x to 2.4.x Linux kernels

Shells  --->

      ---   Ash Shell Options 下的選項全選

[HongGe@Hzh1024n busybox-1.9.0]$ make
......
  LINK busybox_unstripped
Trying libraries: crypt m
 Library crypt is needed
 Library m is needed
Final link with: crypt m
[HongGe@Hzh1024n busybox-1.9.0]$ make install

3)安裝glibc庫

  在開發板上只須要加載器和動態庫,假設要構建的根文件系統目錄爲/home/work/hzh1024n/rootfs,操做以下:

[HongGe@Hzh1024n busybox-1.9.0]$ cd /usr/local/arm/3.3.2/arm-linux/lib

[HongGe@Hzh1024n lib]$ cp *.so* /home/work/hzh1024n/rootfs/lib -d

上面複製的庫文件不是每一個都會被用到,能夠根據應用程序對庫的依賴關係保留須要用到的。經過ldd命令能夠查看一個程序會用到哪些庫,主機自帶的ldd命令不能查看交叉編譯出來的程序,有如下兩種替代方法

*若是有uClibc-0.9.28的代碼,能夠進入utils子目錄生成ldd.host工具。

$cd uClibc-0.9.28/utils

$make ldd.host

而後將生成的ldd.host放到主機/usr/local/bin目錄下便可

$ldd.host busybox

*也可使用下面命令

$arm-linux-readelf -a "your binary" | grep "shared"

好比對於動態連接的Busybox,它的庫依賴關係以下。

 0x00000001 (NEEDED) Shared library: [libcrypt.so.1]
 0x00000001 (NEEDED) Shared library: [libm.so.6]
 0x00000001 (NEEDED) Shared library: [libc.so.6]


4)修改和建立必要的文件。

 

[HongGe@Hzh1024n busybox-1.9.0]$ cp -a examples/bootfloppy/etc/* /home/work/hzh1024n/rootfs/etc/

[HongGe@Hzh1024n busybox-1.9.0]$ cd /home/work/hzh1024n/rootfs/etc/

1、增長爲SHELL導入全局變量的文件/etc/profile

[HongGe@Hzh1024n etc]$ kwrite profile

 

# /etc/profile: system-wide .profile file for the Bourne shells
echo

echo "Processing /etc/profile... "
# no-op


# Set search library path
echo "Set search library path in /etc/profile"
export LD_LIBRARY_PATH=/lib:/usr/lib 

# Set user path
echo "Set user path in /etc/profile"
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH 

# Set PS1
#注意:ash 除了SHELL變量外,支持\u、\h、\W、\$、\!、\n、\w 、\nnn(ASCII字符對應的八進制數)
#以及\e[xx;xxm (彩色特效)等等!
#並且前面還要多加一個 '\'

echo "Set PS1 in /etc/profile"

export PS1="\\e[05;32m[$USER@\\w\\a]\\$\\e[00;34m"

 

echo "Done"

echo

 

2、增長初始化文件

[HongGe@Hzh1024n etc]$ kwrite inittab

::sysinit:/etc/init.d/rcS
::restart:/sbin/init
::askfirst:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a

 

[HongGe@Hzh1024n etc]$ kwrite fstab

proc  /proc proc defaults    0 0

tmpfs /tmp tmpfs defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev tmpfs  defaults 0 0

 

3、增長初始化腳本

[HongGe@Hzh1024n etc]$ kwrite init.d/rcS

#! /bin/sh
ifconfig eth0 192.168.1.67
echo "----------mount all"
/bin/mount -a
/bin/mkdir /dev/pts
echo "----------Starting mdev......"
/bin/mount -t devpts devpts /dev/pts
/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s

echo "*********************************************************"
echo "  HongGe Hzh1024n rootfs    "
echo "   Wellcome  ! !         "
echo "********************************************************"

 

4、刪除備份文件

[HongGe@Hzh1024n etc]$ rm *~ init.d/*~

 

五、爲mdev建立配置文件

[HongGe@Hzh1024n etc]$ vi mdev.conf

建立一個mdev.conf文件,內容無關緊要。

 

到此爲止一個基本的根文件系統已經制做成功,下一步是掛載jffs2根文件系統。

若是要以掛載JFFS2格式的根文件系統,其基本方法就是將這個創建好的根文件系統製做成jffs2鏡像,燒到FLASH中,改改Linux的啓動參數便可。

 
具體作法以下:
1、宿主機HOST編譯製作MTD工具
 
http://www.linux-mtd.infradead.org/下載mtd-utils 的tarball,能夠下載最新的。而後解壓,並在其目錄下 make 就好!
 
2、製做根文件系統的JFFS2鏡像。
 
使用MTD工具中的mkfs.jffs2命令,具體作法以下:

mkfs.jffs2 -r /home/work/hzh1024n/rootfs -o rootfs.jffs2 -e 0x4000 --pad=0x500000 -s 0x200 -n

 
各參數的意義:
(1)-r : 指定要作成image的源資料夾.
(2)-o : 指定輸出image檔案的文件名.
(3)-e : 每一塊要抹除的block size,預設是64KB. 要注意,不一樣的flash, 其block size會不一樣. 個人是三星的K9F1208U0B.
(4)--pad (-p): 用16進制來表示所要輸出檔案的大小,也就是root.jffs2的size。很重要的是, 為了不浪費flash空間, 這個值最好符合flash driver所規劃的區塊大小.以個人板子來說,就是5MB.
(5)若是掛載後會出現相似:CLEANMARKER node found at 0x0042c000 has totlen 0xc != normal 0x0  的警告,則加上 -n 就會消失。
(6) 還有的選項,本身看幫助!-h
 
3、燒寫JFFS2鏡像到NAND FLASH。
將 rootfs.jffs2燒寫到FLASH中,注意內核掛載文件系統的參數,按照參數把文件系統燒寫到相關的位置當中。而後啓動開發板。


Uncompressing Linux........................................................................................................ done, booting the kernel.
Linux version 2.6.22.6 (HongGe@Hzh1024n) (gcc version 3.3.2) #1 Wed Feb 07 11:18:36 CST 2008
CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=c0007177
Machine: Hzh1024n
Memory policy: ECC disabled, Data cache writeback
CPU S3C2440A (id 0x32440001)
S3C244X: core 405.000 MHz, memory 101.250 MHz, peripheral 50.625 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: noinitrd root=/dev/mtdblock4 rootfstype=jffs2 rw console=ttySAC0,115200 init=/linuxrc mem=64M
irq: clearing pending ext status 00000200
irq: clearing subpending status 00000003
irq: clearing subpending status 00000002
PID hash table entries: 256 (order: 8, 1024 bytes)
timer tcon=00500000, tcnt a4ca, tcfg 00000200,00000000, usec 00001e57
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: 61464KB available (2960K code, 306K data, 120K 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
S3C2440: Initialising architecture
S3C2440: IRQ Support
S3C2440: Clock Support, DVS off
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
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)
JFFS2 version 2.2. (NAND) ©; 2001-2006 Red Hat, Inc.
fuse init (API version 7.9)
yaffs Feb 15 2008 10:10:34 Installing.
io scheduler noop registered
io scheduler anticipatory registered (default)
io scheduler deadline registered
io scheduler cfq registered
Serial: 8250/16550 driver $Revision: 1.90 $ 4 ports, IRQ sharing enabled
s3c2440-uart.: s3c2410_serial0 at MMIO 0x50000000 (irq = 70) is a S3C2440
s3c2440-uart.1: s3c2410_serial1 at MMIO 0x50004000 (irq = 73) is a S3C2440
s3c2440-uart.2: s3c2410_serial2 at MMIO 0x50008000 (irq = 76) is a S3C2440
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
loop: module loaded
dm9000 Ethernet Driver
eth0: dm9000 at f6100300,f6100304 IRQ 51 MAC: 08:08:08:08:12:27
Uniform Multi-Platform E-IDE driver Revision: 7.00alpha2
ide: Assuming 50MHz system bus speed for PIO modes; override with idebus=xx
S3C24XX NAND Driver, (c) 2004 Simtec Electronics
s3c2440-nand s3c2440-nand: Tacls=1, 9ns Twrph0=4 39ns, Twrph1=1 9ns
NAND device: Manufacturer ID: 0xec, Chip ID: 0x76 (Samsung NAND 64MiB 3,3V 8-bit)
Scanning device for bad blocks
Bad eraseblock 3579 at 0x037ec000
Creating 7 MTD partitions on "NAND 64MiB 3,3V 8-bit":
0x00000000-0x00020000 : "U-Boot-1.2.0"
0x00020000-0x00030000 : "U-Boot-1.2.0 Parameter"
0x00030000-0x00500000 : "Linux2.6.22.6 Kernel"
0x00500000-0x00a00000 : "Root(cramfs)"
0x00a00000-0x00f00000 : "Root(JFFS2)"
0x00f00000-0x01400000 : "Root(YAFFS)"
0x01400000-0x04000000 : "DATA"
usbmon: debugfs is not available
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-:1.: USB hub found
hub 1-:1.: 2 ports detected
mice: PS/2 mouse device common for all mice
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
s3c2440-i2c s3c2440-i2c: slave address 0x10
s3c2440-i2c s3c2440-i2c: bus frequency set to 98 KHz
s3c2440-i2c s3c2440-i2c: i2c-: 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: setting system clock to 2008-02-17 12:46:08 UTC (1203252368)
VFS: Mounted root (jffs2 filesystem).
Freeing init memory: 120K
init started: BusyBox v1.9.1 (2008-02-16 15:04:08 CST)
starting pid 779, tty '': '/etc/init.d/rcS'
----------mount all
----------Starting mdev......
*********************************************************
  HongGe Hzh1024n rootfs 
     Wellcome  ! ! 

********************************************************
starting pid 783, tty '': '/bin/login'
(none) login: root
Password:
login[783]: root login on 'console'

Processing /etc/profile...
Set search library path in /etc/profile
Set user path in /etc/profile
Set PS1 in /etc/profile
Done

[root@~]#

成功!!!

相關文章
相關標籤/搜索