Notes for initrd (initial RAM disk): how to modify the initrd

The Linux initial RAM disk, for example, initrd-2.4.21-15.ELsmp.img, is a temporary root file system that is mounted during system boot to support the two-state boot process. The initrd contains various executables and drivers that permit the real root file system to be mounted, after which the initrd RAM disk is unmounted and its memory freed. In many embedded Linux systems, the initrd is the final root file system.

Recent, I had a chance to work on the Linux kernel a little bit, and in the process, I learned how to manipulate the ramdisk. As you know, lots of works are done in this stage, such as loading modules etc. This is why this note is from.

Before Fedora Core 3, the default initrd image is gzip-compressed ext2 image file.

If you want to examine or modify, use the following commands to set up and mount a loopback device. Since the initrd image is named with the format like: initrd-2.4.21-15.ELsmp.img, you have to mv the name to .gz file, and then gunzip it.
 

# mv initrd-2.4.21-15.ELsmp.img initrd-2.4.21-15.ELsmp.img.gz
# gunzip initrd-2.4.21-15.ELsmp.img.gz # mount -o loop initrd-2.4.21-15.ELsmp.img <mount_point>

These steps uncompress the image file, associate it with the loopback device, and mount the loopback device on a directory. Replace <mount_point> with the name of the directory (such as /mnt/initrd) on which you want to temporarily mount the loopback device.

After you have finished making any changes, use the following commands to unmount the loopback device and compress the initrd image:

# umount <mount_point>
# gzip initrd-2.4.21-15.ELsmp.img
# mv initrd-2.4.21-15.ELsmp.img.gz initrd-2.4.21-15.ELsmp.img
Also you can use the following commands to associate the ram disk to the loopback device:
# losetup /dev/loop0 initrd-2.4.21-15.ELsmp.img
# mount /dev/loop0 /mnt/initrd
# umount /mnt/initrd 
# losetup -d /dev/loop0  #After you finish modification, then losetup -d

Beginning with Fedora Core 3, the default initrd image is a compressed cpio archive file.

Instead of mounting the file as a compressed image using the loop device, you can use a cpio archive. To inspect the contents of a cpio archive, use the following commands:

# fdisk -l /dev/sda
# mount /dev/sda1 /mnt
# cd /mnt/boot # cp initrd-2.6.9-42.ELsmp.img /tmp; cd /tmp # mv initrd-2.6.9-42.ELsmp.img initrd-2.6.9-42.ELsmp.img.gz # gunzip initrd-2.6.9-42.ELsmp.img.gz # mkdir test; cd test # cpio -idv < ../initrd-2.6.9-42.ELsmp.img.gz # then do modification: # find . | cpio -o -H newc | gzip > ../initrd-2.6.9-42-p3.ELsmp.img

Reference: [1], or mirror [2]