So you are at your linux system and you restarted and all the sudden on boot, you get a: Kernel Panic … What do you do now? Well first of all don’t you panic!
First step to take would be to boot your system in rescue mode. To do that you need any sort of a live CD. Get a Ubuntu CD and load using it. There will be a prompt that says load using the CD.
This is your system booting off the CD. Now that you have access to the Server. What you need to do is get access to your actual physical system.
By default your system is not mounted. Run the following commands to see where your mount points are:
fdisk -l
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 194559 96256 83 Linux
/dev/sda2 196606 490233855 245018625 5 Extended
/dev/sda5 196608 490233855 245018624 8e Linux LVM
Now you need to mount your drives.
In our case, the main partition is in an LVM. So we need to mount that to the /mnt dir and then mount the boot to /mnt/boot
mount /dev/SysVolGroup/root /mnt/
mount /dev/sda1 /mnt/boot/
If you are going to do any sort of action on the kernel/grub, you also need to bind the proc and the dev directory to this mount point.
mount --bind /dev /mnt/dev/
mount --bind /proc/ /mnt/proc/
Now that you have all the mounts correctly setup, lets chroot into ‘our’ system.
chroot /mnt/
This will put you into your system as thought you’ve just ssh’ed. The problem with our system was that, we had a bad kernel that was installed. If you list the /lib/modules you can find the kernels you have. Find a stable one and install the kernel using the following:
update-initramfs -u -k 2.6.32-39-server
We want to get rid of any bad kernels before installing the grub again. To find all the installed kernels, run the following command:
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9]
Append the appropriate grep at the end of it to which you want to uninstall. In our case it was a custom kernel. Thus we uninstall and reinstall the grub using the following commands
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | grep "custom" | xargs sudo apt-get --dry-run remove
dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | grep "custom" | xargs sudo apt-get -y purge
update-grub2
First command will do a dry run to see which kernels will be uninstalled and the second command will actually uninstall those kernels. Then we do an update on the grub to let the system know which kernel to load with.
Reboot!