How to extract/unpack/uncompress the contents of the initramfs image on RHEL7 and RHEL8? How do I list initramfs image file content? How do I modify the contents of an initrd or initramfs? How do I view an initrd or initramfs? How to fix cpio premature end of archive error. How to list initramfs content in RHEL 7 and RHEL 8 using lsinitrd.
You may observe error "cpio premature end of archive" while trying to extract initramfs. I got this error while trying to extract initramfs on my Red Hat Enterprise Linux 7 host but the same problem can also occur in RHEL 8 as in both the initramfs is available in ASCII format unlike old initramfs from RHEL 5 and 6 where the initramfs was in a different format.
With RHEL 7 the initramfs file now stores both CPU microcode and the initial boot image in the one "combined" image file. The CPU microcode is stored with CPIO compression, then the boot image is stored with its own separate compression.
In this article I will share different methods to extract initramfs image file in different variants of Red Hat Enterprise Linux and to fix cpio premature end of archive error. These commands can also be used in other Linux distributions if the file format of the initramfs is same as RHEL.
Identify compression format of the image
Before we extract initramfs it is important to identify the compression format of the image so that we can accordingly extract initramfs 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. If you wish to identify compression format of some other initramfs image then you can also give the full file name
# file /boot/initramfs-3.10.0-957.el7.x86_64.img
Possible initramfs compression format
Below are some of the possible initramfs image file format available over the various Red Hat generations.
# file /boot/initramfs-($uname -r).img /boot/initramfs-3.10.0-327.el7.x86_64.img: gzip compressed data, from Unix, last modified: Fri Oct 7 03:36:28 2016, max compression
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
Or ASCII format image which is displayed as:
# file /boot/initramfs-$(uname -r).img /boot/initramfs-4.18.0-80.el8.x86_64.img: ASCII cpio archive (SVR4 with no CRC)
Uncompress/Extract initramfs with Gzip format
Create a temporary directory where you wish to extract initramfs
# mkdir /tmp/initrd # cd /tmp/initrd
make sure gzip is installed on your setup or you can install it using yum
# yum install gzip
Next execute the below command to extract initramfs
# zcat /boot/initramfs-3.10.0-327.el7.x86_64.img | cpio -i --no-absolute-filenames 88386 blocks
Uncompress/Extract initramfs with xz/LZMA format
Again you can create a temporary directory where you wish to extraxt initramfs content
# mkdir /tmp/initrd # cd /tmp/initrd
To uncompress initramfs execute the below command
# xz -dc < /boot/initrd-$(uname -r).img | cpio -idmv
Fix "cpio premature end of archive" error
In RHEL6 and older, the contents of the initramfs or initrd image could be extracted using cpio as shown below:
# zcat initramfs-2.6.32-431.el6.x86_64.img | cpio -idmv
However, it does not work when I try to extract the contents of initramfs image on RHEL7 as shown below:
# zcat /boot/initrd-$(uname -r).img | cpio -idmv gzip: /boot/initrd-4.18.0-80.el8.x86_64.img.gz: No such file or directory cpio: premature end of archive
So we get cpio premature end of archive error with file format of initramfs is ASCII
# file /boot/initramfs-$(uname -r).img /boot/initramfs-4.18.0-80.el8.x86_64.img: ASCII cpio archive (SVR4 with no CRC)
To fix cpio premature end of archive error and to extract initramfs we must use a separate command to extract the initramfs content
First, create a temporary work directory and switch into it. This will be the location where the initramfs contents will be viewed:
# mkdir /tmp/initramfs # cd /tmp/initramfs
Uncompress and extract the contents of the image in the /boot/ directory:
# /usr/lib/dracut/skipcpio /boot/initramfs-$(uname -r).img | gunzip -c | cpio -idmv
To view the content of initramfs
# ls -l total 44 lrwxrwxrwx. 1 root root 7 Jul 14 09:48 bin -> usr/bin drwxr-xr-x. 2 root root 4096 Jul 14 09:48 dev drwxr-xr-x. 12 root root 4096 Jul 14 09:48 etc lrwxrwxrwx. 1 root root 23 Jul 14 09:48 init -> usr/lib/systemd/systemd lrwxrwxrwx. 1 root root 7 Jul 14 09:48 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Jul 14 09:48 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 4096 Jan 15 22:19 proc drwxr-xr-x. 2 root root 4096 Jan 15 22:19 root drwxr-xr-x. 2 root root 4096 Jan 15 22:19 run lrwxrwxrwx. 1 root root 8 Jul 14 09:48 sbin -> usr/sbin -rwxr-xr-x. 1 root root 3126 Oct 8 2018 shutdown drwxr-xr-x. 2 root root 4096 Jan 15 22:19 sys drwxr-xr-x. 2 root root 4096 Jan 15 22:19 sysroot drwxr-xr-x. 2 root root 4096 Jan 15 22:19 tmp drwxr-xr-x. 7 root root 4096 Jul 14 09:48 usr drwxr-xr-x. 3 root root 4096 Jul 14 09:48 var
When viewing this combined image file on a booted system, it is necessary to use skipcpio to "skip past" the CPIO-compressed CPU microcode to access the boot image.
List initramfs content
You can also list initramfs content without actually extracting it, assuming your requirement is to only view the content of initramfs image file. This can be achieved using lsinitrd as shown below:
# lsinitrd /boot/initramfs-4.18.0-80.el8.x86_64.img
Lastly I hope the steps from the article to fix cpio premature end of archive, List initramfs using lsinitrd & Extract initramfs in RHEL Linux was helpful. So, let me know your suggestions and feedback using the comment section.