Вы находитесь на странице: 1из 6

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
00000000
*
00000400
00000410
00000420
00000430
00000440
00000450
00000460
00000470
00000480
*
000004c0
000004d0
000004e0

./cawan.img
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00

|................|

80
75
00
0d
0d
00
02
ab
00

00
00
80
53
00
80
0a
00
00

00
00
00
00
00
00
e1
00
00

|................|
|u...............|
|. ... ..........|
|...P....S.......|
|...P.N..........|
|............8...|
|...........,OkH.|
|..*.............|
|................|

00 00 00 00 00 00 03 00
00 00 00 00 00 00 00 00
00 00 00 00 4a ad 9b 21

|................|
|................|
|............J..!|

00
00
20
82
82
00
00
02
00

00
00
00
b0
b0
00
00
2a
00

00
00
00
50
50
00
00
9d
00

00
01
00
00
00
0b
01
01
00

04
00
20
00
4e
00
00
fa
00

00
00
00
17
ed
00
00
14
00

00
00
00
00
00
00
00
e5
00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

00
00
00
ef
00
00
80
00
00

00
00
00
01
00
00
ba
00
00

00
00
00
00
00
00
2c
00
00

da
00
00
01
01
38
4f
00
00

03
00
00
00
00
00
6b
00
00

00
00
00
00
00
00
48
00
00

000004f0
00000500
00000510
*
00000560
00000570
*
00000800
00000810
00000820
*
00001800
00001810
*
00001870
00001880
*
00001c00
00001c10
*
00002000
00002010
00002020
*
00002080
00002090
000020a0
000020b0
*
00002300
00002310
00002320
*
00002350
00002360
*
00002500
00002510
00002520
00002530
00002540
00002550
00002560
*
00006000
00006010
00006020
00006030
*
00006400
00006410
00006420
*
00006800
00006810
*
00006c00
00006c10
*
00007000
00007010
*
00007400

6f 8c 49 a6 a6 1d 0d 98
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

0a 38 a9 d8 01 00 00 00
0d 82 b0 50 00 00 00 00
00 00 00 00 00 00 00 00

|o.I......8......|
|...........P....|
|................|

01 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

06 00 00 00 07 00 00 00
02 00 04 00 00 00 00 00
00 00 00 00 00 00 00 00

08 00 00 00 da 03 75 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|..............u.|
|................|
|................|

ff ff ff ff 1f 00 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 00 00 00
ff ff ff ff ff ff ff ff

00 00 00 00 00 00 00 80
ff ff ff ff ff ff ff ff

|................|
|................|

ff 07 00 00 00 00 00 00
ff ff ff ff ff ff ff ff

00 00 00 00 00 00 00 00
ff ff ff ff ff ff ff ff

|................|
|................|

00 00 00 00 00 00 00 00
0d 82 b0 50 00 00 00 00
00 00 00 00 00 00 00 00

0d 82 b0 50 0d 82 b0 50
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|...........P...P|
|...P............|
|................|

ed
0d
00
00

0d
00
18
00

50
00
00
00

|.A.........P...P|
|...P............|
|................|
|................|

80 81 00 00 00 30 04 04
0d 82 b0 50 00 00 00 00
00 00 00 00 00 00 00 00

0d 82 b0 50 0d 82 b0 50
00 00 01 00 08 00 00 00
00 00 00 00 00 00 00 00

|.....0.....P...P|
|...P............|
|................|

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

00 00 00 00 25 00 00 00
00 00 00 00 00 00 00 00

|............%...|
|................|

c0
0d
00
1b
1f
23
00

41
82
00
00
00
00
00

00
b0
00
00
00
00
00

00
50
00
00
00
00
00

00
00
00
1c
20
24
00

30
00
00
00
00
00
00

00
00
00
00
00
00
00

00
00
00
00
00
00
00

0d
00
19
1d
21
00
00

82
00
00
00
00
00
00

b0
02
00
00
00
00
00

50
00
00
00
00
00
00

0d
18
1a
1e
22
00
00

82
00
00
00
00
00
00

b0
00
00
00
00
00
00

