构建基本的嵌入式Linux根文件系统

1、 《Linux系统移植》:一个经典的Linux移植文档,共有95页的PDF文档,内容十分详细,里面有根文件系统的创建,还多地方都有下载(有的网站称之为《Linux系统全线移植文档》等等),很值得参考。在这里感谢文档的作者们。

2、 《构建嵌入式Linux系统》必看!里面对文件系统的构建讲的挺细。

3、 《Filesystem Hierarchy Standard》Linux文件系统的标准规范。我只看到英文的。

其他再遇到不懂的就google,基本上都是Linux的相关知识,都是找得到的。

(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


# 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 3

 

    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

 

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

    echo "done"

 

在你想要建立根文件系统的地方,运行:

[tekkamanninja@Tekkaman-Ninja nfs]$ ./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
[tekkamanninja@Tekkaman-Ninja nfs]$ cd rootfs/dev/
[tekkamanninja@Tekkaman-Ninja dev]$ su
口令:
[root@Tekkaman-Ninja dev]# mknod -m 600 console c 5 1;mknod -m 666 null c 1 3;exit
exit
[tekkamanninja@Tekkaman-Ninja dev]$

(2)配置、编译和安装Busybox-1.9.1

[tekkamanninja@Tekkaman-Ninja source]$ tar -xjvf busybox-1.9.1.tar.bz2 

修改Makefile文件:

[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$ pwd
/home/tekkamanninja/working/source/busybox-1.9.1
[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$ kwrite Makefile


......(第151-154行)
#SUBARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \
#                 -e s/arm.*/arm/ -e s/sa110/arm/ \
#                 -e s/s390x/s390/ -e s/parisc64/parisc/ \
#                 -e s/ppc.*/powerpc/ -e s/mips.*/mips/ )
......(第174行附近)
#ARCH        ?= $(SUBARCH)
#CROSS_COMPILE    ?=
ARCH         = arm
CROSS_COMPILE =/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu-

[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$ make menuconfig

 

在原有的基础上修改如下:

 

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

           (/home/tekkamanninja/working/nfs/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

Login/Password Management Utilities  --->选项全部N掉,后面单独使用TinyLogin。(因为集成的好像不是很好用,我自己的经验是这样)

Linux Module Utilities  ---> 

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

Shells  --->

      ---   Ash Shell Options 下的选项全选

[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$ make
......
  LINK busybox_unstripped
Trying libraries: crypt m
 Library crypt is needed
 Library m is needed
Final link with: crypt m
[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$ make install

我是动态编译所以查看一下需要的动态库

[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu-readelf-d busybox

Dynamic section at offset 0xac014 contains 22 entries:
  Tag Type Name/Value
 0x00000001 (NEEDED) Shared library: [libcrypt.so.1]
 0x00000001 (NEEDED) Shared library: [libm.so.6]
 0x00000001 (NEEDED) Shared library: [libc.so.6]
 0x0000000c (INIT) 0xc0f0
 0x0000000d (FINI) 0x97608
 0x00000004 (HASH) 0x80e8
 0x00000005 (STRTAB) 0xa3c0
 0x00000006 (SYMTAB) 0x8b30
 0x0000000a (STRSZ) 3371 (bytes)
 0x0000000b (SYMENT) 16 (bytes)
 0x00000015 (DEBUG) 0x0
 0x00000003 (PLTGOT) 0xbc0ec
 0x00000002 (PLTRELSZ) 2976 (bytes)
 0x00000014 (PLTREL) REL
 0x00000017 (JMPREL) 0xb550
 0x00000011 (REL) 0xb4e0
 0x00000012 (RELSZ) 112 (bytes)
 0x00000013 (RELENT) 8 (bytes)
 0x6ffffffe (VERNEED) 0xb400
 0x6fffffff (VERNEEDNUM) 3
 0x6ffffff0 (VERSYM) 0xb0ec
 0x00000000 (NULL) 0x0

(3)修改和创建必要的文件。

 

[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$ cp -a examples/bootfloppy/etc/*/home/tekkamanninja/working/nfs/rootfs/etc/

[tekkamanninja@Tekkaman-Ninja busybox-1.9.1]$ cd ../../nfs/rootfs/etc/

1、增加为SHELL导入全局变量的文件/etc/profile

[tekkamanninja@Tekkaman-Ninja 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、增加初始化文件

[tekkamanninja@Tekkaman-Ninja etc]$ kwrite inittab

::sysinit:/etc/init.d/rcS
::respawn:-/bin/login
::restart:/sbin/init 

::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
::shutdown:/sbin/swapoff -a

 

[tekkamanninja@Tekkaman-Ninja etc]$ kwrite 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

 

3、增加初始化脚本

[tekkamanninja@Tekkaman-Ninja etc]$ kwrite init.d/rcS

#! /bin/sh
echo "----------mount all"
/bin/mount -a


echo "----------Starting mdev......"
/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s


echo "*********************************************************"
echo " Tekkaman Ninja 2440 Rootfs(nfs) 2008.2 "
echo " Love Linux ! ! @@ Love Ke Ke ! ! "
echo "********************************************************"

 

4、删除备份文件

[tekkamanninja@Tekkaman-Ninja etc]$ rm *~ init.d/*~

 

5、为mdev创建配置文件

[tekkamanninja@Tekkaman-Ninja etc]$ vi mdev.conf

创建一个mdev.conf文件,内容可有可无。

(4)为使用用户登录功能移植TinyLogin

1、下载

从http://tinylogin.busybox.net/ 下载tinylogin-snapshot.tar.bz2,并解压.

[tekkamanninja@Tekkaman-Ninja source]$ tar -xjvf tinylogin-snapshot.tar.bz2

 

2、修改tinyLogin的Makefile

[tekkamanninja@Tekkaman-Ninja source]$ cd tinylogin

[tekkamanninja@Tekkaman-Ninja tinylogin]$ kwrite Makefile

 

指明tinyLogin使用自己的算法来处理用户密码

USE_SYSTEM_PWD_GRP = false
...... 
CROSS=/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu-
CC = $(CROSS)gcc
AR = $(CROSS)ar
STRIPTOOL = $(CROSS)strip

 3、 编译并安装 

[tekkamanninja@Tekkaman-Ninja tinylogin]$ make PREFIX=/home/tekkamanninja/working/nfs/rootfs install
Aborting install -- You must be root, otherwise I can
't.

make tinylogin be setuid root, which will cause it to fail. 

make: *** [install] 错误 1

[tekkamanninja@Tekkaman-Ninja tinylogin]$su

口令:

[root@Tekkaman-Ninja tinylogin]# make PREFIX=/home/tekkamanninja/working/nfs/rootfs install

[root@Tekkaman-Ninja tinylogin]# exit

exit

 我是动态编译所以查看一下需要的动态库

[tekkamanninja@Tekkaman-Ninja tinylogin]$/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/bin/arm-9tdmi-linux-gnu-readelf-d tinylogin 

Dynamic section at offset 0x9670 contains 21 entries:
  Tag Type Name/Value
 0x00000001 (NEEDED) Shared library: [libcrypt.so.1]
 0x00000001 (NEEDED) Shared library: [libc.so.6]
 0x0000000c (INIT) 0x9c40
 0x0000000d (FINI) 0x10160

 0x00000004 (HASH) 0x8128
 0x00000005 (STRTAB) 0x9070
 0x00000006 (SYMTAB) 0x85e0
 0x0000000a (STRSZ) 1430 (bytes)
 0x0000000b (SYMENT) 16 (bytes)
 0x00000015 (DEBUG) 0x0
 0x00000003 (PLTGOT) 0x19740
 0x00000002 (PLTRELSZ) 1064 (bytes)
 0x00000014 (PLTREL) REL
 0x00000017 (JMPREL) 0x9818
 0x00000011 (REL) 0x97c8

 0x00000012 (RELSZ) 80 (bytes)

 0x00000013 (RELENT) 8 (bytes)
 0x6ffffffe (VERNEED) 0x9758
 0x6fffffff (VERNEEDNUM) 2
 0x6ffffff0 (VERSYM) 0x9606
 0x00000000 (NULL) 0x0

 

 

创建帐号及密码文件:

[tekkamanninja@Tekkaman-Ninja tinylogin]$ cd ../../nfs/rootfs/etc/
[tekkamanninja@Tekkaman-Ninja etc]$ 
su
口令:
[root@Tekkaman-Ninja etc]# cp /etc/passwd . ;cp /etc/shadow . ;cp /etc/group .
[root@Tekkaman-Ninja etc]# kwrite passwd
root:x:0:0:root:/root:/bin/sh
[root@Tekkaman-Ninja etc]# kwrite group

root:x:0:root
[root@Tekkaman-Ninja etc]# 
kwrite shadow
root:$1$N8K8eEQe$.XkJo3xcsjOE6vo1jW9Nk/:13923:0:99999:7:::

[root@Tekkaman-Ninja etc]# 
rm *~
rm:是否删除 普通文件 “group~”? y
rm:是否删除 普通文件 “passwd~”? y
rm:是否删除 普通文件 “shadow~”? y
[root@Tekkaman-Ninja etc]# exit
exit
[tekkamanninja@Tekkaman-Ninja etc]$

这 3个文件是从Host中拷贝过来的,只留下root帐号

那么现在 root的登陆密码和Host的登陆口令一致了,可以登录后再修改以及创建新用户。( 若使用以上文件,则root密码为tekkaman)

 

 

拷贝必需的动态库文件

[tekkamanninja@Tekkaman-Ninja lib]$ cp -d/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/ld* .
cp: 略过目录 “/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/ldscripts”
[tekkamanninja@Tekkaman-Ninja lib]$ cp/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/libc-2.3.2.so.;cp -d/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/libc.so.6 .
[tekkamanninja@Tekkaman-Ninja lib]$ cp/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/libm-* . ;cp-d /home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/libm.s* .
[tekkamanninja@Tekkaman-Ninja lib]$ cp/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/libcrypt-* .;cp -d/home/tekkamanninja/working/gcc4.1.1/gcc-4.1.1-glibc-2.3.2/arm-9tdmi-linux-gnu/arm-9tdmi-linux-gnu/lib/libcrypt.s* .

 

 

以上是最基本的文件。

一个最基本根文件系统构建完成!未压缩的文件系统总大小不到3M。

以下是启动信息:

U-Boot 1.3.1 (Feb 18 2008 - 16:04:40)

DRAM: 64 MB
Flash: 1 MB
NAND: NAND flash probing at 0x4E000000
  64 MB
In: serial
Out: serial
Err: serial
Hit any key to stop autoboot: 0
dm9000 i/o: 0x20000300, id: 0x90000a46
MAC: 08:08:08:08:12:27
operating at 100M full duplex mode
File transfer via NFS from server 192.168.1.22; our IP address is 192.168.1.2
Filename '/home/tekkamanninja/working/nfs/zImage.img'.
Load address: 0x30008000
Loading: #################################################################
         #################################################################
         #################################################################
         #################################################################
         #######################################################
done
Bytes transferred = 1611092 (189554 hex)
## Booting image at 30008000 ...
   Image Name: tekkamanninja
   Created: 2008-02-19 0:48:31 UTC
   Image Type: ARM Linux Kernel Image (uncompressed)
   Data Size: 1611028 Bytes = 1.5 MB
   Load Address: 30008000
   Entry Point: 30008040
   Verifying Checksum ... OK
   XIP Kernel Image ... OK

Starting kernel ...

Uncompressing Linux........................................................................................................done, booting the kernel.
Linux version 2.6.24 (tekkamanninja@Tekkaman-Ninja) (gcc version 4.1.1) #4 Tue Feb 19 08:48:15 CST 2008
CPU: ARM920T [41129200] revision 0 (ARMv4T), cr=c0007177
Machine: Tekkaman2440
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/nfs rw nfsroot=192.168.1.22:/home/tekkamanninja/working/nfs/rootfs ip=192.168.1.2:192.168.1.22::255.255.255.0 console=ttySAC0,115200 init=/linuxrc mem=64M
irq: clearing pending ext status 00000200
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: 61440KB available (2980K code, 309K 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.0: 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
usbcore: registered new interface driver ub
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-0x00030000 : "U-Boot-1.3.1"
0x00030000-0x00040000 : "U-Boot-1.3.1 Parameter"
0x00040000-0x00500000 : "Linux2.6.24 Kernel(JFFS2)"
0x00500000-0x00a00000 : "Root(cramfs)"
0x00a00000-0x00f00000 : "Root(JFFS2)"
0x00f00000-0x01400000 : "Root(YAFFS)"
0x01400000-0x04000000 : "DATA"
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
usb usb1: Product: S3C24XX OHCI
usb usb1: Manufacturer: Linux 2.6.24 ohci_hcd
usb usb1: SerialNumber: s3c24xx
usbcore: registered new interface driver libusual
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-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: setting system clock to 2008-02-20 16:01:20 UTC (1203523280)
eth0: link down
IP-Config: Complete:
      device=eth0, addr=192.168.1.2, mask=255.255.255.0, gw=255.255.255.255,
     host=192.168.1.2, domain=, nis-domain=(none),
     bootserver=192.168.1.22, rootserver=192.168.1.22, rootpath=
Looking up port of RPC 100003/2 on 192.168.1.22
eth0: link up, 100Mbps, full-duplex, lpa 0x45E1
Looking up port of RPC 100005/1 on 192.168.1.22
VFS: Mounted root (nfs filesystem).
Freeing init memory: 120K
init started: BusyBox v1.9.1 (2008-02-20 14:54:41 CST)
starting pid 781, tty '': '/etc/init.d/rcS'
----------mount all
----------Starting mdev......
*********************************************************
  Tekkaman Ninja 2440 Rootfs(nfs) 2008.2
   Love Linux ! ! @@ Love Ke Ke ! !
********************************************************
starting pid 785, tty '': '/bin/login'

192.168.1.2 login: root
Password:
login[785]: 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@/root]#  

 

 

以上我使用nfs挂载的,后面我会介绍如何挂载jffs2、cramfs以及yaffs文件系统,有了这个基本的文件系统以后,其他就好办了!以下是这个文件系统,编译器是gcc4.1.2(制作编译器所用内核为2.6.24)。

你可能感兴趣的:(构建基本的嵌入式Linux根文件系统)