Back to home page

OSCL-LXR

 
 

    


0001 Using the initial RAM disk (initrd)
0002 ===================================
0003 
0004 Written 1996,2000 by Werner Almesberger <werner.almesberger@epfl.ch> and
0005 Hans Lermen <lermen@fgan.de>
0006 
0007 
0008 initrd provides the capability to load a RAM disk by the boot loader.
0009 This RAM disk can then be mounted as the root file system and programs
0010 can be run from it. Afterwards, a new root file system can be mounted
0011 from a different device. The previous root (from initrd) is then moved
0012 to a directory and can be subsequently unmounted.
0013 
0014 initrd is mainly designed to allow system startup to occur in two phases,
0015 where the kernel comes up with a minimum set of compiled-in drivers, and
0016 where additional modules are loaded from initrd.
0017 
0018 This document gives a brief overview of the use of initrd. A more detailed
0019 discussion of the boot process can be found in [#f1]_.
0020 
0021 
0022 Operation
0023 ---------
0024 
0025 When using initrd, the system typically boots as follows:
0026 
0027   1) the boot loader loads the kernel and the initial RAM disk
0028   2) the kernel converts initrd into a "normal" RAM disk and
0029      frees the memory used by initrd
0030   3) if the root device is not ``/dev/ram0``, the old (deprecated)
0031      change_root procedure is followed. see the "Obsolete root change
0032      mechanism" section below.
0033   4) root device is mounted. if it is ``/dev/ram0``, the initrd image is
0034      then mounted as root
0035   5) /sbin/init is executed (this can be any valid executable, including
0036      shell scripts; it is run with uid 0 and can do basically everything
0037      init can do).
0038   6) init mounts the "real" root file system
0039   7) init places the root file system at the root directory using the
0040      pivot_root system call
0041   8) init execs the ``/sbin/init`` on the new root filesystem, performing
0042      the usual boot sequence
0043   9) the initrd file system is removed
0044 
0045 Note that changing the root directory does not involve unmounting it.
0046 It is therefore possible to leave processes running on initrd during that
0047 procedure. Also note that file systems mounted under initrd continue to
0048 be accessible.
0049 
0050 
0051 Boot command-line options
0052 -------------------------
0053 
0054 initrd adds the following new options::
0055 
0056   initrd=<path>    (e.g. LOADLIN)
0057 
0058     Loads the specified file as the initial RAM disk. When using LILO, you
0059     have to specify the RAM disk image file in /etc/lilo.conf, using the
0060     INITRD configuration variable.
0061 
0062   noinitrd
0063 
0064     initrd data is preserved but it is not converted to a RAM disk and
0065     the "normal" root file system is mounted. initrd data can be read
0066     from /dev/initrd. Note that the data in initrd can have any structure
0067     in this case and doesn't necessarily have to be a file system image.
0068     This option is used mainly for debugging.
0069 
0070     Note: /dev/initrd is read-only and it can only be used once. As soon
0071     as the last process has closed it, all data is freed and /dev/initrd
0072     can't be opened anymore.
0073 
0074   root=/dev/ram0
0075 
0076     initrd is mounted as root, and the normal boot procedure is followed,
0077     with the RAM disk mounted as root.
0078 
0079 Compressed cpio images
0080 ----------------------
0081 
0082 Recent kernels have support for populating a ramdisk from a compressed cpio
0083 archive. On such systems, the creation of a ramdisk image doesn't need to
0084 involve special block devices or loopbacks; you merely create a directory on
0085 disk with the desired initrd content, cd to that directory, and run (as an
0086 example)::
0087 
0088         find . | cpio --quiet -H newc -o | gzip -9 -n > /boot/imagefile.img
0089 
0090 Examining the contents of an existing image file is just as simple::
0091 
0092         mkdir /tmp/imagefile
0093         cd /tmp/imagefile
0094         gzip -cd /boot/imagefile.img | cpio -imd --quiet
0095 
0096 Installation
0097 ------------
0098 
0099 First, a directory for the initrd file system has to be created on the
0100 "normal" root file system, e.g.::
0101 
0102         # mkdir /initrd
0103 
0104 The name is not relevant. More details can be found on the
0105 :manpage:`pivot_root(2)` man page.
0106 
0107 If the root file system is created during the boot procedure (i.e. if
0108 you're building an install floppy), the root file system creation
0109 procedure should create the ``/initrd`` directory.
0110 
0111 If initrd will not be mounted in some cases, its content is still
0112 accessible if the following device has been created::
0113 
0114         # mknod /dev/initrd b 1 250
0115         # chmod 400 /dev/initrd
0116 
0117 Second, the kernel has to be compiled with RAM disk support and with
0118 support for the initial RAM disk enabled. Also, at least all components
0119 needed to execute programs from initrd (e.g. executable format and file
0120 system) must be compiled into the kernel.
0121 
0122 Third, you have to create the RAM disk image. This is done by creating a
0123 file system on a block device, copying files to it as needed, and then
0124 copying the content of the block device to the initrd file. With recent
0125 kernels, at least three types of devices are suitable for that:
0126 
0127  - a floppy disk (works everywhere but it's painfully slow)
0128  - a RAM disk (fast, but allocates physical memory)
0129  - a loopback device (the most elegant solution)
0130 
0131 We'll describe the loopback device method:
0132 
0133  1) make sure loopback block devices are configured into the kernel
0134  2) create an empty file system of the appropriate size, e.g.::
0135 
0136         # dd if=/dev/zero of=initrd bs=300k count=1
0137         # mke2fs -F -m0 initrd
0138 
0139     (if space is critical, you may want to use the Minix FS instead of Ext2)
0140  3) mount the file system, e.g.::
0141 
0142         # mount -t ext2 -o loop initrd /mnt
0143 
0144  4) create the console device::
0145 
0146     # mkdir /mnt/dev
0147     # mknod /mnt/dev/console c 5 1
0148 
0149  5) copy all the files that are needed to properly use the initrd
0150     environment. Don't forget the most important file, ``/sbin/init``
0151 
0152     .. note:: ``/sbin/init`` permissions must include "x" (execute).
0153 
0154  6) correct operation the initrd environment can frequently be tested
0155     even without rebooting with the command::
0156 
0157         # chroot /mnt /sbin/init
0158 
0159     This is of course limited to initrds that do not interfere with the
0160     general system state (e.g. by reconfiguring network interfaces,
0161     overwriting mounted devices, trying to start already running demons,
0162     etc. Note however that it is usually possible to use pivot_root in
0163     such a chroot'ed initrd environment.)
0164  7) unmount the file system::
0165 
0166         # umount /mnt
0167 
0168  8) the initrd is now in the file "initrd". Optionally, it can now be
0169     compressed::
0170 
0171         # gzip -9 initrd
0172 
0173 For experimenting with initrd, you may want to take a rescue floppy and
0174 only add a symbolic link from ``/sbin/init`` to ``/bin/sh``. Alternatively, you
0175 can try the experimental newlib environment [#f2]_ to create a small
0176 initrd.
0177 
0178 Finally, you have to boot the kernel and load initrd. Almost all Linux
0179 boot loaders support initrd. Since the boot process is still compatible
0180 with an older mechanism, the following boot command line parameters
0181 have to be given::
0182 
0183   root=/dev/ram0 rw
0184 
0185 (rw is only necessary if writing to the initrd file system.)
0186 
0187 With LOADLIN, you simply execute::
0188 
0189      LOADLIN <kernel> initrd=<disk_image>
0190 
0191 e.g.::
0192 
0193         LOADLIN C:\LINUX\BZIMAGE initrd=C:\LINUX\INITRD.GZ root=/dev/ram0 rw
0194 
0195 With LILO, you add the option ``INITRD=<path>`` to either the global section
0196 or to the section of the respective kernel in ``/etc/lilo.conf``, and pass
0197 the options using APPEND, e.g.::
0198 
0199   image = /bzImage
0200     initrd = /boot/initrd.gz
0201     append = "root=/dev/ram0 rw"
0202 
0203 and run ``/sbin/lilo``
0204 
0205 For other boot loaders, please refer to the respective documentation.
0206 
0207 Now you can boot and enjoy using initrd.
0208 
0209 
0210 Changing the root device
0211 ------------------------
0212 
0213 When finished with its duties, init typically changes the root device
0214 and proceeds with starting the Linux system on the "real" root device.
0215 
0216 The procedure involves the following steps:
0217  - mounting the new root file system
0218  - turning it into the root file system
0219  - removing all accesses to the old (initrd) root file system
0220  - unmounting the initrd file system and de-allocating the RAM disk
0221 
0222 Mounting the new root file system is easy: it just needs to be mounted on
0223 a directory under the current root. Example::
0224 
0225         # mkdir /new-root
0226         # mount -o ro /dev/hda1 /new-root
0227 
0228 The root change is accomplished with the pivot_root system call, which
0229 is also available via the ``pivot_root`` utility (see :manpage:`pivot_root(8)`
0230 man page; ``pivot_root`` is distributed with util-linux version 2.10h or higher
0231 [#f3]_). ``pivot_root`` moves the current root to a directory under the new
0232 root, and puts the new root at its place. The directory for the old root
0233 must exist before calling ``pivot_root``. Example::
0234 
0235         # cd /new-root
0236         # mkdir initrd
0237         # pivot_root . initrd
0238 
0239 Now, the init process may still access the old root via its
0240 executable, shared libraries, standard input/output/error, and its
0241 current root directory. All these references are dropped by the
0242 following command::
0243 
0244         # exec chroot . what-follows <dev/console >dev/console 2>&1
0245 
0246 Where what-follows is a program under the new root, e.g. ``/sbin/init``
0247 If the new root file system will be used with udev and has no valid
0248 ``/dev`` directory, udev must be initialized before invoking chroot in order
0249 to provide ``/dev/console``.
0250 
0251 Note: implementation details of pivot_root may change with time. In order
0252 to ensure compatibility, the following points should be observed:
0253 
0254  - before calling pivot_root, the current directory of the invoking
0255    process should point to the new root directory
0256  - use . as the first argument, and the _relative_ path of the directory
0257    for the old root as the second argument
0258  - a chroot program must be available under the old and the new root
0259  - chroot to the new root afterwards
0260  - use relative paths for dev/console in the exec command
0261 
0262 Now, the initrd can be unmounted and the memory allocated by the RAM
0263 disk can be freed::
0264 
0265         # umount /initrd
0266         # blockdev --flushbufs /dev/ram0
0267 
0268 It is also possible to use initrd with an NFS-mounted root, see the
0269 :manpage:`pivot_root(8)` man page for details.
0270 
0271 
0272 Usage scenarios
0273 ---------------
0274 
0275 The main motivation for implementing initrd was to allow for modular
0276 kernel configuration at system installation. The procedure would work
0277 as follows:
0278 
0279   1) system boots from floppy or other media with a minimal kernel
0280      (e.g. support for RAM disks, initrd, a.out, and the Ext2 FS) and
0281      loads initrd
0282   2) ``/sbin/init`` determines what is needed to (1) mount the "real" root FS
0283      (i.e. device type, device drivers, file system) and (2) the
0284      distribution media (e.g. CD-ROM, network, tape, ...). This can be
0285      done by asking the user, by auto-probing, or by using a hybrid
0286      approach.
0287   3) ``/sbin/init`` loads the necessary kernel modules
0288   4) ``/sbin/init`` creates and populates the root file system (this doesn't
0289      have to be a very usable system yet)
0290   5) ``/sbin/init`` invokes ``pivot_root`` to change the root file system and
0291      execs - via chroot - a program that continues the installation
0292   6) the boot loader is installed
0293   7) the boot loader is configured to load an initrd with the set of
0294      modules that was used to bring up the system (e.g. ``/initrd`` can be
0295      modified, then unmounted, and finally, the image is written from
0296      ``/dev/ram0`` or ``/dev/rd/0`` to a file)
0297   8) now the system is bootable and additional installation tasks can be
0298      performed
0299 
0300 The key role of initrd here is to re-use the configuration data during
0301 normal system operation without requiring the use of a bloated "generic"
0302 kernel or re-compiling or re-linking the kernel.
0303 
0304 A second scenario is for installations where Linux runs on systems with
0305 different hardware configurations in a single administrative domain. In
0306 such cases, it is desirable to generate only a small set of kernels
0307 (ideally only one) and to keep the system-specific part of configuration
0308 information as small as possible. In this case, a common initrd could be
0309 generated with all the necessary modules. Then, only ``/sbin/init`` or a file
0310 read by it would have to be different.
0311 
0312 A third scenario is more convenient recovery disks, because information
0313 like the location of the root FS partition doesn't have to be provided at
0314 boot time, but the system loaded from initrd can invoke a user-friendly
0315 dialog and it can also perform some sanity checks (or even some form of
0316 auto-detection).
0317 
0318 Last not least, CD-ROM distributors may use it for better installation
0319 from CD, e.g. by using a boot floppy and bootstrapping a bigger RAM disk
0320 via initrd from CD; or by booting via a loader like ``LOADLIN`` or directly
0321 from the CD-ROM, and loading the RAM disk from CD without need of
0322 floppies.
0323 
0324 
0325 Obsolete root change mechanism
0326 ------------------------------
0327 
0328 The following mechanism was used before the introduction of pivot_root.
0329 Current kernels still support it, but you should _not_ rely on its
0330 continued availability.
0331 
0332 It works by mounting the "real" root device (i.e. the one set with rdev
0333 in the kernel image or with root=... at the boot command line) as the
0334 root file system when linuxrc exits. The initrd file system is then
0335 unmounted, or, if it is still busy, moved to a directory ``/initrd``, if
0336 such a directory exists on the new root file system.
0337 
0338 In order to use this mechanism, you do not have to specify the boot
0339 command options root, init, or rw. (If specified, they will affect
0340 the real root file system, not the initrd environment.)
0341 
0342 If /proc is mounted, the "real" root device can be changed from within
0343 linuxrc by writing the number of the new root FS device to the special
0344 file /proc/sys/kernel/real-root-dev, e.g.::
0345 
0346   # echo 0x301 >/proc/sys/kernel/real-root-dev
0347 
0348 Note that the mechanism is incompatible with NFS and similar file
0349 systems.
0350 
0351 This old, deprecated mechanism is commonly called ``change_root``, while
0352 the new, supported mechanism is called ``pivot_root``.
0353 
0354 
0355 Mixed change_root and pivot_root mechanism
0356 ------------------------------------------
0357 
0358 In case you did not want to use ``root=/dev/ram0`` to trigger the pivot_root
0359 mechanism, you may create both ``/linuxrc`` and ``/sbin/init`` in your initrd
0360 image.
0361 
0362 ``/linuxrc`` would contain only the following::
0363 
0364         #! /bin/sh
0365         mount -n -t proc proc /proc
0366         echo 0x0100 >/proc/sys/kernel/real-root-dev
0367         umount -n /proc
0368 
0369 Once linuxrc exited, the kernel would mount again your initrd as root,
0370 this time executing ``/sbin/init``. Again, it would be the duty of this init
0371 to build the right environment (maybe using the ``root= device`` passed on
0372 the cmdline) before the final execution of the real ``/sbin/init``.
0373 
0374 
0375 Resources
0376 ---------
0377 
0378 .. [#f1] Almesberger, Werner; "Booting Linux: The History and the Future"
0379     https://www.almesberger.net/cv/papers/ols2k-9.ps.gz
0380 .. [#f2] newlib package (experimental), with initrd example
0381     https://www.sourceware.org/newlib/
0382 .. [#f3] util-linux: Miscellaneous utilities for Linux
0383     https://www.kernel.org/pub/linux/utils/util-linux/