50
00
00
00
00
00
00

|.A...0.....P...P|
|...P............|
|................|
|................|
|.... ...!..."...|
|#...$...........|
|................|

02
0c
6c
00

00
00
6f
00

00
02
73
00

00
02
74
00

0c
2e
2b
00

00
2e
66
00

01
00
6f
00

02
00
75
00

2e
0b
6e
00

00
00
64
00

00
00
00
00

00
00
00
00

02
e8
00
00

00
03
00
00

00
0a
00
00

00
02
00
00

|................|
|................|
|lost+found......|
|................|

0b 00 00 00 0c 00 01 02
f4 03 02 02 2e 2e 00 00
00 00 00 00 00 00 00 00

2e 00 00 00 02 00 00 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00

00 00 00 00 00 00 00 00

|................|

41
82
00
00

00
b0
00
00

00
50
00
00

00
00
00
00

04
00
00
00

00
00
00
00

00
00
00
00

82
00
00
00

b0
03
00
00

50
00
00
00

0d
02
00
00

82
00
00
00

b0
00
00
00

00007410
*
00007800
00007810
*
00007c00
00007c10
*
00008000
00008010
*
00008400
00008410
*
00008800
00008810
*
00008c00
00008c10
*
00009000
00009010
*
00009400
00009410
*
00100000

00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00

|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 03 00 00 00
00 00 00 00 00 00 00 00

04 00 00 00 05 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

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
00000000
*
00000400
00000410
00000420
00000430
00000440
00000450
00000460
00000470
00000480

./cawan.img
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00

|................|

80
74
00
81
0d
00
02
ab
00

00
00
80
53
00
80
0a
00
00

|................|
|t...............|
|. ... ......0..P|
|...P....S.......|
|...P.N..........|
|............8...|
|...........,OkH.|
|..*.............|
|................|

00
00
20
85
82
00
00
02
00

00
00
00
b0
b0
00
00
2a
00

00
00
00
50
50
00
00
9d
00

00
01
00
01
00
0b
01
01
00

04
00
20
00
4e
00
00
fa
00

00
00
00
17
ed
00
00
14
00

00
00
00
00
00
00
00
e5
00

00
00
00
ef
00
00
80
00
00

00
00
00
01
00
00
ba
00
00

00
00
00
00
00
00
2c
00
00

d9
00
30
01
01
38
4f
00
00

03
00
85
00
00
00
6b
00
00

00
00
b0
00
00
00
48
00
00

00
00
50
00
00
00
e1
00
00

*
000004c0
000004d0
000004e0
000004f0
00000500
00000510
*
00000560
00000570
*
00000800
00000810
00000820
*
00001800
00001810
*
00001870
00001880
*
00001c00
00001c10
*
00002000
00002010
00002020
*
00002080
00002090
000020a0
000020b0
*
00002300
00002310
00002320
*
00002350
00002360
*
00002500
00002510
00002520
00002530
00002540
00002550
00002560
*
00002580
00002590
000025a0
000025b0
*
000025e0
000025f0
*
00006000
00006010
00006020
00006030
00006040
*
00006400

00
00
00
6f
00
00

00
00
00
8c
00
00

00
00
21
00
00
00

|................|
|................|
|............J..!|
|o.I......8......|
|...........P....|
|................|

01 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

06 00 00 00 07 00 00 00
02 00 04 00 00 00 00 00
00 00 00 00 00 00 00 00

08 00 00 00 d9 03 74 00
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|..............t.|
|................|
|................|

ff ff ff ff 3f 00 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|....?...........|
|................|

00 00 00 00 00 00 00 00
ff ff ff ff ff ff ff ff

00 00 00 00 00 00 00 80
ff ff ff ff ff ff ff ff

|................|
|................|

ff 0f 00 00 00 00 00 00
ff ff ff ff ff ff ff ff

00 00 00 00 00 00 00 00
ff ff ff ff ff ff ff ff

|................|
|................|

00 00 00 00 00 00 00 00
0d 82 b0 50 00 00 00 00
00 00 00 00 00 00 00 00

