If you’ve read my posts about booting CDs from a USB stick (with ext2 partitions too) and the problems that I encountered, you know that I was pretty pissed-off by the Debian guys, who for some strange reason, decided ext2 support was not needed in the installer initrd.
So off I went to find ways to rebuild or add files to the initrd. But first, what is a initrd?
Quoting from Wikipedia: “An initial ramdisk is a temporary file system used in the boot process of the Linux kernel. initrd and initramfs refer to slightly different schemes for loading this file system into memory. Both are commonly used to make preparations before the real rootfile system can be mounted.”
To put it short, the initrd (for all recent distribuitions, it’s actually a initramfs, but I’ll reffer to it as initrd) is a cpio archive, compressed with one of the three methods: gzip, bzip2 and LZMA.
So, in order to alter a initrd, we will need to:
- locate the initrd file
- decompress the file
- extract the cpio archive contents
- do changes
- rebuild the cpio file
- recompress using the initial method
So let’s get to it. Follow these steps, altering paths and filenames as you need:
Step 1: Locate the initrd file. This is pretty easy, the file is usually called initrd.* or initram.*, with extensions like gz, bz, bz2, igz. Here are some examples from the installer/live CDs that I use:
- SystemRescueCD: initram.igz on /isolinux.
- Ubuntu Desktop and Netbook: initrd.lz on /casper.
- Ubuntu Server and Debian: initrd.gz on /install.
Step 2: Decompress the file. Files compressed with gzip are decompressed with gunzip, bzip2 is decomperessed with gunzip2, and lzma, of course, with unlzma. So this step is simple, just run the corresponding commands:
# cp /mnt/cdrom/location/initrd /tmp # cd /tmp # gunzip < initrd.gz > initrd # bunzip2 < initrd.bz2 > initrd # unlzma < initrd.lz > initrd
Step 3: Extract the contents. If you run “file initrd”, you shouls see something like this:
# file initrd initrd: ASCII cpio archive (SVR4 with no CRC)
So we will extract it using cpio:
# mkdir /tmp/content # cd /tmp/content # cpio -id < ../initrd
Step 4: Do changes! You can now edit scripts, update or copy new files. What I had to do was get the ext2 kernel module from the debian .udeb file and copy it over to the correct location in the initrd’s kernel modules directory:
# cd /tmp # mkdir ext2module # dpkg-deb -x /mnt/cdrom/pool/main/l/linux-kernel-di-i386-2.6/ext2-modules-2.6.26-2-486-di_1.76lenny6_i386.udeb /tmp/ext2module # mkdir /tmp/content/lib/modules/2.6.26-2-486/kernel/fs/ext2 # cp /tmp/ext2module/lib/modules/2.6.26-2-486/kernel/fs/ext2/ext2.ko /tmp/content/lib/modules/2.6.26-2-486/kernel/fs/ext2
Step 5: Rebuild the cpio file. This is also a one-liner:
# cd /tmp/content # find ./ | cpio -H newc -o > /tmp/new-initrd.cpio
Step 6: Recompress the cpio archive. Make sure you use the original name and compression method (well, the compression method should not matter, but the name must be the same!).
# gzip < new-initrd.cpio > initrd.gz # bzip2 < new-initrd.cpio > initrd.bz2 # lzma < new-initrd.cpio > initrd.lz
All you need to do now is overwrite the original file with the newly created one and boot from it. This is really simple if you used my tutorials on booting CDs from a USB stick (also with ext2 partitions). Not so simple, though, if you need to re-create a iso image, and I’ll not write about this now (main reason being that I don’t use CDs or DVDs anymore).
Enjoy and let me know what you think, in the comments section!
Image credit: Fadookie.
Related posts:






