What is initrd img in linux

What is initrd img in linux

The initrd is the «initial ramdisk». It is enough files stored in a ramdisk to store needed drivers . You need the drivers so that the kernel can mount / and kick off init.

You can avoid this if you build your scsi drivers right into the kernel, instead of into modules. (Many persons recommend this).

The mkinitrd utility creates an initrd image in a single command. This is command is peculiar to RedHat. There may be equivalent command of mkinitrd in other distributions of Linux. This is very convenient utility.

You can read the mkinitrd man page.

See the following sections for the manual method of creating an initrd image.

To create /boot/initrd.img see the documentation at /usr/src/linux/Documentation/initrd.txt and see also Loopback-Root-mini-HOWTO.

SCSI Installation Complications and initrd

Some of the following descriptions may be difficult to understand without knowledge of kernel modules explained in Chapter 42. You may want to come back to it later.

Consider a system with zero IDE disks and one SCSI disk containing a LINUX installation. There are BIOS interrupts to read the SCSI disk, just as there were for the IDE, so LILO can happily access a kernel image somewhere inside the SCSI partition. However, the kernel is going to be lost without a kernel module [See Chapter 42. The kernel doesn’t support every possible kind of hardware out there all by itself. It is actually divided into a main part (the kernel image discussed in this chapter) and hundreds of modules (loadable parts that reside in /lib/modules/) that support the many type of SCSI, network, sound etc., peripheral devices.] that understands the particular SCSI driver. So although the kernel can load and execute, it won’t be able to mount its root file system without loading a SCSI module first. But the module itself resides in the root file system in /lib/modules/. This is a tricky situation to solve and is done in one of two ways: either (a) using a kernel with preenabled SCSI support or (b) using what is known as an initrd preliminary root file system image.

The first method is what I recommend. It’s a straightforward (though time-consuming) procedure to create a kernel with SCSI support for your SCSI card built-in (and not in a separate module). Built-in SCSI and network drivers will also autodetect cards most of the time, allowing immediate access to the device—they will work without being given any options [Discussed in Chapter 42.] and, most importantly, without your having to read up on how to configure them. This setup is known as compiled-in support for a hardware driver (as opposed to module support for the driver). The resulting kernel image will be larger by an amount equal to the size of module. Chapter 42 discusses such kernel compiles.

The second method is faster but trickier. LINUX supports what is known as an initrd image ( initial rAM disk image). This is a small, +1.5 megabyte file system that is loaded by LILO and mounted by the kernel instead of the real file system. The kernel mounts this file system as a RAM disk, executes the file /linuxrc, and then only mounts the real file system.

31.6 Creating an initrd Image

Start by creating a small file system. Make a directory /initrd and copy the following files into it.

On my system, the file initrd/bin/insmod is the statically linked [meaning it does not require shared libraries.] version copied from /sbin/insmod.static—a member of the modutils-2.3.13 package. initrd/bin/sash is a statically linked shell from the sash-3.4 package. You can recompile insmod from source if you don’t have a statically linked version. Alternatively, copy the needed DLLs from /lib/ to initrd/lib/. (You can get the list of required DLLs by running ldd /sbin/insmod. Don’t forget to also copy symlinks and run strip -s to reduce the size of the DLLs.)

Now copy into the initrd/lib/ directory the SCSI modules you require. For example, if we have an Adaptec AIC-7850 SCSI adapter, we would require the aic7xxx.o module from /lib/modules//scsi/aic7xxx.o. Then, place it in the initrd/lib/ directory.

Читайте также:  Как установить pycrypto для windows

The file initrd/linuxrc should contain a script to load all the modules needed for the kernel to access the SCSI partition. In this case, just the aic7xxx module [ insmod can take options such as the IRQ and IO-port for the device. See Chapter 42.]:

Now double-check all your permissions and then chroot to the file system for testing.

Now, create a file system image similar to that in Section 19.9:

Finally, gzip the file system to an appropriately named file:

31.7 Modifying lilo.conf for initrd

Your lilo.conf file can be changed slightly to force use of an initrd file system. Simply add the initrd option. For example:

Notice the use of the linear option. This is a BIOS trick that you can read about in lilo(5). It is often necessary but can make SCSI disks nonportable to different BIOSs (meaning that you will have to rerun lilo if you move the disk to a different computer).

Источник

Using the initial RAM disk (initrd)В¶

initrd provides the capability to load a RAM disk by the boot loader. This RAM disk can then be mounted as the root file system and programs can be run from it. Afterwards, a new root file system can be mounted from a different device. The previous root (from initrd) is then moved to a directory and can be subsequently unmounted.

