Initrd
Encyclopedia
In computing, initrd is a scheme for loading a temporary file system
File system
A file system is a means to organize data expected to be retained after a program terminates by providing procedures to store, retrieve and update data, as well as manage the available space on the device which contain it. A file system organizes data in an efficient manner and is tuned to the...

 into memory
Computer memory
In computing, memory refers to the physical devices used to store programs or data on a temporary or permanent basis for use in a computer or other digital electronic device. The term primary memory is used for the information in physical systems which are fast In computing, memory refers to the...

 in the boot process of the Linux kernel
Linux kernel
The Linux kernel is an operating system kernel used by the Linux family of Unix-like operating systems. It is one of the most prominent examples of free and open source software....

. initrd and initramfs refer to slightly different methods of achieving this. Both are commonly used to make preparations before the real root
Root directory
In computer file systems, the root directory is the first or top-most directory in a hierarchy. It can be likened to the root of a tree — the starting point where all branches originate.-Metaphor:...

 file system can be mount
Mount (Unix)
The Unix command line utility mount instructs the operating system that a file system is ready to use, and associates it with a particular point in the system's file system hierarchy . The counterpart umount instructs the operating system that the file system should be disassociated from its mount...

ed.

Rationale

Many Linux distribution
Linux distribution
A Linux distribution is a member of the family of Unix-like operating systems built on top of the Linux kernel. Such distributions are operating systems including a large collection of software applications such as word processors, spreadsheets, media players, and database applications...

s ship a single, generic kernel image that is intended to boot as wide a variety of hardware as possible. The device driver
Device driver
In computing, a device driver or software driver is a computer program allowing higher-level computer programs to interact with a hardware device....

s for this generic kernel image are included as loadable modules
Loadable Kernel Module
In computing, a loadable kernel module is an object file that contains code to extend the running kernel, or so-called base kernel, of an operating system...

, as it is not possible to statically compile them all into the one kernel without making it too large to boot from computers with limited memory or from lower-capacity media like floppy disks.

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).

To further complicate matters, the root file system may be on a software RAID
RAID
RAID is a storage technology that combines multiple disk drive components into a logical unit...

 volume, LVM
Logical Volume Manager (Linux)
LVM is a logical volume manager for the Linux kernel; it manages disk drives and similar mass-storage devices, in particular large ones. The term "volume" refers to a disk drive or partition thereof...

, NFS (on diskless workstations), or on an encrypted partition. All of these require special preparations to mount.

Another complication is kernel support for hibernation
Hibernate (OS feature)
Hibernation in computing is powering down a computer while retaining its state.Upon hibernation, the computer saves the contents of its random access memory to a hard disk or other non-volatile storage...

, 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 would contain user-space helpers that would do the hardware detection, module loading and device discovery necessary to get the real root file system mounted.

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
    El Torito (CD-ROM standard)
    The El Torito Bootable CD Specification is an extension to the ISO 9660 CD-ROM specification. It is designed to allow a computer to boot from a CD-ROM...

     on an optical disc
    Optical disc
    In computing and optical disc recording technologies, an optical disc is a flat, usually circular disc which encodes binary data in the form of pits and lands on a special material on one of its flat surfaces...

  • A small ext3
    Ext3
    The ext3 or third extended filesystem is a journaled file system that is commonly used by the Linux kernel. It is the default file system for many popular Linux distributions, including Debian...

     or FAT
    File Allocation Table
    File Allocation Table is a computer file system architecture now widely used on many computer systems and most memory cards, such as those used with digital cameras. FAT file systems are commonly found on floppy disks, flash memory cards, digital cameras, and many other portable devices because of...

    -formatted partition on a local disk (a boot partition)
  • A TFTP
    Trivial File Transfer Protocol
    Trivial File Transfer Protocol is a file transfer protocol known for its simplicity. It is generally used forautomated transfer of configuration or boot files between machines in a local environment....

     server (on systems that can boot from Ethernet
    Ethernet
    Ethernet is a family of computer networking technologies for local area networks commercially introduced in 1980. Standardized in IEEE 802.3, Ethernet has largely replaced competing wired LAN technologies....

    )


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. The driver for that file system must be compiled statically into the kernel. Many distributions originally used compressed ext2
    Ext2
    The ext2 or second extended filesystem is a file system for the Linux kernel. It was initially designed by Rémy Card as a replacement for the extended file system ....

     file system images. Others (including Debian 3.1) used cramfs
    Cramfs
    The compressed ROM file system is a free read-only Linux file system designed for simplicity and space-efficiency. It is mainly used in embedded systems and small-footprint systems....

     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. 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.
  • In the initramfs scheme (available in Linux 2.6.13 onwards), the image may be a cpio
    Cpio
    cpio is a general file archiver utility and its associated file format. It is primarily installed on Unix-like computer operating systems. The software utility was originally intended as a tape archiving program as part of the Programmer's Workbench , and has been a component of virtually every...

     archive (optionally compressed). The archive is unpacked by the kernel into a special instance of a tmpfs
    TMPFS
    tmpfs is a common name for a temporary file storage facility on many Unix-like operating systems. It is intended to appear as a mounted file system, but stored in volatile memory instead of a persistent storage device...

     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.
On an initramfs, the kernel executes /init as its first process. /init is not expected to exit.


Depending on which algorithms were compiled statically into it, the kernel can currently unpack initrd/initramfs images compressed with gzip
Gzip
Gzip is any of several software applications used for file compression and decompression. The term usually refers to the GNU Project's implementation, "gzip" standing for GNU zip. It is based on the DEFLATE algorithm, which is a combination of Lempel-Ziv and Huffman coding...

