I am customizing an Ubuntu 20.04 ISO, in this process, I have to edit the files inside initrd file which is inside the casper directory Ubuntu20.04-ISO/casper/initrd

How to recompile this file type of CPIO-archive (application/x-cpio)

I am able to unpack its contents with below command.

unmkinitramfs initrd .

But I am not able to recompile it, How can I Achieve this?

Looking for a practical step by step answer, I mean copy pasting the initrd file to new directory and recompiling it and proven to boot with this newly created say myinitrd file.

  • Please don't post screenshots. Usually you would pack an initramfs with find . | cpio -oH | gzip > ../newinitramfs but there can be more than 1 archive in a given file.
    – icarus
    Jun 6 '20 at 6:53
  • The option -H requires an arguent (the archive type), where is this from? BTW: You could run star -t -print-artype < archive to find the archive type of the original archive.
    – schily
    Jun 6 '20 at 7:11
  • That comment is wrong. The problem with cpio is that there is no cpio archive format, but 4 distinct and incompatible formats. You need to know which format to create to be compatible.
    – schily
    Jun 6 '20 at 7:21
  • my goal is simle.. I have initrd file. I can extract the contents of it with the said command. I want to create a new initrd file with those extracted files.
    – UnKNOWn
    Jun 6 '20 at 7:22
  • You should install star. It is the oldest free tar implementation. Much older than the gtar clone implementation found on most linux distros.
    – schily
    Jun 6 '20 at 7:23

Depending on the targeted device/config initrd may have different format, you can check its format with:

binwalk ./initrd

According to your cross posts the required format for your initrd is cpio formatted with newc along with lzma compression, and thus a method of identification would be the following... note that this is just to explain how to identify an initrd:

binwalk initrd

# 0         0x0             LZMA compressed data

cp initrd compressedinit.xz
xz -d compressedinit.xz
binwalk compressedinit

# 0         0x0             ASCII cpio archive...
# 112       0x70            ASCII cpio archive...
# ...

This permit to identify the initrd as cpio formatted with newc along with lzma compression. Packing/Repacking an initrd depend on it's format. With binwalk, xz, gzip etc. you should be able to discover the used format and thus recreate it after extraction according to the used compression.

Note that cpio -H option determine the archive format, this can be one of the following crc newc odc bin ustar tar depending on the targeted format.

As your extracted initrd contains:

~/test$ ls
early  early2  main

You can use the following steps to unpack and repack it (source: your superuser's QA):

# Extract and prepare the working directories
# -------------------------------------------

mkdir /tmp/tmp/
mkdir /tmp/tmp/extracted/
cp initrd /tmp/tmp/
cd /tmp/tmp
unmkinitramfs initrd ./extracted
cd extracted

# Add the first microcode firmware
# --------------------------------

cd early
find . -print0 | cpio --null --create --format=newc > /tmp/tmp/newinitrd

# Add the second microcode firmware
# ---------------------------------

cd ../early2
find kernel -print0 | cpio --null --create --format=newc >> /tmp/tmp/newinitrd

# Add the ram fs file system
# --------------------------

cd ../main
find . | cpio --create --format=newc | xz --format=lzma >> /tmp/tmp/newinitrd

# Verify both initrds 
# -------------------

cd /tmp/tmp
binwalk initrd
binwalk newinitrd

Note that this method is adapted to your situation, on other case where an initrd is a cpio formatted with newc along with lzma compression we may use:

cd /extracted/initrd
find . | cpio -o -H newc | xz --format=lzma > ../newinitrd

cpio: option requires an argument -- 'H'

It really needs that -H newc option. I tried once without and it did not work. It works without compression.

  • I tried many ways.. I think a practical real step by step procedure only the solution. I mean any one successfully made initrd recently and successfully booted the live iso. superuser.com/q/1556241/976939
    – UnKNOWn
    Jun 8 '20 at 16:01
  • there is no mkinitramfs, besides the unmkinitramfs in Ubuntu? There is no Ubuntu Tag on your Q?
    – user373503
    Jun 11 '20 at 13:44
  • Ok, If i am not clear when you answered, I am sorry about that. I hope you read the Edited Question few hours ago. Thank You!!!
    – UnKNOWn
    Jun 11 '20 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.