ramfs-rootfs-initramfs.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===========================
  3. Ramfs, rootfs and initramfs
  4. ===========================
  5. October 17, 2005
  6. Rob Landley <rob@landley.net>
  7. =============================
  8. What is ramfs?
  9. --------------
  10. Ramfs is a very simple filesystem that exports Linux's disk caching
  11. mechanisms (the page cache and dentry cache) as a dynamically resizable
  12. RAM-based filesystem.
  13. Normally all files are cached in memory by Linux. Pages of data read from
  14. backing store (usually the block device the filesystem is mounted on) are kept
  15. around in case it's needed again, but marked as clean (freeable) in case the
  16. Virtual Memory system needs the memory for something else. Similarly, data
  17. written to files is marked clean as soon as it has been written to backing
  18. store, but kept around for caching purposes until the VM reallocates the
  19. memory. A similar mechanism (the dentry cache) greatly speeds up access to
  20. directories.
  21. With ramfs, there is no backing store. Files written into ramfs allocate
  22. dentries and page cache as usual, but there's nowhere to write them to.
  23. This means the pages are never marked clean, so they can't be freed by the
  24. VM when it's looking to recycle memory.
  25. The amount of code required to implement ramfs is tiny, because all the
  26. work is done by the existing Linux caching infrastructure. Basically,
  27. you're mounting the disk cache as a filesystem. Because of this, ramfs is not
  28. an optional component removable via menuconfig, since there would be negligible
  29. space savings.
  30. ramfs and ramdisk:
  31. ------------------
  32. The older "ram disk" mechanism created a synthetic block device out of
  33. an area of RAM and used it as backing store for a filesystem. This block
  34. device was of fixed size, so the filesystem mounted on it was of fixed
  35. size. Using a ram disk also required unnecessarily copying memory from the
  36. fake block device into the page cache (and copying changes back out), as well
  37. as creating and destroying dentries. Plus it needed a filesystem driver
  38. (such as ext2) to format and interpret this data.
  39. Compared to ramfs, this wastes memory (and memory bus bandwidth), creates
  40. unnecessary work for the CPU, and pollutes the CPU caches. (There are tricks
  41. to avoid this copying by playing with the page tables, but they're unpleasantly
  42. complicated and turn out to be about as expensive as the copying anyway.)
  43. More to the point, all the work ramfs is doing has to happen _anyway_,
  44. since all file access goes through the page and dentry caches. The RAM
  45. disk is simply unnecessary; ramfs is internally much simpler.
  46. Another reason ramdisks are semi-obsolete is that the introduction of
  47. loopback devices offered a more flexible and convenient way to create
  48. synthetic block devices, now from files instead of from chunks of memory.
  49. See losetup (8) for details.
  50. ramfs and tmpfs:
  51. ----------------
  52. One downside of ramfs is you can keep writing data into it until you fill
  53. up all memory, and the VM can't free it because the VM thinks that files
  54. should get written to backing store (rather than swap space), but ramfs hasn't
  55. got any backing store. Because of this, only root (or a trusted user) should
  56. be allowed write access to a ramfs mount.
  57. A ramfs derivative called tmpfs was created to add size limits, and the ability
  58. to write the data to swap space. Normal users can be allowed write access to
  59. tmpfs mounts. See Documentation/filesystems/tmpfs.rst for more information.
  60. What is rootfs?
  61. ---------------
  62. Rootfs is a special instance of ramfs (or tmpfs, if that's enabled), which is
  63. always present in 2.6 systems. You can't unmount rootfs for approximately the
  64. same reason you can't kill the init process; rather than having special code
  65. to check for and handle an empty list, it's smaller and simpler for the kernel
  66. to just make sure certain lists can't become empty.
  67. Most systems just mount another filesystem over rootfs and ignore it. The
  68. amount of space an empty instance of ramfs takes up is tiny.
  69. If CONFIG_TMPFS is enabled, rootfs will use tmpfs instead of ramfs by
  70. default. To force ramfs, add "rootfstype=ramfs" to the kernel command
  71. line.
  72. What is initramfs?
  73. ------------------
  74. All 2.6 Linux kernels contain a gzipped "cpio" format archive, which is
  75. extracted into rootfs when the kernel boots up. After extracting, the kernel
  76. checks to see if rootfs contains a file "init", and if so it executes it as PID
  77. 1. If found, this init process is responsible for bringing the system the
  78. rest of the way up, including locating and mounting the real root device (if
  79. any). If rootfs does not contain an init program after the embedded cpio
  80. archive is extracted into it, the kernel will fall through to the older code
  81. to locate and mount a root partition, then exec some variant of /sbin/init
  82. out of that.
  83. All this differs from the old initrd in several ways:
  84. - The old initrd was always a separate file, while the initramfs archive is
  85. linked into the linux kernel image. (The directory ``linux-*/usr`` is
  86. devoted to generating this archive during the build.)
  87. - The old initrd file was a gzipped filesystem image (in some file format,
  88. such as ext2, that needed a driver built into the kernel), while the new
  89. initramfs archive is a gzipped cpio archive (like tar only simpler,
  90. see cpio(1) and Documentation/driver-api/early-userspace/buffer-format.rst).
  91. The kernel's cpio extraction code is not only extremely small, it's also
  92. __init text and data that can be discarded during the boot process.
  93. - The program run by the old initrd (which was called /initrd, not /init) did
  94. some setup and then returned to the kernel, while the init program from
  95. initramfs is not expected to return to the kernel. (If /init needs to hand
  96. off control it can overmount / with a new root device and exec another init
  97. program. See the switch_root utility, below.)
  98. - When switching another root device, initrd would pivot_root and then
  99. umount the ramdisk. But initramfs is rootfs: you can neither pivot_root
  100. rootfs, nor unmount it. Instead delete everything out of rootfs to
  101. free up the space (find -xdev / -exec rm '{}' ';'), overmount rootfs
  102. with the new root (cd /newmount; mount --move . /; chroot .), attach
  103. stdin/stdout/stderr to the new /dev/console, and exec the new init.
  104. Since this is a remarkably persnickety process (and involves deleting
  105. commands before you can run them), the klibc package introduced a helper
  106. program (utils/run_init.c) to do all this for you. Most other packages
  107. (such as busybox) have named this command "switch_root".
  108. Populating initramfs:
  109. ---------------------
  110. The 2.6 kernel build process always creates a gzipped cpio format initramfs
  111. archive and links it into the resulting kernel binary. By default, this
  112. archive is empty (consuming 134 bytes on x86).
  113. The config option CONFIG_INITRAMFS_SOURCE (in General Setup in menuconfig,
  114. and living in usr/Kconfig) can be used to specify a source for the
  115. initramfs archive, which will automatically be incorporated into the
  116. resulting binary. This option can point to an existing gzipped cpio
  117. archive, a directory containing files to be archived, or a text file
  118. specification such as the following example::
  119. dir /dev 755 0 0
  120. nod /dev/console 644 0 0 c 5 1
  121. nod /dev/loop0 644 0 0 b 7 0
  122. dir /bin 755 1000 1000
  123. slink /bin/sh busybox 777 0 0
  124. file /bin/busybox initramfs/busybox 755 0 0
  125. dir /proc 755 0 0
  126. dir /sys 755 0 0
  127. dir /mnt 755 0 0
  128. file /init initramfs/init.sh 755 0 0
  129. Run "usr/gen_init_cpio" (after the kernel build) to get a usage message
  130. documenting the above file format.
  131. One advantage of the configuration file is that root access is not required to
  132. set permissions or create device nodes in the new archive. (Note that those
  133. two example "file" entries expect to find files named "init.sh" and "busybox" in
  134. a directory called "initramfs", under the linux-2.6.* directory. See
  135. Documentation/driver-api/early-userspace/early_userspace_support.rst for more details.)
  136. The kernel does not depend on external cpio tools. If you specify a
  137. directory instead of a configuration file, the kernel's build infrastructure
  138. creates a configuration file from that directory (usr/Makefile calls
  139. usr/gen_initramfs.sh), and proceeds to package up that directory
  140. using the config file (by feeding it to usr/gen_init_cpio, which is created
  141. from usr/gen_init_cpio.c). The kernel's build-time cpio creation code is
  142. entirely self-contained, and the kernel's boot-time extractor is also
  143. (obviously) self-contained.
  144. The one thing you might need external cpio utilities installed for is creating
  145. or extracting your own preprepared cpio files to feed to the kernel build
  146. (instead of a config file or directory).
  147. The following command line can extract a cpio image (either by the above script
  148. or by the kernel build) back into its component files::
  149. cpio -i -d -H newc -F initramfs_data.cpio --no-absolute-filenames
  150. The following shell script can create a prebuilt cpio archive you can
  151. use in place of the above config file::
  152. #!/bin/sh
  153. # Copyright 2006 Rob Landley <rob@landley.net> and TimeSys Corporation.
  154. # Licensed under GPL version 2
  155. if [ $# -ne 2 ]
  156. then
  157. echo "usage: mkinitramfs directory imagename.cpio.gz"
  158. exit 1
  159. fi
  160. if [ -d "$1" ]
  161. then
  162. echo "creating $2 from $1"
  163. (cd "$1"; find . | cpio -o -H newc | gzip) > "$2"
  164. else
  165. echo "First argument must be a directory"
  166. exit 1
  167. fi
  168. .. Note::
  169. The cpio man page contains some bad advice that will break your initramfs
  170. archive if you follow it. It says "A typical way to generate the list
  171. of filenames is with the find command; you should give find the -depth
  172. option to minimize problems with permissions on directories that are
  173. unwritable or not searchable." Don't do this when creating
  174. initramfs.cpio.gz images, it won't work. The Linux kernel cpio extractor
  175. won't create files in a directory that doesn't exist, so the directory
  176. entries must go before the files that go in those directories.
  177. The above script gets them in the right order.
  178. External initramfs images:
  179. --------------------------
  180. If the kernel has initrd support enabled, an external cpio.gz archive can also
  181. be passed into a 2.6 kernel in place of an initrd. In this case, the kernel
  182. will autodetect the type (initramfs, not initrd) and extract the external cpio
  183. archive into rootfs before trying to run /init.
  184. This has the memory efficiency advantages of initramfs (no ramdisk block
  185. device) but the separate packaging of initrd (which is nice if you have
  186. non-GPL code you'd like to run from initramfs, without conflating it with
  187. the GPL licensed Linux kernel binary).
  188. It can also be used to supplement the kernel's built-in initramfs image. The
  189. files in the external archive will overwrite any conflicting files in
  190. the built-in initramfs archive. Some distributors also prefer to customize
  191. a single kernel image with task-specific initramfs images, without recompiling.
  192. Contents of initramfs:
  193. ----------------------
  194. An initramfs archive is a complete self-contained root filesystem for Linux.
  195. If you don't already understand what shared libraries, devices, and paths
  196. you need to get a minimal root filesystem up and running, here are some
  197. references:
  198. - https://www.tldp.org/HOWTO/Bootdisk-HOWTO/
  199. - https://www.tldp.org/HOWTO/From-PowerUp-To-Bash-Prompt-HOWTO.html
  200. - http://www.linuxfromscratch.org/lfs/view/stable/
  201. The "klibc" package (https://www.kernel.org/pub/linux/libs/klibc) is
  202. designed to be a tiny C library to statically link early userspace
  203. code against, along with some related utilities. It is BSD licensed.
  204. I use uClibc (https://www.uclibc.org) and busybox (https://www.busybox.net)
  205. myself. These are LGPL and GPL, respectively. (A self-contained initramfs
  206. package is planned for the busybox 1.3 release.)
  207. In theory you could use glibc, but that's not well suited for small embedded
  208. uses like this. (A "hello world" program statically linked against glibc is
  209. over 400k. With uClibc it's 7k. Also note that glibc dlopens libnss to do
  210. name lookups, even when otherwise statically linked.)
  211. A good first step is to get initramfs to run a statically linked "hello world"
  212. program as init, and test it under an emulator like qemu (www.qemu.org) or
  213. User Mode Linux, like so::
  214. cat > hello.c << EOF
  215. #include <stdio.h>
  216. #include <unistd.h>
  217. int main(int argc, char *argv[])
  218. {
  219. printf("Hello world!\n");
  220. sleep(999999999);
  221. }
  222. EOF
  223. gcc -static hello.c -o init
  224. echo init | cpio -o -H newc | gzip > test.cpio.gz
  225. # Testing external initramfs using the initrd loading mechanism.
  226. qemu -kernel /boot/vmlinuz -initrd test.cpio.gz /dev/zero
  227. When debugging a normal root filesystem, it's nice to be able to boot with
  228. "init=/bin/sh". The initramfs equivalent is "rdinit=/bin/sh", and it's
  229. just as useful.
  230. Why cpio rather than tar?
  231. -------------------------
  232. This decision was made back in December, 2001. The discussion started here:
  233. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1538.html
  234. And spawned a second thread (specifically on tar vs cpio), starting here:
  235. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1587.html
  236. The quick and dirty summary version (which is no substitute for reading
  237. the above threads) is:
  238. 1) cpio is a standard. It's decades old (from the AT&T days), and already
  239. widely used on Linux (inside RPM, Red Hat's device driver disks). Here's
  240. a Linux Journal article about it from 1996:
  241. http://www.linuxjournal.com/article/1213
  242. It's not as popular as tar because the traditional cpio command line tools
  243. require _truly_hideous_ command line arguments. But that says nothing
  244. either way about the archive format, and there are alternative tools,
  245. such as:
  246. http://freecode.com/projects/afio
  247. 2) The cpio archive format chosen by the kernel is simpler and cleaner (and
  248. thus easier to create and parse) than any of the (literally dozens of)
  249. various tar archive formats. The complete initramfs archive format is
  250. explained in buffer-format.txt, created in usr/gen_init_cpio.c, and
  251. extracted in init/initramfs.c. All three together come to less than 26k
  252. total of human-readable text.
  253. 3) The GNU project standardizing on tar is approximately as relevant as
  254. Windows standardizing on zip. Linux is not part of either, and is free
  255. to make its own technical decisions.
  256. 4) Since this is a kernel internal format, it could easily have been
  257. something brand new. The kernel provides its own tools to create and
  258. extract this format anyway. Using an existing standard was preferable,
  259. but not essential.
  260. 5) Al Viro made the decision (quote: "tar is ugly as hell and not going to be
  261. supported on the kernel side"):
  262. http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1540.html
  263. explained his reasoning:
  264. - http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1550.html
  265. - http://www.uwsg.iu.edu/hypermail/linux/kernel/0112.2/1638.html
  266. and, most importantly, designed and implemented the initramfs code.
  267. Future directions:
  268. ------------------
  269. Today (2.6.16), initramfs is always compiled in, but not always used. The
  270. kernel falls back to legacy boot code that is reached only if initramfs does
  271. not contain an /init program. The fallback is legacy code, there to ensure a
  272. smooth transition and allowing early boot functionality to gradually move to
  273. "early userspace" (I.E. initramfs).
  274. The move to early userspace is necessary because finding and mounting the real
  275. root device is complex. Root partitions can span multiple devices (raid or
  276. separate journal). They can be out on the network (requiring dhcp, setting a
  277. specific MAC address, logging into a server, etc). They can live on removable
  278. media, with dynamically allocated major/minor numbers and persistent naming
  279. issues requiring a full udev implementation to sort out. They can be
  280. compressed, encrypted, copy-on-write, loopback mounted, strangely partitioned,
  281. and so on.
  282. This kind of complexity (which inevitably includes policy) is rightly handled
  283. in userspace. Both klibc and busybox/uClibc are working on simple initramfs
  284. packages to drop into a kernel build.
  285. The klibc package has now been accepted into Andrew Morton's 2.6.17-mm tree.
  286. The kernel's current early boot code (partition detection, etc) will probably
  287. be migrated into a default initramfs, automatically created and used by the
  288. kernel build.