How to unpack/uncompress and repack/re-compress an initial ramdisk (initrd/initramfs) boot image file on RHEL 5,6 ?
Environment
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 5
Note: For RHEL7 and RHEL8, refer to How to extract the contents of initramfs image on RHEL7?
Issue
- How do I unpack or uncompress, and then repack or re-compress, an initrd or initramfs boot image file?
- How do I modify the contents of an initrd or initramfs?
- How do I view an initrd or initramfs?
Resolution
First, create a temporary work directory and switch into it. This will be the location where the initramfs/initrd contents will be viewed, edited, and re-compressed if required:
mkdir /tmp/initrd
cd /tmp/initrd
Identify compression format of the image
Use the file
command on the initramfs/initrd to identify the compression format:
file /boot/initramfs-$(uname -r).img
The $(uname -r)
will use the file for the current kernel version. You may also specify a specific file, such as:
file /boot/initramfs-2.6.32-754.el6.x86_64.img
The most common is a gzip-format image which displays as:
# file /boot/initramfs-$(uname -r).img
/boot/initramfs-2.6.32-754.el6.x86_64.img: gzip compressed data
However, there may also be an XZ/LZMA-format image which displays as:
# file /boot/initramfs-$(uname -r).img
/boot/initramfs-2.6.32-754.el6.x86_64.img: LZMA compressed data
Select the appropriate instructions below to extract or repack the correct image type for your system.
gzip format - Extract / Uncompress
Uncompress and extract the contents of the image in the /boot/
directory:
zcat /boot/initrd-$(uname -r).img | cpio -idmv
gzip format - Repack / Recompress
Still in the working directory, find all files and add them to a new boot image file:
find . | cpio -o -c -R root:root | gzip -9 > /boot/new.img
xz/LZMA format - Extract / Uncompress
Uncompress and extract the contents of the image in the /boot/
directory:
xz -dc < /boot/initrd-$(uname -r).img | cpio -idmv
xz/LZMA format - Repack / Recompress
Still in the working directory, find all files and add them to a new boot image file:
find . 2>/dev/null | cpio -o -c -R root:root | xz -9 --format=lzma > /boot/new.img
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.
Comments