Fedora 12: Creating an initramfs image for self-compiled kernel

This post was written by eli on September 23, 2010
Posted Under: Linux

So I compiled the kernel I downloaded from kernel.org like I’ve always done, but the system wouldn’t boot, and it had good reasons not to: My root filesystem is both encrypted and RAID-5′ed, which requires, at least, a password to be entered. That job has to be done by some script which runs before my root filesystem is mounted. So obviously, a clever environment is necessary at that stage.

Before trying to fiddle with the existing image, I figured out there must be a script creating that image. And so it did. It’s called dracut and seems to be what Fedora uses in its distribution kernels.

So, go as root (or an internal ldconfig call will fail, not that I know what effect that has)

# dracut initramfs-2.6.35.4-ELI1.img 2.6.35.4-ELI1

This created a 92 Mbyte compressed image file. Viewing the image (uncompressed and opened into files), it turns out that 308 MB out of the 324 MB this image occupies are for kernel modules. Nice, but way too much. And also causes the stage between leaving GRUB and until prompted for password take something like two minutes (!) during which a blank screen is shown. But eventually the system booted up, and ran normally.

So this initramfs is definitely sufficient, but it includes too much junk on the way. Solution: Using the -H flag, so that only the modules necessary for this certain computer are loaded. This is maybe dangerous in case of a hardware change but it reduced the kernel size to 16 MBytes, which is slightly larger than the  distribution initramfs (12MBytes). Which I couldn’t care less about, in particular since the RAM is freed when the real root file system is mounted.

I ran dracut again while the target kernel was running. I don’t know if this has any significance.

# dracut -H initramfs-2.6.35.4-ELI1.img 2.6.35.4-ELI1

Dissection

And as a final note, I’d just mention, that if you want to know what’s inside that image, there’s always

$ lsinitrd initramfs.img

or open the image: Get yourself to a directory which you don’t care about filling with junk, and go:

$ zcat initramfs-2.6.35.4-ELI1 | cpio -i -d -H newc --no-absolute-filenames

To build an image, go from the root of the directory to pack

$ find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs.img

(note the verbose flag, so all files are printed out)

This may not be necessary, as recent versions of dracut supports injecting custom files and other tweaks.

Add a Comment

required, use real name
required, will not be published
optional, your blog address