0d 82 b0 50 0d 82 b0 50
00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|...........P...P|
|...P............|
|................|

ed
53
00
00

5f
00
18
00

50
00
00
00

|.A......_..PS..P|
|S..P............|
|................|
|................|

80 81 00 00 00 30 04 04
0d 82 b0 50 00 00 00 00
00 00 00 00 00 00 00 00

0d 82 b0 50 0d 82 b0 50
00 00 01 00 08 00 00 00
00 00 00 00 00 00 00 00

|.....0.....P...P|
|...P............|
|................|

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

00 00 00 00 25 00 00 00
00 00 00 00 00 00 00 00

|............%...|
|................|

c0
0d
00
1b
1f
23
00

41
82
00
00
00
00
00

00
b0
00
00
00
00
00

00
50
00
00
00
00
00

00
00
00
1c
20
24
00

30
00
00
00
00
00
00

00
00
00
00
00
00
00

00
00
00
00
00
00
00

0d
00
19
1d
21
00
00

82
00
00
00
00
00
00

b0
02
00
00
00
00
00

50
00
00
00
00
00
00

0d
18
1a
1e
22
00
00

82
00
00
00
00
00
00

b0
00
00
00
00
00
00

50
00
00
00
00
00
00

|.A...0.....P...P|
|...P............|
|................|
|................|
|.... ...!..."...|
|#...$...........|
|................|

a4
53
00
00

81
85
00
00

00
b0
00
00

00
50
00
00

0c
00
00
00

00
00
00
00

00
00
00
00

00
00
00
00

67
00
26
00

85
00
00
00

b0
01
00
00

50
00
00
00

53
02
00
00

85
00
00
00

b0
00
00
00

50
00
00
00

|........g..PS..P|
|S..P............|
|........&.......|
|................|

00 00 00 00 6b f9 df 6c
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|....k..l........|
|................|

02
0c
6c
d4
00

2e
0b
6e
6e
00

00
02
00
00
00

|................|
|................|
|lost+found......|
|....cawan.txt...|
|................|

2e 00 00 00 02 00 00 00

|................|

41
85
00
00

00
00
6f
03
00

00
00
00
49
00
00

00
b0
00
00

00
02
73
09
00

00
00
00
a6
00
00

00
50
00
00

00
02
74
01
00

00
00
00
a6
00
00

00
00
00
00

0c
2e
2b
63
00

00
00
00
1d
00
00

04
00
00
00

00
2e
66
61
00

00
00
00
0d
00
00

00
00
00
00

01
00
6f
77
00

00
00
00
98
00
00

00
00
00
00

02
00
75
61
00

0b 00 00 00 0c 00 01 02

00
00
00
0a
0d
00

00
00
00
38
82
00

85
00
00
00

00
00
64
2e
00

00
00
00
a9
b0
00

b0
03
00
00

00
00
00
74
00

00
00
00
d8
50
00

50
00
00
00

00
00
00
78
00

00
00
4a
01
00
00

53
02
00
00

02
14
0c
74
00

00
00
ad
00
00
00

85
00
00
00

00
00
00
00
00

03
00
9b
00
00
00

b0
00
00
00

00
0a
00
00
00

00006410
00006420
*
00006800
00006810
*
00006c00
00006c10
*
00007000
00007010
*
00007400
00007410
*
00007800
00007810
*
00007c00
00007c10
*
00008000
00008010
*
00008400
00008410
*
00008800
00008810
*
00008c00
00008c10
*
00009000
00009010
*
00009400
00009410
*
00009800
00009810
*
00100000
Host:#

f4 03 02 02 2e 2e 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 00 04 00 00
00 00 00 00 00 00 00 00

00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

00 00 00 00 03 00 00 00
00 00 00 00 00 00 00 00

04 00 00 00 05 00 00 00
00 00 00 00 00 00 00 00

|................|
|................|

68 65 6c 6c 6f 20 63 61
00 00 00 00 00 00 00 00

77 61 6e 0a 00 00 00 00
00 00 00 00 00 00 00 00

|hello cawan.....|
|................|

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

2012 ./cawan.img

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.

Вам также может понравиться