Purpose: We will learn how to decompress and then re-compress an initramfs image from your Debian or Ubuntu systems. I find the whole initramfs and initrd topic to be one of the most fascinating one in Linux. I always wanted to write a series of post on these topics and I think this might be the first one in the line. So let’s get started…

What is initramfs?

Think of initramfs as a temporary “root” file system that gets loaded into your system’s memory (RAM) and this filesystem is responsible for finding and loading your real “root” filesystem either from your hard drive, network (nfs), RAID, LVM, etc. The purpose of initramfs is solve the well know chicken and egg problem in Linux during the boot – how do you detect and load modules for your intended filesystem if the module for doing that resides on the filesystem itself? That’s where initramfs comes into the picture and you get the idea…If you don’t just search for initrd or initramfs and read the Wikipedia article. Also the initramfs is now also referred as “early user space”.

What is the difference between initrd and initramfs?

This topic deserves a separate post and therefore we won’t go into the details of it. For now just remember that initramfs is the successor of initrd. They both intended to solve the same problem – the chicken and egg problem as described above. Although they are fundamentally (technically) different they provide solution to the same problem.

Where can I find my initramfs image?

If you installed a stock Debian system, for example, say Lenny (or Squeeze), then you can find your initramfs image in the /boot directory:

debian:~# ls /boot/initrd.img-2.6.30*

The above file, initrd.img-2.6.30-2-686 is your initramfs file. Basically this is a gziped-compressed cpio archive.

How do I verify if the image is initramfs or initrd?

You can confirm this by the following command:

debian:~# file /boot/initrd.img-2.6.30-2-686
/boot/initrd.img-2.6.30-2-686: gzip compressed data, from Unix, last modified: Sun Nov  1 12:50:11 2009

And your GRUB’s configuration file, menu.lst, will supply this initramfs file to your corresponding booting kernel.

debian:~# less /boot/grub/menu.lst
title           Debian GNU/Linux, kernel 2.6.30-2-686
root            (hd0,4)
kernel          /boot/vmlinuz-2.6.30-2-686 root=/dev/sda5 ro quiet
initrd          /boot/initrd.img-2.6.30-2-686

Note: Although the last line says “initrd”, it is really a supplying a initramfs file. This use to confuse me a lot when I was new to this whole initrd/initramfs topic. Historically the label “initrd” in the menu.lst file has not changed to reflect the usage of initramfs file. So basically the file /boot/initrd.img-2.6.30-2-686 is an initramfs file even though the starting word (label) says “initrd”. This will save you  a lot of frustration and time and will help you to understand the concept.

How do I decompress it?

Now I am sure, this whole initramfs thing might have piqued your interest to inspect what is inside it, right? Let’s see how to decompress the file:

$ mkdir -p /tmp/initramfs
$ cd /tmp/initramfs
$ cp /boot/initrd.img-2.6.14-1-686 initramfs.gz
$ gzip -d initramfs.gz
$ cpio -i < initramfs
50243 blocks
rm initramfs

At this point of time your initramfs image is decompressed and unpacked. You can inspect the contents like this:

debian:/tmp/initramfs# ls
bin  conf  etc  init  lib  sbin  scripts  usr

Play around with the unpacked initramfs and see what contents are there inside. As you can see it almost looks like a regular “root” of a filesystem with directories like bin, etc, lib, sbin, usr, etc. There is no fixed format for a initramfs except the fact that all initramfs should contain an executable script called “init”. The Linux kernel after un-compressing the initramfs into your system’s memory (RAM) looks for this script and execute it as PID (Process ID) 1and rest of the booting process is taken care by this “init” script.

How do I re-compress back again?

So let’s say that you make some modification in your uncompressed initramfs and now you want to pack it again so that your kernel can use it on the next boot. To pack it, give the following command from the root directory where you un-compressed your initramfs:

debian:/tmp/initramfs# find . | cpio --quiet -H newc -o | gzip -9 -n > /boot/imagefile.img

Now you can modify your menu.lst file to pass this new modified initramfs to your kernel:

title           Debian GNU/Linux, kernel 2.6.30-2-686
root            (hd0,4)
kernel          /boot/vmlinuz-2.6.30-2-686 root=/dev/sda5 ro quiet
initrd          /boot/imagefile.img

That’s it. Upon your next reboot the GRUB will load your kernel and the new initramfs image.

Part 2: What is initrd and similarities with initramfs

Be Sociable, Share!