LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using DD to restore MBR (GRUB) doesn't work (http://www.linuxquestions.org/questions/linux-general-1/using-dd-to-restore-mbr-grub-doesnt-work-4175451542/)

Frank64 02-24-2013 06:54 PM

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!

bloody 02-25-2013 03:34 AM

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..

syg00 02-25-2013 03:39 AM

<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.

Frank64 02-25-2013 07:24 AM

Quote:

Originally Posted by bloody (Post 4899111)
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 (Post 4899113)
<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!

bloody 02-25-2013 03:28 PM

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

Frank64 02-25-2013 04:36 PM

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. :)

rng 02-27-2013 06:38 AM

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.

bloody 02-27-2013 08:01 AM

Quote:

Originally Posted by rng (Post 4900740)
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.. :D

darthaxul 08-16-2013 07:02 PM

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.


All times are GMT -5. The time now is 11:28 AM.