blob: a03dabaaf3a3d2fc9ce22982fde9746b25871739 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001Using the initial RAM disk (initrd)
2===================================
3
4Written 1996,2000 by Werner Almesberger <werner.almesberger@epfl.ch> and
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -03005Hans Lermen <lermen@fgan.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7
8initrd provides the capability to load a RAM disk by the boot loader.
9This RAM disk can then be mounted as the root file system and programs
10can be run from it. Afterwards, a new root file system can be mounted
11from a different device. The previous root (from initrd) is then moved
12to a directory and can be subsequently unmounted.
13
14initrd is mainly designed to allow system startup to occur in two phases,
15where the kernel comes up with a minimum set of compiled-in drivers, and
16where additional modules are loaded from initrd.
17
18This document gives a brief overview of the use of initrd. A more detailed
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030019discussion of the boot process can be found in [#f1]_.
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21
22Operation
23---------
24
25When using initrd, the system typically boots as follows:
26
27 1) the boot loader loads the kernel and the initial RAM disk
28 2) the kernel converts initrd into a "normal" RAM disk and
29 frees the memory used by initrd
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030030 3) if the root device is not ``/dev/ram0``, the old (deprecated)
Domenico Andreoli9d9a2002007-05-23 13:58:11 -070031 change_root procedure is followed. see the "Obsolete root change
32 mechanism" section below.
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030033 4) root device is mounted. if it is ``/dev/ram0``, the initrd image is
Domenico Andreoli9d9a2002007-05-23 13:58:11 -070034 then mounted as root
35 5) /sbin/init is executed (this can be any valid executable, including
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 shell scripts; it is run with uid 0 and can do basically everything
Domenico Andreoli9d9a2002007-05-23 13:58:11 -070037 init can do).
38 6) init mounts the "real" root file system
39 7) init places the root file system at the root directory using the
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 pivot_root system call
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030041 8) init execs the ``/sbin/init`` on the new root filesystem, performing
Domenico Andreoli9d9a2002007-05-23 13:58:11 -070042 the usual boot sequence
43 9) the initrd file system is removed
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45Note that changing the root directory does not involve unmounting it.
46It is therefore possible to leave processes running on initrd during that
47procedure. Also note that file systems mounted under initrd continue to
48be accessible.
49
50
51Boot command-line options
52-------------------------
53
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030054initrd adds the following new options::
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56 initrd=<path> (e.g. LOADLIN)
57
58 Loads the specified file as the initial RAM disk. When using LILO, you
59 have to specify the RAM disk image file in /etc/lilo.conf, using the
60 INITRD configuration variable.
61
62 noinitrd
63
64 initrd data is preserved but it is not converted to a RAM disk and
65 the "normal" root file system is mounted. initrd data can be read
66 from /dev/initrd. Note that the data in initrd can have any structure
67 in this case and doesn't necessarily have to be a file system image.
68 This option is used mainly for debugging.
69
70 Note: /dev/initrd is read-only and it can only be used once. As soon
71 as the last process has closed it, all data is freed and /dev/initrd
72 can't be opened anymore.
73
Greg Kroah-Hartman890fbae2005-06-20 21:15:16 -070074 root=/dev/ram0
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 initrd is mounted as root, and the normal boot procedure is followed,
Domenico Andreoli9d9a2002007-05-23 13:58:11 -070077 with the RAM disk mounted as root.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Thomas Horsley0a5eca62006-07-30 03:04:12 -070079Compressed cpio images
80----------------------
81
82Recent kernels have support for populating a ramdisk from a compressed cpio
Randy Dunlap18107322007-10-16 23:29:29 -070083archive. On such systems, the creation of a ramdisk image doesn't need to
84involve special block devices or loopbacks; you merely create a directory on
Thomas Horsley0a5eca62006-07-30 03:04:12 -070085disk with the desired initrd content, cd to that directory, and run (as an
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030086example)::
Thomas Horsley0a5eca62006-07-30 03:04:12 -070087
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030088 find . | cpio --quiet -H newc -o | gzip -9 -n > /boot/imagefile.img
Thomas Horsley0a5eca62006-07-30 03:04:12 -070089
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030090Examining the contents of an existing image file is just as simple::
Thomas Horsley0a5eca62006-07-30 03:04:12 -070091
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -030092 mkdir /tmp/imagefile
93 cd /tmp/imagefile
94 gzip -cd /boot/imagefile.img | cpio -imd --quiet
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96Installation
97------------
98
99First, a directory for the initrd file system has to be created on the
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300100"normal" root file system, e.g.::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300102 # mkdir /initrd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300104The name is not relevant. More details can be found on the
105:manpage:`pivot_root(2)` man page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107If the root file system is created during the boot procedure (i.e. if
108you're building an install floppy), the root file system creation
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300109procedure should create the ``/initrd`` directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111If initrd will not be mounted in some cases, its content is still
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300112accessible if the following device has been created::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300114 # mknod /dev/initrd b 1 250
115 # chmod 400 /dev/initrd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117Second, the kernel has to be compiled with RAM disk support and with
118support for the initial RAM disk enabled. Also, at least all components
119needed to execute programs from initrd (e.g. executable format and file
120system) must be compiled into the kernel.
121
122Third, you have to create the RAM disk image. This is done by creating a
123file system on a block device, copying files to it as needed, and then
124copying the content of the block device to the initrd file. With recent
125kernels, at least three types of devices are suitable for that:
126
127 - a floppy disk (works everywhere but it's painfully slow)
128 - a RAM disk (fast, but allocates physical memory)
129 - a loopback device (the most elegant solution)
130
131We'll describe the loopback device method:
132
133 1) make sure loopback block devices are configured into the kernel
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300134 2) create an empty file system of the appropriate size, e.g.::
135
136 # dd if=/dev/zero of=initrd bs=300k count=1
137 # mke2fs -F -m0 initrd
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 (if space is critical, you may want to use the Minix FS instead of Ext2)
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300140 3) mount the file system, e.g.::
141
142 # mount -t ext2 -o loop initrd /mnt
143
144 4) create the console device::
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 # mkdir /mnt/dev
147 # mknod /mnt/dev/console c 5 1
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 5) copy all the files that are needed to properly use the initrd
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300150 environment. Don't forget the most important file, ``/sbin/init``
151
152 .. note:: ``/sbin/init`` permissions must include "x" (execute).
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 6) correct operation the initrd environment can frequently be tested
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300155 even without rebooting with the command::
156
157 # chroot /mnt /sbin/init
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 This is of course limited to initrds that do not interfere with the
160 general system state (e.g. by reconfiguring network interfaces,
161 overwriting mounted devices, trying to start already running demons,
162 etc. Note however that it is usually possible to use pivot_root in
163 such a chroot'ed initrd environment.)
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300164 7) unmount the file system::
165
166 # umount /mnt
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 8) the initrd is now in the file "initrd". Optionally, it can now be
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300169 compressed::
170
171 # gzip -9 initrd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173For experimenting with initrd, you may want to take a rescue floppy and
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300174only add a symbolic link from ``/sbin/init`` to ``/bin/sh``. Alternatively, you
175can try the experimental newlib environment [#f2]_ to create a small
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176initrd.
177
178Finally, you have to boot the kernel and load initrd. Almost all Linux
179boot loaders support initrd. Since the boot process is still compatible
180with an older mechanism, the following boot command line parameters
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300181have to be given::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700183 root=/dev/ram0 rw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Greg Kroah-Hartman890fbae2005-06-20 21:15:16 -0700185(rw is only necessary if writing to the initrd file system.)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300187With LOADLIN, you simply execute::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 LOADLIN <kernel> initrd=<disk_image>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300191e.g.::
192
193 LOADLIN C:\LINUX\BZIMAGE initrd=C:\LINUX\INITRD.GZ root=/dev/ram0 rw
194
195With LILO, you add the option ``INITRD=<path>`` to either the global section
196or to the section of the respective kernel in ``/etc/lilo.conf``, and pass
197the options using APPEND, e.g.::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 image = /bzImage
200 initrd = /boot/initrd.gz
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700201 append = "root=/dev/ram0 rw"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300203and run ``/sbin/lilo``
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205For other boot loaders, please refer to the respective documentation.
206
207Now you can boot and enjoy using initrd.
208
209
210Changing the root device
211------------------------
212
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700213When finished with its duties, init typically changes the root device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214and proceeds with starting the Linux system on the "real" root device.
215
216The procedure involves the following steps:
217 - mounting the new root file system
218 - turning it into the root file system
219 - removing all accesses to the old (initrd) root file system
220 - unmounting the initrd file system and de-allocating the RAM disk
221
222Mounting the new root file system is easy: it just needs to be mounted on
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300223a directory under the current root. Example::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300225 # mkdir /new-root
226 # mount -o ro /dev/hda1 /new-root
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228The root change is accomplished with the pivot_root system call, which
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300229is also available via the ``pivot_root`` utility (see :manpage:`pivot_root(8)`
230man page; ``pivot_root`` is distributed with util-linux version 2.10h or higher
231[#f3]_). ``pivot_root`` moves the current root to a directory under the new
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232root, and puts the new root at its place. The directory for the old root
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300233must exist before calling ``pivot_root``. Example::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300235 # cd /new-root
236 # mkdir initrd
237 # pivot_root . initrd
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700239Now, the init process may still access the old root via its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240executable, shared libraries, standard input/output/error, and its
241current root directory. All these references are dropped by the
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300242following command::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300244 # exec chroot . what-follows <dev/console >dev/console 2>&1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300246Where what-follows is a program under the new root, e.g. ``/sbin/init``
Greg Kroah-Hartman890fbae2005-06-20 21:15:16 -0700247If the new root file system will be used with udev and has no valid
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300248``/dev`` directory, udev must be initialized before invoking chroot in order
249to provide ``/dev/console``.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251Note: implementation details of pivot_root may change with time. In order
252to ensure compatibility, the following points should be observed:
253
254 - before calling pivot_root, the current directory of the invoking
255 process should point to the new root directory
256 - use . as the first argument, and the _relative_ path of the directory
257 for the old root as the second argument
258 - a chroot program must be available under the old and the new root
259 - chroot to the new root afterwards
260 - use relative paths for dev/console in the exec command
261
262Now, the initrd can be unmounted and the memory allocated by the RAM
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300263disk can be freed::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300265 # umount /initrd
266 # blockdev --flushbufs /dev/ram0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268It is also possible to use initrd with an NFS-mounted root, see the
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300269:manpage:`pivot_root(8)` man page for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272Usage scenarios
273---------------
274
275The main motivation for implementing initrd was to allow for modular
276kernel configuration at system installation. The procedure would work
277as follows:
278
279 1) system boots from floppy or other media with a minimal kernel
280 (e.g. support for RAM disks, initrd, a.out, and the Ext2 FS) and
281 loads initrd
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300282 2) ``/sbin/init`` determines what is needed to (1) mount the "real" root FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 (i.e. device type, device drivers, file system) and (2) the
284 distribution media (e.g. CD-ROM, network, tape, ...). This can be
285 done by asking the user, by auto-probing, or by using a hybrid
286 approach.
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300287 3) ``/sbin/init`` loads the necessary kernel modules
288 4) ``/sbin/init`` creates and populates the root file system (this doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 have to be a very usable system yet)
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300290 5) ``/sbin/init`` invokes ``pivot_root`` to change the root file system and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 execs - via chroot - a program that continues the installation
292 6) the boot loader is installed
293 7) the boot loader is configured to load an initrd with the set of
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300294 modules that was used to bring up the system (e.g. ``/initrd`` can be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 modified, then unmounted, and finally, the image is written from
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300296 ``/dev/ram0`` or ``/dev/rd/0`` to a file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 8) now the system is bootable and additional installation tasks can be
298 performed
299
300The key role of initrd here is to re-use the configuration data during
301normal system operation without requiring the use of a bloated "generic"
302kernel or re-compiling or re-linking the kernel.
303
304A second scenario is for installations where Linux runs on systems with
305different hardware configurations in a single administrative domain. In
306such cases, it is desirable to generate only a small set of kernels
307(ideally only one) and to keep the system-specific part of configuration
308information as small as possible. In this case, a common initrd could be
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300309generated with all the necessary modules. Then, only ``/sbin/init`` or a file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310read by it would have to be different.
311
Randy Dunlap18107322007-10-16 23:29:29 -0700312A third scenario is more convenient recovery disks, because information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313like the location of the root FS partition doesn't have to be provided at
314boot time, but the system loaded from initrd can invoke a user-friendly
315dialog and it can also perform some sanity checks (or even some form of
316auto-detection).
317
318Last not least, CD-ROM distributors may use it for better installation
319from CD, e.g. by using a boot floppy and bootstrapping a bigger RAM disk
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300320via initrd from CD; or by booting via a loader like ``LOADLIN`` or directly
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321from the CD-ROM, and loading the RAM disk from CD without need of
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300322floppies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324
325Obsolete root change mechanism
326------------------------------
327
328The following mechanism was used before the introduction of pivot_root.
329Current kernels still support it, but you should _not_ rely on its
330continued availability.
331
332It works by mounting the "real" root device (i.e. the one set with rdev
333in the kernel image or with root=... at the boot command line) as the
334root file system when linuxrc exits. The initrd file system is then
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300335unmounted, or, if it is still busy, moved to a directory ``/initrd``, if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336such a directory exists on the new root file system.
337
338In order to use this mechanism, you do not have to specify the boot
339command options root, init, or rw. (If specified, they will affect
340the real root file system, not the initrd environment.)
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342If /proc is mounted, the "real" root device can be changed from within
343linuxrc by writing the number of the new root FS device to the special
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300344file /proc/sys/kernel/real-root-dev, e.g.::
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 # echo 0x301 >/proc/sys/kernel/real-root-dev
347
348Note that the mechanism is incompatible with NFS and similar file
349systems.
350
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300351This old, deprecated mechanism is commonly called ``change_root``, while
352the new, supported mechanism is called ``pivot_root``.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700355Mixed change_root and pivot_root mechanism
356------------------------------------------
357
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300358In case you did not want to use ``root=/dev/ram0`` to trigger the pivot_root
359mechanism, you may create both ``/linuxrc`` and ``/sbin/init`` in your initrd
360image.
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700361
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300362``/linuxrc`` would contain only the following::
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700363
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300364 #! /bin/sh
365 mount -n -t proc proc /proc
366 echo 0x0100 >/proc/sys/kernel/real-root-dev
367 umount -n /proc
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700368
369Once linuxrc exited, the kernel would mount again your initrd as root,
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300370this time executing ``/sbin/init``. Again, it would be the duty of this init
371to build the right environment (maybe using the ``root= device`` passed on
372the cmdline) before the final execution of the real ``/sbin/init``.
Domenico Andreoli9d9a2002007-05-23 13:58:11 -0700373
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375Resources
376---------
377
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300378.. [#f1] Almesberger, Werner; "Booting Linux: The History and the Future"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 http://www.almesberger.net/cv/papers/ols2k-9.ps.gz
Mauro Carvalho Chehab5d0ad552016-09-21 16:52:26 -0300380.. [#f2] newlib package (experimental), with initrd example
381 https://www.sourceware.org/newlib/
382.. [#f3] util-linux: Miscellaneous utilities for Linux
383 https://www.kernel.org/pub/linux/utils/util-linux/