Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Other Community Discussions > Tutorials & Tips
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Tutorials & Tips
The place to find Ubuntu related Tips & Tricks.

 
Thread Tools Display Modes
Old February 25th, 2007   #1
JohnPhys
A Carafe of Ubuntu
 
JohnPhys's Avatar
 
Join Date: Mar 2006
Beans: 127
Ubuntu 7.04 Feisty Fawn
HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

This HOWTO will tell you how to recover your lost partitions if the partition data on your hard drive got wiped by some malicious program (windows in my case). If the new/incorrect partitions have been formatted, then you've probably lost data. If all that got wiped was the partition table, then this should help you recover all of your data! I searched in the Tutorials and Tips forum and couldn't find anything quite like this, so I thought I'd write it up. If someone knows of a better thread, point me to it!

I have tested this on a Dapper install using an Edgy LiveCD.

All that is needed is an Edgy LiveCD, a cd drive you can boot from, and a working internet connection. A dapper cd would most likely work, but I have not tested it.
WARNING: This HOWTO is most likely to be used at your own risk, as I'm not sure if I'll be able to test it on future distributions (nor do I feel like destroying my partition info to do so).

1.) Enable your computer to boot from the cd drive. You may have to enter your computer's BIOS to do this.

2.) Boot from the Edgy LiveCD.

3.) Assuming the LiveCD loaded just fine, enable the universe and multiverse repositories by System -> Administration -> Software Properties and checking the appropriate boxes.

4.) Open up a terminal (Applications -> Accessories -> Terminal), and type

Code:
sudo apt-get install gpart
Yes, gpart, not gparted. gpart is a program that will scan your drive for existing partitions, and output the cylinders that they are on.

5.) In the terminal, run

Code:
sudo gpart /dev/hda
or substitute whichever drive you are looking at (hdc, sda, etc.) and note the output. Mine looks like:

Code:
 sudo gpart /dev/hdc

Begin scan...
Possible partition(Linux ext2), size(149660mb), offset(0mb)
Possible extended partition at offset(149660mb)
   Possible partition(Linux swap), size(2965mb), offset(149660mb)
End scan.

Checking partitions...
Partition(Linux ext2 filesystem): primary
Partition(Linux swap or Solaris/x86): primary
Ok.

Guessed primary partition table:
Primary partition(1)
   type: 131(0x83)(Linux ext2 filesystem)
   size: 149660mb #s(306504072) s(63-306504134)
   chs:  (0/1/1)-(1023/254/63)d (0/1/1)-(19078/254/63)r

Primary partition(2)
   type: 130(0x82)(Linux swap or Solaris/x86)
   size: 2965mb #s(6072504) s(306504198-312576701)
   chs:  (1023/254/63)-(1023/254/63)d (19079/1/1)-(19456/254/60)r

Primary partition(3)
   type: 000(0x00)(unused)
   size: 0mb #s(0) s(0-0)
   chs:  (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)r

Primary partition(4)
   type: 000(0x00)(unused)
   size: 0mb #s(0) s(0-0)
   chs:  (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)r
The "size" line is telling you first the number of mb of the partition, then the number of sectors (#s) and then the actual sectors that the partition is on (s). The last set of numbers (actual sectors the partition is on) is what we want. Write those down! With those in hand,

5.a) Now we have to restore the partition table. We'll use parted.

Code:
sudo parted /dev/hda
again substituting the drive you're interested in for hda.

You should be at a prompt that looks like

Code:
(parted)
5.b) At that prompt, type

Code:
unit s
to make sure that parted is using units of sectors.


5.c) Just in case we want to undo any changes, type "print", and be sure to write down the start and end sectors of all of the partitions on the disk! NOTE: The reason you don't want to use this method to get the start/end sectors of the partitions you're trying to restore is that parted just reads the partition table on the disk itself, even if it is incorrect or corrupt. If parted throws up errors about nonexisting partitions or a corrupt table, then there is no need for this step.

6.) Now we actually restore the partitions! Still at the (parted) prompt, type

Code:
rescue
, and you will be prompted for the start and end sectors of the partition you want to rescue. In my example, these numbers would be 63 for the start and 306504134 for the end of the first partition.

7.) Repeat step 6 for all partitions that need to be recued.

8.)
Code:
quit
to exit out of parted.

That's it! You should have access to all of your old partitions, and you can check this by mounting the various partitions in the live cd.

9.) If you find out you messed with the partition table on the wrong disk, and you followed step 5.c), you can use steps 6-8 to restore the partition table you just wiped, using the numbers you wrote down from the print command in parted rather than the output from gpart.

NOTE: If your partition table was wiped, it's quite possible (as it was in my case) that your MBR got killed as well, and you'll need to restore grub. You can try starting here: How to install Grub from a live Ubuntu CD, though I'll admit the first method failed for me. If the admins let this HOWTO through, I'll add on what I did to get GRUB back.

Please post any comments/questions, and let me know if this is helpful!

Last edited by JohnPhys; March 15th, 2007 at 04:37 PM.. Reason: change "restore" to "rescue in parted, thanks to jan - 2007.03.11
JohnPhys is offline   Reply With Quote
Old March 11th, 2007   #2
jan
A Carafe of Ubuntu
 
jan's Avatar
 
Join Date: Jan 2005
Location: Czech Republic
Beans: 122
Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

Hi,

nice HOWTO - i just had to use command "rescue" since no "restore" existed. Seemed to work the same way.

However it seems whole my GRUB got wiped so it was good for nothing .
__________________
interPC
jan is offline   Reply With Quote
Old March 11th, 2007   #3
JohnPhys
A Carafe of Ubuntu
 
JohnPhys's Avatar
 
Join Date: Mar 2006
Beans: 127
Ubuntu 7.04 Feisty Fawn
Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

Well, at least you have your partitions back now (assuming they were gone before!). Reinstalling grub shouldn't be *too* much of a hassle. There's a link at the end of my original post that tells you how to reinstall grub. If that doesn't work, I can tell you how I managed to do it (probably not the shortest/easiest way, but it worked).

Thanks for catching the rescue/restore bit, I could have sworn I double checked that!

EDIT: I've fixed the command now, thanks again!

Last edited by JohnPhys; March 11th, 2007 at 11:46 AM..
JohnPhys is offline   Reply With Quote
Old March 27th, 2007   #4
geoisonfire
First Cup of Ubuntu
 
Join Date: Nov 2006
Beans: 8
Angry Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

plz help me!!!!
My partition table was gone since I mistakenly detected my partition during the re-installation of xp, However, good thing was i stop the next step, (didn't format my hard disk), but now I can't boot to xp anymore and my Partition table is gone. btw, I just detected ubuntu 2 days before that, so I can only access ubuntu using live cd.

Here's what it looked like after I used sudo gpart /dev/hda

ubuntu@ubuntu:~$ sudo gpart /dev/hda

** Error: invalid extended ptbl found at sector(53657100).

Begin scan...
Possible partition(Windows NT/W2K FS), size(18002mb), offset(0mb)
Possible extended partition at offset(18002mb)
Possible partition(Windows NT/W2K FS), size(8197mb), offset(18002mb)
Possible partition(Windows NT/W2K FS), size(21799mb), offset(26199mb)
Possible partition(Windows NT/W2K FS), size(10001mb), offset(47998mb)
Possible partition(Windows NT/W2K FS), size(18308mb), offset(58000mb)
End scan.

Checking partitions...

* Warning: more than one extended partition: 2.
Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): primary
Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): logical
Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): invalid
Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): invalid
Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): primary
Ok.

Guessed primary partition table:
Primary partition(1)
type: 007(0x07)(OS/2 HPFS, NTFS, QNX or Advanced UNIX)
size: 18002mb #s(36869104) s(63-36869166)
chs: (0/1/1)-(1023/254/63)d (0/1/1)-(2294/254/55)r

Primary partition(2)
type: 015(0x0F)(Extended DOS, LBA)
size: 29996mb #s(61432560) s(36869175-98301734)
chs: (1023/254/63)-(1023/254/63)d (2295/0/1)-(6118/254/63)r

Primary partition(3)
type: 007(0x07)(OS/2 HPFS, NTFS, QNX or Advanced UNIX)
size: 18308mb #s(37495640) s(118784673-156280312)
chs: (1023/254/63)-(1023/254/63)d (7394/1/1)-(9727/254/56)r

Primary partition(4)
type: 000(0x00)(unused)
size: 0mb #s(0) s(0-0)
chs: (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)r


My actual partition table using fdisk /dev/hda

Device Boot Start End Blocks Id System
/dev/hda1 1 2295 18433024 7 HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/hda2 3341 9728 51311610 f W95 Ext'd (LBA)
/dev/hda3 207176 8283 549881402+ ff BBT
Partition 3 does not end on cylinder boundary.
/dev/hda5 3341 6119 22322286 7 HPFS/NTFS
/dev/hda6 207176 8283 549881402+ ff BBT


What do i need to do now!!!
I had two primary partitions, I want my first one back ( 18G), the second one(8G) is the one I created in order to install x86 osx , however, my partition was gone even before I get chance to try that.
I have really important files in my logical partitions, What do i need to do now!

I am almost crying now, plz help me to get my partition table back, as well as all my files. thx

Last edited by geoisonfire; March 28th, 2007 at 01:28 AM..
geoisonfire is offline   Reply With Quote
Old March 28th, 2007   #5
JohnPhys
A Carafe of Ubuntu
 
JohnPhys's Avatar
 
Join Date: Mar 2006
Beans: 127
Ubuntu 7.04 Feisty Fawn
Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

