Saturday, November 24, 2012

How To Create A Ramdisk File and Make Use in Embedded System ?

How To Create A Ramdisk File and Make Use in Embedded System ?

by cawan (cawan[at]ieee.org or chuiyewleong[at]hotmail.com)

on 24/11/2012

A ramdisk file is simply a file image with file system. The variety of file system
in embedded scope is quite chaotic, there are CRAMFS, JFFS, JFFS2, SquashFS, ROMFS,
and etc. Each of them has their own pros and cons to the embedded design, well, it
is depends to the application. However, the issue right now is how we can create
such ramdisk file ? It is simple, let us make use of ext3 as an example here.

Host:# dd if=/dev/zero of=./cawan.img bs=1k count=1024
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.0107335 s, 97.7 MB/s
Host:# hd cawan.img
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00100000
Host:# mkfs.ext3 -F -m0 ./cawan.img
mke2fs 1.41.3 (12-Oct-2008)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
128 inodes, 1024 blocks
0 blocks (0.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=1048576
1 block group
8192 blocks per group, 8192 fragments per group
128 inodes per group

Writing inode tables: done                           

Filesystem too small for a journal
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

Well, the -F option in mkfs means to force the formatting process to run on a file.
Besides, the -m0 option means to reserve zero block for super user on the file
system. It is reasonable because embedded system is normally a single-user system.
Now, let us check the hexdump of cawan.img again.

Host:# hd ./cawan.img
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400  80 00 00 00 00 04 00 00  00 00 00 00 da 03 00 00  |................|
00000410  75 00 00 00 01 00 00 00  00 00 00 00 00 00 00 00  |u...............|
00000420  00 20 00 00 00 20 00 00  80 00 00 00 00 00 00 00  |. ... ..........|
00000430  0d 82 b0 50 00 00 17 00  53 ef 01 00 01 00 00 00  |...P....S.......|
00000440  0d 82 b0 50 00 4e ed 00  00 00 00 00 01 00 00 00  |...P.N..........|
00000450  00 00 00 00 0b 00 00 00  80 00 00 00 38 00 00 00  |............8...|
00000460  02 00 00 00 01 00 00 00  0a 80 ba 2c 4f 6b 48 e1  |...........,OkH.|
00000470  ab 02 2a 9d 01 fa 14 e5  00 00 00 00 00 00 00 00  |..*.............|
00000480  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000004c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 03 00  |................|
000004d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000004e0  00 00 00 00 00 00 00 00  00 00 00 00 4a ad 9b 21  |............J..!|
000004f0  6f 8c 49 a6 a6 1d 0d 98  0a 38 a9 d8 01 00 00 00  |o.I......8......|
00000500  00 00 00 00 00 00 00 00  0d 82 b0 50 00 00 00 00  |...........P....|
00000510  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000560  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000570  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000800  06 00 00 00 07 00 00 00  08 00 00 00 da 03 75 00  |..............u.|
00000810  02 00 04 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000820  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001800  ff ff ff ff 1f 00 00 00  00 00 00 00 00 00 00 00  |................|
00001810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001870  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 80  |................|
00001880  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00001c00  ff 07 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00001c10  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00002000  00 00 00 00 00 00 00 00  0d 82 b0 50 0d 82 b0 50  |...........P...P|
00002010  0d 82 b0 50 00 00 00 00  00 00 00 00 00 00 00 00  |...P............|
00002020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002080  ed 41 00 00 00 04 00 00  0d 82 b0 50 0d 82 b0 50  |.A.........P...P|
00002090  0d 82 b0 50 00 00 00 00  00 00 03 00 02 00 00 00  |...P............|
000020a0  00 00 00 00 00 00 00 00  18 00 00 00 00 00 00 00  |................|
000020b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002300  80 81 00 00 00 30 04 04  0d 82 b0 50 0d 82 b0 50  |.....0.....P...P|
00002310  0d 82 b0 50 00 00 00 00  00 00 01 00 08 00 00 00  |...P............|
00002320  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002350  00 00 00 00 00 00 00 00  00 00 00 00 25 00 00 00  |............%...|
00002360  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002500  c0 41 00 00 00 30 00 00  0d 82 b0 50 0d 82 b0 50  |.A...0.....P...P|
00002510  0d 82 b0 50 00 00 00 00  00 00 02 00 18 00 00 00  |...P............|
00002520  00 00 00 00 00 00 00 00  19 00 00 00 1a 00 00 00  |................|
00002530  1b 00 00 00 1c 00 00 00  1d 00 00 00 1e 00 00 00  |................|
00002540  1f 00 00 00 20 00 00 00  21 00 00 00 22 00 00 00  |.... ...!..."...|
00002550  23 00 00 00 24 00 00 00  00 00 00 00 00 00 00 00  |#...$...........|
00002560  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006000  02 00 00 00 0c 00 01 02  2e 00 00 00 02 00 00 00  |................|
00006010  0c 00 02 02 2e 2e 00 00  0b 00 00 00 e8 03 0a 02  |................|
00006020  6c 6f 73 74 2b 66 6f 75  6e 64 00 00 00 00 00 00  |lost+found......|
00006030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006400  0b 00 00 00 0c 00 01 02  2e 00 00 00 02 00 00 00  |................|
00006410  f4 03 02 02 2e 2e 00 00  00 00 00 00 00 00 00 00  |................|
00006420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006800  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00006810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006c00  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00006c10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007000  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007400  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007800  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007c00  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007c10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008000  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008400  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008800  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008c00  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008c10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00009000  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00009010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00009400  00 00 00 00 03 00 00 00  04 00 00 00 05 00 00 00  |................|
00009410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00100000

Nice, it seems the file system already created. Let us mount onto it and add some
files into it.

Host:# mkdir cawan
Host:# mount -o loop ./cawan.img ./cawan
Host:# cd cawan
Host:# ls
lost+found
Host:# echo hello cawan > cawan.txt
Host:# ls -l
total 13
-rw-r--r-- 1 root root    12 2012-11-24 16:29 cawan.txt
drwx------ 2 root root 12288 2012-11-24 16:15 lost+found
Host:# cat ./cawan.txt
hello cawan
Host:# cd ..
Host:# umount ./cawan
Host:#

Fine, we make use of loop device to mount onto the cawan.img. After that, we create
a cawan.txt into the ramdisk file and the content of cawan.txt is "hello cawan".
Let us check the hexdump of cawan.img again.

Host:# hd ./cawan.img
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400  80 00 00 00 00 04 00 00  00 00 00 00 d9 03 00 00  |................|
00000410  74 00 00 00 01 00 00 00  00 00 00 00 00 00 00 00  |t...............|
00000420  00 20 00 00 00 20 00 00  80 00 00 00 30 85 b0 50  |. ... ......0..P|
00000430  81 85 b0 50 01 00 17 00  53 ef 01 00 01 00 00 00  |...P....S.......|
00000440  0d 82 b0 50 00 4e ed 00  00 00 00 00 01 00 00 00  |...P.N..........|
00000450  00 00 00 00 0b 00 00 00  80 00 00 00 38 00 00 00  |............8...|
00000460  02 00 00 00 01 00 00 00  0a 80 ba 2c 4f 6b 48 e1  |...........,OkH.|
00000470  ab 02 2a 9d 01 fa 14 e5  00 00 00 00 00 00 00 00  |..*.............|
00000480  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000004c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 03 00  |................|
000004d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000004e0  00 00 00 00 00 00 00 00  00 00 00 00 4a ad 9b 21  |............J..!|
000004f0  6f 8c 49 a6 a6 1d 0d 98  0a 38 a9 d8 01 00 00 00  |o.I......8......|
00000500  00 00 00 00 00 00 00 00  0d 82 b0 50 00 00 00 00  |...........P....|
00000510  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000560  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000570  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000800  06 00 00 00 07 00 00 00  08 00 00 00 d9 03 74 00  |..............t.|
00000810  02 00 04 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000820  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001800  ff ff ff ff 3f 00 00 00  00 00 00 00 00 00 00 00  |....?...........|
00001810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001870  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 80  |................|
00001880  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00001c00  ff 0f 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00001c10  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
*
00002000  00 00 00 00 00 00 00 00  0d 82 b0 50 0d 82 b0 50  |...........P...P|
00002010  0d 82 b0 50 00 00 00 00  00 00 00 00 00 00 00 00  |...P............|
00002020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002080  ed 41 00 00 00 04 00 00  5f 85 b0 50 53 85 b0 50  |.A......_..PS..P|
00002090  53 85 b0 50 00 00 00 00  00 00 03 00 02 00 00 00  |S..P............|
000020a0  00 00 00 00 00 00 00 00  18 00 00 00 00 00 00 00  |................|
000020b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002300  80 81 00 00 00 30 04 04  0d 82 b0 50 0d 82 b0 50  |.....0.....P...P|
00002310  0d 82 b0 50 00 00 00 00  00 00 01 00 08 00 00 00  |...P............|
00002320  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002350  00 00 00 00 00 00 00 00  00 00 00 00 25 00 00 00  |............%...|
00002360  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002500  c0 41 00 00 00 30 00 00  0d 82 b0 50 0d 82 b0 50  |.A...0.....P...P|
00002510  0d 82 b0 50 00 00 00 00  00 00 02 00 18 00 00 00  |...P............|
00002520  00 00 00 00 00 00 00 00  19 00 00 00 1a 00 00 00  |................|
00002530  1b 00 00 00 1c 00 00 00  1d 00 00 00 1e 00 00 00  |................|
00002540  1f 00 00 00 20 00 00 00  21 00 00 00 22 00 00 00  |.... ...!..."...|
00002550  23 00 00 00 24 00 00 00  00 00 00 00 00 00 00 00  |#...$...........|
00002560  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002580  a4 81 00 00 0c 00 00 00  67 85 b0 50 53 85 b0 50  |........g..PS..P|
00002590  53 85 b0 50 00 00 00 00  00 00 01 00 02 00 00 00  |S..P............|
000025a0  00 00 00 00 00 00 00 00  26 00 00 00 00 00 00 00  |........&.......|
000025b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000025e0  00 00 00 00 6b f9 df 6c  00 00 00 00 00 00 00 00  |....k..l........|
000025f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006000  02 00 00 00 0c 00 01 02  2e 00 00 00 02 00 00 00  |................|
00006010  0c 00 02 02 2e 2e 00 00  0b 00 00 00 14 00 0a 02  |................|
00006020  6c 6f 73 74 2b 66 6f 75  6e 64 00 00 0c 00 00 00  |lost+found......|
00006030  d4 03 09 01 63 61 77 61  6e 2e 74 78 74 00 00 00  |....cawan.txt...|
00006040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006400  0b 00 00 00 0c 00 01 02  2e 00 00 00 02 00 00 00  |................|
00006410  f4 03 02 02 2e 2e 00 00  00 00 00 00 00 00 00 00  |................|
00006420  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006800  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00006810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00006c00  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00006c10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007000  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007400  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007800  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00007c00  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00007c10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008000  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008400  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008800  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008c00  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00008c10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00009000  00 00 00 00 00 04 00 00  00 00 00 00 00 00 00 00  |................|
00009010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00009400  00 00 00 00 03 00 00 00  04 00 00 00 05 00 00 00  |................|
00009410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00009800  68 65 6c 6c 6f 20 63 61  77 61 6e 0a 00 00 00 00  |hello cawan.....|
00009810  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00100000
Host:#

Good, it seems everything fine and the cawan.txt is already in the cawan.img.
But, how we can make use of it ? Let us get to embedded environment now.

tango3[bin]# ls -l ./cawan.img
-rw-r--r--    1 root     root      1048576 Nov 24  2012 ./cawan.img
tango3[bin]# cat ./cawan.img > /dev/sigmblockh
tango3[bin]# cd cawan     
tango3[cawan]# ls
tango3[cawan]# cd ..
tango3[bin]# mount /dev/sigmblockh ./cawan       
tango3[bin]# cd cawan
tango3[cawan]# ls
cawan.txt   lost+found/
tango3[cawan]# cat cawan.txt
hello cawan
tango3[cawan]#

Well, once we cat the cawan.img into partition /dev/sigmblockh, the partition should
be ready to mount now. After it is being mounted, we can see our cawan.txt and of
course the content of it should be "hello cawan". By applying the same technique, if
the cawan.img is being mapped into RAM, then it is a volatile ramdisk. Of course, in
real implementation, it is up to you to choose what type of file system to be used in
you embedded system design.

pdf version:

http://www.scribd.com/doc/114266495/How-to-Create-a-Ramdisk-File-and-Make-Use-in-Embedded-System

No comments:

Post a Comment