initrd.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. Using the initial RAM disk (initrd)
  2. ===================================
  3. Written 1996,2000 by Werner Almesberger <werner.almesberger@epfl.ch> and
  4. Hans Lermen <lermen@fgan.de>
  5. initrd provides the capability to load a RAM disk by the boot loader.
  6. This RAM disk can then be mounted as the root file system and programs
  7. can be run from it. Afterwards, a new root file system can be mounted
  8. from a different device. The previous root (from initrd) is then moved
  9. to a directory and can be subsequently unmounted.
  10. initrd is mainly designed to allow system startup to occur in two phases,
  11. where the kernel comes up with a minimum set of compiled-in drivers, and
  12. where additional modules are loaded from initrd.
  13. This document gives a brief overview of the use of initrd. A more detailed
  14. discussion of the boot process can be found in [1].
  15. Operation
  16. ---------
  17. When using initrd, the system typically boots as follows:
  18. 1) the boot loader loads the kernel and the initial RAM disk
  19. 2) the kernel converts initrd into a "normal" RAM disk and
  20. frees the memory used by initrd
  21. 3) initrd is mounted read-write as root
  22. 4) /linuxrc is executed (this can be any valid executable, including
  23. shell scripts; it is run with uid 0 and can do basically everything
  24. init can do)
  25. 5) linuxrc mounts the "real" root file system
  26. 6) linuxrc places the root file system at the root directory using the
  27. pivot_root system call
  28. 7) the usual boot sequence (e.g. invocation of /sbin/init) is performed
  29. on the root file system
  30. 8) the initrd file system is removed
  31. Note that changing the root directory does not involve unmounting it.
  32. It is therefore possible to leave processes running on initrd during that
  33. procedure. Also note that file systems mounted under initrd continue to
  34. be accessible.
  35. Boot command-line options
  36. -------------------------
  37. initrd adds the following new options:
  38. initrd=<path> (e.g. LOADLIN)
  39. Loads the specified file as the initial RAM disk. When using LILO, you
  40. have to specify the RAM disk image file in /etc/lilo.conf, using the
  41. INITRD configuration variable.
  42. noinitrd
  43. initrd data is preserved but it is not converted to a RAM disk and
  44. the "normal" root file system is mounted. initrd data can be read
  45. from /dev/initrd. Note that the data in initrd can have any structure
  46. in this case and doesn't necessarily have to be a file system image.
  47. This option is used mainly for debugging.
  48. Note: /dev/initrd is read-only and it can only be used once. As soon
  49. as the last process has closed it, all data is freed and /dev/initrd
  50. can't be opened anymore.
  51. root=/dev/ram0
  52. initrd is mounted as root, and the normal boot procedure is followed,
  53. with the RAM disk still mounted as root.
  54. Compressed cpio images
  55. ----------------------
  56. Recent kernels have support for populating a ramdisk from a compressed cpio
  57. archive, on such systems, the creation of a ramdisk image doesn't need to
  58. involve special block devices or loopbacks, you merely create a directory on
  59. disk with the desired initrd content, cd to that directory, and run (as an
  60. example):
  61. find . | cpio --quiet -c -o | gzip -9 -n > /boot/imagefile.img
  62. Examining the contents of an existing image file is just as simple:
  63. mkdir /tmp/imagefile
  64. cd /tmp/imagefile
  65. gzip -cd /boot/imagefile.img | cpio -imd --quiet
  66. Installation
  67. ------------
  68. First, a directory for the initrd file system has to be created on the
  69. "normal" root file system, e.g.
  70. # mkdir /initrd
  71. The name is not relevant. More details can be found on the pivot_root(2)
  72. man page.
  73. If the root file system is created during the boot procedure (i.e. if
  74. you're building an install floppy), the root file system creation
  75. procedure should create the /initrd directory.
  76. If initrd will not be mounted in some cases, its content is still
  77. accessible if the following device has been created:
  78. # mknod /dev/initrd b 1 250
  79. # chmod 400 /dev/initrd
  80. Second, the kernel has to be compiled with RAM disk support and with
  81. support for the initial RAM disk enabled. Also, at least all components
  82. needed to execute programs from initrd (e.g. executable format and file
  83. system) must be compiled into the kernel.
  84. Third, you have to create the RAM disk image. This is done by creating a
  85. file system on a block device, copying files to it as needed, and then
  86. copying the content of the block device to the initrd file. With recent
  87. kernels, at least three types of devices are suitable for that:
  88. - a floppy disk (works everywhere but it's painfully slow)
  89. - a RAM disk (fast, but allocates physical memory)
  90. - a loopback device (the most elegant solution)
  91. We'll describe the loopback device method:
  92. 1) make sure loopback block devices are configured into the kernel
  93. 2) create an empty file system of the appropriate size, e.g.
  94. # dd if=/dev/zero of=initrd bs=300k count=1
  95. # mke2fs -F -m0 initrd
  96. (if space is critical, you may want to use the Minix FS instead of Ext2)
  97. 3) mount the file system, e.g.
  98. # mount -t ext2 -o loop initrd /mnt
  99. 4) create the console device:
  100. # mkdir /mnt/dev
  101. # mknod /mnt/dev/console c 5 1
  102. 5) copy all the files that are needed to properly use the initrd
  103. environment. Don't forget the most important file, /linuxrc
  104. Note that /linuxrc's permissions must include "x" (execute).
  105. 6) correct operation the initrd environment can frequently be tested
  106. even without rebooting with the command
  107. # chroot /mnt /linuxrc
  108. This is of course limited to initrds that do not interfere with the
  109. general system state (e.g. by reconfiguring network interfaces,
  110. overwriting mounted devices, trying to start already running demons,
  111. etc. Note however that it is usually possible to use pivot_root in
  112. such a chroot'ed initrd environment.)
  113. 7) unmount the file system
  114. # umount /mnt
  115. 8) the initrd is now in the file "initrd". Optionally, it can now be
  116. compressed
  117. # gzip -9 initrd
  118. For experimenting with initrd, you may want to take a rescue floppy and
  119. only add a symbolic link from /linuxrc to /bin/sh. Alternatively, you
  120. can try the experimental newlib environment [2] to create a small
  121. initrd.
  122. Finally, you have to boot the kernel and load initrd. Almost all Linux
  123. boot loaders support initrd. Since the boot process is still compatible
  124. with an older mechanism, the following boot command line parameters
  125. have to be given:
  126. root=/dev/ram0 init=/linuxrc rw
  127. (rw is only necessary if writing to the initrd file system.)
  128. With LOADLIN, you simply execute
  129. LOADLIN <kernel> initrd=<disk_image>
  130. e.g. LOADLIN C:\LINUX\BZIMAGE initrd=C:\LINUX\INITRD.GZ root=/dev/ram0
  131. init=/linuxrc rw
  132. With LILO, you add the option INITRD=<path> to either the global section
  133. or to the section of the respective kernel in /etc/lilo.conf, and pass
  134. the options using APPEND, e.g.
  135. image = /bzImage
  136. initrd = /boot/initrd.gz
  137. append = "root=/dev/ram0 init=/linuxrc rw"
  138. and run /sbin/lilo
  139. For other boot loaders, please refer to the respective documentation.
  140. Now you can boot and enjoy using initrd.
  141. Changing the root device
  142. ------------------------
  143. When finished with its duties, linuxrc typically changes the root device
  144. and proceeds with starting the Linux system on the "real" root device.
  145. The procedure involves the following steps:
  146. - mounting the new root file system
  147. - turning it into the root file system
  148. - removing all accesses to the old (initrd) root file system
  149. - unmounting the initrd file system and de-allocating the RAM disk
  150. Mounting the new root file system is easy: it just needs to be mounted on
  151. a directory under the current root. Example:
  152. # mkdir /new-root
  153. # mount -o ro /dev/hda1 /new-root
  154. The root change is accomplished with the pivot_root system call, which
  155. is also available via the pivot_root utility (see pivot_root(8) man
  156. page; pivot_root is distributed with util-linux version 2.10h or higher
  157. [3]). pivot_root moves the current root to a directory under the new
  158. root, and puts the new root at its place. The directory for the old root
  159. must exist before calling pivot_root. Example:
  160. # cd /new-root
  161. # mkdir initrd
  162. # pivot_root . initrd
  163. Now, the linuxrc process may still access the old root via its
  164. executable, shared libraries, standard input/output/error, and its
  165. current root directory. All these references are dropped by the
  166. following command:
  167. # exec chroot . what-follows <dev/console >dev/console 2>&1
  168. Where what-follows is a program under the new root, e.g. /sbin/init
  169. If the new root file system will be used with udev and has no valid
  170. /dev directory, udev must be initialized before invoking chroot in order
  171. to provide /dev/console.
  172. Note: implementation details of pivot_root may change with time. In order
  173. to ensure compatibility, the following points should be observed:
  174. - before calling pivot_root, the current directory of the invoking
  175. process should point to the new root directory
  176. - use . as the first argument, and the _relative_ path of the directory
  177. for the old root as the second argument
  178. - a chroot program must be available under the old and the new root
  179. - chroot to the new root afterwards
  180. - use relative paths for dev/console in the exec command
  181. Now, the initrd can be unmounted and the memory allocated by the RAM
  182. disk can be freed:
  183. # umount /initrd
  184. # blockdev --flushbufs /dev/ram0
  185. It is also possible to use initrd with an NFS-mounted root, see the
  186. pivot_root(8) man page for details.
  187. Note: if linuxrc or any program exec'ed from it terminates for some
  188. reason, the old change_root mechanism is invoked (see section "Obsolete
  189. root change mechanism").
  190. Usage scenarios
  191. ---------------
  192. The main motivation for implementing initrd was to allow for modular
  193. kernel configuration at system installation. The procedure would work
  194. as follows:
  195. 1) system boots from floppy or other media with a minimal kernel
  196. (e.g. support for RAM disks, initrd, a.out, and the Ext2 FS) and
  197. loads initrd
  198. 2) /linuxrc determines what is needed to (1) mount the "real" root FS
  199. (i.e. device type, device drivers, file system) and (2) the
  200. distribution media (e.g. CD-ROM, network, tape, ...). This can be
  201. done by asking the user, by auto-probing, or by using a hybrid
  202. approach.
  203. 3) /linuxrc loads the necessary kernel modules
  204. 4) /linuxrc creates and populates the root file system (this doesn't
  205. have to be a very usable system yet)
  206. 5) /linuxrc invokes pivot_root to change the root file system and
  207. execs - via chroot - a program that continues the installation
  208. 6) the boot loader is installed
  209. 7) the boot loader is configured to load an initrd with the set of
  210. modules that was used to bring up the system (e.g. /initrd can be
  211. modified, then unmounted, and finally, the image is written from
  212. /dev/ram0 or /dev/rd/0 to a file)
  213. 8) now the system is bootable and additional installation tasks can be
  214. performed
  215. The key role of initrd here is to re-use the configuration data during
  216. normal system operation without requiring the use of a bloated "generic"
  217. kernel or re-compiling or re-linking the kernel.
  218. A second scenario is for installations where Linux runs on systems with
  219. different hardware configurations in a single administrative domain. In
  220. such cases, it is desirable to generate only a small set of kernels
  221. (ideally only one) and to keep the system-specific part of configuration
  222. information as small as possible. In this case, a common initrd could be
  223. generated with all the necessary modules. Then, only /linuxrc or a file
  224. read by it would have to be different.
  225. A third scenario are more convenient recovery disks, because information
  226. like the location of the root FS partition doesn't have to be provided at
  227. boot time, but the system loaded from initrd can invoke a user-friendly
  228. dialog and it can also perform some sanity checks (or even some form of
  229. auto-detection).
  230. Last not least, CD-ROM distributors may use it for better installation
  231. from CD, e.g. by using a boot floppy and bootstrapping a bigger RAM disk
  232. via initrd from CD; or by booting via a loader like LOADLIN or directly
  233. from the CD-ROM, and loading the RAM disk from CD without need of
  234. floppies.
  235. Obsolete root change mechanism
  236. ------------------------------
  237. The following mechanism was used before the introduction of pivot_root.
  238. Current kernels still support it, but you should _not_ rely on its
  239. continued availability.
  240. It works by mounting the "real" root device (i.e. the one set with rdev
  241. in the kernel image or with root=... at the boot command line) as the
  242. root file system when linuxrc exits. The initrd file system is then
  243. unmounted, or, if it is still busy, moved to a directory /initrd, if
  244. such a directory exists on the new root file system.
  245. In order to use this mechanism, you do not have to specify the boot
  246. command options root, init, or rw. (If specified, they will affect
  247. the real root file system, not the initrd environment.)
  248. If /proc is mounted, the "real" root device can be changed from within
  249. linuxrc by writing the number of the new root FS device to the special
  250. file /proc/sys/kernel/real-root-dev, e.g.
  251. # echo 0x301 >/proc/sys/kernel/real-root-dev
  252. Note that the mechanism is incompatible with NFS and similar file
  253. systems.
  254. This old, deprecated mechanism is commonly called "change_root", while
  255. the new, supported mechanism is called "pivot_root".
  256. Resources
  257. ---------
  258. [1] Almesberger, Werner; "Booting Linux: The History and the Future"
  259. http://www.almesberger.net/cv/papers/ols2k-9.ps.gz
  260. [2] newlib package (experimental), with initrd example
  261. http://sources.redhat.com/newlib/
  262. [3] Brouwer, Andries; "util-linux: Miscellaneous utilities for Linux"
  263. ftp://ftp.win.tue.nl/pub/linux-local/utils/util-linux/