, bzip2
Bzip2
bzip2 is a free and open source implementation of the Burrows–Wheeler algorithm. It is developed and maintained by Julian Seward. Seward made the first public release of bzip2, version 0.15, in July 1996.-Compression efficiency:...

, LZMA
LZMA
The Lempel–Ziv–Markov chain algorithm is an algorithm used to perform data compression. It has been under development since 1998 and was first used in the 7z format of the 7-Zip archiver...

 and XZ
Xz
xz is a lossless data compression file format incorporating the LZMA2 compression algorithm. Like gzip and bzip2, concatenation is supported to compress multiple files, but the convention is to bundle a file that is an archive itself, such as those created by the tar or cpio Unix...

.

Mount preparations

Some Linux distributions will generate a customized initrd image which contains only whatever is necessary to boot some particular computer, such as ATA, SCSI
SCSI
Small Computer System Interface is a set of standards for physically connecting and transferring data between computers and peripheral devices. The SCSI standards define commands, protocols, and electrical and optical interfaces. SCSI is most commonly used for hard disks and tape drives, but it...

 and filesystem kernel modules
Loadable Kernel Module
In computing, a loadable kernel module is an object file that contains code to extend the running kernel, or so-called base kernel, of an operating system...

. These typically embed the location and type of the root file system.

Other Linux distributions (such as Fedora
Fedora (operating system)
Fedora is a RPM-based, general purpose collection of software, including an operating system based on the Linux kernel, developed by the community-supported Fedora Project and sponsored by Red Hat...

 and Ubuntu
Ubuntu (operating system)
Ubuntu is a computer operating system based on the Debian Linux distribution and distributed as free and open source software. It is named after the Southern African philosophy of Ubuntu...

) generate a more generic initrd image. These start only with the device name of the root file system (or its UUID
Universally Unique Identifier
A universally unique identifier is an identifier standard used in software construction, standardized by the Open Software Foundation as part of the Distributed Computing Environment ....

) and must discover everything else at boot time. In this case, a complex cascade of tasks must be performed 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
    Splash screen
    A splash screen is an image that appears while a game or program is loading. It may also be used to describe an introduction page on a website. Splash screens sometimes do not cover the entire screen, but only a rectangle near the center...

    , 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
      Network card
      A network interface controller is a computer hardware component that connects a computer to a computer network....

      .
    • Invoke a DHCP
      Dynamic Host Configuration Protocol
      The Dynamic Host Configuration Protocol is a network configuration protocol for hosts on Internet Protocol networks. Computers that are connected to IP networks must be configured before they can communicate with other hosts. The most essential information needed is an IP address, and a default...

       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
    Mdadm
    mdadm is a Linux utility used to manage software RAID devices.The name is derived from the md device nodes it administers or manages, and it replaced a previous utility mdctl...

     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
    Volume group
    Volume Group is the highest level abstraction used within the Logical Volume Manager . It gathers together a collection of Logical Volumes and Physical Volumes into one administrative unit....

     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
      Smart card
      A smart card, chip card, or integrated circuit card , is any pocket-sized card with embedded integrated circuits. A smart card or microprocessor cards contain volatile memory and microprocessor components. The card is made of plastic, generally polyvinyl chloride, but sometimes acrylonitrile...

       or a USB security dongle
      Dongle
      A software protection dongle is a small piece of hardware that plugs into an electrical connector on a computer and serves as an electronic "key" for a piece of software; the program will only run when the dongle is plugged in...

      ).
    • Create a decryption target with the device mapper
      Device mapper
      In the Linux kernel, the device-mapper serves as a generic framework to map one block device onto another. It forms the foundation of LVM2 and EVMS, software RAIDs, dm-crypt disk encryption, and offers additional features such as file-system snapshots....

      .


Some distributions use an event-driven hotplug agent such as udev
Udev
udev is the device manager for the Linux kernel. Primarily, it manages device nodes in /dev. It is the successor of devfs and hotplug, which means that it handles the /dev directory and all user space actions when adding/removing devices, including firmware load.-History:udev was new in Linux...

, 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
    Named pipe
    In computing, a named pipe is an extension to the traditional pipe concept on Unix and Unix-like systems, and is one of the methods of inter-process communication. The concept is also found in Microsoft Windows, although the semantics differ substantially...

    ) 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. 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
BusyBox
BusyBox provides several stripped-down Unix tools in a single executable. It runs in a variety of POSIX environments such as Linux, Android, FreeBSD and others, such as proprietary kernels, although many of the tools it provides are designed to work with interfaces provided by the Linux kernel. It...

 toolkit). To further save space, the shell, utilities and their supporting libraries are typically compiled with space optimizations enabled (such as with gcc
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...

's "-Os" flag) and linked against klibc
Klibc
In computing, klibc is a minimalistic subset of the standard C library developed by H. Peter Anvin. It was developed mainly to be used during the Linux startup process, and it is part of the early user space, i.e. components used during kernel startup, but which do not run in kernel mode...

, a minimal version of the C library
C standard library
The C Standard Library is the standard library for the programming language C, as specified in the ANSI C standard.. It was developed at the same time as the C POSIX library, which is basically a superset of it...

 written specifically for this purpose.

Other uses

Installers for Linux distributions are 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
Tiny Core Linux
Tiny Core Linux is a minimal Linux operating system focusing on providing a base system using BusyBox and FLTK. The lead developer of the project is Robert Shingledecker. The distribution is notable for its size under 12MB, with additional functionality provided by extensions of which there are...

 also runs from initrd.

Puppy Linux distros also runs from initrd.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK