Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1187233
  • 博文数量: 232
  • 博客积分: 7563
  • 博客等级: 少将
  • 技术积分: 1930
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-21 11:17
文章分类

全部博文(232)

文章存档

2011年(17)

2010年(90)

2009年(66)

2008年(59)

分类: LINUX

2008-05-21 11:45:40


2.2 基于Flash的文件系统

 

鉴于Flash存储介质的读写特点,传统的Linux文件系统己经不适合应用在嵌入式系统中,像Ext2fs文件系统是为像IDE那样的块设备设计的,这些设备的逻辑块是512字节、1024字节等大小,没有提供很好的扇区擦写支持,不支持损耗平衡,没有掉电保护,也没有特别完美的扇区管理,这不太适合于扇区大小因设备类型而划分的闪存设备。基于这样的原因,产生了很多专为Flash设备而设计的文件系统,常见的专用于闪存设备的文件系统如下:

    

2.2.1 Romfs

   

传统型的Romfs文件系统是最常使用的一种文件系统,它是一种简单的、紧凑的、只读的文件系统,不支持动态擦写保存;它按顺序存放所有的文件数据,所以这种文件系统格式支持应用程序以XIP方式运行,在系统运行时,可以获得可观的RAM节省空间。uClinux系统通常采用Romfs文件系统。

  

2.2.2 Cramfs

    

CramfsLinux的创始人Linus Torvalds开发的一种可压缩只读文件系统在Cramfs文件系统中,每一页被单独压缩,可以随机页访问,其压缩比高达2:1,为嵌入式系统节省大量 Flash存储空间。Cramfs文件系统以压缩方式存储,在运行时解压缩,所以不支持应用程序以XIP方式运行,所有的应用程序要求被拷到RAM里去 运行,但这并不代表比Ramfs需求的RAM 空间要大一点,因为Cramfs是采用分页压缩的方式存放档案,在读取档案时,不会一下子就耗用过多的内存空间,只针对目前实际读取的部分分配内存,尚没有读取的部分不分配内存空间,当我们读取的档案不在内存时, Cramfs文件系统自动计算压缩后的资料所存的位置,再即时解压缩到RAM中。

 

另外,它的速度快,效率高,其只读的特点有利于保护文件系统免受破坏,提高了系统的可靠性;但是它的只读属性同时又是它的一大缺陷,使得用户无法对其内容 对进扩充。Cramfs映像通常是放在Flash中,但是也能放在别的文件系统里,使用loopback设备可以把它安装别的文件系统里。使用 mkcramfs工具可以创建Cramfs映像。

    

2.2.3 Ramfs/Tmpfs

   

Ramfs也是Linus Torvalds开发的,Ramfs文件系统把所有的文件都放在RAM里运行,通常是Flash系统用来存储一些临时性或经常要修改的数据,相对于 ramdisk来说,Ramfs的大小可以随着所含文件内容大小变化,不像ramdisk的大小是固定的。Tmpfs是基于内存的文件系统,因为 tmpfs驻留在RAM 中,所以写/读操作发生在RAM 中。tmpfs文件系统大小可随所含文件内容大小变化,使得能够最理想地使用内存;tmpfs驻留在RAM,所以读和写几乎都是瞬时的。tmpfs的一个缺点是当系统重新引导时会丢失所有数据。

    

2.2.4 JFFS2

   

JFFS2RedHat公司基于JFFS开发的闪存文件系统,最初是针对RedHat公司的嵌入式产品eCos开发的嵌入式文件系统,所以JFFS2 可以用在Linux,uCLinux中。JFFS文件系统最早是由瑞典Axis Communications公司基于Linux2.0的内核为嵌入式系统开发的文件系统。JFFS2是一个可读写的、压缩的、日志型文件系统,并提供了 崩溃/掉电安全保护,克服了JFFS的一些缺点:使用了基于哈希表的日志节点结构,大大加快了对节点的操作速度;支持数据压缩;提供了“写平衡”支持;支持多种节点类型;提高了对闪存的利用率,降低了内存的消耗。这些特点使JFFS2文件系统成为目前Flash设备上最流行的文件系统格式,它的缺点就是当文件系统已满或接近满时,JFFS2运行会变慢,这主要是因为碎片收集的问题。

 

