CentOS / RHEL 5 : How to rebuild Initial Ramdisk Image

When adding new hardware to a system, or after changing configuration files that may be used earlier in the boot process, or when changing the options on a kernel module, it may be necessary to rebuild the initial ramdisk (also known as initrd or initramfs) to include the proper kernel modules, files, and configuration directives. In the examples below you will see the usage of $(uname -r), which is a way to pass the current kernel version into a command without actually typing it out.

Procedure

1. First lets backup the original Initial Ramdisk:

# cp /boot/initrd-`uname -r`.img /boot/initrd-`uname -r`.img.bak

2. Creating a new Initial Ramdisk ( after you have added the new Modules in /etc/modprobe.conf or /etc/modules.conf for Kernel 2.4):

# mkinitrd -f /boot/initrd-`uname -r`-new.img `uname -r`

You can see a new initial ramdisk file initrd-[kernel-version]-new.img has been created in /boot directory. mkinitrd parameters ( please see man mkinitrd for more details ):

# mkinitrd --help
usage: mkinitrd [--version] [--help] [-v] [-f] [--preload [module]]
       [--force-ide-probe] [--force-scsi-probe | --omit-scsi-modules]
       [--image-version] [--force-raid-probe | --omit-raid-modules]
       [--with=[module]] [--force-lvm-probe | --omit-lvm-modules]
       [--builtin=module] [--omit-dmraid] [--net-dev=interface]
       [--fstab=fstab] [--nocompress] [initrd-image] [kernel-version]

The most used parameters are:

-f Allows mkinitrd to overwrite an existing image file.
–preload=module – Load the module module in the initial ramdisk image. The module gets loaded before any SCSI modules which are specified in /etc/modprobe.conf. This option may be used as many times as necessary.
–with=module – Load the modules module in the initial ramdisk image. The module gets loaded after any SCSI modules which are specified in /etc/modprobe.conf. This option may be used as many times as necessary.

If you have chosen a new name for your Initial Ramdisk File, you have to add a new entry in /etc/grub.conf to be able to use it:

title Red Hat Enterprise Linux 5 (2.6.32-200.13.1.el5)
root (hd0,0)
kernel /vmlinuz-2.6.32-200.13.1.el5 ro root=LABEL=/ 
initrd /initrd-2.6.32-200.13.1.el5.img

After that, you will be able to choose the Entry “New initrd” in your Grub Menu when booting.

Working with Backups

As mentioned previously, it is recommended that you take a backup of the previous initrd in case something goes wrong with the new one. If required, it is possible to create a separate entry in /boot/grub/grub.conf for the backup initial ramdisk image, to conveniently choose the old version at boot time without needing to restore the backup. This example configuration allows selection of either the new or old initial ramdisk image from the grub menu :

title Red Hat Enterprise Linux 5 (2.6.32-200.13.1.el5)
root (hd0,0)
kernel /vmlinuz-2.6.32-200.13.1.el5 ro root=LABEL=/ 
initrd /initrd-2.6.32-200.13.1.el5.img
title Red Hat Enterprise Linux 5 w/ old initrd (2.6.32-200.13.1.el5)
root (hd0,0)
kernel /vmlinuz-2.6.32-200.13.1.el5 ro root=LABEL=/ 
initrd /initrd-2.6.32-200.13.1.el5.img.bak

Alternatively, you can enter edit-mode in grub if you need to choose the old initrd and did not make a separate entry in grub.conf before rebooting. To do so:

If grub is secured with a password, press p and enter the password
Use the arrow keys to highlight the entry for the kernel you wish to boot
Press e for edit
Highlight the initrd line and press e again
Change the path for the initrd to the backup copy you made (such as /initrd-2.6.32-200.13.1.el5.img.bak)
Press Enter to temporarily save the changes you have made
Press b for boot

Note: This procedure does not actually make any persistent change. At the next boot, the system will continue using the original grub.conf configuration unless it is updated.
Related Post