Friday, January 15, 2010

Extracting the contents of a ramdisk image

Sometimes it is necessary to examine the contents of a ramdisk image (initrd). An initrd image is basically a gzip-compressed cpio archive.

Here are the steps used to extract the files contained in a ramdisk:

gunzip initrd
mkdir tmp
cd tmp
cpio -i

Similarly, to build an initial ramdisk image from a directory:

cd tmp
find . | cpio -o -H newc | gzip -9 ../initrd.img

Where 'newc' is the name of the format used in the cpio archive.

In a Fedora distribution, the ramdisk init script is a nash shell-script. Nash is a very reduced footprint shell with built-in commands targetted to ramdisk operations such as device node creation, module loading, root device creation, root pivot, etc.

One of the most important nash commands is 'mkrootdev', which creates the root device specified as an argument. After this command is run, the "mount /sysroot" command is executed, which mounts the root filesystem on the root device.

If the root device is not correctly specified here, the following error will show up:

"mount: error mounting /dev/root on /sysroot as ext3: No such file or

mkinitrd is the responsible for creating the init script contained in a initrd image, and the name of the root device is obtained from the /etc/fstab file, when the kernel rpm is installed (not when it is built!). So, it is important to have a correct fstab file in the filesystem before the kernel is installed, otherwise mkinitrd won't be able to figure out the root device and set the correct arguments to the mkrootdev command.

No comments: