Wednesday, January 25, 2012

Unpack and edit initramfs (for blacklisting modules, but can be used for other reasons)

I did this originally to blacklist the loading a kernel module, which may have been loaded by initramfs (adding rdblacklist to the kernel command line didn't work). However, the module still got loaded and what I should have done in the first instance was check modules.dep, as it was a dependency for another module that I wanted anyway. You also must make sure any other processes in init don't cause it to get loaded. Anyway, it was a useful exercise in how to modify initramfs!

How to unpack and modify a initramfs image

(tested with fedora 13)

Make a directory to unpack it in
Make a backup of the original initramfs image
cd to your unpack dir and unpack with: 'gzip -dc initramfs-2.6.34.9-69.fc13.x86_64.img | cpio -id'

The 'i' flag is for taking input, and the 'd' makes dirs

Modify the files as required

Recreate the image (I like to do it in 2 separate steps cos I'm superstitous):


find . | cpio -H newc -o > /some/dir/initramfs-2.6.34.9-69.fc13.x86_64.cpio

'-H' - format
newc - is the newc format type

'-o' - output

cat /tmp/initramfs-2.6.34.9-69.fc13.x86_64.cpio | gzip -9 > /tmp/initramfs-2.6.34.9-69.fc13.x86_64.img

Maximum compression is selected as that is what it is used in the original

Then copy it back

I haven't yet tried dracut