initrd: Initial Ramdisk

Introduction

On power-up, the first software run from secondary storage is the bootloader, which prepares an environment for the operating system and then runs the kernel. The operating system's environment includes the root filesystem, which must be available before the kernel is started.

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 the initial ramdisk, or initrd. Device node /dev/initrd is a read-only RAM drive initialised by the bootloader before the kernel is started. The kernel can then mount an initial root filesystem from the contents of /dev/initrd.

Implementation

If an initial root filesystem is used, 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 often a small ext2 or FAT-formatted volume in a data storage device (a boot volume).

The bootloader loads the kernel and initial root filesystem image into memory and then starts the kernel, passing in the memory address of the initial root filesystem image. At the end of its boot sequence, the kernel makes the image available as a special block device which is then mounted as the initial root filesystem.

Once the initial root filesystem is mounted, the kernel executes /linuxrc (linux run command) as its first process.

This article continues from the initial bootloader article and explains how to load an initial root filesystem and pass control from the bootloader to the Linux kernel. The first process started by Linux is linuxrc, and this article demonstrates use of a linuxrc shell script. Finally the article demonstrates use of a GRUB menu. Booting from flash drive and floppy disk are both discussed. A flash drive has enough storage to hold the bootloader, kernel and initrd image in its boot volume; a floppy disk, on the other hand, does not and two floppy disks are required. Throughout the article, the terms single boot medium and twin boot media are used to identify scenarios where one or two recording media are required.

Once the bootloader is running, the startup process discussed in this article performs the following operations:

1. 

The bootloader loads the kernel program into primary storage from file /boot/bzImage in the boot volume or kernel volume.

2. 

The bootloader loads an initrd image containing the root filesystem into memory from file /boot/initrd.gz in the boot volume.

3. 

The kernel program is started and the operating system is booted.

4. 

The kernel executes linuxrc as its first process.

5. 

linuxrc starts the Unix shell.

Concepts

initial root filesystem 

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.

initrd 

The initial ramdisk, or initrd, is a file containing an initial root filesystem.

The special device node /dev/initrd accesses a read-only block device. The device is a RAM drive initialised by the bootloader before the kernel is started. The kernel can then mount an initial root filesystem from the contents of /dev/initrd.

linuxrc 

linuxrc (linux run command) is the first process executed after initrd is mounted.

Once the initial root filesystem is mounted, if executable file /linuxrc is present, the kernel executes it as its first process.

File /linuxrc can be any valid executable, including an executable program, symbolic link or shell script.

root volume 

The root volume is the volume that contains the root filesystem.

The root filesystem is the filesystem containing the root directory (/).

boot volume 

The boot volume is the volume that contains the bootloader.

kernel volume 

The kernel volume is the volume that contains the kernel image.

Prerequisites

The following article must have been completed:

  • initial bootloader: revision 1 or later

If deploying to twin boot media, an additional recording medium will be required. For example, if deploying to floppy disk media, a floppy disk will be required in addition to the one used in the initial bootloader article.

Download

Right-click here and select save. This will save a file named initrd.tar.gz. Note the directory initrd.tar.gz is downloaded to: this will be referred to as the download directory ([download]).

Installation

1. 

If twin boot media are used, label boot media.

1. 

The recording medium created in the initial bootloader article should already be labelled boot; if not, write “boot” on a label and attach it to the recording medium created in the initial bootloader article. This recording medium will be referred to as the boot disk.

2. 

Write “kernel” on a label and attach it to the additional recording medium. This recording medium will be referred to as the kernel disk.

2. 

Collect information.

1. 

Note the target device ([target]).

Note the drive letter used to represent the drive that will contain the target recording medium. The drive letter is referred to later in this article as the target device.

If the target device is not permanently connected to the computer (for example, if it is connected via USB), the drive letter may be different the next time the computer is switched on. For this reason it is important to note the target device each time the process in this article is followed.

2. 

If deploying to twin boot media, note whether the additional recording medium has been formatted ([format]).

If the additional recording medium has not been formatted, the value of [format] is format; otherwise the value is noformat.

Note that as well as being formatted, the additional recording medium must be empty - it must contain no files or directories.

If in doubt, assume that the recording medium has not been formatted ([format] = format).

3. 

Create sources directory ([sources]).

1. 

Create a directory from which the installation will be performed. For example, a suitable directory could be c:\Unix\source. This directory will be referred to as the sources directory.

4. 

Unpack sources.

Start a command prompt and type the following commands. (Do not type the > character - it is the command prompt.)

1. 


> copy [download]\initrd.tar.gz [sources]

Copy the sources from the download directory to the sources directory.

When typing the command replace [download] with the path name of the download directory including the drive letter and replace [sources] with the path name of the sources directory including the drive letter. For example, if initrd.tar.gz was downloaded to d:\download and the sources directory is c:\Unix\source, type:


> copy d:\download\initrd.tar.gz c:\Unix\source

2. 


> [sources drive]:

Change to the drive containing the sources directory.

When typing the command replace [sources drive] with the drive letter of the drive containing the sources directory. For example if the sources directory is c:\Unix\source, type:


> c:

3. 


> cd [sources]

Change to the sources directory.

When typing the command replace [sources] with the path name of the sources directory excluding the drive letter. For example if the sources directory is c:\Unix\source, type:


> cd \Unix\source

4. 


> gzip -d initrd.tar.gz

Decompress the compressed file.

5. 


> tar -xf initrd.tar

Extract the archive.

6. 


> cd initrd

5. 

Deploy.

1. 


> deploy [target] [switch] [format]

Deploy kernel and initrd image.

When typing the command replace [target], [switch] and [format] as follows:

  • Replace [target] with the target device.

  • If deploying to twin boot media, replace [switch] with switch; if deploying to a single boot medium replace [switch] with noswitch. For example, there is insufficient space on a single floppy disk to store all the required data, and it is therefore necessary to switch floppy disks during the boot process. This is indicated in the [switch] argument.

  • If deploying to twin boot media and the additional recording medium is unformatted or if it is not known whether the additional recording medium is formatted, replace [format] with format; otherwise replace [format] with noformat.

For example, to deploy to a flash drive represented by drive letter f with sufficient space for all data, type the following:


> deploy f noswitch noformat

To deploy to floppy disks represented by drive letter a with the second floppy disk requiring formatting, type the following:


> deploy a switch format

If formatting, during the deployment a prompt will be issued to insert a new disk and hit ENTER. This is part of the format process: just press the enter key. When asked whether to format another disk, respond by pressing the n and then the enter key.

6. 

Start Unix.

1. 

Reboot the computer with the device still attached and the recording medium still inserted if a removable medium (e.g. floppy disk) is being used.

2. 

If booting from twin boot media, after the grub> prompt is displayed, insert the kernel disk.

3. 

Load the kernel program by typing the following command. (Do not type the grub> command prompt.)


grub> kernel /boot/bzImage

4. 

If booting from twin boot media, insert the boot disk.

5. 

Load the initrd image by typing the following command.


grub> initrd /boot/initrd.gz

6. 

Start the kernel program and boot the operating system by typing the following command.


grub> boot

The kernel runs and displays messages as it performs its startup activities. After all startup activities are complete, the screen is similar to the following:


NET: Registered protocol family 10                                              

IPv6 over IPv4 tunneling driver                                                 

NET: Registered protocol family 17                                              

RPC: Registered udp transport module                                            

RPC: Registered tcp transport module                                            

Using IPI Shortcut mode                                                         

Synaptics Touchpad, model: 1, fw: 6.2, id: 0x1a0b1, caps: 0xa04713/0x200000     

input: SynPS/2 Synaptics TouchPad as /devices/platform/i8042/serio4/input/input0

input: AT Translated Set 2 keyboard as /devices/platform/i8042/serio0/input/inpu

t1                                                                              

RAMDISK: Compressed image found at block 0                                      

