Wednesday, May 4, 2011

How to recover from a failing Kernel


Or perhaps i should call this “Do you remember i was upgrading to Ubuntu 11.04 ?”.
Well something is gone bad and when i rebooted to start my new Xubuntu 11.04 i got:
error: cannot read the Linux header.
error: you need to load the kernel first.

  Failed to boot both default and fallback entries.

Press any key to continue...
During the upgrade, Ubuntu has removed all old Kernel but one, and so..i find myself without a way to boot my machine.
Fortunately I did a lot of experience with Gentoo in the past, that on this occasion was very useful to recover the system.



When you have an error like that you can do 2 things:
1) Reinstall from zero, but i don’t have a dedicated /home partition so this would erase all my documents and configurations, if for you it’s a fresh install perhaps you can just retry the install from zero.
2) Try to fix the problem, this need some work and the use of chroot
This is how to implement the solution number 2:

1 Boot with another Linux

What do you need: a Live CD/USB Stick, it’s better to use a recent distribution, i used for this Ubuntu 11.04 on a USB Stick (1 GB).
Once that the live distribution has booted, open a terminal and become root (usually a sudo -i is enough).
Also check the internet connection, we could need to download something later.

2 Mount of your filesystem

Now you have to find the partition where you keep your / and if you use it the /boot.
To see the status of your partitions use:
fdisk -l
on my computer I’ve the following output:
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        2292    18408568+   7  HPFS/NTFS
/dev/sda2            2292        2658     2940840    c  W95 FAT32 (LBA)
/dev/sda3            2658        4864    17720609    f  W95 Ext'd (LBA)
/dev/sda5            2658        2667       68008+  83  Linux
/dev/sda6            2667        2730      506488+  82  Linux swap / Solaris
/dev/sda7            2731        4864    17141323+  83  Linux
Looking at the dimensions (number of blocks) i realized that my partitions are:
/dev/sda5= /boot
/dev/sda6= swap
/dev/sda7= /
They are both ext3 and i don’t use LVM, so it’s pretty easy to mount them:
mkdir /mnt/oldubuntu/
mount /dev/sda7 /mnt/oldubuntu/
mount /dev/sda5  /mnt/oldubuntu/boot
They mounted without problems (so i excluded problems on the filesystems) and moving in these directory i did not find any visible problem.

3 chroot there !

Now we are about to use chroot , anyone using gentoo should be familiar with this concept, because one stage of installation consist in booting from a live CD and then switch into the filesystem, and we’ll do exactly the same, mount the meta filesystems:
mount -o bind /proc /mnt/oldubuntu/proc
mount -o bind /dev /mnt/oldubuntu/dev
mount -o bind /sys /mnt/oldubuntu/sys
Why a “mount -o bind” ?
The difference is that ordinary mount only allows you to mount a block device in one single place, but mount –bind allows you to mount something you’ve already mounted elsewhere in a second place; so you end up with the same stuff mounted in two difference places.
Now that we have all the filesystems ready we can use chroot, this command creates a “jailed” copy of the current operating environment within the current operating environment, so you can run stuff without affecting your main environment:
chroot /mnt/oldubuntu /bin/bash
After this command you’ll find yourself as root on your main system, now it’s time to fix the system.
In my case i noticed that /boot was full, so probably something went wrong when the initrd was created during the install of the new 2.6.38 Kernel, what I’ve done to fix it has been:
1) Delete some old stuff in /boot
2) run aptitude pressing the key g i noticed that there were some unfinished task, including the final configuration of the kernel 2.6.38
3) So i just pressed g again and aptitude finished correctly the installation of the new Kernel

4 Reboot and enjoy your system

Now remove your live distro and reboot into your original system !
This is just an example, but you can use this method to recover a Linux system from many problems.

2 comments:

  1. Hi, when I execute

    mount /dev/sda5 /mnt/oldubuntu/boot

    I get,

    mount: you must specify the filesystem type

    what should I do?

    ReplyDelete
  2. Seems an unknown filesystem, an unformatted partition, or an unknown partition ID on /dev/sda5.

    What is the output of "fdisk -l"?

    ReplyDelete