ramfs-rootfs-initramfs.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ramfs, rootfs and initramfs
  2. October 17, 2005
  3. Rob Landley <rob@landley.net>
  4. =============================
  5. What is ramfs?
  6. --------------
  7. Ramfs is a very simple filesystem that exports Linux's disk caching
  8. mechanisms (the page cache and dentry cache) as a dynamically resizable
  9. ram-based filesystem.
  10. Normally all files are cached in memory by Linux. Pages of data read from
  11. backing store (usually the block device the filesystem is mounted on) are kept
  12. around in case it's needed again, but marked as clean (freeable) in case the
  13. Virtual Memory system needs the memory for something else. Similarly, data
  14. written to files is marked clean as soon as it has been written to backing
  15. store, but kept around for caching purposes until the VM reallocates the
  16. memory. A similar mechanism (the dentry cache) greatly speeds up access to
  17. directories.
  18. With ramfs, there is no backing store. Files written into ramfs allocate
  19. dentries and page cache as usual, but there's nowhere to write them to.
  20. This means the pages are never marked clean, so they can't be freed by the
  21. VM when it's looking to recycle memory.
  22. The amount of code required to implement ramfs is tiny, because all the
  23. work is done by the existing Linux caching infrastructure. Basically,
  24. you're mounting the disk cache as a filesystem. Because of this, ramfs is not
  25. an optional component removable via menuconfig, since there would be negligible
  26. space savings.
  27. ramfs and ramdisk:
  28. ------------------
  29. The older "ram disk" mechanism created a synthetic block device out of
  30. an area of ram and used it as backing store for a filesystem. This block
  31. device was of fixed size, so the filesystem mounted on it was of fixed
  32. size. Using a ram disk also required unnecessarily copying memory from the
  33. fake block device into the page cache (and copying changes back out), as well
  34. as creating and destroying dentries. Plus it needed a filesystem driver
  35. (such as ext2) to format and interpret this data.
  36. Compared to ramfs, this wastes memory (and memory bus bandwidth), creates
  37. unnecessary work for the CPU, and pollutes the CPU caches. (There are tricks
  38. to avoid this copying by playing with the page tables, but they're unpleasantly
  39. complicated and turn out to be about as expensive as the copying anyway.)
  40. More to the point, all the work ramfs is doing has to happen _anyway_,
  41. since all file access goes through the page and dentry caches. The ram
  42. disk is simply unnecessary, ramfs is internally much simpler.
  43. Another reason ramdisks are semi-obsolete is that the introduction of
  44. loopback devices offered a more flexible and convenient way to create
  45. synthetic block devices, now from files instead of from chunks of memory.
  46. See losetup (8) for details.
  47. ramfs and tmpfs:
  48. ----------------
  49. One downside of ramfs is you can keep writing data into it until you fill
  50. up all memory, and the VM can't free it because the VM thinks that files
  51. should get written to backing store (rather than swap space), but ramfs hasn't
  52. got any backing store. Because of this, only root (or a trusted user) should
  53. be allowed write access to a ramfs mount.
  54. A ramfs derivative called tmpfs was created to add size limits, and the ability
  55. to write the data to swap space. Normal users can be allowed write access to
  56. tmpfs mounts. See Documentation/filesystems/tmpfs.txt for more information.
  57. What is rootfs?
  58. ---------------
  59. Rootfs is a special instance of ramfs, which is always present in 2.6 systems.
  60. (It's used internally as the starting and stopping point for searches of the
  61. kernel's doubly-linked list of mount points.)
  62. Most systems just mount another filesystem over it and ignore it. The
  63. amount of space an empty instance of ramfs takes up is tiny.
  64. What is initramfs?
  65. ------------------
  66. All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is
  67. extracted into rootfs when the kernel boots up. After extracting, the kernel
  68. checks to see if rootfs contains a file "init", and if so it executes it as PID
  69. 1. If found, this init process is responsible for bringing the system the
  70. rest of the way up, including locating and mounting the real root device (if
  71. any). If rootfs does not contain an init program after the embedded cpio
  72. archive is extracted into it, the kernel will fall through to the older code
  73. to locate and mount a root partition, then exec some variant of /sbin/init
  74. out of that.
  75. All this differs from the old initrd in several ways:
  76. - The old initrd was a separate file, while the initramfs archive is linked
  77. into the linux kernel image. (The directory linux-*/usr is devoted to
  78. generating this archive during the build.)
  79. - The old initrd file was a gzipped filesystem image (in some file format,
  80. such as ext2, that had to be built into the kernel), while the new
  81. initramfs archive is a gzipped cpio archive (like tar only simpler,
  82. see cpio(1) and Documentation/early-userspace/buffer-format.txt).
  83. - The program run by the old initrd (which was called /initrd, not /init) did
  84. some setup and then returned to the kernel, while the init program from
  85. initramfs is not expected to return to the kernel. (If /init needs to hand
  86. off control it can overmount / with a new root device and exec another init
  87. program. See the switch_root utility, below.)
  88. - When switching another root device, initrd would pivot_root and then
  89. umount the ramdisk. But initramfs is rootfs: you can neither pivot_root
  90. rootfs, nor unmount it. Instead delete everything out of rootfs to
  91. free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs
  92. with the new root (cd /newmount; mount --move . /; chroot .), attach
  93. stdin/stdout/stderr to the new /dev/console, and exec the new init.
  94. Since this is a remarkably persnickity process (and involves deleting
  95. commands before you can run them), the klibc package introduced a helper
  96. program (utils/run_init.c) to do all this for you. Most other packages
  97. (such as busybox) have named this command "switch_root".
  98. Populating initramfs:
  99. ---------------------
  100. The 2.6 kernel build process always creates a gzipped cpio format initramfs
  101. archive and links it into the resulting kernel binary. By default, this
  102. archive is empty (consuming 134 bytes on x86). The config option
  103. CONFIG_INITRAMFS_SOURCE (for some reason buried under devices->block devices
  104. in menuconfig, and living in usr/Kconfig) can be used to specify a source for
  105. the initramfs archive, which will automatically be incorporated into the
  106. resulting binary. This option can point to an existing gzipped cpio archive, a
  107. directory containing files to be archived, or a text file specification such
  108. as the following example:
  109. dir /dev 755 0 0
  110. nod /dev/console 644 0 0 c 5 1
  111. nod /dev/loop0 644 0 0 b 7 0
  112. dir /bin 755 1000 1000
  113. slink /bin/sh busybox 777 0 0
  114. file /bin/busybox initramfs/busybox 755 0 0
  115. dir /proc 755 0 0
  116. dir /sys 755 0 0
  117. dir /mnt 755 0 0
  118. file /init initramfs/init.sh 755 0 0
  119. Run "usr/gen_init_cpio" (after the kernel build) to get a usage message
  120. documenting the above file format.
  121. One advantage of the text file is that root access is not required to
  122. set permissions or create device nodes in the new archive. (Note that those
  123. two example "file" entries expect to find files named "init.sh" and "busybox" in
  124. a directory called "initramfs", under the linux-2.6.* directory. See
  125. Documentation/early-userspace/README for more details.)
  126. The kernel does not depend on external cpio tools, gen_init_cpio is created
  127. from usr/gen_init_cpio.c which is entirely self-contained, and the kernel's
  128. boot-time extractor is also (obviously) self-contained. However, if you _do_
  129. happen to have cpio installed, the following command line can extract the
  130. generated cpio image back into its component files:
  131. cpio -i -d -H newc -F initramfs_data.cpio --no-absolute-filenames
  132. Contents of initramfs:
  133. ----------------------
  134. If you don't already understand what shared libraries, devices, and paths
  135. you need to get a minimal root filesystem up and running, here are some
  136. references:
  137. http://www.tldp.org/HOWTO/Bootdisk-HOWTO/
  138. http://www.tldp.org/HOWTO/From-PowerUp-To-Bash-Prompt-HOWTO.html
  139. http://www.linuxfromscratch.org/lfs/view/stable/
  140. The "klibc" package (http://www.kernel.org/pub/linux/libs/klibc) is
  141. designed to be a tiny C library to statically link early userspace
  142. code against, along with some related utilities. It is BSD licensed.
  143. I use uClibc (http://www.uclibc.org) and busybox (http://www.busybox.net)
  144. myself. These are LGPL and GPL, respectively. (A self-contained initramfs
  145. package is planned for the busybox 1.2 release.)
  146. In theory you could use glibc, but that's not well suited for small embedded
  147. uses like this. (A "hello world" program statically linked against glibc is
  148. over 400k. With uClibc it's 7k. Also note that glibc dlopens libnss to do
  149. name lookups, even when otherwise statically linked.)
  150. Why cpio rather than tar?
  151. -------------------------
  152. This decision was made back in December, 2001. The discussion started here:
  153. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1538.html
  154. And spawned a second thread (specifically on tar vs cpio), starting here:
  155. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1587.html
  156. The quick and dirty summary version (which is no substitute for reading
  157. the above threads) is:
  158. 1) cpio is a standard. It's decades old (from the AT&T days), and already
  159. widely used on Linux (inside RPM, Red Hat's device driver disks). Here's
  160. a Linux Journal article about it from 1996:
  161. http://www.linuxjournal.com/article/1213
  162. It's not as popular as tar because the traditional cpio command line tools
  163. require _truly_hideous_ command line arguments. But that says nothing
  164. either way about the archive format, and there are alternative tools,
  165. such as:
  166. http://freshmeat.net/projects/afio/
  167. 2) The cpio archive format chosen by the kernel is simpler and cleaner (and
  168. thus easier to create and parse) than any of the (literally dozens of)
  169. various tar archive formats. The complete initramfs archive format is
  170. explained in buffer-format.txt, created in usr/gen_init_cpio.c, and
  171. extracted in init/initramfs.c. All three together come to less than 26k
  172. total of human-readable text.
  173. 3) The GNU project standardizing on tar is approximately as relevant as
  174. Windows standardizing on zip. Linux is not part of either, and is free
  175. to make its own technical decisions.
  176. 4) Since this is a kernel internal format, it could easily have been
  177. something brand new. The kernel provides its own tools to create and
  178. extract this format anyway. Using an existing standard was preferable,
  179. but not essential.
  180. 5) Al Viro made the decision (quote: "tar is ugly as hell and not going to be
  181. supported on the kernel side"):
  182. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1540.html
  183. explained his reasoning:
  184. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1550.html
  185. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1638.html
  186. and, most importantly, designed and implemented the initramfs code.
  187. Future directions:
  188. ------------------
  189. Today (2.6.14), initramfs is always compiled in, but not always used. The
  190. kernel falls back to legacy boot code that is reached only if initramfs does
  191. not contain an /init program. The fallback is legacy code, there to ensure a
  192. smooth transition and allowing early boot functionality to gradually move to
  193. "early userspace" (I.E. initramfs).
  194. The move to early userspace is necessary because finding and mounting the real
  195. root device is complex. Root partitions can span multiple devices (raid or
  196. separate journal). They can be out on the network (requiring dhcp, setting a
  197. specific mac address, logging into a server, etc). They can live on removable
  198. media, with dynamically allocated major/minor numbers and persistent naming
  199. issues requiring a full udev implementation to sort out. They can be
  200. compressed, encrypted, copy-on-write, loopback mounted, strangely partitioned,
  201. and so on.
  202. This kind of complexity (which inevitably includes policy) is rightly handled
  203. in userspace. Both klibc and busybox/uClibc are working on simple initramfs
  204. packages to drop into a kernel build, and when standard solutions are ready
  205. and widely deployed, the kernel's legacy early boot code will become obsolete
  206. and a candidate for the feature removal schedule.
  207. But that's a while off yet.