Tuesday 9 November 2010

Building and using a Linux initramfs

If your kernel needs to do 'stuff' during boot up or load modules that aren't compiled into it then an initramfs provides a way (initramfs supersedes the old initrd approach). Basically it is a file (cpio and gzipped) that contains a directory hierarchy containing all the necessary items to provide for loading of modules etc.

To enable the use of the initramfs you'll need to enable the relevant kernel option (e.g. General Setup --> Initial RAM filesystem and RAM disk (initramfs/initrd) support) and them supply the appropriate boot arguments to your boot loader (e.g. append initrd=my-initrd .....). At the very beginning of boot up you should see [on the console] the initrd being loaded (Loading initrd................), then further down it executes the ./init script within the initrd.

To build an initramfs from a suitably populated directory:
find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../my-initrd
To check the contents of an initramfs:
gunzip -c my-initrd | cpio -t
To extract the contents of an initramfs into the current directory (you might want to create a temporary directory to unpack it into mkdir initrd.d ; cd initrd.d):
gunzip -c ../my-initrd | cpio -i -d -H newc --no-absolute-filenames

If you've been trying to use an initramfs and you're getting this error on boot up:
Failed to execute /init
Kernel panic - not syncing: No init found.  Try passing init= option to kernel.
Then maybe your ramfs image doesn't contain init, or it's not executable, or you're trying to use a shell within init that incompatible with your kernel's architecture (e.g. trying to use an amd64 arch shell with an i686 kernel).

There's a good Gentoo guide on building them.

On Ubuntu/debian there's a command to build an initrd called mkinitramfs. When running mkinitramfs whilst you can use an alternative config directory, it can only obtain kernel modules from /lib/modules/<kernel_version>. So you can run it using chroot, though its only a shell script so it can of course be tweaked....

No comments:

Post a Comment