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