≡ Menu

How to View, Modify and Recreate initrd.img

Question: How do I view, modify and recreate the new initrd.img on Ubuntu, Debian, CentOS, Fedora, Red-Hat, Arch Linux, or SUSE distributions?

1. How To View Content Of initrd.img file?

initrd.img is in gzip format.  So move initrd.img to initrd.gz as shown below.

# cp /tftpboot/el5/initrd.img  .

# ls
cdrom   initrd.img

# mv initrd.img initrd.gz

Unzip the initrd.gz file as shown below.

# gunzip initrd.gz

# ls
cdrom  initrd

After unziping the initrd.gz file, the initrd is further in cpio ‘newc’ format. So extract the files from initrd using cpio ‘newc’ format as shown below.
Note: info cpio will give more information about ‘newc’ format.

# mkdir tmp2

# cd tmp2/

# cpio -id < ../initrd
16524 blocks

Now you can view the content of initrd.img file

# ls
bin  dev  etc  init  modules proc  sbin  selinux  sys  tmp  var

2. How To Modify Content of Image and Recreate New Image?

After extracting the file as shown below, make appropriate modification to any of those files. Then pack the files back into the archive using the following commands. Pack the modified files back to cpio ‘newc’ format.

# find . | cpio --create --format='newc' > /tmp/newinitrd
16524 blocks

# ls /tmp/
cdrom  initrd  newinitrd  tmp2

# ls -l /tmp/newinitrd
-rw-r--r-- 1 root root 8460288 Jul  2 14:50 /tmp/newinitrd

Gzip the archive file.

# gzip newinitrd

# ls
cdrom  initrd  newinitrd.gz  tmp2

# ls -l newinitrd.gz
-rw-r--r--  1 root root 6649867 Jul  2 14:50 newinitrd.gz

Move file as an image file. You can use the newinitrd.img as your new boot image.

# mv newinitrd.gz newinitrd.img

