How to View, Modify and Recreate initrd.img

by Sasikala on July 10, 2009

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
Download Free eBook - Linux 101 Hacks

Get free Unix tutorials, tips and tricks straight to your email in-box.

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

  1. How To Mount and View ISO File as Root and Regular User in Linux
  2. Open & View 10 Different File Types with Linux Less Command – The Ultimate Power of Less
  3. Ubuntu Tips: How To View System Log Files in GUI
  4. Unix LS Command: 15 Practical Examples
  5. 3 Methods To View tail -f output of Multiple Log Files in One Terminal
  

Vim 101 Hacks Book

{ 1 trackback }

links for 2009-11-16 « Donghai Ma
November 16, 2009 at 10:06 pm

{ 5 comments… read them below or add one }

1 Lakshmanan July 10, 2009 at 3:06 am

Nice one…

2 runlevel0 July 10, 2009 at 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!

3 beparas July 10, 2009 at 11:15 pm

This is very useful.

4 Ramesh Natarajan July 14, 2009 at 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.

5 lenny September 7, 2009 at 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?

Leave a Comment

Previous post:

Next post: