The initial RAM disk (initrd) and initial RAM filesystem (initramfs) are crucial components in the Linux boot process. They contain a temporary root file system loaded into memory when the system boots, allowing the kernel to load necessary drivers and modules before the actual root file system becomes available. While initrd.img and initramfs.img serve similar purposes, initramfs is a successor to initrd and uses a more flexible format. This guide will walk you through the steps to extract, modify, and repackage an initrd, which can be useful for troubleshooting, customizing the boot process, or adding new drivers. Note that this applies to older systems, not just older RHEL versions.
In this tutorial you will learn:
- How to extract the contents of an initrd file using cpio
- How to modify the extracted contents
- How to repackage the modified contents into a new initrd file

| Category | Requirements, Conventions or Software Version Used |
|---|---|
| System | Debian/Ubuntu, Red Hat Enterprise Linux 6 or 5, or any older Linux distribution |
| Software | gzip, cpio, xz |
| Other | Root privileges for some commands |
| Conventions | # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Extracting and Repackaging initrd
Extracting and repackaging the initrd file involves a series of steps that require careful handling of the file system and the tools used. Below are detailed steps to guide you through the process. Note that these steps apply to older systems such as Red Hat Enterprise Linux 5 and 6, as well as other older Linux distributions. For RHEL 7 and 8, refer to the specific guides for those versions.
- Create a temporary working directory: First, create a temporary directory and switch into it. This will be the location where the initrd contents will be viewed, edited, and re-compressed if required.
$ mkdir /tmp/initrd $ cd /tmp/initrd
- Identify the compression format: Use the
filecommand on the initrd to identify the compression format.# file /boot/initrd-$(uname -r).img
This will output the format of the initrd. For example:
/boot/initrd-2.6.31-653.el6.x86_64.img: gzip compressed data
Or:
/boot/initrd-2.6.31-653.el6.x86_64.img: LZMA compressed data
- Extract the initrd file: Depending on the format identified, use the appropriate commands to extract the contents.For gzip compressed data:
# zcat /boot/initrd-$(uname -r).img | cpio -idmv
For LZMA compressed data:
# xz -dc < /boot/initrd-$(uname -r).img | cpio -idmv
This command extracts the contents of the initrd image into the current directory, preserving the file structure and permissions.
- Modify the extracted contents: Once the contents are extracted, you can modify them as needed. This could involve adding new drivers, scripts, or configuration files. For example, to add a new kernel module, you might need to create the necessary directories and place your files there:
# mkdir -p ./lib/modules/$(uname -r) # cp /path/to/module.ko ./lib/modules/$(uname -r)/
Ensure that any changes made maintain the correct permissions and file structure.
COMPARING BOOT FILES: INITRD.IMG VS INITRAMFS.IMG
initrd.imgandinitramfs.imgare both used during the Linux boot process to provide an initial root filesystem, but they differ significantly in their implementations and usage.initrd.img, short for “initial RAM disk,” is an older format that uses a file system image, requiring a loopback device for mounting and typically containing a minimal file system with necessary drivers and executables. In contrast,initramfs.img, or “initial RAM file system,” is a more modern approach that uses a compressed cpio archive, which the kernel directly unpacks into a temporary file system in memory. This method does not need a loopback device and offers greater flexibility and simplicity, makinginitramfs.imgthe preferred choice in most contemporary Linux distributions.
- Repackage the initrd file: After making the necessary modifications, you need to repackage the initrd file. Navigate to the directory containing the extracted contents and run the following command.For gzip format:
# find . | cpio -o -c -R root:root | gzip -9 > /boot/new.img
For LZMA format:
# find . 2>/dev/null | cpio -o -c -R root:root | xz -9 --format=lzma > /boot/new.img
This command creates a new initrd image from the modified contents and compresses it using the appropriate format.
- Update the bootloader configuration: Finally, update the bootloader configuration to use the new initrd file.
# update-grub
This command updates the GRUB bootloader configuration to include the new initrd file, ensuring that it will be used the next time the system boots.
Conclusion
By following these steps, you can successfully extract, modify, and repackage the initrd file in an older Linux system. This process can be particularly useful for customizing the boot process, adding new drivers, or troubleshooting boot-related issues. Always ensure you have a backup of the original initrd file and understand the changes you’re making to avoid any potential boot failures.