March 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031

Categories

March 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031

Extract contents of initrd

How to extract and view contents of initrd

Depending on your version of linux, the /boot/initrd may be one of two formats:

cpio archive
ext2 filesystem data
The easiest way to determine what you’re working with is by using the file command. Here are examples showing the output for each file type listed in the above bullet points:

[root@linux ~]# file /boot/initramfs-2.6.32-220.17.1.el6.x86_64.img
/boot/initramfs-2.6.32-220.17.1.el6.x86_64.img: gzip compressed data, from Unix, last modified: Thu Jun 28 18:33:36 2012, max compression

[root@linux ~]# file /boot/initramfs64-catalyst
initramfs64-catalyst: gzip compressed data, from Unix, max compression
To be sure what you’re dealing with, supply the -z option to the file command as this will attempt to look inside the compressed files to see what the contents are:

[root@linux ~]# file -z /boot/initrd-2.6.9-023stab053.2-smp.img
/boot/initrd-2.6.9-023stab053.2-smp.img: Linux rev 1.0 ext2 filesystem data (gzip compressed data, from Unix, max compression)
[root@linux ~]# file -z /boot/initramfs64-catalyst
initramfs64-catalyst: ASCII cpio archive (SVR4 with no CRC) (gzip compressed data, from Unix, max compression)
Mount ext2 filesystem data (gzip compressed) initrd contents

I like to work with copies of the initrd, so I created a dir in /root/initrd and work with it there:

[root@linux root]# mkdir /root/initrd; cd /root/initrd
[root@linux initrd]# cp /boot/initrd-2.6.9-023stab053.2-smp.img .
Some online documents indicate the initrd should be renamed with the .gz extension so that gunzip can be used to extract the compressed file, but most linux distros ship with the handy zcat command, which saves us a step from having to rename files. This is the method I prefer:

[root@linux initrd]# zcat initrd-2.6.9-023stab053.2-smp.img > initrd-2.6.9-023stab053.2-smp
[root@linux initrd]# file -z initrd-2.6.9-023stab053.2-smp
initrd-2.6.9-023stab053.2-smp: Linux rev 1.0 ext2 filesystem data
Now it’s ready to be mounted as a loop device:

[root@linux initrd]# mount -o loop ./initrd-2.6.9-023stab053.2-smp /mnt
[root@linux initrd]# ls -l /mnt
total 9
drwxr-xr-x 2 root root 1024 Jun 25 15:40 bin
drwxr-xr-x 2 root root 1024 Jun 25 15:40 dev
drwxr-xr-x 4 root root 1024 Jun 25 15:40 etc
drwxr-xr-x 2 root root 1024 Jun 25 15:40 lib
-rwxr-xr-x 1 root root 869 Jun 25 15:40 linuxrc
drwxr-xr-x 2 root root 1024 Jun 25 15:40 loopfs
drwxr-xr-x 2 root root 1024 Jun 25 15:40 proc
lrwxrwxrwx 1 root root 3 Jun 25 15:40 sbin -> bin
drwxr-xr-x 2 root root 1024 Jun 25 15:40 sys
drwxr-xr-x 2 root root 1024 Jun 25 15:40 sysroot
Extract cpio archive initrd contents

[root@linux initrd]# cp /boot/initramfs64-catalyst .
[root@linux initrd]# zcat initramfs64-catalyst | cpio -id
[root@vpsrep tmp]# ls -l
total 387232
drwxr-xr-x 2 root root 4096 Jun 26 21:57 bin
drwxr-xr-x 2 root root 4096 Jun 26 21:57 boot
drwxr-xr-x 3 root root 4096 Jun 26 21:58 dev
drwxr-xr-x 32 root root 4096 Jun 26 21:58 etc
drwxr-x— 5 root root 4096 Jun 26 21:58 evolution
drwxr-xr-x 2 root root 4096 Jun 26 21:58 home
lrwxrwxrwx 1 root root 15 Jun 26 21:58 init -> /evolution/init
-rw-r–r– 1 root root 396057600 Jun 26 21:55 initramfs64-catalyst.cpio
lrwxrwxrwx 1 root root 5 Jun 26 21:58 lib -> lib64
drwxr-xr-x 2 root root 4096 Jun 26 21:58 lib32
drwxr-xr-x 9 root root 4096 Jun 26 21:58 lib64
drwxr-xr-x 2 root root 4096 Jun 26 21:58 media
drwxr-xr-x 2 root root 4096 Jun 26 21:58 mnt
drwxr-xr-x 2 root root 4096 Jun 26 21:58 opt
drwxr-xr-x 2 root root 4096 Jun 26 21:58 proc
drwx—— 2 root root 4096 Jun 26 21:58 root
drwxr-xr-x 2 root root 4096 Jun 26 21:58 sbin
drwxr-xr-x 2 root root 4096 Jun 26 21:58 sys
drwxrwxrwt 2 root root 4096 Jun 26 21:58 tmp
drwxr-xr-x 12 root root 4096 Jun 26 21:58 usr
drwxr-xr-x 9 root root 4096 Jun 26 21:58 var
Note: the –verbose option may be supplied to list the files to the console as they are being extracted, or adding v to the options in the example above, as in cpio -idv

cpio -itv will simply list the contents without extracting them.

The -itv options are short hand for –extract –list –verbose
The -idv options are short hand for –extract –make-directories –verbose

Repackaging

If some some reason a change needs to be made to the initrd and it needs to be buttoned back up, this is how to do it:

ext2 filesystem data

copy whatever files you want to whereever the initrd ext2 filesystem was mounted. Once finished, umount the filesystem, gzip it (which will give it the .gz extension), then rename it to the original initrd file that was copied or some other name like initrd-2.6.9-023stab053.2-smp-mpath.img. If a new name is given, be sure to update the boot loader configuration files like /boot/grub/grub.conf.

cpio archive

From within the directory the contents were extracted, first remove or move the .cpio file to another parent directory outside to ensure it’s not part of the new cpio archive then recreate the archive:

find . | cpio –quiet -c -o | gzip -9 -n > ../initramfs64-catalyst

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>