initrd is mainly designed to allow system startup to occur in two phases, where the kernel comes up with a minimum set of compiled-in drivers, and where additional modules are loaded from initrd.

This document gives a brief overview of the use of initrd. A more detailed discussion of the boot process can be found in 1.

OperationВ¶

When using initrd, the system typically boots as follows:

the boot loader loads the kernel and the initial RAM disk

the kernel converts initrd into a “normal” RAM disk and frees the memory used by initrd

if the root device is not /dev/ram0 , the old (deprecated) change_root procedure is followed. see the “Obsolete root change mechanism” section below.

root device is mounted. if it is /dev/ram0 , the initrd image is then mounted as root

/sbin/init is executed (this can be any valid executable, including shell scripts; it is run with uid 0 and can do basically everything init can do).

init mounts the “real” root file system

init places the root file system at the root directory using the pivot_root system call

init execs the /sbin/init on the new root filesystem, performing the usual boot sequence

the initrd file system is removed

Note that changing the root directory does not involve unmounting it. It is therefore possible to leave processes running on initrd during that procedure. Also note that file systems mounted under initrd continue to be accessible.

Boot command-line optionsВ¶

initrd adds the following new options:

Compressed cpio imagesВ¶

Recent kernels have support for populating a ramdisk from a compressed cpio archive. On such systems, the creation of a ramdisk image doesn’t need to involve special block devices or loopbacks; you merely create a directory on disk with the desired initrd content, cd to that directory, and run (as an example):

Examining the contents of an existing image file is just as simple:

InstallationВ¶

First, a directory for the initrd file system has to be created on the “normal” root file system, e.g.:

The name is not relevant. More details can be found on the pivot_root(2) man page.

If the root file system is created during the boot procedure (i.e. if you’re building an install floppy), the root file system creation procedure should create the /initrd directory.

If initrd will not be mounted in some cases, its content is still accessible if the following device has been created:

Second, the kernel has to be compiled with RAM disk support and with support for the initial RAM disk enabled. Also, at least all components needed to execute programs from initrd (e.g. executable format and file system) must be compiled into the kernel.

Third, you have to create the RAM disk image. This is done by creating a file system on a block device, copying files to it as needed, and then copying the content of the block device to the initrd file. With recent kernels, at least three types of devices are suitable for that:

Читайте также:  What is ssh tunneling linux

a floppy disk (works everywhere but it’s painfully slow)

a RAM disk (fast, but allocates physical memory)

a loopback device (the most elegant solution)

We’ll describe the loopback device method:

make sure loopback block devices are configured into the kernel

create an empty file system of the appropriate size, e.g.:

(if space is critical, you may want to use the Minix FS instead of Ext2)

mount the file system, e.g.:

create the console device:

copy all the files that are needed to properly use the initrd environment. Don’t forget the most important file, /sbin/init

/sbin/init permissions must include “x” (execute).

correct operation the initrd environment can frequently be tested even without rebooting with the command:

This is of course limited to initrds that do not interfere with the general system state (e.g. by reconfiguring network interfaces, overwriting mounted devices, trying to start already running demons, etc. Note however that it is usually possible to use pivot_root in such a chroot’ed initrd environment.)

unmount the file system:

the initrd is now in the file “initrd”. Optionally, it can now be compressed:

For experimenting with initrd, you may want to take a rescue floppy and only add a symbolic link from /sbin/init to /bin/sh . Alternatively, you can try the experimental newlib environment 2 to create a small initrd.

Finally, you have to boot the kernel and load initrd. Almost all Linux boot loaders support initrd. Since the boot process is still compatible with an older mechanism, the following boot command line parameters have to be given:

(rw is only necessary if writing to the initrd file system.)

With LOADLIN, you simply execute:

With LILO, you add the option INITRD=

to either the global section or to the section of the respective kernel in /etc/lilo.conf , and pass the options using APPEND, e.g.:

and run /sbin/lilo

For other boot loaders, please refer to the respective documentation.

Now you can boot and enjoy using initrd.

Changing the root deviceВ¶

When finished with its duties, init typically changes the root device and proceeds with starting the Linux system on the “real” root device.

The procedure involves the following steps:

mounting the new root file system

turning it into the root file system

removing all accesses to the old (initrd) root file system

unmounting the initrd file system and de-allocating the RAM disk

Mounting the new root file system is easy: it just needs to be mounted on a directory under the current root. Example:

The root change is accomplished with the pivot_root system call, which is also available via the pivot_root utility (see pivot_root(8) man page; pivot_root is distributed with util-linux version 2.10h or higher 3). pivot_root moves the current root to a directory under the new root, and puts the new root at its place. The directory for the old root must exist before calling pivot_root . Example:

Now, the init process may still access the old root via its executable, shared libraries, standard input/output/error, and its current root directory. All these references are dropped by the following command:

Where what-follows is a program under the new root, e.g. /sbin/init If the new root file system will be used with udev and has no valid /dev directory, udev must be initialized before invoking chroot in order to provide /dev/console .

Note: implementation details of pivot_root may change with time. In order to ensure compatibility, the following points should be observed:

before calling pivot_root, the current directory of the invoking process should point to the new root directory

use . as the first argument, and the _relative_ path of the directory for the old root as the second argument

a chroot program must be available under the old and the new root

chroot to the new root afterwards

use relative paths for dev/console in the exec command

Now, the initrd can be unmounted and the memory allocated by the RAM disk can be freed:

It is also possible to use initrd with an NFS-mounted root, see the pivot_root(8) man page for details.

Usage scenariosВ¶

The main motivation for implementing initrd was to allow for modular kernel configuration at system installation. The procedure would work as follows:

Читайте также:  Посмотреть пользователей системы линукс

system boots from floppy or other media with a minimal kernel (e.g. support for RAM disks, initrd, a.out, and the Ext2 FS) and loads initrd

/sbin/init determines what is needed to (1) mount the “real” root FS (i.e. device type, device drivers, file system) and (2) the distribution media (e.g. CD-ROM, network, tape, …). This can be done by asking the user, by auto-probing, or by using a hybrid approach.

/sbin/init loads the necessary kernel modules

/sbin/init creates and populates the root file system (this doesn’t have to be a very usable system yet)

/sbin/init invokes pivot_root to change the root file system and execs — via chroot — a program that continues the installation

the boot loader is installed

the boot loader is configured to load an initrd with the set of modules that was used to bring up the system (e.g. /initrd can be modified, then unmounted, and finally, the image is written from /dev/ram0 or /dev/rd/0 to a file)

now the system is bootable and additional installation tasks can be performed

The key role of initrd here is to re-use the configuration data during normal system operation without requiring the use of a bloated “generic” kernel or re-compiling or re-linking the kernel.

A second scenario is for installations where Linux runs on systems with different hardware configurations in a single administrative domain. In such cases, it is desirable to generate only a small set of kernels (ideally only one) and to keep the system-specific part of configuration information as small as possible. In this case, a common initrd could be generated with all the necessary modules. Then, only /sbin/init or a file read by it would have to be different.

A third scenario is more convenient recovery disks, because information like the location of the root FS partition doesn’t have to be provided at boot time, but the system loaded from initrd can invoke a user-friendly dialog and it can also perform some sanity checks (or even some form of auto-detection).

Last not least, CD-ROM distributors may use it for better installation from CD, e.g. by using a boot floppy and bootstrapping a bigger RAM disk via initrd from CD; or by booting via a loader like LOADLIN or directly from the CD-ROM, and loading the RAM disk from CD without need of floppies.

Obsolete root change mechanismВ¶

The following mechanism was used before the introduction of pivot_root. Current kernels still support it, but you should _not_ rely on its continued availability.

It works by mounting the “real” root device (i.e. the one set with rdev in the kernel image or with root=… at the boot command line) as the root file system when linuxrc exits. The initrd file system is then unmounted, or, if it is still busy, moved to a directory /initrd , if such a directory exists on the new root file system.

In order to use this mechanism, you do not have to specify the boot command options root, init, or rw. (If specified, they will affect the real root file system, not the initrd environment.)

If /proc is mounted, the “real” root device can be changed from within linuxrc by writing the number of the new root FS device to the special file /proc/sys/kernel/real-root-dev, e.g.:

Note that the mechanism is incompatible with NFS and similar file systems.

This old, deprecated mechanism is commonly called change_root , while the new, supported mechanism is called pivot_root .

Mixed change_root and pivot_root mechanismВ¶

In case you did not want to use root=/dev/ram0 to trigger the pivot_root mechanism, you may create both /linuxrc and /sbin/init in your initrd image.

/linuxrc would contain only the following:

Once linuxrc exited, the kernel would mount again your initrd as root, this time executing /sbin/init . Again, it would be the duty of this init to build the right environment (maybe using the root= device passed on the cmdline) before the final execution of the real /sbin/init .

ResourcesВ¶

Almesberger, Werner; “Booting Linux: The History and the Future” https://www.almesberger.net/cv/papers/ols2k-9.ps.gz

newlib package (experimental), with initrd example https://www.sourceware.org/newlib/

© Copyright The kernel development community.

Источник

Оцените статью