Today, we'll show you how you can fix your broken or accidentally deleted Initrd Image ie. Linux Kernel Image and GRUB Loader in your Ubuntu installed machine. It should also work for Ubuntu derivatives as well as other distribution.
Initrd is a scheme for loading a temporary root file system into memory in the boot process of the Linux kernel. It is a cpio archive of the initial file system that gets loaded into memory during the Linux startup process. The Kernel mounts it as root file system and starts the init process from it.
The Initrd image may be broken or corrupt if the kernel you installed was corrupted or due to some modifications made on the system's kernel or the boot process which in results the system crash :(
As we know, "When there is will there is way". Yes, we can fix or rebuild the initrd image. In my case I will be showing you with my Ubuntu 13.10 Operating System.
Here are easy step wise steps below with screenshots which will help us rebuild our corrupted Kernel Image (Initrd) :-
1. Boot your machine with a Live Media
You can use your Ubuntu CD/DVD or USB stick to boot live into your machine. If you don't have one, there are a lot of ways to create bootable media. You can download the latest Ubuntu Distribution from their official site ie. http://ubuntu.com/ and burn the image to a Blank DVD or use unetbootin to create your USB Stick bootable.
2. Open a terminal console and get partitions path
You can easily run a terminal console by pressing Ctrl+ Alt +T in Ubuntu or you can switch to tty1 by pressing Ctrl+ Alt+f1. What we need to do is, we need to mount our the filesystem from our Hard Disk. For that, we need to know the device path. To get the device path you the need to run:
$ sudo fdisk -l
Here, from above result, in my case, sda1 is the one I need to mount.
3. Mount the filesystems
The filesystem we need to mount the hard disk's partitions such as / (root), /boot, /var and should mount bind /sys, /proc and /dev under a directory /mnt. So, that can be done by steps below:
$ sudo mount /dev/sdaX /mnt
Note: Here in /dev/sdaX, X refers to the partition number 1 in my case, which we got from step 2.
$ sudo mount --bind /dev /mnt/dev
$ sudo mount --bind /proc /mnt/proc
$ sudo mount --bind /sys /mnt/sys
4. Chroot /mnt and creating a Backup of the initrd image
Now, we need to chroot /mnt so that we can setup chrooted environment for the Hard Disk's partition.
$ sudo chroot /mnt
After Chroot is done, we will be backing up the image file of initrd, you can see the image files under /boot.
# ls /boot/*initrd*
# ls /lib/modules/
You will be able to get the version of the kernel you have currently in the system from the above command.
Consider replacing version 220.127.116.11-generic with the own you got from above command.
# mv /boot/initrd.img-18.104.22.168-generic /boot/old-initrd.img-22.214.171.124-generic-old
Note: the file format of the initrd image may differ from Ubuntu to Fedora Based Systems.
If your initrd image has been deleted, you won't be able to run the above command hence, can't create its backup.
5. Building Initrd Image
Now, the real stuff is here. We are gonna build the initrd kernel image here using the command below:
# mkinitrd /boot/initrd.img-126.96.36.199-generic 188.8.131.52-generic-old
The above command works for the other distributions like CentOS, Fedora, Red Hat,etc not for Ubuntu
For Ubuntu Based Distribution:
# update-initramfs -c -k 184.108.40.206-generic
6. Finalizing Grub Loader and unmounting
We will go for unmounting the mounted path as:
If your system has Fedora, RHEL, CentOS installed, try this:
# grub2-mkconfig -o /boot/grub2/grub.cfg
If your system has Ubuntu and its derivative installed:
Now, we have successfully left our Chroot Environment.
$ sudo umount /dev
$ sudo umount /proc
$ sudo umount /sys
$ sudo umount /mnt
Hurray, Now, reboot your machine and eject your Bootable Media. We have done it successfully, boot your system back to the normal and enjoy Linux Computing. Greetings to FOSS and the Freedom.