recovery - Use `dd` linux program to save / recover a disk's MBR

06
2014-04
  • Graduate

    I have an Ubuntu OS installed on my laptop. I want to install Windows 7 as well to another disk partition (I will do it by recovering it from a special partition on my laptop).

    After installing Windows, I want to recover my hard drive MBR to be able to load Ubuntu. I have a plan to use linux dd program:

    1) (Before installing, perform this command in Linux) dd if=/dev/sda of=/home/user/mbr_backup bs=512 count=1

    2) (after installing, load Ubuntu Live CD and launch this) dd if=/home/user/mbr_backup of=/dev/sda bs=512 count=1

    3) Load Ubuntu on PC and re-configure the GRUB2 to be able start Windows

    I need your advice, I want to be sure I won't damage the disk (it's partition table).

  • Answers
  • NoxHarmonium

    You are correct that the first 512 bytes of the drive contains the MBR and your method will restore it to the previous state but that is probably not the best course of action. Ubuntu should leave your MBR intact. As long as the partition table is still good and you can still see your partitions, just install grub as a bootloader. If you are using a modern Ubuntu version than you will probably have grub 2 which is great, it automatically detects Windows partitions and adds them to your grub list. Just run update-grub2. If you didn't install a boot loader with Ubuntu it might be a bit harder. Something like this would help.


  • Related Question

    recovery - Reinstalling GRUB on Linux netbook
  • Fergus

    I have a distribution called Easy Peasy on my netbook, it's Ubuntu-based.

    Today when I started it up it told me 'grub corrupt'. On subsequent start-ups it displays 'unknown filesystem.' I'm given a prompt labelled 'grub-rescue>' but I can't get any commands to work (even just 'help').

    I've tried booting off a Live CD of Easy Peasy. That works fine, but I can't get to my files. I've tried using a program called photorec and it can recover files from the drive but it dumps out gigs upon gigs of unlabelled files, many of which are things like system files or web browser cache -- I only have a few dozen text files I actually need, so this is pretty unworkable.

    I'm trying to reinstall grub, which I understand to be part of the booting process, but I've had no luck; any set of instructions I've followed has inevitably run into some error or a step I don't understand.

    How can I get at my files in an easy to recognise way (such that I can navigate the original directories and get what I want)?

    OR

    How can I easily reinstall grub such that I can just use the system like before without having to reinstall everything and lose my files?

    I think my drive is sda or sda0. In grub's device.map it's called hd0.

    I have almost no linux knowledge. Simple steps would be much appreciated.


  • Related Answers
  • Journeyman Geek

    Photorec is pretty bad about that. Unless something very bad happened to your disk (in which case testdisk - by the same people who wrote photorecmay help), any linux system should be able to mount and copy out your information. If not, you need to look at recovery...

    As with any recovery scenario, i'd strongly suggest imaging your disk before trying anything. Then if you cannot recover the contents of the disk, try running testdisk to try to recover the whole file system.

    Should that work you're likely to be able to recover your disk without any further steps, or at most recovering grub. To recover grub this process should help - its for ubuntu but easy peasy is a varient, and should work.

  • Douglas Leeder

    If you can't get the main partition mounted, you won't have much luck reinstalling grub.

    So let's first look at the disk:

    fdisk -l /dev/sda
    

    That should tell us if the disk is being recognized, and if the partition table is ok.

  • CarlF

    Assuming the drive is not corrupt or broken, the simplest thing would be to simply reinstall Easy Peasy. It only takes half an hour, and if you do not choose to format the drive it won't lose any of your data.