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!


Unknown said...

This solution worked for me in a very similar situation.

I conjecture that you can't run "e2fsck /dev/sda1" from Ubintu Live CD because Ubuntu tries to mount the (now unmountable) partition during its start-up process, and the mounting process just sits there without giving up. This is why if you do "sudo lsof |grep sda1", you get a report like this:

jbd2/sda1 296 root cwd DIR 0,17 300 2 /

jbd2/sda1 296 root rtd DIR 0,17 300 2 /

jbd2/sda1 296 root txt unknown /proc/296/exe

and then, when you do "ps auxw | grep 296",
you learn that it is a kernel-originating process that keeps the device busy:

root 296 0.0 0.0 0 0 ? S 21:36 0:00 [jbd2/sda1-8]

I tried to figure out how to prevent Ubuntu Live CD from trying to mount /dev/sda1, but couldn't: it seemed that adding options such as "sda=noprobe" (or should it have been "sda1=noprobe"?) to the boot command line had no effect.

It seemed that other people with the same problem
( )
solved it by booting from a Slax live CD, rather than a Ubuntu one. But as I did not have a working Slax CD (the CD writer i used was not quite compatible with the CD reader), your solution with "dd" was the only one that worked for me.

Now, I first tried to copy the data with "dd" from sda1 to a USB device, but soon realized that all my USB devices were either to small to copy the entire sda1 to them, or were already formatted with vfat and thus could not store files bigger than 4 GB. So I ended up unearthing an old internal hard disk drive, opening up my computer, and connecting this old drive in (so it became sdb1). Then everything worked!

Incidentally, it is useful to know that "dd" can read the unmountable device, and then write to it, even when that device appears as "busy" to e2fsck.

Anonymous said...

This solutuin works for me and very thanks to you
let me describe about my machine
i got a 250 gb hdd with multiple booting
40 gb primary partition win xp
50 gb primary partition win 7
80 gb primary partition ntfs
80 gb extended partiton for linux within that
20 gb for root partition of ultimate edition 2.8 (derivative of ubuntu)
58 gb for home partiton
2 gb for swap

after restarting my machine i am able to boot into windows but my linux os says that try passing init
drop into busybox . I googled a lot with no success. At last i find this blogspot and wanna a try

first thing was, me a desk top user with least knowledge of computer and i used to follw step by step things to get things done. The first problem was my root partition was 20gb so no thought of usb and i had neither another HDD so i thought of copying to one partition of my hdd.

I booted through live dvd then applications – accessories – terminal opened terminal and typed the following command to become root

sudo -su

#dd if=/dev/sda5 of=/dev/sda3/sda5.img

out put shows ded/sda3 was not a directory (dev/sda5 was my root partition and dev/sda3 was primary partition with ntfs format)so i want to copy my root partition of linux to anothe partition in my hdd but unable to do so . Then i mounted dev/sda3 opened the partition searched files then i right click in the partition in the dialogue window scripts – open terminal here(scripts option found in ultimate edition 2.8 i am not sure of other os but beleives they may be similar too)tehn i saw on the upper bar of the terminal i showed that /media/xxxxxxxxx#(xx are numbrers)

then i got an idea that this partition was read as / media/xxxxx
so i opened a terminal again by applications-accessories-terminal and typed the following
sudo -su

#dd if=/dev/sda5 of=/media/xxxxxxx/sda5.img

bingo it worked it had tken 1 hour 20 mts to copy my root partition

then i typed in the terminal

e2fsck -f /media/disk/sda5.img

the out put was recovering journal etc...

then i typed this command in the terminal

dd if=/media/xxxxxx/sda5.img of=/dev/sda5

i had taken 40 mts to copy and showed and inpot out put error

then i ran

e2fsck -f /dev/sda5 command but my bad luck the out put was

fsck.ext4: Device or resource busy while trying to open /dev/sda5
Filesystem mounted or opened exclusively by another program?

Same error message on fsck command before this entire process i got little bit depressed then i resarted my machine and entered into recovery mode and yes system recovery was done and superblock etc unknown words for me were displayed and everything went fine. Then i rebooted via the normal mode and everything was back

it worked for me

thank a lot sanjayak for sharing this with us and if you permit me i wanna link your url to many others suffering the same problen


Anonymous said...

I am very happy to know that my post is helping others. Please feel free to link and share since that has a much greater benefit to everyone.

Chandni Verma said...

Thanks for the idea!
I had an actual broken disk and used ddrescue instead of dd command.

I have provided a link to this post on my blog post too!

Anonymous said...

Thanx a lot for the detailed explanation!

I had the exact problem, and I followed your instructions step-by-step like a disciplined kid ;-).

And it worked, and now I'm back to "life" again!.

Thank you,


DrDamnit said...

Worked for me when my VM that ran my jabber server went down. Awesome guide. Thanks for posting.

Diego said...

Thank you so much, this method is giving me light in such dark times. I'm currently copying the whole of my /dev/sda6 (30GB, it's taking hours), and then I hope it shall work ;)

Diego Again said...

Ahah Thank you! I can backup my data! (after 10hours of thrilling copy, actually it copies the whole partition, even if it's half empty).

Such a good method, I'm glad I know the dd comand now, it's absolutely useful.


Unknown said...

It seems that with a Live CD of the now most current Ubuntu Linux version (i.e., Ubunto 11.10), it is possible to run

e2fsck /dev/sda1

even in a situation where Linux from an older versions Ubuntu Live CD refused to do so (as described in the original posting). So nowadays this may be an easier solution.

Jennifer said...

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. 4inkjets coupons