Friday, April 5, 2013

Playing with initrd

Wanna mess around with a Linux initrd?  Add some tools to help debug an install problem, maybe?   The initrd image is a compressed root filesystem, so it's easy to change.

Note that this discussion is about initrd and not initramfs.  The two terms are often used interchangeably, but the initramfs found in the /boot directory on newer editions of Fedora and Red Hat Enterprise Linux are a bit more complex than the initrd found in the isolinux directory of the install CD.  I'll leave initramfs alone for now -- especially since I know very little about initramfs creation and dracut.

Let's assume you have an initrd.img file right in your home directory and you want to unpack it into a new directory called initrd.  All you need to do is feed the image into to a decompressor and feed the output of that into an archive extractor:

mkdir initrd
zcat /boot/initrd.img | $(cd initrd; cpio -id)

That will work for RHEL5 or CentOS 5, but they changed the compression algorithm from from LZ77 to LZMA for RHEL/CentOS 6 and newer editions of Fedora.  (Someone remind me which Fedora that change was made.)  That means you need use the "lz" commands instead of the "z" or "gz" commands.

mkdir initrd
lzcat /boot/initrd.img | $(cd initrd; cpio -id)

Now you can jump into the new initrd directory and start exploring.  Say you want to drop the strace command in:

cd initrd
cp /usr/bin/strace bin

When installing binary executables, you'll need to include any and all shared libraries that it depends on.  You can find this out by running ldd against the executable.

When you're satisfied with your changes, repack the file tree.

cd initrd
find | cpio -co | gzip -9 > ../initrd-debug.img
Or, for newer distros:

cd initrd
find | cpio -co | lzma > ../initrd-debug.img

Note that I specified a new name for the resulting image.  It's optional.  I like to keep the original where it was and distinguish the modified version by giving it a new name.

That's all there is to it!

No comments:

Post a Comment