blob: 33e987df071ea050f8f6cfa56f642e6dc88b6968 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001Using the RAM disk block device with Linux
2------------------------------------------
3
4Contents:
5
6 1) Overview
7 2) Kernel Command Line Parameters
8 3) Using "rdev -r"
Nathan Scott086626a72006-07-14 00:24:10 -07009 4) An Example of Creating a Compressed RAM Disk
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11
121) Overview
13-----------
14
15The RAM disk driver is a way to use main system memory as a block device. It
16is required for initrd, an initial filesystem used if you need to load modules
17in order to access the root filesystem (see Documentation/initrd.txt). It can
18also be used for a temporary filesystem for crypto work, since the contents
19are erased on reboot.
20
21The RAM disk dynamically grows as more space is required. It does this by using
22RAM from the buffer cache. The driver marks the buffers it is using as dirty
23so that the VM subsystem does not try to reclaim them later.
24
Randy Dunlap18107322007-10-16 23:29:29 -070025The RAM disk supports up to 16 RAM disks by default, and can be reconfigured
26to support an unlimited number of RAM disks (at your own risk). Just change
27the configuration symbol BLK_DEV_RAM_COUNT in the Block drivers config menu
28and (re)build the kernel.
29
30To use RAM disk support with your system, run './MAKEDEV ram' from the /dev
31directory. RAM disks are all major number 1, and start with minor number 0
32for /dev/ram0, etc. If used, modern kernels use /dev/ram0 for an initrd.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34The old "ramdisk=<ram_size>" has been changed to "ramdisk_size=<ram_size>" to
35make it clearer. The original "ramdisk=<ram_size>" has been kept around for
36compatibility reasons, but it may be removed in the future.
Randy Dunlap18107322007-10-16 23:29:29 -070037There are also config symbols (in the Block drivers config menu) for these
38variables: BLK_DEV_RAM_SIZE defaults to 4096 and BLK_DEV_RAM_BLOCKSIZE
39defaults to 1024.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41The new RAM disk also has the ability to load compressed RAM disk images,
Nathan Scott086626a72006-07-14 00:24:10 -070042allowing one to squeeze more programs onto an average installation or
Linus Torvalds1da177e2005-04-16 15:20:36 -070043rescue floppy disk.
44
45
462) Kernel Command Line Parameters
47---------------------------------
48
49 ramdisk_size=N
50 ==============
51
52This parameter tells the RAM disk driver to set up RAM disks of N k size. The
53default is 4096 (4 MB) (8192 (8 MB) on S390).
54
55 ramdisk_blocksize=N
56 ===================
57
58This parameter tells the RAM disk driver how many bytes to use per block. The
Nathan Scott086626a72006-07-14 00:24:10 -070059default is 1024 (BLOCK_SIZE).
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61
623) Using "rdev -r"
63------------------
64
65The usage of the word (two bytes) that "rdev -r" sets in the kernel image is
66as follows. The low 11 bits (0 -> 10) specify an offset (in 1 k blocks) of up
67to 2 MB (2^11) of where to find the RAM disk (this used to be the size). Bit
6814 indicates that a RAM disk is to be loaded, and bit 15 indicates whether a
69prompt/wait sequence is to be given before trying to read the RAM disk. Since
70the RAM disk dynamically grows as data is being written into it, a size field
71is not required. Bits 11 to 13 are not currently used and may as well be zero.
72These numbers are no magical secrets, as seen below:
73
74./arch/i386/kernel/setup.c:#define RAMDISK_IMAGE_START_MASK 0x07FF
75./arch/i386/kernel/setup.c:#define RAMDISK_PROMPT_FLAG 0x8000
76./arch/i386/kernel/setup.c:#define RAMDISK_LOAD_FLAG 0x4000
77
Nathan Scott086626a72006-07-14 00:24:10 -070078Consider a typical two floppy disk setup, where you will have the
Linus Torvalds1da177e2005-04-16 15:20:36 -070079kernel on disk one, and have already put a RAM disk image onto disk #2.
80
81Hence you want to set bits 0 to 13 as 0, meaning that your RAM disk
82starts at an offset of 0 kB from the beginning of the floppy.
83The command line equivalent is: "ramdisk_start=0"
84
85You want bit 14 as one, indicating that a RAM disk is to be loaded.
86The command line equivalent is: "load_ramdisk=1"
87
88You want bit 15 as one, indicating that you want a prompt/keypress
89sequence so that you have a chance to switch floppy disks.
90The command line equivalent is: "prompt_ramdisk=1"
91
92Putting that together gives 2^15 + 2^14 + 0 = 49152 for an rdev word.
93So to create disk one of the set, you would do:
94
95 /usr/src/linux# cat arch/i386/boot/zImage > /dev/fd0
96 /usr/src/linux# rdev /dev/fd0 /dev/fd0
97 /usr/src/linux# rdev -r /dev/fd0 49152
98
99If you make a boot disk that has LILO, then for the above, you would use:
100 append = "ramdisk_start=0 load_ramdisk=1 prompt_ramdisk=1"
101Since the default start = 0 and the default prompt = 1, you could use:
102 append = "load_ramdisk=1"
103
104
Nathan Scott086626a72006-07-14 00:24:10 -07001054) An Example of Creating a Compressed RAM Disk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106----------------------------------------------
107
108To create a RAM disk image, you will need a spare block device to
109construct it on. This can be the RAM disk device itself, or an
Nathan Scott086626a72006-07-14 00:24:10 -0700110unused disk partition (such as an unmounted swap partition). For this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111example, we will use the RAM disk device, "/dev/ram0".
112
113Note: This technique should not be done on a machine with less than 8 MB
114of RAM. If using a spare disk partition instead of /dev/ram0, then this
115restriction does not apply.
116
117a) Decide on the RAM disk size that you want. Say 2 MB for this example.
118 Create it by writing to the RAM disk device. (This step is not currently
119 required, but may be in the future.) It is wise to zero out the
120 area (esp. for disks) so that maximal compression is achieved for
121 the unused blocks of the image that you are about to create.
122
123 dd if=/dev/zero of=/dev/ram0 bs=1k count=2048
124
125b) Make a filesystem on it. Say ext2fs for this example.
126
127 mke2fs -vm0 /dev/ram0 2048
128
129c) Mount it, copy the files you want to it (eg: /etc/* /dev/* ...)
130 and unmount it again.
131
132d) Compress the contents of the RAM disk. The level of compression
133 will be approximately 50% of the space used by the files. Unused
134 space on the RAM disk will compress to almost nothing.
135
136 dd if=/dev/ram0 bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz
137
138e) Put the kernel onto the floppy
139
140 dd if=zImage of=/dev/fd0 bs=1k
141
142f) Put the RAM disk image onto the floppy, after the kernel. Use an offset
143 that is slightly larger than the kernel, so that you can put another
144 (possibly larger) kernel onto the same floppy later without overlapping
145 the RAM disk image. An offset of 400 kB for kernels about 350 kB in
146 size would be reasonable. Make sure offset+size of ram_image.gz is
147 not larger than the total space on your floppy (usually 1440 kB).
148
149 dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400
150
151g) Use "rdev" to set the boot device, RAM disk offset, prompt flag, etc.
152 For prompt_ramdisk=1, load_ramdisk=1, ramdisk_start=400, one would
153 have 2^15 + 2^14 + 400 = 49552.
154
155 rdev /dev/fd0 /dev/fd0
156 rdev -r /dev/fd0 49552
157
158That is it. You now have your boot/root compressed RAM disk floppy. Some
159users may wish to combine steps (d) and (f) by using a pipe.
160
161--------------------------------------------------------------------------
162 Paul Gortmaker 12/95
163
164Changelog:
165----------
166
16710-22-04 : Updated to reflect changes in command line options, remove
168 obsolete references, general cleanup.
169 James Nelson (james4765@gmail.com)
170
171
17212-95 : Original Document