LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
 
LinkBack Search this Thread
Old 02-24-2013, 06:54 PM   #1
Frank64
Member
 
Registered: Feb 2008
Distribution: Kubuntu 11.10 64-bits
Posts: 70

Rep: Reputation: 15
Using DD to restore MBR (GRUB) doesn't work


Hi,

I've been using DD to backup and restore my partitions for years now and it's absolutely amazing.

I have backed up my MBR and MBR+Partition Table into a .img file each. In case I need to restore the MBR or Part Table. Actually for the part table sfdisk can make a backup in a txt file and you can reload it with "-d > [file.name]", that's awesome too.

So I use 2 IDENTICAL hard drives, only the serial number differs. The 2nd HDD is my backup and test HDD. I can image any partition and restore at any time.

Since I currently have GRUB2 in my backup/test HDD and GRUB LEGACY in my main, the 2 MBRs don't contain the same data, obviously. Now I want to bring back my test HDD to its original state which is an exact copy of my main HDD (my main HDD is failing! Hardware, it's dying). Due to the way I have partitioned my HDDs, I can simply restore my / partition and my MBR and I should be good.

Great cuz I have an image of both!
Wrong. I follow what I have found on internet and for some reason my MBR doesn't restore properly. I need to fire up the kernel using on sdb using sda's GRUB (sda=main HDD, the one failing).
Yes my sdb1 (root) partition is marked bootable. Don't get me wrong, both part tables of both HDDs are identical.

So restoring my sda1 to sdb1 partition works ALL the time. Then I have my distro restored.
I then can boot sdb1 from the GRUB menu of sda1, since I have a direct boot of the kernel on sdb1 (hd1,0) as menu entry on sda1 (within GRUB).
However, when I boot from my 2nd HDD only (sda disconnected) or if I chainload GRUB LEGACY on hd1 through GRUB LEGACY on hd0 it just says:

Code:
Starting up disk...
GRUB
And freezes there.

If I regenerate GRUB LEGACY using "setup grub" function, it copies GRUB to sdb and all works fine! My grub menu is back on sdb.

But I want to be able to use dd to restore MBR, I don't want to be dependent from setting up grub all the time... I'm missing something or doing something wrong but I can't figure it out.

Here's what I do to (try to) restore the MBR from sda or from image to sdb:

Option #1
Code:
dd if=/dev/sda of=/dev/sdb bs=446 count=1 conv=notrunc,noerror
Option #2
Code:
dd if=/dev/sda of=/dev/sdb bs=446 count=1
Option #3
Code:
After imaging the working MBR with dd,
dd if=/img/MBR.img of=/dev/sdb bs=446 count=1 conv=notrunc,noerror
Option #4
Code:
After imagine the working MBR with dd,
dd if=/img/MBR.img of=/dev/sdb bs=446 count=1
Option #5
Code:
dd if=/img/MBR.img of=/dev/sdb


All 5 options do the same result and it's still unbootable (GRUB doesn't load menu).


What am I missing?

tnx!
 
Old 02-25-2013, 03:34 AM   #2
bloody
Member
 
Registered: Feb 2013
Location: Berlin
Distribution: Gentoo, Debian
Posts: 158

Rep: Reputation: 22
The MBR does not contain the entire GRUB bootloader code. Most of the code (about 29 KB for GRUB2) follows in the next sectors which are usually 62 sectors until the first partition starts, giving the GRUB bootloader 31.5 KB free space to put it's core.img into.

Second, the location of the grub menu to be loaded is identified by a device ID, stored in the bootloader area.

Best thing you can do is, boot into the moved/restored system while the other harddisk is still connected, then run

grub-install --no-floppy /dev/sdX

..where sdX is your new harddisk where you've just booted from (e.g. sdb).

That will write a fresh and complete GRUB bootloader into the MBR + the follow-up sectors which will refer to the boot/grub directory on the system you've been running grub-install from.

