initrd
In computing, initrd
(initial ramdisk) is a scheme for loading a temporary root file system into memory, which may be used as part of the Linux startup process. initrd
and initramfs
refer to two different methods of achieving this. Both are commonly used to make preparations before the real root file system can be mounted.
Rationale
Many Linux distributions ship a single, generic Linux kernel image – one that the distribution's developers create specifically to boot on a wide variety of hardware. The device drivers for this generic kernel image are included as loadable kernel modules because statically compiling many drivers into one kernel causes the kernel image to be much larger, perhaps too large to boot on computers with limited memory. This then raises the problem of detecting and loading the modules necessary to mount the root file system at boot time, or for that matter, deducing where or what the root file system is.[1]
To further complicate matters, the root file system may be on a software RAID volume, LVM, NFS (on diskless workstations), or on an encrypted partition. All of these require special preparations to mount.[2]
Another complication is kernel support for hibernation, which suspends the computer to disk by dumping an image of the entire contents of memory to a swap partition or a regular file, then powering off. On next boot, this image has to be made accessible before it can be loaded back into memory.
To avoid having to hardcode handling for so many special cases into the kernel, an initial boot stage with a temporary root file-system — now dubbed early user space — is used. This root file-system can contain user-space helpers which do the hardware detection, module loading and device discovery necessary to get the real root file-system mounted.[2]
Implementation
An image of this initial root file system (along with the kernel image) must be stored somewhere accessible by the Linux bootloader or the boot firmware of the computer. This can be:
- The root file system itself
- A boot image on an optical disc
- A small, generally ext3 or FAT-formatted, partition on a local disk (a boot partition)
- A TFTP server (on systems that can boot from Ethernet)
The bootloader will load the kernel and initial root file system image into memory and then start the kernel, passing in the memory address of the image. At the end of its boot sequence, the kernel tries to determine the format of the image from its first few blocks of data:
- In the initrd scheme, the image may be a file system image (optionally compressed), which is made available in a special block device (/dev/ram) that is then mounted as the initial root file system.[3] The driver for that file system must be compiled statically into the kernel. Many distributions originally used compressed ext2 file system images. Others (including Debian 3.1) used cramfs in order to boot on memory-limited systems, since the cramfs image can be mounted in-place without requiring extra space for decompression.
- Once the initial root file system is up, the kernel executes /linuxrc as its first process.[4] When it exits, the kernel assumes that the real root file system has been mounted and executes /sbin/init to begin the normal user-space boot process.[3]
- In the initramfs scheme (available in Linux 2.6.13 onwards), the image may be a cpio archive (optionally compressed). The archive is unpacked by the kernel into a special instance of a tmpfs that becomes the initial root file system. This scheme has the advantage of not requiring an intermediate file system or block drivers to be compiled into the kernel.[5] Some systems use the dracut package to create an initramfs image.[6]
- In the initramfs scheme, the kernel executes /init as its first process, which is not expected to exit.[5]
Depending on which algorithms were compiled statically into it, the kernel can currently unpack initrd/initramfs images compressed with gzip, bzip2, LZMA, XZ and LZO.
Mount preparations
Some Linux distributions such as Debian will generate a customized initrd image which contains only whatever is necessary to boot some particular computer, such as ATA, SCSI and filesystem kernel modules. These typically embed the location and type of the root file system.
Other Linux distributions (such as Fedora and Ubuntu) generate a more generic initrd image. These start only with the device name of the root file system (or its UUID) and must discover everything else at boot time. In this case, the software must perform a complex cascade of tasks to get the root file system mounted:
- Any hardware drivers that the boot process depends on must be loaded. A common arrangement is to pack kernel modules for common storage devices onto the initrd and then invoke a hotplug agent to pull in modules matching the computer's detected hardware.
- On systems which display a boot splash screen, the video hardware must be initialized and a user-space helper started to paint animations onto the display in lockstep with the boot process.
- If the root file system is on NFS, it must then:
- Bring up the primary network interface.
- Invoke a DHCP client, with which it can obtain a DHCP lease.
- Extract the name of the NFS share and the address of the NFS server from the lease.
- Mount the NFS share.
- If the root file system appears to be on a software RAID device, there is no way of knowing which devices the RAID volume spans; the standard MD utilities must be invoked to scan all available block devices and bring the required ones online.
- If the root file system appears to be on a logical volume, the LVM utilities must be invoked to scan for and activate the volume group containing it.
- If the root file system is on an encrypted block device:
- Invoke a helper script to prompt the user to type in a passphrase and/or insert a hardware token (such as a smart card or a USB security dongle).
- Create a decryption target with the device mapper.
Some distributions use an event-driven hotplug agent such as udev, which invokes helper programs as hardware devices, disk partitions and storage volumes matching certain rules come online. This allows discovery to run in parallel, and to progressively cascade into arbitrary nestings of LVM, RAID or encryption to get at the root file system.
When the root file system finally becomes visible:
- Any maintenance tasks which cannot run on a mounted root file system are done.
- The root file system is mounted read-only.
- Any processes which must continue running (such as the splash screen helper and its command FIFO) are hoisted into the newly mounted root file system.
The final root file system cannot simply be mounted over /, since that would make the scripts and tools on the initial root file system inaccessible for any final cleanup tasks:
- On an initrd, the new root is mounted at a temporary mount point and rotated into place with pivot_root(8) (which was introduced specifically for this purpose). This leaves the initial root file system at a mount point (such as /initrd) where normal boot scripts can later unmount it to free up memory held by the initrd.
- On an initramfs, the initial root file system cannot be rotated away.[7] Instead, it is simply emptied and the final root file system mounted over the top.
Most initial root file systems implement /linuxrc or /init as a shell script and thus include a minimal shell (usually /bin/ash) along with some essential user-space utilities (usually the BusyBox toolkit). To further save space, the shell, utilities and their supporting libraries are typically compiled with space optimizations enabled (such as with gcc's "-Os" flag) and linked against klibc, a minimal version of the C library written specifically for this purpose.[8]
Other uses
Installers for Linux distributions typically run entirely from an initramfs, as they must be able to host the installer interface and supporting tools before any persistent storage has been set up.
Tiny Core Linux[9] and Puppy Linux[10] can run entirely from initrd.
Similarities in other operating systems
Since Windows Vista, Windows can boot off a WIM disk image file, whose file format is officially published; it is rather similar to ZIP except that it supports hardlinks, uses chunk-by-chunk compression and can support deduplicated chunks. In this case, the whole WIM is first loaded into RAM, what is followed by a kernel init. After this, the loaded in-RAM WIM is available as a SystemRoot with an assigned drive letter. Windows installer uses this; it boots off BOOT.WIM, and then uses INSTALL.WIM as the collection of the Windows files to be installed.
Also, Windows Preinstallation Environment (Windows PE) uses the same, being a base for separate-boot versions of some antivirus and backup/disaster recovery software.
There is also a possibility to install Windows in a way it will always boot off a WIM or VHD file placed on a physical disk. Though, this is rarely used since the Windows boot loader is capable of loading the .sys files for boot-time kernel modules itself, which is the task that requires initrd in Linux.
See also
References
- ↑ Almesberger, Werner (2000), "Booting linux: the history and the future", Proceedings of the Ottawa Linux Symposium
- ↑ 2.0 2.1 Landley, Rob (15 March 2005), Introducing initramfs, a new model for initial RAM disks, linuxdevices.com, archived from the original on 2013-01-04
- ↑ 3.0 3.1 Almesberger, Werner; and Lermen, Hans (2000), Using the initial RAM disk (initrd)
- ↑ github/torvalds/linux: do_mounts_initrd.c
- ↑ 5.0 5.1 Landley, Rob (2005-10-17), ramfs, rootfs, and initramfs docs, take 2, Linux kernel source tree
- ↑ Petersen, Richard Leland (2010). Fedora 13: Administration, Networking, Security. Alameda, California: Surfing Turtle Press. p. 76. ISBN 978-1-936280-02-5.
Dracut uses kernel parameters listed on the GRUB kernel command line to configure the initramfs RAM file system on the fly, providing more flexibiltity and furthercutting down on RAM file system code.
- ↑ Fish, Richard (2005-07-06), pivot_root from initramfs causes circular reference in mount tree, Linux Kernel Bug Tracker, retrieved 2009-02-28
- ↑ Garzik, Jeff (2002-11-02), initramfs merge, part 1 of N, Linux kernel mailing list
- ↑ http://distro.ibiblio.org/tinycorelinux/concepts.html
- ↑ http://distro.ibiblio.org/puppylinux/puppy-5.3/release-Slacko-5.3.htm
External links
- Detailed comparison of initrd-generating toolkits
- Kernel documentation on early userspace support
- "Motivation for switch from initrd to initramfs". Archived from the original on 2013-01-04.
|
|