Path: blob/master/Documentation/blockdev/ramdisk.txt
10821 views
Using the RAM disk block device with Linux1------------------------------------------23Contents:451) Overview62) Kernel Command Line Parameters73) Using "rdev -r"84) An Example of Creating a Compressed RAM Disk910111) Overview12-----------1314The RAM disk driver is a way to use main system memory as a block device. It15is required for initrd, an initial filesystem used if you need to load modules16in order to access the root filesystem (see Documentation/initrd.txt). It can17also be used for a temporary filesystem for crypto work, since the contents18are erased on reboot.1920The RAM disk dynamically grows as more space is required. It does this by using21RAM from the buffer cache. The driver marks the buffers it is using as dirty22so that the VM subsystem does not try to reclaim them later.2324The RAM disk supports up to 16 RAM disks by default, and can be reconfigured25to support an unlimited number of RAM disks (at your own risk). Just change26the configuration symbol BLK_DEV_RAM_COUNT in the Block drivers config menu27and (re)build the kernel.2829To use RAM disk support with your system, run './MAKEDEV ram' from the /dev30directory. RAM disks are all major number 1, and start with minor number 031for /dev/ram0, etc. If used, modern kernels use /dev/ram0 for an initrd.3233The new RAM disk also has the ability to load compressed RAM disk images,34allowing one to squeeze more programs onto an average installation or35rescue floppy disk.3637382) Kernel Command Line Parameters39---------------------------------4041ramdisk_size=N42==============4344This parameter tells the RAM disk driver to set up RAM disks of N k size. The45default is 4096 (4 MB) (8192 (8 MB) on S390).4647ramdisk_blocksize=N48===================4950This parameter tells the RAM disk driver how many bytes to use per block. The51default is 1024 (BLOCK_SIZE).5253543) Using "rdev -r"55------------------5657The usage of the word (two bytes) that "rdev -r" sets in the kernel image is58as follows. The low 11 bits (0 -> 10) specify an offset (in 1 k blocks) of up59to 2 MB (2^11) of where to find the RAM disk (this used to be the size). Bit6014 indicates that a RAM disk is to be loaded, and bit 15 indicates whether a61prompt/wait sequence is to be given before trying to read the RAM disk. Since62the RAM disk dynamically grows as data is being written into it, a size field63is not required. Bits 11 to 13 are not currently used and may as well be zero.64These numbers are no magical secrets, as seen below:6566./arch/i386/kernel/setup.c:#define RAMDISK_IMAGE_START_MASK 0x07FF67./arch/i386/kernel/setup.c:#define RAMDISK_PROMPT_FLAG 0x800068./arch/i386/kernel/setup.c:#define RAMDISK_LOAD_FLAG 0x40006970Consider a typical two floppy disk setup, where you will have the71kernel on disk one, and have already put a RAM disk image onto disk #2.7273Hence you want to set bits 0 to 13 as 0, meaning that your RAM disk74starts at an offset of 0 kB from the beginning of the floppy.75The command line equivalent is: "ramdisk_start=0"7677You want bit 14 as one, indicating that a RAM disk is to be loaded.78The command line equivalent is: "load_ramdisk=1"7980You want bit 15 as one, indicating that you want a prompt/keypress81sequence so that you have a chance to switch floppy disks.82The command line equivalent is: "prompt_ramdisk=1"8384Putting that together gives 2^15 + 2^14 + 0 = 49152 for an rdev word.85So to create disk one of the set, you would do:8687/usr/src/linux# cat arch/i386/boot/zImage > /dev/fd088/usr/src/linux# rdev /dev/fd0 /dev/fd089/usr/src/linux# rdev -r /dev/fd0 491529091If you make a boot disk that has LILO, then for the above, you would use:92append = "ramdisk_start=0 load_ramdisk=1 prompt_ramdisk=1"93Since the default start = 0 and the default prompt = 1, you could use:94append = "load_ramdisk=1"9596974) An Example of Creating a Compressed RAM Disk98----------------------------------------------99100To create a RAM disk image, you will need a spare block device to101construct it on. This can be the RAM disk device itself, or an102unused disk partition (such as an unmounted swap partition). For this103example, we will use the RAM disk device, "/dev/ram0".104105Note: This technique should not be done on a machine with less than 8 MB106of RAM. If using a spare disk partition instead of /dev/ram0, then this107restriction does not apply.108109a) Decide on the RAM disk size that you want. Say 2 MB for this example.110Create it by writing to the RAM disk device. (This step is not currently111required, but may be in the future.) It is wise to zero out the112area (esp. for disks) so that maximal compression is achieved for113the unused blocks of the image that you are about to create.114115dd if=/dev/zero of=/dev/ram0 bs=1k count=2048116117b) Make a filesystem on it. Say ext2fs for this example.118119mke2fs -vm0 /dev/ram0 2048120121c) Mount it, copy the files you want to it (eg: /etc/* /dev/* ...)122and unmount it again.123124d) Compress the contents of the RAM disk. The level of compression125will be approximately 50% of the space used by the files. Unused126space on the RAM disk will compress to almost nothing.127128dd if=/dev/ram0 bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz129130e) Put the kernel onto the floppy131132dd if=zImage of=/dev/fd0 bs=1k133134f) Put the RAM disk image onto the floppy, after the kernel. Use an offset135that is slightly larger than the kernel, so that you can put another136(possibly larger) kernel onto the same floppy later without overlapping137the RAM disk image. An offset of 400 kB for kernels about 350 kB in138size would be reasonable. Make sure offset+size of ram_image.gz is139not larger than the total space on your floppy (usually 1440 kB).140141dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400142143g) Use "rdev" to set the boot device, RAM disk offset, prompt flag, etc.144For prompt_ramdisk=1, load_ramdisk=1, ramdisk_start=400, one would145have 2^15 + 2^14 + 400 = 49552.146147rdev /dev/fd0 /dev/fd0148rdev -r /dev/fd0 49552149150That is it. You now have your boot/root compressed RAM disk floppy. Some151users may wish to combine steps (d) and (f) by using a pipe.152153--------------------------------------------------------------------------154Paul Gortmaker 12/95155156Changelog:157----------15815910-22-04 : Updated to reflect changes in command line options, remove160obsolete references, general cleanup.161James Nelson ([email protected])16216316412-95 : Original Document165166167