After that you should be able to shutdown, remove the old harddisk and then boot from the new one.

Of course you can also use dd to copy bs=512 count=63 but that is a bit more delicate. You always need to make dam sure that you won't overwrite too much if you restore the data on a disk where the first partition starts earlier than expected.
I still resommend the grub-install method because grub won't touch disk sectors which are occupied by a partition.

Also, GRUB legacy and GRUB2 use a different menu syntax aswell as external modules and so on, so the one cannot be replaced by the other with just an MBR copy..
 
1 members found this post helpful.
Old 02-25-2013, 03:39 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 11,792

Rep: Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923Reputation: 923
<soapbox>
"dd" is absolutely the wrong tool for this. It's wrong for imaging a filesystem, and it's even wrong for dealing with the MBR.
</soapbox>

You may think you've got everything covered, but if there are any faults on the source (filesystem or hardware), "dd" will blithely proceed and neglect to tell you.
As for the bootloader, you already have the answer for that - chroot and re-install. That way you get a clean build.
For the partition table, I like sfdisk (manages logical partitions as well), but I prefer to re-create things as I want them then restore - from a filesystem aware backup.
Quote:
What am I missing?
Been a while since even grub classic has been able to keep it's code in sector zero. You would need to backup all the used sectors - and be aware that other software may also use some of those so-called "unused" sectors.
 
2 members found this post helpful.
Old 02-25-2013, 07:24 AM   #4
Frank64
Member
 
Registered: Feb 2008
Distribution: Kubuntu 11.10 64-bits
Posts: 70

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bloody View Post
The MBR does not contain the entire GRUB bootloader code. Most of the code (about 29 KB for GRUB2) follows in the next sectors which are usually 62 sectors until the first partition starts, giving the GRUB bootloader 31.5 KB free space to put it's core.img into.

Second, the location of the grub menu to be loaded is identified by a device ID, stored in the bootloader area.

Best thing you can do is, boot into the moved/restored system while the other harddisk is still connected, then run

grub-install --no-floppy /dev/sdX

..where sdX is your new harddisk where you've just booted from (e.g. sdb).

That will write a fresh and complete GRUB bootloader into the MBR + the follow-up sectors which will refer to the boot/grub directory on the system you've been running grub-install from.

After that you should be able to shutdown, remove the old harddisk and then boot from the new one.

Of course you can also use dd to copy bs=512 count=63 but that is a bit more delicate. You always need to make dam sure that you won't overwrite too much if you restore the data on a disk where the first partition starts earlier than expected.
I still resommend the grub-install method because grub won't touch disk sectors which are occupied by a partition.

Also, GRUB legacy and GRUB2 use a different menu syntax aswell as external modules and so on, so the one cannot be replaced by the other with just an MBR copy..
Ok that's interesting! Looks like I don't know the disk's structure like I thought I did. I will improve my knowledge on that.

Yes GRUB2 and LEGACY don't use the same syntax that's why I needed to bring back LEGACY, otherwise I would simply restore my root partition and leave the MBR+core.img untouched.

The thing with grub-install is that I still need to make sure I install either LEGACY or GRUB2, depending which one I want to restore. But in another way if it's the safest way rather than restoring the sectors, I'm better off the safest way. I don't need to restore GRUB often I must admit. Except that when I really need it I would like it to work. lolll So I'll change my method and use grub-install.

Or use a different tool...


Quote:
Originally Posted by syg00 View Post
<soapbox>
"dd" is absolutely the wrong tool for this. It's wrong for imaging a filesystem, and it's even wrong for dealing with the MBR.
</soapbox>

You may think you've got everything covered, but if there are any faults on the source (filesystem or hardware), "dd" will blithely proceed and neglect to tell you.
As for the bootloader, you already have the answer for that - chroot and re-install. That way you get a clean build.
For the partition table, I like sfdisk (manages logical partitions as well), but I prefer to re-create things as I want them then restore - from a filesystem aware backup.Been a while since even grub classic has been able to keep it's code in sector zero. You would need to backup all the used sectors - and be aware that other software may also use some of those so-called "unused" sectors.
That's interesting as well. Definitely I was too much ignorant about disk's structure, I apologize for that.

