Tuesday, March 17, 2009

interactive debugging /boot/initrd.img

Sometimes, it is handy to figure out what is going wrong in your initrd file.  This can be tricky, as it’s a single script that if it’s breaking, usually results in a kernel panic.  I needed to figure out today why the call to mdadm to assemble the raid was not working, so I could add something of value to bug #48844.  To debug an initrd.img, here is what I do:

  • Boot rescue mode from a Mandriva CD.
  • Go to the console, mount your /boot partition into /mnt, unpack your initrd
mount /dev/sda1 /mnt
mkdir /mnt/initrd ; cd /mnt/initrd
cat ../initrd.img | gzip -d | cpio -i
  • copy in bash and necessary libraries to run a shell
cp /lib/* lib/
cp /bin/* bin/
cp /lib64/* lib64/
  • edit init and add in /bin/bash at the point you would like a shell
  • rebuild your new initrd
find . | cpio -H newc -o | gzip -9 > ../initrd-new.img
  • edit ../grub/menu.1st to use your the new initrd-new.img file.  Also turn off graphical boot.
  • umount /mnt
  • reboot

If you want to run some of the actions in /init from the command line manually, they are nash builtin’s, so cannot be run directly from bash.  I run them by creating an file with vi containing

#!/bin/nash
mkblkdevs

then running the file.

4 comments:

yoho said...

About the second part of your article : wouldn't it be easier to just create a symbolic link to nash ?

Glen said...

as far as I've been able to tell, nash is a script interperter and does not provide a standard shell. I did initially try using nash, but could not get it to provide an interactive shell. An alternative I think would work would be busybox, but didn't want have to compile it.

blino said...

To run nash commands, you can also do the following:
echo mkblkdevs | nash --force

This way, you don't need to write a script for each command you run.

stevec said...

Hi, thanks for this, it really helped. I was a bit surprised to see you copying all of /lib and /bin into the initrd. In my own case, I compiled busybox, and used that, and it did what I needed to do, and I was able to debug the problem I was having. I wouldn't have thought to try that were it not for this blog posting I came across by googling. So thanks, it really helped. Thanks to the commenters as well. blino, I used "echo mkblkdevs | nash" today as well in my debugging (of a block driver).

Great blog you've got here.