ramdisk.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. Using the RAM disk block device with Linux
  2. ------------------------------------------
  3. Contents:
  4. 1) Overview
  5. 2) Kernel Command Line Parameters
  6. 3) Using "rdev -r"
  7. 4) An Example of Creating a Compressed RAM Disk
  8. 1) Overview
  9. -----------
  10. The RAM disk driver is a way to use main system memory as a block device. It
  11. is required for initrd, an initial filesystem used if you need to load modules
  12. in order to access the root filesystem (see Documentation/initrd.txt). It can
  13. also be used for a temporary filesystem for crypto work, since the contents
  14. are erased on reboot.
  15. The RAM disk dynamically grows as more space is required. It does this by using
  16. RAM from the buffer cache. The driver marks the buffers it is using as dirty
  17. so that the VM subsystem does not try to reclaim them later.
  18. The RAM disk supports up to 16 RAM disks by default, and can be reconfigured
  19. to support an unlimited number of RAM disks (at your own risk). Just change
  20. the configuration symbol BLK_DEV_RAM_COUNT in the Block drivers config menu
  21. and (re)build the kernel.
  22. To use RAM disk support with your system, run './MAKEDEV ram' from the /dev
  23. directory. RAM disks are all major number 1, and start with minor number 0
  24. for /dev/ram0, etc. If used, modern kernels use /dev/ram0 for an initrd.
  25. The new RAM disk also has the ability to load compressed RAM disk images,
  26. allowing one to squeeze more programs onto an average installation or
  27. rescue floppy disk.
  28. 2) Parameters
  29. ---------------------------------
  30. 2a) Kernel Command Line Parameters
  31. ramdisk_size=N
  32. ==============
  33. This parameter tells the RAM disk driver to set up RAM disks of N k size. The
  34. default is 4096 (4 MB).
  35. 2b) Module parameters
  36. rd_nr
  37. =====
  38. /dev/ramX devices created.
  39. max_part
  40. ========
  41. Maximum partition number.
  42. rd_size
  43. =======
  44. See ramdisk_size.
  45. 3) Using "rdev -r"
  46. ------------------
  47. The usage of the word (two bytes) that "rdev -r" sets in the kernel image is
  48. as follows. The low 11 bits (0 -> 10) specify an offset (in 1 k blocks) of up
  49. to 2 MB (2^11) of where to find the RAM disk (this used to be the size). Bit
  50. 14 indicates that a RAM disk is to be loaded, and bit 15 indicates whether a
  51. prompt/wait sequence is to be given before trying to read the RAM disk. Since
  52. the RAM disk dynamically grows as data is being written into it, a size field
  53. is not required. Bits 11 to 13 are not currently used and may as well be zero.
  54. These numbers are no magical secrets, as seen below:
  55. ./arch/x86/kernel/setup.c:#define RAMDISK_IMAGE_START_MASK 0x07FF
  56. ./arch/x86/kernel/setup.c:#define RAMDISK_PROMPT_FLAG 0x8000
  57. ./arch/x86/kernel/setup.c:#define RAMDISK_LOAD_FLAG 0x4000
  58. Consider a typical two floppy disk setup, where you will have the
  59. kernel on disk one, and have already put a RAM disk image onto disk #2.
  60. Hence you want to set bits 0 to 13 as 0, meaning that your RAM disk
  61. starts at an offset of 0 kB from the beginning of the floppy.
  62. The command line equivalent is: "ramdisk_start=0"
  63. You want bit 14 as one, indicating that a RAM disk is to be loaded.
  64. The command line equivalent is: "load_ramdisk=1"
  65. You want bit 15 as one, indicating that you want a prompt/keypress
  66. sequence so that you have a chance to switch floppy disks.
  67. The command line equivalent is: "prompt_ramdisk=1"
  68. Putting that together gives 2^15 + 2^14 + 0 = 49152 for an rdev word.
  69. So to create disk one of the set, you would do:
  70. /usr/src/linux# cat arch/x86/boot/zImage > /dev/fd0
  71. /usr/src/linux# rdev /dev/fd0 /dev/fd0
  72. /usr/src/linux# rdev -r /dev/fd0 49152
  73. If you make a boot disk that has LILO, then for the above, you would use:
  74. append = "ramdisk_start=0 load_ramdisk=1 prompt_ramdisk=1"
  75. Since the default start = 0 and the default prompt = 1, you could use:
  76. append = "load_ramdisk=1"
  77. 4) An Example of Creating a Compressed RAM Disk
  78. ----------------------------------------------
  79. To create a RAM disk image, you will need a spare block device to
  80. construct it on. This can be the RAM disk device itself, or an
  81. unused disk partition (such as an unmounted swap partition). For this
  82. example, we will use the RAM disk device, "/dev/ram0".
  83. Note: This technique should not be done on a machine with less than 8 MB
  84. of RAM. If using a spare disk partition instead of /dev/ram0, then this
  85. restriction does not apply.
  86. a) Decide on the RAM disk size that you want. Say 2 MB for this example.
  87. Create it by writing to the RAM disk device. (This step is not currently
  88. required, but may be in the future.) It is wise to zero out the
  89. area (esp. for disks) so that maximal compression is achieved for
  90. the unused blocks of the image that you are about to create.
  91. dd if=/dev/zero of=/dev/ram0 bs=1k count=2048
  92. b) Make a filesystem on it. Say ext2fs for this example.
  93. mke2fs -vm0 /dev/ram0 2048
  94. c) Mount it, copy the files you want to it (eg: /etc/* /dev/* ...)
  95. and unmount it again.
  96. d) Compress the contents of the RAM disk. The level of compression
  97. will be approximately 50% of the space used by the files. Unused
  98. space on the RAM disk will compress to almost nothing.
  99. dd if=/dev/ram0 bs=1k count=2048 | gzip -v9 > /tmp/ram_image.gz
  100. e) Put the kernel onto the floppy
  101. dd if=zImage of=/dev/fd0 bs=1k
  102. f) Put the RAM disk image onto the floppy, after the kernel. Use an offset
  103. that is slightly larger than the kernel, so that you can put another
  104. (possibly larger) kernel onto the same floppy later without overlapping
  105. the RAM disk image. An offset of 400 kB for kernels about 350 kB in
  106. size would be reasonable. Make sure offset+size of ram_image.gz is
  107. not larger than the total space on your floppy (usually 1440 kB).
  108. dd if=/tmp/ram_image.gz of=/dev/fd0 bs=1k seek=400
  109. g) Use "rdev" to set the boot device, RAM disk offset, prompt flag, etc.
  110. For prompt_ramdisk=1, load_ramdisk=1, ramdisk_start=400, one would
  111. have 2^15 + 2^14 + 400 = 49552.
  112. rdev /dev/fd0 /dev/fd0
  113. rdev -r /dev/fd0 49552
  114. That is it. You now have your boot/root compressed RAM disk floppy. Some
  115. users may wish to combine steps (d) and (f) by using a pipe.
  116. --------------------------------------------------------------------------
  117. Paul Gortmaker 12/95
  118. Changelog:
  119. ----------
  120. 10-22-04 : Updated to reflect changes in command line options, remove
  121. obsolete references, general cleanup.
  122. James Nelson (james4765@gmail.com)
  123. 12-95 : Original Document