Where are you getting the "testdisk" info from?

Also, in linux, run

Code:
sudo fdisk -l /dev/hda
and post the output, please.
JohnPhys is offline   Reply With Quote
Old March 28th, 2007   #6
geoisonfire
First Cup of Ubuntu
 
Join Date: Nov 2006
Beans: 8
Angry Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

"Where are you getting the "testdisk" info from?"
Sorry, I think all i did was just using fdisk /dev/hda
and I get the second result in my post.



I believe never I used testdisk, all i used is gpart and fdisk.
(I will correct my original post soon, thx a lot for mention that!)

After I type sudo fdisk -l /dev/hda
I get this

Device Boot Start End Blocks Id System
/dev/hda1 1 2295 18433024 7 HPFS/NTFS
Partition 1 does not end on cylinder boundary.
/dev/hda2 3341 9728 51311610 f W95 Ext'd (LBA)
/dev/hda3 207176 8283 549881402+ ff BBT
Partition 3 does not end on cylinder boundary.
/dev/hda5 3341 6119 22322286 7 HPFS/NTFS
/dev/hda6 207176 8283 549881402+ ff BBT


It seems to be kinda weird although I don't understand it.
Btw, after I type sudo gpart /dev/hda
It give me

Begin scan...
Possible partition(Windows NT/W2K FS), size(18002mb), offset(0mb)
Possible extended partition at offset(18002mb)
Possible partition(Windows NT/W2K FS), size(8197mb), offset(18002mb)
Possible partition(Windows NT/W2K FS), size(21799mb), offset(26199mb)
Possible partition(Windows NT/W2K FS), size(10001mb), offset(47998mb)
Possible partition(Windows NT/W2K FS), size(18308mb), offset(58000mb)
End scan.

Then Checking partitions... and other stuff like I post before, all i know is that these 5 possible partitions has the correct size as my partitons in xp and they are listed even in the right order. I hope I can restore these partitions back.
Thx for your help!

Last edited by geoisonfire; March 28th, 2007 at 08:15 AM..
geoisonfire is offline   Reply With Quote
Old April 1st, 2007   #7
geoisonfire
First Cup of Ubuntu
 
Join Date: Nov 2006
Beans: 8
Smile Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

Thanks for your help anyway, JohnPhys. I got all my partitions back and lost none of my data. Gpart was not the only tool that I used, but still it did help me a lot!
geoisonfire is offline   Reply With Quote
Old August 14th, 2007   #8
elmo541992
5 Cups of Ubuntu
 
elmo541992's Avatar
 
Join Date: Jun 2007
Beans: 43
Ubuntu 7.04 Feisty Fawn
Angry Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

i need help...

i know my mbt and partition table is gone, but im having trouble getting it back. i do sudo fdisk -l /dev/sdb, and i get this:
Code:
max@max-desktop:~$ sudo fdisk -l /dev/sdb 

Disk /dev/sdb: 41.1 GB, 41121316864 bytes 
255 heads, 63 sectors/track, 4999 cylinders 
Units = cylinders of 16065 * 512 = 8225280 bytes 

Disk /dev/sdb doesn't contain a valid partition table
this info i know is correct. i also know that the layout of my partitions are something like this:

~38gb ext3
~1.5gb swap

but, when i use gpart, it detects the swap drive, but not the ext3 part. i can still use this info it spit out at me to assume the sectors of the ext3 part. so then i continue to parted. heres what i get:
Code:
max@max-desktop:~$ sudo parted /dev/sdb

GNU Parted 1.7.1

Using /dev/sdb

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) unit s                                                           

(parted) rescue

Error: Unable to open /dev/sdb - unrecognised disk label.                 

(parted)
i get the same error when i tell it to print, too. how can i get around this??? and so you know, im 64-bit, and i used the feisty install on another hard drive. also, if theres a way to just get files off of it, that would be fine too.
elmo541992 is offline   Reply With Quote
Old November 9th, 2007   #9
mafiastyle
First Cup of Ubuntu
 
Join Date: Nov 2007
Beans: 1
Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

hello!
I have some problems with my partition table. I think it is corrupt. when I check it from windows everything appears to be ok, but when I use gparted it shows i have no partition on my sata drive. when I use gpart it shows me the info I need. Please advise me what to do so I can restore my partitions.
Thanks !
mafiastyle is offline   Reply With Quote
Old November 10th, 2007   #10
JohnPhys
A Carafe of Ubuntu
 
JohnPhys's Avatar
 
Join Date: Mar 2006
Beans: 127
Ubuntu 7.04 Feisty Fawn
Re: HOWTO: Restore Lost Partitions to a Deleted or Corrupt Partition Table

Can you mount any of the partitions on your sata drive in linux? Please give some more info, including your partition & drive setup.
JohnPhys is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:28 PM.


vBulletin ©2000 - 2010, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry