Saturday, October 30, 2010

Recovering an unmountable partition from Linux

If your computer suddenly get switched off during the system boot up you may end up having unmountable partitions. This could happen to a laptop/netbook with very low battery or in this part of the world to a desktop without an UPS.

So this happen to my wife's netbook and I felt it will be useful for someone else to know the steps I have taken to recover it. In this case it was the root partition and system couldn't go beyond grub.

Since the system is unable to proceed beyond grub, I have to use a bootable usb-stick. In my case Kubuntu but this should be possible with any Linux distribution.

First I tried running e2fsck to repair the file system. Got in to root by 'sudo su'.

# e2fsck /dev/sda1

It refused to run stating "Device or resource busy while trying to open /dev/sda. Filesystem mounted or opened exclusively by another program?".

-f option won't help here since it cannot open the device. -F option simply freezes the command and I have to restart the system (kill didn't work).

# e2fsck /dev/sda2

did work. I was happy since it is '/home' so her data can be saved. So I immediately took a backup of the entire home directory to an external drive.

Then I though of using a very powerful Unix command 'dd'. It can do low level (byte level) read from a device or file and write to a device or file.

# dd if=/dev/sda1 of=/media/disk/sda1.img

This command creates an image of the partition sda1 as a file named 'sda1.img'. 'e2fsck' can be run on this image and correct errors.

# e2fsck -f /media/disk/sda1.img

This image can be mounted as a loop device. I created a directory in the 'media' directory as 'sda1-img' (Mount point, this could be anywhere any name you like) and mount the image as below.

# mount -o loop /media/disk/sda1.img /media/sda1-img

Now via '/media/sda1-img' the backup image of the partition sda1 can be browse. At this point if any files need to be backed up, that can be done. For example if the home directory is also in this partition I should have taken a backup of that. However, in my case the home directory is in sda2 and I have taken a backup of that.

At this point I have an image of sda1 without any errors and the file system in it can be accessed. So I wrote the image back in to sda1 using 'dd'.

# dd if=/media/disk/sda1.img of=/dev/sda1

At the end I got an error message stating it cannot write the last block. This could be due to a modification done by e2fsck to the file system in the image. I just ignored it since sda1 was never full.

Then I tried accessing it through the file manager (Dolphin). YES, it got mounted and I can access the files. Then I unmount it again through the file manager. I could have done this through the command line as well. I don't think that could have made any difference.

Just to be safe (since I got a dd error) I ran e2fsck on sda1.

# e2fsck -f /dev/sda1

It said that the file system is clean.

Shutdown the machine, remove the usb-stick, and boot the machine. It works fine now!