LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-08-2010, 07:20 PM   #1
kinematic
LQ Newbie
 
Registered: Dec 2005
Location: behind you
Distribution: Debian testing
Posts: 18

Rep: Reputation: 0
Unhappy Oops! Erased mbr, no more partition table. Any way to reconstruct?


Hey guys, I have a little problem. Yesterday I installed Windows 7 thinking I could just reinstall grub afterwards from the Debian testing weekly build installer but it failed exiting with a code 1. I had to add an entry to the Windows bootloader to get back into Debian. Then I tried to reinstall grub from Debian but the Windows bootloader was still there. Now here comes the dumb part. I booted into Debian again and I thought; "oh well, I'll just erase the mbr to get rid of the Windows bootloader and reinstall grub". So I used "dd if=/dev/zero of=/dev/sda bs=512 count=1". Obviously I wasn't thinking clearly otherwise I wouldn't have used that command and after I realized what I had done it was too late. There's nothing on the mbr anymore. I'm still in Debian so there must be some information about the partitions stored somewhere in a file(s) and Debians disk utility still shows the partitions. Is there a way to reconstruct the partition table and write it to the mbr? (don't have a backup of the mbr).

I already tried testdisk but it only finds the first primary partition with Windows on it and the second primary ext3 partition with Debian on it.
 
Old 08-08-2010, 07:43 PM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Just.. reinstall grub to the MBR? http://www.sorgonet.com/linux/grubrestore/

Quote:
I already tried testdisk but it only finds the first primary partition with Windows on it and the second primary ext3 partition with Debian on it.
Don't you only have two OSes? I don't see the problem here? What is 'testdisk' supposed to do?

Grub should also just pick up your Windows install and give you a menu entry for it that passes control to the Windows bootloader.
 
Old 08-08-2010, 08:03 PM   #3
kinematic
LQ Newbie
 
Registered: Dec 2005
Location: behind you
Distribution: Debian testing
Posts: 18

Original Poster
Rep: Reputation: 0
Unhappy

Quote:
Originally Posted by AlucardZero View Post
Just.. reinstall grub to the MBR? http://www.sorgonet.com/linux/grubrestore/

Don't you only have two OSes? I don't see the problem here? What is 'testdisk' supposed to do?

Grub should also just pick up your Windows install and give you a menu entry for it that passes control to the Windows bootloader.
You can't install grub without a partition table since it can no longer find any partitions without it and thus is unable to find the root partition. The other problem is that it's a 500GB drive with 3 primary partitions on it and an extended partition containing 3 logical partitions so if testdisk only finds 2 that's not a good sign.
 
Old 08-08-2010, 09:21 PM   #4
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Oh, I misunderstood.

I don't know.
 
Old 08-08-2010, 10:48 PM   #5
lumak
Member
 
Registered: Aug 2008
Location: Phoenix
Distribution: Arch
Posts: 799
Blog Entries: 32

Rep: Reputation: 111Reputation: 111
Hrmmms I'm not sure on this... but when I was playing with hard drive partitions before, is that the node information for each file is on each individual partition header. If you delete a partition, write the partition table to the disk, then re create a new partition table in EXACTLY the same locations, you "should" have all your data still... assuming you didn't write to the disk and corrupt something... This was done as part of resizing partitions which involves a few more steps.

This is all speculation... but assuming you didn't write to the disk, I would guess you could repartition in exactly the same locations as before and regain your data.

After you do that, then you could reinstall grub.

BTW, if I remember correctly, to delete only the boot information, you need to use "bs=446". This will also destroy all executable code which should be fixed when you reinstall your boot loader. Lilo has a special option that safely overwrites this area and installs generic partition boot information with "lilo -M /dev/sdx mbr" don't know if grub has a similar command...

Last edited by lumak; 08-08-2010 at 10:50 PM.
 
Old 08-08-2010, 11:25 PM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
If you are still in debin and you can see how big the partitions are supposed to be including there start and end locations you can just recreate them. This will not remove the data on the partitions if you use cfdisk or fdisk to accomplish this task. Remember tho you will need to make the partitions exactly like they were.
 
Old 08-08-2010, 11:48 PM   #7
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 67
Are you still using the computer for which you've deleted the MBR? Don't reboot it!!!

Code:
$ cat /proc/partitions
$ cfdisk /dev/sda
Take a look at the current partition layout, and recreate them exactly with cfdisk
 
Old 08-09-2010, 04:08 AM   #8
kinematic
LQ Newbie
 
Registered: Dec 2005
Location: behind you
Distribution: Debian testing
Posts: 18

Original Poster
Rep: Reputation: 0
Here's the output from cat /proc/partitions but it only shows the number of blocks.
Quote:
major minor #blocks name

8 0 488386584 sda
8 1 20992965 sda1
8 2 13619200 sda2
8 3 13619200 sda3
8 4 1 sda4
8 5 20992000 sda5
8 6 416089088 sda6
8 7 3071776 sda7
sda4 is the extended partition. I also remember there is 1MB of free space between sda1 and sda2, sda2 and sda3, sda3 and sda4. I guess gparted did that automatically. So now that I know the blocksizes, how do I use that in fdisk or cfdisk to recreate the partitions?
 
Old 08-09-2010, 04:40 AM   #9
Freex
LQ Newbie
 
Registered: Oct 2006
Location: Belgium, Europe
Distribution: OpenSUSE 11.3 KDE, PCLinuxOS 2010 KDE
Posts: 29

Rep: Reputation: 17
I used the tool 'gpart' to restore my mbr once. It should be in your distro's package manager.

See http://www.brzitwa.de/mb/gpart/index.html
 
Old 08-09-2010, 04:45 AM   #10
kinematic
LQ Newbie
 
Registered: Dec 2005
Location: behind you
Distribution: Debian testing
Posts: 18

Original Poster
Rep: Reputation: 0
I tried gpart as well and let it run all night but it didn't find anything.
 
Old 08-09-2010, 04:53 AM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
I would use losetup to try to locate the start of the partitions. Also, If you haven't rebooted, run "df -B512" to double check that you have the sizes of the partitions in 512 byte blocks. For XP, the first partition would start on block 63. For Vista or Windows 7, it will start on block 2048.

sudo losetup -fs /dev/sda -o $((512*2048))
Code:
sudo /sbin/losetup -fs /dev/sda -o $((2048*512))
/dev/loop0
You have new mail in /var/mail/jschiwal
jschiwal@elite:~> sudo file -s /dev/loop0
/dev/loop0: x86 boot sector, code offset 0x52, OEM-ID "NTFS    ", sectors/cluster 8, reserved sectors 0, Media descriptor 0xf8, heads 255, hidden sectors 2048, dos < 4.0 BootSector (0x80)
Even if you are working from a live distro, now you could mount the first partition. Then run "fs -B512" to get the size of the filesystem. Using 512 byte blocks will eliminate rounding errors.

If the next partition doesn't start at the first offset + size, you could search for it in a loop:
Code:
for example:
for ((offset=206840; offset<206860; offset++)); do
   losetup /dev/loop2 /dev/sda -o $(($offset*512))
   echo -n "$offset :";file -s /dev/loop2
   losetup -d /dev/loop2
done | grep -v -e data -e ASCII

Last edited by jschiwal; 08-09-2010 at 04:55 AM.
 
Old 08-09-2010, 05:11 AM   #12
kinematic
LQ Newbie
 
Registered: Dec 2005
Location: behind you
Distribution: Debian testing
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
losetup -fs /dev/sda -o $((512*2048))
That doesn't do anything except output /dev/loop0 as a result. It doesn't send any mail?

Quote:
fs -B512
Hmmm, command not found...
 
Old 08-09-2010, 08:14 AM   #13
colorpurple21859
Senior Member
 
Registered: Jan 2008
Location: florida panhandle
Distribution: slackware64-current, arch, ubuntu, others
Posts: 3,555

Rep: Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584Reputation: 584
There is a cd called "ulimate bootable" CD that has a mbr recovery program on it that I've used in the past to restore partitions. Don't remember exactly which program It was as there are serveral programs on the cd.
 
Old 08-10-2010, 08:10 PM   #14
kinematic
LQ Newbie
 
Registered: Dec 2005
Location: behind you
Distribution: Debian testing
Posts: 18

Original Poster
Rep: Reputation: 0
Well, in the end I couldn't recover the partitions so I had to start over. It gave me the oppertunity to partition the right way with fdisk so that the partitions start and end on cilinder boundaries. After installing Debian and Win XP I made a backup of the mbr with dd just to be sure.
 
Old 08-16-2010, 06:17 PM   #15
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Quote:
Originally Posted by kinematic View Post
That doesn't do anything except output /dev/loop0 as a result. It doesn't send any mail?


Hmmm, command not found...
The /dev/loop0 output tells you which loop device was used. You use the loop device as you would a disk device node such as /dev/sdb1. Look at it with "sudo file -s /dev/loop0". If there is a filesystem, then mount the loop device. The "df" command is pretty standard.
Quote:
Originally Posted by df manpage
NAME
df - report file system disk space usage
Knowing the size of the first partition will give you an idea on which block the second partition starts. If you can locate each partition using LO setup, you can either mount the loop devices to backup your files, or use the offsets to repair your partition table.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can dd restore the entire hard disk including partition table and MBR? depam Linux - General 17 01-01-2007 08:05 PM
Newbie erased his MBR... michelbehr Linux - Newbie 9 01-09-2006 04:42 AM
Whether I can saving my system, lost MBR and partition table jiawj Fedora 2 09-16-2004 03:04 PM
Redhat with damaged MBR/Partition Table pawlub Linux - General 13 12-11-2003 08:51 AM
MBR & Partition Table ORiGiNAl-SiN Linux - Newbie 3 10-17-2003 06:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration