Backing Up the MBR October 20, 2005
Posted by Carthik in guides, ubuntu.trackback
I found myself in the situation of helping a friend reinstall windows on a system which also has Ubuntu on it. In other words, it was a dual boot system, and we were both afraid that re-installing Windows will overwrite the Master Boot Record (MBR) and make it impossible to boot into Ubuntu in the short term. So we devised a plan, with a little help from google to backup his MBR before reinstalling windows, and then restoring it after reinstalling windows.
Create a backup of your MBR by doing a:
$dd if=/dev/hdx of=MBR-backup bs=512 count=1
That should read “create a disk dump of the input file, which is /dev/hdx (change to hda, or hdb or sda, depending on where the MBR is on your computer), and save it in the output-file MBR-backup in the directory from where the command is issued. Backup the first sector only, while you are at it”.
Now that is the backup of your MBR. Restore it later using:
$dd if=MBR-backup of=/dev/hdx bs=512 count=1
Again, change hdx to hda, or hdb or wherever the MBR needs to be restored to. You may have to use a live cd to restore the backup since you will be unable to login to Linux after you reinstall Windows.
As with all other advice, take this with a pinch of salt, and search on google for a solution to your problem, understand the solution and then embark on your mission!







If you are going to need to use a Live CD to recover the backup MBR then you could have booted into rescue mode from the install CD (of any linux distro you choose) and mounted the linux partition, chrooted to the root partition and typed grub-install /dev/hdx.
mkdir /mnt/temp
mount /dev/hda3 /mnt/temp ## if hda3 was the linux partition
chroot /mnt/temp
grub-install /dev/hda
That would install the grub first stage boot loader to you the MBR of the hard disk and presented you with the same options you had before the Windows reinstall. no need for backup MBR records!
Just my two bobs worth - there is always more than one way to skin a cat.
Michael Manning
Thanks Michael!
Very useful post, like so many here. Thanks for putting together a great Ubuntu blog.
Note that the grub-install method has the added protection of not inadvertantly overwriting your partition table if it had been modified since your last MBR backup.
This is because the 512byte MBR sector is actually two parts:
1) The first 446 bytes is the grub stage1 bootloader (or the windows bootloader after you’ve reinstalled windows and it “helpfully” overwrites grub).
2) The last 64 bytes is where your partition table is stored.
So, if you only want to backup the bootloader in the MBR, remember to change the bs=512 to bs=446.
this is one of the “problems” of the single disk ubuntu … they actually don’t have a real rescue mode so you need a live-cd or another distro 1st cd …
they know it, since there are some posts on this subject dated some month ago in the archives …
If you are only going to backup the bootloader section of the MBR, please do it right and use 448 in the “bs” parameter (since 512 - 64 = 448, not 446).
So, the command would be something like,
$dd if=MBR-backup of=/dev/hdx bs=448 count=1
This is one of those times where those two little bytes DO matter…
Nevermind, the last two bytes of the MBR are always a constant, so it is 446…
disregard previous post!
Actually, there is a way to do this with the standard Ubuntu CD:
1) Go through the installer as normal. However, when it gets to the screen asking for options for partitioning, STOP. Don’t go any further or you are likely to screw Ubuntu (at least).
2) Press ALT+F2. It’ll bring up a command line, press Enter to activate it if it asks.
3) Make sure you are in the root of the filesystem.
# cd /
4) Make a directory to mount your hard drive in.
# mkdir target
5) Mount your Ubuntu partition to the directory.
# mount /dev/hdax /target
where hdax is your Ubuntu partition (hda1 for those that run Ubuntu only, likely hda2 for those of us that dual-boot, possibly sdax for SATA/SCSI/RAID/etc.)
6) Change the root directory to your hard drive.
# chroot /target
7) Reinstall grub.
Reboot. If everything goes well, you should be good to go.
# grub-install /dev/hdax
I can’t re-install my Windows2000. I have a hda1 partition of 10GB (fat32) free, and Ubuntu in other three partitions. GRUB is installed on the MBR and the /boot partition.
Even if I do a “fdisk /mbr” command before installing Win2k, after copying the files from the CD-ROM to the hard-drive, the windows installer won’t boot. With, or without grub, it doesn’t work. What can I do?
Ubuntu 5.10 has a rescue function which is very helpful after the reinstallation of windows.
1. Use the Ubuntu 5.10 installation CD to boot the computer.
2. Under “boot:” type rescue.
3. Wait the screen to bring up a list of all the partitions.
4. Choose the Ubuntu partition.
5. A command line will pop up.
6. Type grub-install /dev/hda.
7. Reboot.
Hi,
I have the same problem. I’ve installed Windows before Ubuntu, and now I’m stuck because i get a message in the screen that is very annoying:
something about a bug in xfs_freeze and stops the installation telling me that because /boot/grub is not an XFS filesystem it cannot continue the installation…
My configuration is:
one HDD:
hda1 linux
hda2 windows xp
hda3 linux
hda5 linux
No linux MBR, only windows MBR and it works.
Please help me to know what means this…
me restart computer and mount HDD
not mounted…. :((((((
i don’t know :(((
D partition Empyt not mounted C not mounted…
fucking restart
this is a command maybe don’t know pls help
\etc\rc.d\rc.local /dev/hda3 /mnt/temp ## if hda3 was the linux partition
and computer start this command start ?!
Their is a dirty way of doing this:
http://doc.gwos.org/index.php/Restore_Grub
Just make some faults in installeren when mounting the harddrives and skip all steps to GRUB and then just reboot!
Worked fine for me
Arno
I’ve also found another way, as the previous didn’t work for me…
With your solution I got:
root@ubuntu:/# grub-install /dev/hda1
/dev/hda1: Not found or not a block device.
root@ubuntu:/# grub-install /dev/hda
/dev/hda: Not found or not a block device.
So I did following to get it to work:
sudo grub // Start Grub
grub> root (hd0,6) // Partition with the Boot image
grub> setup (hd0) // Install Grub into the MBR
grub> quit
Très bonne explication !
J’ai guidé un ami par téléphone, en suivant cette page,
et il y est arrivé.
Merci
Very Good explication
I help a friend by phone, following this page
and he succeed to reinstall Grub
Ubuntu 6.06 LTS
Really Thanks,
After reinstalling windows, if you have a backup of your MBR, you could use dd for windows instead of using a live cd.
http://www.chrysocome.net/dd
http://unxutils.sourceforge.net/
The reason that you get a “Not found or not a block device.” when trying to run grub-install /dev/hdx or sdx is because after you chroot to /mnt/temp, the /dev you are referring to is on your defunct filesystem, therefore it is not active. I just threw Vista on my machine that already had XP and Kubuntu 6.06.1 LTS and after fixing GRUB, it works perfectly. To access either vers. of Windows, I just select the “Microsoft Windows XP Professional” link in GRUB and it actually takes me to Vista’s loader to choose which. Here is my successful procedure:
1. Boot to an available Linux prompt (live CD? Terminal.)
2. Make sure you are root or sudo your commands.
3. (sudo) mkdir /mnt/temp
4. (sudo) mount /dev/hdx (or sdx) /mnt/temp
5. (sudo) grub-install /dev/hdx –root-directory=/mnt/dev/boot/grub
That allows GRUB to access the device, yet it will store its settings and components where you want them, on your linux partition. Hope this helps.
Sorry, please note in that last message that line 5 should read:
5.(sudo) grub-install /dev/hdx — root-directory=/mnt/temp/boot/grub
make sure you use two dashes in front of root-directory. They look run together.
This was not written for Ubuntu, but also covers Grub and a Dual Boot scenario. You could use this if you wanted to use a Windows Boot Loader to boot Ubuntu. Obviously change the Fedora references to Ubuntu
http://www.johnspencer.org.uk/LinuxTutorials/DualBoot/dual_booting_windows_xp_and_fedora.htm
wher does fedora 5 saves the mbr of windows ? I suppose that the stage 1 ist in the first 446 bytes of MBR. I actually want to restore the mbr to contain the window loader an start fedora 5 from diskette not from hard disk?
Please disregard post 6. Anonymous Coward - October 30, 2005
since the command line written there is incorrect (actually will do a restore of MBR) and can prove FATAL!!!
$dd if=MBR-backup of=/dev/hdx bs=448 count=1
Instead it should be (for backup of MBR)
$dd if=/dev/hdx of=MBR-backup bs=448 count=1
Sorry, please note in that last message that line 5 should read:
5.(sudo) grub-install /dev/hdx — root-directory=/mnt/temp/boot/grub
make sure you use two dashes in front of root-directory. They look run together.
Please note that this is wrong again. The root-directory should be only /mnt/temp and NOT /mnt/temp/boot/grub. So the (succesfull) command is:
(sudo) grub-install /dev/hdx (or sdx) –root-directory=/mnt/temp
Help!
I was running a dual boot machine with Windows XP(my original OS) and Ubuntu 5.10 ( i installed it later) on a 40GB HDD, I also have an old 2GB HDD … all worked well till i added another 40GB HDD, now only ubuntu Boots, when i select the option for windows xp under grub menu at startup i get this:
Booting ‘Microsoft Windows XP Professional’
root(hd0,0)
Filesystem type unknown, partition type 0×7
savedefault
makeactive
chainloader+1
and then two ASCII characters one of them ASCII 212 and the other one a vertical box shaped smiley I cudnt find the ASCII code to…followed by a blinking cursor.
I have not made any changes whatsoever apart from adding the new hard disk, ubuntu shows it under disks.
thanks a ton for having this blog, any help on this is welcome
[...] Figure out how to fix Lilo/Grub when I break it with the Win install. [see here] [...]
i think this guide is a lot clearer
http://johnny.chadda.se/2007/02/17/restore-grub-in-ubuntu-after-installing-windows/
For what its worth - thanks for your update on the ways of doing the grub fix… almost sorted the problem out with my FC5.
This is what I did:
Boot FC5 in “linux rescue” mode.
Don’t worry about network interfaces
Click “continue” to mount /mnt/sysimage
DONT do a chroot!
copy grub for /sbin (there seems to be a bug with this one_)
cp /usr/sbin/grub* /sbin
grub-install /dev/sda –root-directory=/mnt/sysimage
reboot - problem fixed!
I have grub installed on an external USB hard disk (but it would work just as well on a floppy, cd, or USB thumbdrive). Whenever Windows decides to break grub for me, I just boot grub off the external disk. From there it’s simple:
grub > root (hd1,5) #My Ubuntu root partition; the usb drive is (hd0) because I booted from it
grub > setup(hd1)
And grub works again. It only takes about a minute.
In reply to 14. Carundi - July 21, 2006:
do not forget to empty /etc/mtab or insert relevant rows
After this step is funktional command grub-install /dev/xxx
Chain Load from the start and save the headaches…
Install GAG boot manager to the MBR, before or after, OS installs when ever you need to - if you….
Install Linux’s boot loader “grub” into /boot or / partition, NOT the MBR
Install Windows where ever but setup boot.ini correctly
re-install GAG boot manager if needed etc…
I use and recommend Super Grub, no matter how bad I trashed the mbr I can load the os of my choice (linux) and repair from there
Cart
[...] i copied the windows boot sector from the internal SSD to the boot sector of the SD using dd. See here. Then i mounted the SD card’s FAT partition in linux and modified the boot.ini file so it [...]
seven card on line
intersected baptismal phoning Simonson
christian grant for debt relief
abjection Remy.audit starlight:totally neckties
Thanks for the info guys.
debt consolidation pros and cons
squabbled outperforms,crags
nice little tutorial. imo the easiest way to backup the mbr (or any other part of devices) and way more fast and reliable to noobies than chrooting.