2.2.5 YAFFS

    

YAFFS/YAFFS2是一种和JFFSx类似的闪存文件系统,它是专为嵌入式系统使用NAND型闪存而设计的一种日志型文件系统。和JFFS2相比它减少了一些功能,所以速度更快,而且对内存的占用比较小。此外,YAFFS自带NAND芯片的驱动,并且为嵌入式系统提供了直接访问文件系统的API,用户可以不使用Linux中的MTDVFS,直接对文件系统操作。YAFFS2支持大页面的NAND设备,并且对大页面的NAND设备做了优化。JFFS2NAND闪存上表现并不稳定,更适合于NOR闪存,所以相对大容量的NAND闪存,YAFFS是更好的选择。

在具体的嵌入式系统设计中可根据不同目录存放的内容不同以及存放的文件属性,确定使用何种文件系统。对于Romfs采用XIP技术显然过于复杂,YAFFS在资源有限的FLASH中没有优势,JFFS2在最近遇到的十几个case中都没有人提议使用,我也无法评论,可能在以后再补充关于JFFS2的内容。下面将介绍制做Ramfscramfssquashfs三种文件系统的详细过程。

三、根文件目录:

    根文件目录在嵌入式系统启动以后被挂载到/ 下。一个linux的最简根文件系统应该包括支持linux系统正常运行的基本内容,包括系统使用的软件和库,以及所有用来为用户提供基本支持的架构和指令。

在根文件目录rootfs下建立bindevetclibprocsbinroottmp等一系列必备的目录,把所需的配置文件、动态函数库放到相应的目录。采用BusyBox是缩小根文件系统的好办法。BusyBox以很小的体积集成了最常用的linux命令和应用程序,大大简化了制作 linux根文件系统的过程。

3.1     建一个目录rootfs 用来装文件系统;

 

3.2     在该目录下mkdir bin  dev  etc  lib  proc  sbin  tmp  usr  var

 

3.3     ln -fs  bin/busybox linuxrc(使用busybox简化文件系统)

 

3.4     到系统 /dev 把所有的device打一个包,拷贝到 dev下面(最省事的做法);或者直接使用mknod来自己建所需要的device,我自己用的如下:

 

crwxrwxrwx  1 root root   5,  1    2007-10-08 10:12 console

crwxrwxrwx  1 root root   5, 64             2007-10-08 10:12 cua0

crwxrwxrwx  1 root root  63,  0    2007-10-08 10:12 dk0

crwxrwxrwx  1 root root  63,  1    2007-10-08 10:12 dk1

drwxrwxrwx  2 root root    4096    2007-10-08 10:12 flash

brwxrwxrwx  1 root root   3,  0    2007-10-08 10:12 hda

crwxrwxrwx  1 root root  36, 10     2007-10-08 10:12 ipsec

crwxrwxrwx  1 root root 241,  0     2007-10-08 10:12 ixNpe

crwxrwxrwx  1 root root   1,  2    2007-10-08 10:12 kmem

crwxrwxrwx  1 root root 126,  0     2007-10-08 10:12 ledman

lrwxrwxrwx  1 root root      16     2007-10-08 13:20 log -> /tmp/var/log/log

crwxrwxrwx  1 root root   1,  1     2007-10-08 10:12 mem

crwxrwxrwx  1 root root  90,  0    2007-10-08 10:12 mtd0

brwxrwxrwx  1 root root  31,  0    2007-10-08 10:12 mtdblock0

brwxrwxrwx  1 root root  31,  1    2007-10-08 10:12 mtdblock1

brwxrwxrwx  1 root root  31,  2    2007-10-08 10:12 mtdblock2

brwxrwxrwx  1 root root  31,  3    2007-10-08 10:12 mtdblock3

brwxrwxrwx  1 root root  31,  4    2007-10-08 10:12 mtdblock4

brwxrwxrwx  1 root root  31,  5    2007-10-08 10:12 mtdblock5

brwxrwxrwx  1 root root  31,  6    2007-10-08 10:12 mtdblock6

crwxrwxrwx  1 root root  90,  1    2007-10-08 10:12 mtdr0

crwxrwxrwx  1 root root   1,  3    2007-10-08 10:12 null

crwxrwxrwx  1 root root 108,  0     2007-10-08 10:12 ppp

crwxrwxrwx  1 root root   5,  2    2007-10-08 10:12 ptmx

drwxrwxrwx  2 root root    4096    2007-10-08 10:12 pts

crwxrwxrwx  1 root root   2,  0    2007-10-08 10:12 ptyp0

brwxrwxrwx  1 root root   1,  0    2007-10-08 10:12 ram0

crwxrwxrwx  1 root root   1,  8    2007-10-08 10:12 random

crwxrwxrwx  1 root root   5,  0    2007-10-08 10:12 tty

crwxrwxrwx  1 root root   4,  0    2007-10-08 10:12 tty0

crwxrwxrwx  1 root root   3,  0    2007-10-08 10:12 ttyp0

crwxrwxrwx  1 root root   4, 64     2007-10-08 10:12 ttyS0

crwxrwxrwx  1 root root   1,  9     2007-10-08 10:12 urandom

crwxrwxrwx  1 root root   1,  5    2007-10-08 10:12 zero

 

举例: mknod console c 5 1 这样  crw-rw-rw-  1 root   root     5,  1 2007-10-08 13:12 console

 

2.6.x内核,要使系统启动后进入BusyBox控制台,在/rootfs/dev下添加控制台设备文件:“mknod -m 600 console c 5 1”是必须的。

 

3.5     将编译好的busybox拷贝到/bin下面,除了busybox外,所有其他的命令都是他的link

 

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 ash -> busybox

-rwxr-xr-x    1 root root 246052        2007-10-08 13:42 busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 cat -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 chmod -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 chown -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 cp -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 df -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 dmesg -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 echo -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 egrep -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 fgrep -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 grep -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 gzip -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 hostname -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 ip -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 kill -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 ln -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 ls -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 mkdir -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 mount -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 mv -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 netstat -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 ping -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 ping2file -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 ps -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 pwd -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 rm -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 sh -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 sleep -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 sysinfo -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 tar -> busybox

lrwxrwxrwx  1 root root      7 2007-10-08 13:42 uname -> busybox

 

3.6     同样/sbin下面也是busyboxlink

 

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 ifconfig -> ../bin/busybox

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 init -> ../bin/busybox

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 insmod -> ../bin/busybox

lrwxrwxrwx  1 root root     7 2007-10-08 13:42 klogd -> syslogd

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 losetup -> ../bin/busybox

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 lsmod -> ../bin/busybox

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 modprobe -> ../bin/busybox

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 reboot -> ../bin/busybox

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 rmmod -> ../bin/busybox

lrwxrwxrwx  1 root root    14 2007-10-08 13:42 route -> ../bin/busybox

 

