Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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