How to rebuild the initial ramdisk image in Red Hat Enterprise Linux
Environment
- Red Hat Enterprise Linux (RHEL) 3, 4, 5, 6, 7, 8, 9 and 10
- initrd / initramfs image
Issue
- How to rebuild the initial ramdisk image in Red Hat Enterprise Linux
- How to rebuild initial ram disk image in Red Hat Enterprise Linux
- How to remake or recreate the initrd or initramfs
- The
/etc/lvm/lvm.confand root file system resides on a logical volume. How to ensure the changes are applied upon rebooting? - The
/etc/multipath.confand my root file system resides on a multipath device. How to ensure the changes are applied upon rebooting? - The module options in
/etc/modprobe.confor/etc/modprobe.d/have been modified. How to ensure the changes are applied upon rebooting?
Resolution
When adding new hardware to a system, or after changing configuration files that may be used very early 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.
If you are adding a new module in the initrd, first follow the instructions in How can I ensure certain modules are included in the initrd or initramfs in RHEL?, or if it is a configuration change then make that change now.
Once the necessary modifications have been made, it is time to rebuild the initrd. This process differs based on the version of RHEL. In these examples 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. If you are working with a version of the kernel other than what is currently running, then replace $(uname -r) with the actual kernel version, such as 2.6.18-274.el5.
- Rebuilding the initramfs (RHEL 6, 7, 8, 9, 10)
- Checking initramfs (RHEL 8, 9, 10)
- Checking initramfs (RHEL 7)
- Rebuilding the initrd (RHEL 3, 4, 5)
- Working with backups (RHEL 3, 4, 5, 6)
1. Rebuilding the initramfs (RHEL 6, 7, 8, 9, 10)
If you are in a kernel version different to the initramfs you are building (REQUIRED if you are in Rescue Mode) you must specify the full kernel version, including architecture. It is also recommended you make a backup copy of the initramfs in case the new version has an unexpected problem:
Make backup
# cp /boot/initramfs-<kernelVersion>.img /boot/initramfs-<kernelVersion>.img.bak
Build Initramfs
# dracut -f /boot/initramfs-<kernelVersion>.img <kernelVersion>
IMPORTANT NOTE: Replace <kernelVersion> with the full version of the kernel you wish to rebuild. EXAMPLE: (for kernel version "4.18.0-305.19.1.el8_4.x86_64")
# cp /boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.img /boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.bak.$(date +%m-%d-%H%M%S).img
# dracut -f /boot/initramfs-4.18.0-305.19.1.el8_4.x86_64.img 4.18.0-305.19.1.el8_4.x86_64
NOTE: Common Shortcut -- If you are currently booted into the kernel version that matches the initramfs version you need to rebuild, then you may omit the kernel versions from the above dracut command and use the shortened commands EXACTLY as written below.Caution! YOU MUST BE 100% CERTAIN THAT YOU ARE BOOTED TO THE CORRECT VERSION OR YOU MAY CAUSE ADDITIONAL DAMAGE TO THE SYSTEM. IF YOU'RE NOT CERTAIN, THEN USE THE ABOVE METHOD.Make Backup
# cp /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).bak.$(date +%m-%d-%H%M%S).imgBuild Initramfs
# dracut -f -v
2. Checking initramfs (RHEL 8, 9, 10)
In RHEL8 and 9, be certain that the BLS configuration files in /boot/loader/entries includes the menu to the newly installed or created custom initramfs
# grep initrd /boot/loader/entries/*
/boot/loader/entries/f81f518abf514003b7afe1b89fd649fe-0-rescue.conf:initrd /initramfs-0-rescue-f81f518abf514003b7afe1b89fd649fe.img
/boot/loader/entries/f81f518abf514003b7afe1b89fd649fe-4.18.0-240.el8.x86_64.conf:initrd /initramfs-4.18.0-240.el8.x86_64.img $tuned_initrd
3. Checking initramfs (RHEL 7)
In RHEL 7, be certain that the /etc/grub2.cfg and /boot/grub2/grub.cfg includes the menu to the newly installed or created custom initramfs
# grep initrd /etc/grub2.cfg
initrd16 /initramfs-3.10.0-514.6.1.el7.x86_64.img
initrd16 /initramfs-3.10.0-514.el7.x86_64.img
initrd16 /initramfs-0-rescue-29579289bce743ebbf3d42aa22ebd5fe.img
# grep "menuentry " /boot/grub2/grub.cfg
menuentry 'Red Hat Enterprise Linux Server (3.10.0-514.6.1.el7.x86_64) ...'
menuentry 'Red Hat Enterprise Linux Server (3.10.0-514.el7.x86_64) ... '
menuentry 'Red Hat Enterprise Linux Server (0-rescue-29579289bce743ebbf3d42aa22ebd5fe) ...'
If the customized kernel menu entry does not appear in the grub configuration file(s), rebuild the grub menu. This rebuild is nominally performed by dracut, but can not be successfully completed in some corner cases.
How to create a custom menu entry in grub
Rebuild grub.cfg file after creating the custom menu entry:
-
On BIOS-based machines, issue the following command as root:
# grub2-mkconfig -o /boot/grub2/grub.cfg -
On UEFI-based machines, issue the following command as root:
# grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg
4. Rebuilding the initrd (RHEL 3, 4, 5)
It is recommended you make a backup copy of the initrd in case the new version has an unexpected problem:
# cp /boot/initrd-$(uname -r).img /boot/initrd-$(uname -r).bak.$(date +%m-%d-%H%M%S).img
Now build the initrd:
# mkinitrd -f -v /boot/initrd-$(uname -r).img $(uname -r)
- The
-vverbose flag causesmkinitrdto display the names of all the modules it is including in the initial ramdisk. - The
-foption will force an overwrite of any existing initial ramdisk image at the path you have specified
If you are in a kernel version different to the initrd you are building (including if you are in Rescue Mode) you must specify the full kernel version, without architecture:
# mkinitrd -f -v /boot/initrd-2.6.18-348.2.1.el5.img 2.6.18-348.2.1.el5
5. Working with backups (RHEL 3, 4, 5, 6)
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 desired, 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.18-274.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-274.el5 ro root=LABEL=/
initrd /initrd-2.6.18-274.el5.img
title Red Hat Enterprise Linux 5 w/ old initrd (2.6.18-274.el5)
root (hd0,0)
kernel /vmlinuz-2.6.18-274.el5 ro root=LABEL=/
initrd /initrd-2.6.18-274.el5.bak.MM-DD-HHMMSS.img
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.18-274.el5.bak.01-01-123456.img) - Press Enter to temporarily save the changes you have made
- Press b for boot
Note: This procedure does not actually make the change persistent. The next boot will continue to use the original grub.conf configuration unless it is updated.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.