3.7     同样/usr/bin下面也是busyboxlink

 

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 [ -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 dirname -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 expr -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 free -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 id -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 killall -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 sort -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 test -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 tr -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 traceroute -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 tty -> ../../bin/busybox

lrwxrwxrwx 1 root root   17 2007-10-08 13:42 wc -> ../../bin/busybox

 

当然这样做是有一点点麻烦了,因此最简单的办法是在 busybox/applets/install.sh”中改变“prefix=../../rootfs”。在busybox使用”make menuconfig”选择你想要的命令,不要忘记在build option中指定编译器。

 

3.8     /usr/sbin下面放着所有编译完的可执行文件,具体就不多说了;.

 

3.9     非常重要之/lib,务必重视;

 

-rwxrwxrwx  1 root root  19640 2007-10-08 10:12 ld-uClibc-0.9.28.so

lrwxrwxrwx  1 root root     19 2007-10-08 13:20 ld-uClibc.so.0 -> ld-uClibc-0.9.28.so

-rwxrwxrwx  1 root root  10964 2007-10-08 10:12 libcrypt-0.9.28.so

lrwxrwxrwx  1 root root     18 2007-10-08 13:20 libcrypt.so.0 -> libcrypt-0.9.28.so

lrwxrwxrwx  1 root root     19 2007-10-08 13:20 libc.so.0 -> libuClibc-0.9.28.so

-rwxrwxrwx  1 root root   6972 2007-10-08 10:12 libdl-0.9.28.so

lrwxrwxrwx  1 root root     10 2007-10-08 13:20 libdl.so -> libdl.so.0

lrwxrwxrwx  1 root root     15 2007-10-08 13:20 libdl.so.0 -> libdl-0.9.28.so

lrwxrwxrwx  1 root root     13 2007-10-08 13:20 libgcc_s.so -> libgcc_s.so.1

-rwxrwxrwx  1 root root  28428 2007-10-08 10:12 libgcc_s.so.1

lrwxrwxrwx  1 root root     11 2007-10-08 13:20 libiw.so -> libiw.so.27

-rwxrwxrwx  1 root root  18808 2007-10-08 10:12 libiw.so.26

-rwxrwxrwx  1 root root  22860 2007-10-08 10:12 libiw.so.27

-rwxrwxrwx  1 root root  36300 2007-10-08 10:12 libixml.so

-rwxrwxrwx  1 root root  54656 2007-10-08 10:12 libm-0.9.28.so

-rwxr-xr-x  1 root root    76828 2007-10-08 13:42 libmatrixssl.so

lrwxrwxrwx  1 root root      9 2007-10-08 13:20 libm.so -> libm.so.0

lrwxrwxrwx  1 root root     14 2007-10-08 13:20 libm.so.0 -> libm-0.9.28.so

-rwxr-xr-x  1 root root   6592 2007-10-08 13:42 libnvram.so

-rwxrwxrwx  1 root root  87557 2007-10-08 10:12 libpthread-0.9.28.so

lrwxrwxrwx  1 root root     20 2007-10-08 13:20 libpthread.so.0 -> libpthread-0.9.28.so

-rwxrwxrwx  1 root root   1952 2007-10-08 10:12 libresolv-0.9.28.so

lrwxrwxrwx  1 root root     14 2007-10-08 13:20 libresolv.so -> libresolv.so.0

lrwxrwxrwx  1 root root     19 2007-10-08 13:20 libresolv.so.0 -> libresolv-0.9.28.so

-rwxrwxrwx  1 root root  16140 2007-10-08 10:12 libthreadutil.so

-rwxrwxrwx  1 root root 313620       2007-10-08 10:12 libuClibc-0.9.28.so

-rwxrwxrwx  1 root root   4468 2007-10-08 10:12 libutil-0.9.28.so

lrwxrwxrwx  1 root root     12 2007-10-08 13:20 libutil.so -> libutil.so.0

lrwxrwxrwx  1 root root     17 2007-10-08 13:20 libutil.so.0 -> libutil-0.9.28.so

 

      找到你编译环境的target目录,把需要的lib文件先用strip压縮(非target目录下的,而是编译环境提供的strip),先把最基本的libc, ld等等,必须同样做跟target/lib里面一样的link。然后根据特定的应用加相应的lib,不要把不用的加进去,lib比较占空间。

 

3.10  /etc下面加上需要的配置文件,最最重要的是rcS

#!/bin/sh

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/sbin/scripts:/usr/sbin/ath

 

UTC=yes

 

mount -n -t proc proc /proc

mount -n -t ramfs ramfs /tmp (如果不是ramfs,必须加上这个形成一个可以读写的目录)

 

insmod /lib/gpio.o

insmod /lib/ixp400.o

insmod /lib/ixp400_eth.o

 

# build var directories

/bin/mkdir -m 0777 /tmp/var

/bin/mkdir -m 0777 /var/lock

/bin/mkdir -m 0777 /var/log

/bin/mkdir -m 0777 /var/run

/bin/mkdir -m 0777 /var/tmp

 

# start ethnet

ifconfig eth1 up

/usr/sbin/brctl addbr br0

ifconfig br0 up

/usr/sbin/brctl addif br0 eth1

/usr/sbin/brctl setfd br0 0.1

ifconfig br0 mtu 1490

 

ifconfig lo 127.0.0.1

route add -net 127.0.0.0 netmask 255.255.0.0 lo > /dev/null 2>&1

 

下面的具体应用没有再举例加上了,这个是系统init必须的。

 

四、生成ramfs文件:

#!/bin/sh

 

MODULE_NAME=ramdisk

RAMPATH=`pwd`

TMPPATH=${RAMPATH}/tmp

SOURCE=${RAMPATH}/../target

 

if [ ! -d ${TMPPATH} ]

then

    mkdir ${TMPPATH}

fi

 

if [ `whoami` != 'root' ]

then {

    echo "You should run the shell as root, Please rerun as a root."

    echo "Aborting."

    exit 1

    }

fi

 

# Clear in tmp path

rm -rf ${TMPPATH}/tmpmnt

rm -rf ${TMPPATH}/ramrootfs

mkdir ${TMPPATH}/tmpmnt

 

# Clear the old ramdisk

rm -f ${RAMPATH}/$MODULE_NAME

 

# Make a temp file which size is suitable

dd if=/dev/zero of=${TMPPATH}/ramrootfs bs=1k count=6144

 

# Create a ext2 filesystem

mke2fs -F -m 0 -i 2000 ${TMPPATH}/ramrootfs

 

# Mount it to tmpmnt/

mount -o loop -t ext2 ${TMPPATH}/ramrootfs ${TMPPATH}/tmpmnt

 

# Copy everything from kernel to this.

cd ${TMPPATH}/tmpmnt

echo ${SOURCE}

cp -av ${SOURCE}/*  .

cd ${TMPPATH}

# Unmount it the ext2 filesystem

cp ${TMPPATH}/tmpmnt ${RAMPATH}/filesystem -fr

umount ${TMPPATH}/tmpmnt

cat ${TMPPATH}/ramrootfs | gzip -9 > /${RAMPATH}/ramdisk

echo Copying ramdisk image to ${RAMPATH}

sync

 

这里给出一个自动生成脚本,在启动的时候先把这个文件拷贝到ram里面,然后告诉kernel它的地址在什么地方就可以了。

 

五、生成一个cramfs

 

找到cramfstoolchain

./mkcramfs -r $(FS_DIR) $(FS_NAME).bin

 

六、生成一个mksquashfs:

找到squashfstoolchain

./mksquashfs $(FS_DIR) $(FS_NAME) -noappend -be -lzma -no-fragments –noI


总结:

Ramfs是生成rootfs中最复杂的一个,但是在执行中是最快的一个;squashfs是和kernelpatch一起发布的,打上补丁后使用还是比较方便,但是使用时候必须注意是否目录可写;cramfs现在已经整合到kernel中,可能使用会有bug,我也没有查出来。因此推荐使用的时候RamfssquashfsRamfs比较快但是可写的空间比较小,squashfs稍微慢点,但是有比较大的空间储存文件。

阅读(1293) | 评论(0) | 转发(0) |
0

上一篇:实验四 内核模块

下一篇:人是需要鼓励的

给主人留下些什么吧!~~