A temporary initial root filesystem can be used in the boot process of a Linux kernel. It is typically mounted in primary storage and used to make necessary preparations before the normal root filesystem can be mounted.
Mechanisms for providing an initial root filesystem include:
An initial root filesystem facilitates modular kernel configuration during startup.
During system startup, the Unix kernel (e.g. Linux) is loaded into memory, and it remains resident throughout the operation of the system.
To minimise the amount of code loaded into memory, for maximum modularity, and sometimes to provide a single generic kernel image, the Linux kernel can be compiled omitting code that is necessary to load the operating system, since it is not possible to statically compile all the code into the kernel without making it too large to start on computers with limited memory or from lower-capacity media. Some of this code is located in modules known as kernel modules; the rest is userspace applications. This raises the problem of detecting and loading the necessary modules to mount the normal root filesystem.
Another situation raising similar problems is hibernation, which suspends the computer to disk by dumping an image of the entire system and then powering off. On next boot, this image has to be accessible before it can be loaded back into memory.
To detect and load necessary modules for the normal root filesystem, an initial root filesystem is used. This filesystem contains utilities that perform the hardware detection, module loading and device discovery necessary to mount the normal root filesystem.
The initial ramdisk, or initrd is a file containing an initial root filesystem.
Device node /dev/initrd references a read-only block device. The device is a RAM drive initialised by the bootloader before the kernel is started. The kernel can then use device /dev/initrd in the first phase of a two-phased system startup.
In the first startup phase, the kernel mounts an initial root filesystem from the contents of /dev/initrd.
In the second phase, additional drivers and/or other modules are loaded from the initial root filesystem. After loading the drivers and modules, a new root filesystem (the normal root filesystem) is mounted from a different device.
The following bootloader options when used with initrd, affect the kernel's boot-up operation:
initrd=filename |
This option specifies the file to load as the contents of /dev/initrd. The file name specified with this option will typically be a gzipped filesystem image. |
noinitrd |
This bootloader option disables the two phase boot-up operation. The kernel performs the usual boot sequence as if /dev/initrd was not initialised. With this option, any contents of /dev/initrd loaded into memory by the bootloader contents are preserved. This option permits the contents of /dev/initrd to be any data and need not be limited to a filesystem image. However, device /dev/initrd is read-only and can be read only one time after system startup. |
root=device-name |
This option specifies the device to be used as the normal root filesystem. The device specified by this option must be a mountable device having a suitable root filesystem. |
linuxrc (linux run command) is the first process executed after initrd is mounted.
This makes it possible to boot a small modularised kernel and to load the drivers that are really needed as modules. linuxrc assists in loading relevant drivers manually.
linuxrc has numerous potential uses, including the following:
During the boot process, linuxrc can be used as a boot tool.
linuxrc can be used for an independent RAM drive based rescue system.
An installation process can be controlled by linuxrc.
Once the initial root filesystem is mounted, if executable file /linuxrc is present, the kernel executes it as its first process. When /linuxrc exits, the kernel assumes that it has mounted the real root filesystem and executes /sbin/init to begin the normal boot process.
The file /linuxrc can be any valid executable, including an executable program, symbolic link or shell script.
initramfs was introduced in Linux kernel 2.6.13.
A RAM-based filesystem known as tmpfs or shmfs is a standard component of the kernel. This system is in many ways much more flexible and efficient than the original fixed-size ramdisk: it does not require formatting, and uses as little or as much memory as is required to hold the data. Another similar system, ramfs, is available, and users may choose which particular dynamic RAM filesystem to use.
Along with the code for tmpfs, which is used in the kernel of nearly all Linux configurations, the only requirement to make a suitable virtual drive for booting to is the ability to decompress an archive of files. Files are compressed using the cpio archive format and the decompressed files are stored to a tmpfs-like filesystem. This system is known as initramfs.
The kernel image and an image of the initial root filesystem must be stored somewhere accessible by the boot firmware of the computer or the bootloader. This is usually one of the following:
The bootloader loads the kernel and initial root filesystem image into memory and then starts 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:
If the image is a (optionally gzip-compressed) filesystem image, it is made available as a special block device (/dev/ram), which is then mounted as the initial root filesystem. The driver for the filesystem must be compiled statically into the kernel. Filesystems used as initrd images include compressed ext2 and cramfs, which is used on memory-limited systems since the cramfs image can be mounted in-place without requiring extra space for decompression.
Once the initial root filesystem is mounted, the kernel executes /linuxrc (linux run command) as its first process. When /linuxrc exits, the kernel assumes that /linuxrc has mounted the real root filesystem and executes /sbin/init to begin the normal boot process.
If the image is a gzip-compressed cpio archive, it is unpacked by the kernel in an intermediate step into an initramfs, which then becomes the initial root filesystem.
On an initramfs, the kernel executes /init as its first process. /init is not expected to exit.
The normal root filesystem cannot simply be mounted over /, since that would make the scripts and tools on the initial root filesystem inaccessible for any final cleanup tasks:
On an initrd, it is mounted at a temporary mount point and rotated into place with pivot_root. This leaves the initial root filesystem at a mount point where normal boot scripts can later unmount it to free up memory held by the initrd.
On an initramfs, the initial root filesystem cannot be rotated away. Instead, it is simply emptied and the final root filesystem mounted over the top.
Most initial root filesystems implement /linuxrc or /init as a shell script and thus include a minimal shell along with some essential utilities.
example | initrd: initial ramdisk |
home | Home Page |