VFS: Mounted root (ext2 filesystem).                                            

sh: no job control in this shell                                                

sh-2.05b# scsi 2:0:0:0: Direct-Access     USB      2.0 Flash Drive  0.00 PQ: 0 A

NSI: 2                                                                          

sd 2:0:0:0: [sdb] 1967616 512-byte hardware sectors (1007 MB)                   

sd 2:0:0:0: [sdb] Write Protect is off                                          

sd 2:0:0:0: [sdb] Assuming drive cache: write through                           

sd 2:0:0:0: [sdb] 1967616 512-byte hardware sectors (1007 MB)                   

sd 2:0:0:0: [sdb] Write Protect is off                                          

sd 2:0:0:0: [sdb] Assuming drive cache: write through                           

 sdb: sdb1                                                                      

sd 2:0:0:0: [sdb] Attached SCSI removable disk                                  

                                                                                

sh-2.05b#                                                                       

The last activity is a check for attached USB devices. This check sometimes completes after the command prompt (#) has been displayed. If this happens, the display stops updating and the command prompt is not visible, giving the impression that the screen has frozen. Hit enter and the command prompt will be displayed again.

7. 

Copy GRUB menu.

Type the following commands. (Do not type the # character - it is the command prompt.)

1. 


# mount /dev/[source device] /mnt -n

It will be necessary to establish the source device. If there is only one hard drive in the computer and the hard drive has only one partition, the following command should be issued.


# mount /dev/sda1 /mnt -n

For the second partition in a second hard drive, [source device] is normally sdb2. It may be necessary to experiment with various sd?? options until the correct one is found. Command ls /mnt will display the root directory of the mounted volume.

2. 


# cp /mnt/[sources]/initrd/[menu] ./menu.lst

For example, if the sources directory is /Unix/source and a single boot medium is being used, issue the following command:


# cp /mnt/Unix/source/initrd/menu_noswitch.lst ./menu.lst

If the sources directory is /Unix/source and twin boot media are being used, issue the following command:


# cp /mnt/Unix/source/initrd/menu_switch.lst ./menu.lst

3. 


# umount /mnt

The following warning is displayed, but the device is still unmounted.


warning: can't open /etc/mtab: No such file or directory

4. 


# mount /dev/[destination device] /mnt -n

If the boot medium contains one partition (e.g. a flash drive) and is represented by sdb, issue the following command:


# mount /dev/sdb1 /mnt -n

If the boot medium is not partitioned (e.g. a floppy disk) and is represented by sdb, issue the following command:


# mount /dev/sdb /mnt -n

Again, some experimentation may be necessary.

5. 


# cp menu.lst /mnt/boot/grub

6. 


# umount /mnt

The following warning is displayed, but the device is still unmounted.


warning: can't open /etc/mtab: No such file or directory

8. 

Restart Unix.

1. 

Reboot the computer by pressing ctrl-alt-delete (press the ctrl, alt and delete keys at the same time).

2. 

The computer restarts and displays a GRUB menu.


                                                                                

    GRUB  version 0.93  (638K lower / 251392K upper memory)                     

                                                                                

 ┌───────────────────────────────────────────────────────────────────────────┐  

 │Unix                                                                       │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 │                                                                           │  

 └───────────────────────────────────────────────────────────────────────────┘  

     Use the ↑ and ↓ keys to select which entry is highlighted                  

     Press enter to boot the selected OS, 'e' to edit the                       

     commands before booting, or 'c' for a command-line                         

                                                                                

                                                                                

                                                                                

                                                                                

                                                                                

Select Unix from the menu.

If booting from twin boot media, the boot process will prompt for insertion of kernel and boot disks.

9. 

Start Windows.

10. 

Clean sources directory.

Start a command prompt and type the following commands.

1. 


> [sources drive]:

Change to the drive containing the sources directory.

When typing the command replace [sources drive] with the drive letter of the drive containing the sources directory. For example if the sources directory is c:\Unix\source, type:


> c:

2. 


> cd [sources]

Change to the sources directory.

When typing the command replace [sources] with the path name of the sources directory excluding the drive letter. For example if the sources directory is c:\Unix\source, type:


> cd \Unix\source

3. 


> del initrd.tar

4. 


> rd initrd /S

Terms

file 

A computer file is a resource for storing information that is available to a computer program and is usually based on some form of durable data storage.

Files stored in the newly created operating system are listed in the Further Comments section.

directory 

A file can be located in a directory. A directory can contain files and other directories, facilitating the existence of directory hierarchies. These other directories are known as subdirectories. Subdirectories can contain further files and directories and so on, constructing a tree-like structure in which one master directory, or root directory, can contain any number of levels of other directories and files.

Directories in the newly created operating system are listed in the Further Comments section.

symbolic link 

A symbolic link is a reference to another file. This special file is stored as a textual representation of the referenced file's path.

The /bin directory contains symbolic link sh. sh points to bash and enables the shell to be started with command sh as well as bash.

device node 

Device nodes are used to apply access rights and to direct operations to appropriate device drivers.

Device nodes are located in the /dev directory.

kernel 

The kernel is the central component of most operating systems. Its responsibilities include managing the system's resources (the communication between hardware and software components).

In Unix-like operating systems, the kernel is started by a bootloader.

process 

A process is an instance of a program that is being executed by a computer system. A program is just a passive collection of instructions, while a process is the actual execution of those instructions.

Under Linux, if file /linuxrc exists, it is executed as the first process.

shell 

A shell is a program that provides an interface for users. An operating system shell provides access to the services of an operating system kernel, and is employed to issue commands to the kernel.

In the startup process discussed in this article, the Unix shell is started by linuxrc.

Scripts

The following scripts are used:

deploy.bat

is run from the Windows command prompt to perform the installation.

linuxrc

is the first process executed after initrd is mounted.

menu_noswitch.lst

is the GRUB menu script for a single boot medium.

menu_switch.lst

is the GRUB menu script for twin boot media.

deploy.bat

The deploy.bat script is run from the Windows command prompt to perform the installation.

It accepts the target drive letter, whether to switch media during deployment and whether to format the second recording medium as arguments and copies the kernel and initrd images to the target recording media.

linuxrc

The linuxrc script is the first process executed after initrd is mounted.

This script is contained inside the initrd image and can only be examined after initrd is mounted.

The linuxrc script is used to start the Unix shell - a program that provides an interface for users. An operating system shell provides access to the services of an operating system kernel, and is employed to issue commands to the kernel.

menu_noswitch.lst

The menu_noswitch.lst script is the GRUB menu script for a single boot medium.

It is copied to the grub directory with the name menu.lst, where it controls the contents of the GRUB menu when the computer is started.

menu_switch.lst

The menu_switch.lst script is the GRUB menu script for twin boot media.

It is copied to the grub directory with the name menu.lst, where it controls the contents of the GRUB menu when the computer is started.

Further Comments

The boot volume used in this article is the FAT formatted volume with GRUB deployed created in the initial bootloader article. During this article, to construct a system to run Unix, two files, bzImage and initrd.gz, are copied to the boot volume and also a kernel volume if twin boot media are used, and to automate the boot process, the GRUB menu file, menu.lst, is also copied.

If Unix is booting from a single boot medium, files and directories in the boot volume are as follows:


\

└ boot   bzImage
  │      initrd.gz
  │      
  └ grub stage1
         stage2
         fat_stage1_5
         menu.lst

If twin boot media are used, files and directories are as above, except that file bzImage is located in the kernel volume.

Also, if the volume is formatted as FAT32, file fat_stage1_5 is included, but if it is formatted as FAT, file fat_stage1_5 is not included.

The filesystem in initrd image initrd.gz contains the following files and directories:


/     linuxrc

├ bin bash cp ls mount umount

│     sh -> bash

├ dev initrd null systty tty1 tty2 tty3 tty4 console

│     ram ram0 ram1 ram2 ram3

│     sda
│     sda1  sda2  sda3  sda4  sda5  sda6 sda7 sda8 sda9 sda10
│     sda11 sda12 sda13 sda14 sda15
│     sdb
│     sdb1  sdb2  sdb3  sdb4  sdb5  sdb6 sdb7 sdb8 sdb9 sdb10
│     sdb11 sdb12 sdb13 sdb14 sdb15
│     sdc
│     sdc1  sdc2  sdc3  sdc4  sdc5  sdc6 sdc7 sdc8 sdc9 sdc10
│     sdc11 sdc12 sdc13 sdc14 sdc15
│     sdd
│     sdd1  sdd2  sdd3  sdd4  sdd5  sdd6 sdd7 sdd8 sdd9 sdd10
│     sdd11 sdd12 sdd13 sdd14 sdd15

│     scd0 scd1 scd2 scd3

├ lib libdl-2.3.2.so
│     libc-2.3.2.so
│     ld-2.3.2.so
│     librt-2.3.2.so
│     libpthread-0.10.so
│     libncurses.so.5.3

│     libdl.so.2      -> libdl-2.3.2.so
│     libc.so.6       -> libc-2.3.2.so
│     ld-linux.so.2   -> ld-2.3.2.so
│     librt.so.1      -> librt-2.3.2.so
│     libpthread.so.0 -> libpthread-0.10.so
│     libncurses.so.5 -> libncurses.so.5.3

└ mnt

The root directory / contains one file, linuxrc, and the following subdirectories:

  • bin
  • dev
  • lib
  • mnt

linuxrc is the linux run command, or the first file executed after initrd is mounted.

The subdirectories are described below.

bin 

The bin directory contains the following executable files:

  • bash
  • cp
  • ls
  • mount
  • umount

bash 

is the shell that provides the command-line interface to the operating system.

cp 

is a command that facilitates copying of files.

ls 

is a command to list files and directories.

mount and umount 

are commands to mount and unmount volumes.

The directory also contains symbolic link sh. sh points to bash and enables the shell to be started with command sh as well as bash.

dev 

The dev directory contains device nodes required by the system.

Device nodes in the dev directory serve the following purposes:

initrd 

The initrd device node is a RAM disk initialised before the kernel is started that contains the initial root filesystem.

null 

The null device node provides access to the null device.

systty 

Device node systty is a symbolic link to the device that is used as the attached keyboard and monitor.

tty?

The tty device nodes support the terminal interfaces.

console

The console device node provides access to the system console.

ram, ram?

The ram device nodes make RAM disk devices available.

sd*

The sd device nodes provide access to drivers for SCSI disk drives.

scd?

The scd device nodes provide access to CD-ROM drivers.

lib 

The lib directory contains library files and symbolic links required by the executable files in the bin directory.

mnt 

The mnt directory is used as a mount point for mounting volumes.

Scripts

To understand the process followed in this article, it is worth examining the scripts.

The installation is performed by Windows script deploy.bat.

The following commands in the deploy.bat script are executed if a second recording medium is to be used:


format %1:      /v:kernel
mkdir  %1:\boot

These commands format the second recording medium and create a boot directory on it.

The following commands in the deploy.bat script copy the kernel and initrd images to the target media:


copy bzImage   %1:\boot
copy initrd.gz %1:\boot

The linuxrc script is used to start the Unix shell. Its text is as follows:



#!/bin/sh

# linuxrc

/bin/sh

Unix command /bin/sh in the linuxrc script starts the shell.

The following lines in the menu.lst scripts start Unix:


title Unix
  kernel /boot/bzImage
  initrd /boot/initrd.gz

These are the same commands as those issued to start Unix manually without the boot command, which is issued implicitly by GRUB.

In addition to the above commands, the menu.lst script created from menu_switch.lst contains the following commands to prompt the user to switch boot media:

pause  Insert kernel disk and hit any key.

pause  Insert boot disk and hit any key.


home Home Page