I agree dd is a hardcore tool, yes. Maybe more than I thought.

Any ideas of a different tool that would be safest and perform as you suggest?


Tnx a lot guys for that, really helpful!

Last edited by Frank64; 02-25-2013 at 10:44 AM.
 
Old 02-25-2013, 03:28 PM   #5
bloody
Member
 
Registered: Feb 2013
Location: Berlin
Distribution: Gentoo, Debian
Posts: 158

Rep: Reputation: 22
On all of my PC's i got a small partition at the end with a Debian-stable console with a bunch of lowlevel maintenance tools. That system also deals with GRUB(2) and the booting of everything else. I also have another system like that on a USB stick, in case something goes wrong with the entire GRUB thing, leaving me unable to boot anything. Then i boot from the stick and restore the bootloader from there. One can't be paranoid enough. :P
 
Old 02-25-2013, 04:36 PM   #6
Frank64
Member
 
Registered: Feb 2008
Distribution: Kubuntu 11.10 64-bits
Posts: 70

Original Poster
Rep: Reputation: 15
Yes I know that, that's how I am too. And it prove me right when my main HDD just failed on me!

Then I'll definitely keep a CD or USB key with the minimal stuff and instead of playing around too much with dd I'll use that bootable media to do a fresh reinstall of GRUB and it's required support stuff.
 
Old 02-27-2013, 06:38 AM   #7
rng
Member
 
Registered: Aug 2011
Posts: 746

Rep: Reputation: 23
One may find things to be much simpler if only grub legacy is used. I have similar small partition on hard disk as well as usb install but both with grub legacy.
 
Old 02-27-2013, 08:01 AM   #8
bloody
Member
 
Registered: Feb 2013
Location: Berlin
Distribution: Gentoo, Debian
Posts: 158

Rep: Reputation: 22
Quote:
Originally Posted by rng View Post
One may find things to be much simpler if only grub legacy is used. I have similar small partition on hard disk as well as usb install but both with grub legacy.
You know what's funny? I only use GRUB2 instead of the old GRUB because of one reason: it can display a 24-bit JPEG in a decent resolution as bootsplash. Otherwise, i've learned to hate GRUB2. Insane installation size, nothing works right, real pain in the butt - yet, i got my 24-bit picture..

Last edited by bloody; 02-27-2013 at 08:48 AM.
 
Old 08-16-2013, 07:02 PM   #9
darthaxul
Member
 
Registered: Aug 2008
Distribution: Gentoo; FreeBSD
Posts: 195

Rep: Reputation: 18
good grub

Had similar issue after copying install to new machine, thought it would work by just copying files but no... Set up grub boot partition on disk one and OS on disk two. Booting from grub showed file not found. Used live cd to dd with count=63, grub showed this time but still not loaded menu. Went up to count=128 and now grub showed with menu but now the filesystem got bungled. reformatted it again and copied over grub files and it works.
 
  


Reply

Tags
dd, grub, mbr


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dual boot problem: GRUB to MBR doesn't seem to work newuser98 Fedora - Installation 5 12-14-2010 02:40 AM
Need to restore the MBR's GRUB after reinstallign Win XP jako-r Linux - Newbie 3 09-27-2009 09:34 AM
Dual-booting: GRUB on the /boot partition instead of the MBR doesn't work for me jem Linux - Newbie 3 11-26-2008 09:38 AM
restore MBR to use GRUB after OS rebuild goBoating Linux - General 3 06-16-2006 08:54 AM
restore mbr (in lilo and grub) abs Slackware 5 02-07-2004 02:39 PM


All times are GMT -5. The time now is 02:31 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration