1 |
1625 |
jcastillo |
|
2 |
|
|
Using the RAM disk block device with Linux
|
3 |
|
|
------------------------------------------
|
4 |
|
|
|
5 |
|
|
Contents:
|
6 |
|
|
|
7 |
|
|
1) Overview
|
8 |
|
|
2) Kernel Command Line Parameters
|
9 |
|
|
3) Using "rdev -r" With New Kernels
|
10 |
|
|
4) An Example of Creating a Compressed ramdisk
|
11 |
|
|
|
12 |
|
|
|
13 |
|
|
1) Overview
|
14 |
|
|
-----------
|
15 |
|
|
|
16 |
|
|
As of kernel v1.3.48, the ramdisk driver was substantially changed.
|
17 |
|
|
|
18 |
|
|
The older versions would grab a chunk of memory off the top before
|
19 |
|
|
handing the remainder to the kernel at boot time. Thus a size parameter
|
20 |
|
|
had to be specified via "ramdisk=1440" or "rdev -r /dev/fd0 1440" so
|
21 |
|
|
that the driver knew how much memory to grab.
|
22 |
|
|
|
23 |
|
|
Now the ramdisk dynamically grows as more space is required. It does
|
24 |
|
|
this by using RAM from the buffer cache. The driver marks the buffers
|
25 |
|
|
it is using with a new "BH_Protected" flag so that the kernel does
|
26 |
|
|
not try to reuse them later. This means that the old size parameter
|
27 |
|
|
is no longer used, new command line parameters exist, and the behavior
|
28 |
|
|
of the "rdev -r" or "ramsize" (usually a symbolic link to "rdev")
|
29 |
|
|
command has changed.
|
30 |
|
|
|
31 |
|
|
Also, the new ramdisk supports up to 16 ramdisks out of the box, and can
|
32 |
|
|
be reconfigured in rd.c to support up to 255 ramdisks. To use multiple
|
33 |
|
|
ramdisk support with your system, run 'mknod /dev/ramX b 1 X' and chmod
|
34 |
|
|
(to change it's permissions) it to your liking. The default /dev/ram(disk)
|
35 |
|
|
uses minor #1, so start with ram2 and go from there.
|
36 |
|
|
|
37 |
|
|
The old "ramdisk=" has been changed to "ramdisk_size="
|
38 |
|
|
to make it clearer. The original "ramdisk=" has been kept around
|
39 |
|
|
for compatibility reasons, but it will probably be removed in 2.1.x.
|
40 |
|
|
|
41 |
|
|
The new ramdisk also has the ability to load compressed ramdisk images,
|
42 |
|
|
allowing one to squeeze more programs onto an average installation or
|
43 |
|
|
rescue floppy disk.
|
44 |
|
|
|
45 |
|
|
Notes: You may have "dev/ram" or "/dev/ramdisk" or both. They are
|
46 |
|
|
equivalent from the standpoint of this document. Also, the new ramdisk
|
47 |
|
|
is a config option. When running "make config", make sure you enable
|
48 |
|
|
ramdisk support for the kernel you intend to use the ramdisk with.
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
2) Kernel Command Line Parameters
|
52 |
|
|
---------------------------------
|
53 |
|
|
|
54 |
|
|
ramdisk_start=NNN
|
55 |
|
|
=================
|
56 |
|
|
|
57 |
|
|
To allow a kernel image to reside on a floppy disk along with a compressed
|
58 |
|
|
ramdisk image, the "ramdisk_start=" command was added. The kernel
|
59 |
|
|
can't be included into the compressed ramdisk filesystem image, because
|
60 |
|
|
it needs to be stored starting at block zero so that the BIOS can load the
|
61 |
|
|
bootsector and then the kernel can bootstrap itself to get going.
|
62 |
|
|
|
63 |
|
|
Note: If you are using an uncompressed ramdisk image, then the kernel can
|
64 |
|
|
be a part of the filesystem image that is being loaded into the ramdisk,
|
65 |
|
|
and the floppy can be booted with LILO, or the two can be separate as
|
66 |
|
|
is done for the compressed images.
|
67 |
|
|
|
68 |
|
|
If you are using a two-disk boot/root setup (kernel on #1, ramdisk image
|
69 |
|
|
on #2) then the ramdisk would start at block zero, and an offset of
|
70 |
|
|
zero would be used. Since this is the default value, you would not need
|
71 |
|
|
to actually use the command at all.
|
72 |
|
|
|
73 |
|
|
If instead, you have a "zImage" of about 350k, and a "fs_image.gz" of
|
74 |
|
|
say about 1MB, and you want them both on the same disk, then you
|
75 |
|
|
would use an offset. If you stored the "fs_image.gz" onto the floppy
|
76 |
|
|
starting at an offset of 400kB, you would use "ramdisk_start=400".
|
77 |
|
|
|
78 |
|
|
|
79 |
|
|
load_ramdisk=N
|
80 |
|
|
==============
|
81 |
|
|
|
82 |
|
|
This parameter tells the kernel whether it is to try to load a
|
83 |
|
|
ramdisk image or not. Specifying "load_ramdisk=1" will tell the
|
84 |
|
|
kernel to load a floppy into the ramdisk. The default value is
|
85 |
|
|
zero, meaning that the kernel should not try to load a ramdisk.
|
86 |
|
|
|
87 |
|
|
|
88 |
|
|
prompt_ramdisk=N
|
89 |
|
|
================
|
90 |
|
|
|
91 |
|
|
This parameter tells the kernel whether or not to give you a prompt
|
92 |
|
|
asking you to insert the floppy containing the ramdisk image. In
|
93 |
|
|
a single floppy configuration the ramdisk image is on the same floppy
|
94 |
|
|
as the kernel that just finished loading/booting and so a prompt
|
95 |
|
|
is not needed. In this case one can use "prompt_ramdisk=0". In a
|
96 |
|
|
two floppy configuration, you will need the chance to switch disks,
|
97 |
|
|
and thus "prompt_ramdisk=1" can be used. Since this is the default
|
98 |
|
|
value, it doesn't really need to be specified.
|
99 |
|
|
|
100 |
|
|
ramdisk_size=N
|
101 |
|
|
==============
|
102 |
|
|
|
103 |
|
|
This parameter tells the ramdisk driver to set up ramdisks of Nk size. The
|
104 |
|
|
default is 4096 (4MB).
|
105 |
|
|
|
106 |
|
|
3) Using "rdev -r" With New Kernels
|
107 |
|
|
-----------------------------------
|
108 |
|
|
|
109 |
|
|
The usage of the word (two bytes) that "rdev -r" sets in the kernel image
|
110 |
|
|
has changed. The low 11 bits (0 -> 10) specify an offset (in 1k blocks)
|
111 |
|
|
of up to 2MB (2^11) of where to find the ramdisk (this used to be the
|
112 |
|
|
size). Bit 14 indicates that a ramdisk is to be loaded, and bit 15
|
113 |
|
|
indicates whether a prompt/wait sequence is to be given before trying
|
114 |
|
|
to read the ramdisk. Since the ramdisk dynamically grows as data is
|
115 |
|
|
being written into it, a size field is no longer required. Bits 11
|
116 |
|
|
to 13 are not presently used and may as well be zero. These numbers
|
117 |
|
|
are no magical secrets, as seen below:
|
118 |
|
|
|
119 |
|
|
./arch/i386/kernel/setup.c:#define RAMDISK_IMAGE_START_MASK 0x07FF
|
120 |
|
|
./arch/i386/kernel/setup.c:#define RAMDISK_PROMPT_FLAG 0x8000
|
121 |
|
|
./arch/i386/kernel/setup.c:#define RAMDISK_LOAD_FLAG 0x4000
|
122 |
|
|
|
123 |
|
|
Consider a typical two floppy disk setup, where you will have the
|
124 |
|
|
kernel on disk one, and have already put a ramdisk image onto disk #2.
|
125 |
|
|
|
126 |
|
|
Hence you want to set bits 0 to 13 as zero, meaning that your ramdisk
|
127 |
|
|
starts at an offset of zero kB from the beginning of the floppy.
|
128 |
|
|
The command line equivalent is: "ramdisk_start=0"
|
129 |
|
|
|
130 |
|
|
You want bit 14 as one, indicating that a ramdisk is to be loaded.
|
131 |
|
|
The command line equivalent is: "load_ramdisk=1"
|
132 |
|
|
|
133 |
|
|
You want bit 15 as one, indicating that you want a prompt/keypress
|
134 |
|
|
sequence so that you have a chance to switch floppy disks.
|
135 |
|
|
The command line equivalent is: "prompt_ramdisk=1"
|
136 |
|
|
|
137 |
|
|
Putting that together gives 2^15 + 2^14 + 0 = 49152 for an rdev word.
|
138 |
|
|
So to create disk one of the set, you would do:
|
139 |
|
|
|
140 |
|
|
/usr/src/linux# cat arch/i386/boot/zImage > /dev/fd0
|
141 |
|
|
/usr/src/linux# rdev /dev/fd0 /dev/fd0
|
142 |
|
|
/usr/src/linux# rdev -r /dev/fd0 49152
|
143 |
|
|
|
144 |
|
|
If you make a boot disk that has LILO, then for the above, you would use:
|
145 |
|
|
append = "ramdisk_start=0 load_ramdisk=1 prompt_ramdisk=1"
|
146 |
|
|
Since the default start = 0 and the default prompt = 1, you could use:
|
147 |
|
|
append = "load_ramdisk=1"
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
4) An Example of Creating a Compressed ramdisk
|
151 |
|
|
----------------------------------------------
|
152 |
|
|
|
153 |
|
|
To create a ramdisk image, you will need a spare block device to
|
154 |
|
|
construct it on. This can be the ramdisk device itself, or an
|
155 |
|
|
unused disk partition (such as an unmounted swap partition). For this
|
156 |
|
|
example, we will use the ramdisk device, "/dev/ram".
|
157 |
|
|
|
158 |
|
|
Note: This technique should not be done on a machine with less than 8MB
|
159 |
|
|
of RAM. If using a spare disk partition instead of /dev/ram, then this
|
160 |
|
|
restriction does not apply.
|
161 |
|
|
|
162 |
|
|
a) Decide on the ramdisk size that you want. Say 2MB for this example.
|
163 |
|
|
Create it by writing to the ramdisk device. (This step is not presently
|
164 |
|
|
required, but may be in the future.) It is wise to zero out the
|
165 |
|
|
area (esp. for disks) so that maximal compression is achieved for
|
166 |
|
|
the unused blocks of the image that you are about to create.
|
167 |
|
|
|
168 |
|
|
dd if=/dev/zero of=/dev/ram bs=1k count=2048
|
169 |
|
|
|
170 |
|
|
b) Make a filesystem on it. Say ext2fs for this example.
|
171 |
|
|
|
172 |
|
|
mke2fs -vm0 /dev/ram 2048
|
173 |
|
|
|
174 |
|
|
c) Mount it, copy the files you want to it (eg: /etc/* /dev/* ...)
|
175 |
|
|
and unmount it again.
|
176 |
|
|
|
177 |
|
|
d) Compress the contents of the ramdisk. The level of compression
|
178 |
|
|
will be approximately 50% of the space used by the files. Unused
|
179 |
|
|
space on the ramdisk will compress to almost nothing.
|
180 |
|
|
|
181 |
|
|
dd if=/dev/ram bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz
|
182 |
|
|
|
183 |
|
|
e) Put the kernel onto the floppy
|
184 |
|
|
|
185 |
|
|
dd if=zImage of=/dev/fd0 bs=1k
|
186 |
|
|
|
187 |
|
|
f) Put the ramdisk image onto the floppy, after the kernel. Use an offset
|
188 |
|
|
that is slightly larger than the kernel, so that you can put another
|
189 |
|
|
(possibly larger) kernel onto the same floppy later without overlapping
|
190 |
|
|
the ramdisk image. An offset of 400kB for kernels about 350kB in
|
191 |
|
|
size would be reasonable. Make sure offset+size of ram_image.gz is
|
192 |
|
|
not larger than the total space on your floppy (usually 1440kB).
|
193 |
|
|
|
194 |
|
|
dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400
|
195 |
|
|
|
196 |
|
|
g) Use "rdev" to set the boot device, ramdisk offset, prompt flag, etc.
|
197 |
|
|
For prompt_ramdisk=1, load_ramdisk=1, ramdisk_start=400, one would
|
198 |
|
|
have 2^15 + 2^14 + 400 = 49552.
|
199 |
|
|
|
200 |
|
|
rdev /dev/fd0 /dev/fd0
|
201 |
|
|
rdev -r /dev/fd0 49552
|
202 |
|
|
|
203 |
|
|
That is it. You now have your boot/root compressed ramdisk floppy. Some
|
204 |
|
|
users may wish to combine steps (d) and (f) by using a pipe.
|
205 |
|
|
|
206 |
|
|
--------------------------------------------------------------------------
|
207 |
|
|
Paul Gortmaker 12/95
|