# ls -l newinitrd.img
-rw-r--r-- 1 root root 6649867 Jul  2 14:50 newinitrd.img
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Lakshmanan July 10, 2009, 3:06 am

    Nice one…

  • runlevel0 July 10, 2009, 8:23 am

    Neat!

    I have always done my initrd’s using Debian’s make-kpgk utils… but this hack is real neat!

    Kudos for your site!

  • beparas July 10, 2009, 11:15 pm

    This is very useful.

  • Ramesh Natarajan July 14, 2009, 10:13 pm

    @Lakshmanan, @beparas, Thanks for your comments. I’m very glad that you found this helpful.

    @runlevel0, Building kernel using make-kpkg by installing ‘kernel-package’ and ‘fakeroot’ packages definitely has it’s own advantages. Thanks for bringing make-kpkg utility to our attention.

  • lenny September 7, 2009, 1:14 am

    hello!
    I want to know if I recreate new image file after modify something.using:
    dd if=tmp2 of=newinitrd.img
    I want to know It’s right or wrong?why?

  • Balakrishnan Mariyappan June 7, 2010, 1:14 pm

    very good article..
    thanks.

  • greyzlii August 12, 2010, 6:14 am

    Thank you so much for this article !

  • Coogrrr October 8, 2010, 9:02 am

    Thank you soo much!!!

    This was the ticket I could modify the grub and logon screens but the boot splash that was the hard one. Not any more! I have my own theme from end to end now!

    YOU ROCK!!!!!

    Coog

  • CharlesS January 20, 2011, 11:50 am

    Thanks for the article. I tend to have a use for this about once or twice a year.

    The extraction can be merged into a single command, simplifying this a bit:

    mkdir /tmp/initrd; cd /tmp/initrd
    gunzip -c ../newinitrd.img

  • David November 18, 2011, 1:54 pm

    Hi fellas, just wanted to say I appreciate this post. I was able to prevent the loading of modules I didn’t want by changing the file under /conf in the archive. I found it easier to copy the thing to my home directory and then do all the stuff. About to see if I can use XZ to compress the image and make it take up less space ( I know it’s pointless but I don’t care 🙂 ). Thank you for the post. I now have more useful tools 🙂
    -Dave

  • Petri Asikainen February 21, 2012, 2:30 am

    Thanks! This saved my day.
    Just fixed one virtual machine with this information. Copied initrd from other virtual machine and then edited initrd to fit in vm configuration. Needed to edit run_all.sh and config/* scripts and replace rootdevice information and filesystem type and added correct fsck.

  • Deepak Mohanty March 16, 2012, 2:41 am

    Thanks. I wanted to inspect the contents of an initrd image and I found most of the articles on the web misleading / partially correct. Most of them either assume the image to be a gzipped image, or a cpio file or a plain image. The fact is that (at least on my Linux distro), it is a gzipped cpio archive. Your article was helpful.

  • Deepak Mohanty March 16, 2012, 2:44 am

    Another way I could mount the image is using sysfs

    ex.: mount -t sysfs initrd-2.6.18-1.2798.fc6.img drtini

  • Deepak Mohanty March 19, 2012, 10:57 am

    A lot of information about initrd/initramfs is available at http://en.wikipedia.org/wiki/Initrd. This article explains the old initrd format (compressed image) and the newer initramfs format (cpio archive – optionally compressed).

  • Aniket July 24, 2012, 7:30 pm

    Nice one 🙂

  • Vipul September 3, 2013, 1:59 am

    Hello everybody,
    can anyone tell me how to modify the boot image and resave the cpio file
    for changing the plymouth theme (custom) in rhel6

  • Chris September 11, 2013, 4:09 am

    Modern linuxes (eg: CentOS 6.4) use a new compression format – here’s how I just added a new driver module:

    [root@z600 ~]# mkdir tmo
    [root@z600 ~]# cd tmo
    [root@z600 tmo]# cat /media/LIVE/syslinux/initrd.img | xz –format=lzma –decompress –stdout | cpio -id
    147921 blocks
    [root@z600 tmo]# cp /media/LIVE/ocz10xx.ko modules/2.6.32-358.el6.x86_64/extra/
    [root@z600 tmo]# find . | cpio –create –format=’newc’ | xz –format=lzma –compress –stdout >/tmp/newinitrd.img
    153313 blocks
    [root@z600 tmo]# ls -lsa /media/LIVE/syslinux/initrd.img /media/LIVE/ocz10xx.ko /tmp/newinitrd.img
    2696 -rwxr-xr-x. 1 cnd cnd 2760604 Dec 11 2012 /media/LIVE/ocz10xx.ko
    31732 -rwxr-xr-x. 1 cnd cnd 32491856 Sep 11 19:13 /media/LIVE/syslinux/initrd.img
    32764 -rw-r–r–. 1 root root 33546904 Sep 11 20:05 /tmp/newinitrd.img
    [root@z600 tmo]# cp /media/LIVE/syslinux/initrd.img /tmp/
    [root@z600 tmo]# cp /tmp/newinitrd.img /media/LIVE/syslinux/initrd.img
    cp: overwrite `/media/LIVE/syslinux/initrd.img’? y
    [root@z600 tmo]# reboot

  • sanjeev September 16, 2013, 5:55 am

    I am not getting the dev directory after cpio option
    # cpio -id < ../initrd
    92370 blocks
    when Iview the content of initrd.img file

    # ls
    bin dev(missing) etc init modules proc sbin selinux sys tmp var(missing)

    also I am getting cpio : cannot symlink to Readonly File System

  • Vipul September 20, 2013, 12:43 am

    can anyone tell me how to modify the boot image and resave the cpio file
    for changing the plymouth theme (custom) in rhel6..
    thanks in advance

  • Tonza October 24, 2014, 9:04 am

    Hello, I can’t see the root filesystem but I only see this:
    [code]
    $mkdir tmp2
    $cd tmp2
    $cpio -id < ../initrd.img
    896 lohkoa
    $ls
    kernel
    $ls kernel/
    x86
    $ls kernel/x86/
    microcode
    $ls kernel/x86/microcode/
    GenuineIntel.bin
    [/code]

  • J G Miller December 7, 2016, 2:50 pm

    Tonza wrote “I can’t see the root filesystem but I only see this”

    This is because you have a multi-part initramfs file which are becoming standard on most distributions. As you observed, this first part on the initramfs file contains the code to load the corrective Intel microcode for your x86 processor.

    The actual normal initramfs files are in a subsequent part of the file. Assuming you have a two part initramfs, the simplest way to extract the “init” section is to grab the skipcpio program (available in the dracut -core package) and to do

    skipcpio initrd.img-{kernel_version} > intrd.img.part2.gz

    (assuming it has been gzip compressed — you can check wifh file on the output file and then rename it appropriately according to the compression type)

    then gunzip intrd.img.part2.gz and then do the
    mkdir initrd
    cd initrd
    cpio -id < intrd.img.part2

  • Linda April 24, 2017, 2:59 am

    Nice article thanks.