tags:

views:

2694

answers:

3

My PC has a hard disk at /dev/hda and boots from its MBR using the GRUB bootloader.
I connected a second hard disk at /dev/hdc, formatted it and copied to it all files from my original hard disk.
Now I want to make the second hard disk bootable, when it is connected as the primary hard disk (i.e. as /dev/hda).

(The second hard disk has different partitioning, so dd if=/dev/hda of=/dev/hdc won't cut it.)

Note that in both disks, the first partition (/dev/hda0 and /dev/hdc0) is the /boot partition, which includes the Linux kernels and ./grub subdirectory.

My naive attempt to use the commands:
grub> root (hd1,0)
grub> setup (hd1)
while the cloned hard disk was /dev/hdc (known as hd1 to grub) caused garbage to be displayed when booting from the cloned hard disk (when connected as /dev/hda).

What is the correct way to configure grub on a cloned hard disk, if one does not want to boot the PC from a diskette or CD-ROM due to any reason?

A: 

If /boot on both hdds are equal - i.e. they contain stage 1.5 and 2 of grub, then just copying the MBR should solve your problem.

Note - first make backup of your MBR in case something goes wrong.

Now, with your "new" hdd as /dev/hdc:

# dd if=/dev/hdc of=backupMBR bs=512 count=1  <-this makes backup

# dd if=/dev/hda of=/dev/hdc bs=512 count=1 <-this copies the MBR from hda to hdc

You should be all set, replace the drives and test.

Sunny
1. Currently, /dev/hdc is not bootable - no need to keep backup of its MBR.2. If stage 1.5 of grub is located at different sectors in /dev/hda and /dev/hdc, then the MBR needs to be different.
Omer Zak
As the asking person pointed out, the other stages are not on the same position, the partition was copied file by file
ypnos
+1  A: 

root is where it will try to find the grub partition while booting. So, if you say root (hd1,0), you are saying that after booting it should look for the grub partition on the second drive.

You can use rootnoverify to set the correct root partition (probably (hd0,0) if it's the first partition on the disk). You then probably will need some extra parameters to the setup command (to tell it where to find the images); see the manual for that command for details.

Of course, repeat your test (with only the second disk connected) after it's done. If it gets to the grub menu (if you are using one), it's working enough (you do not need to actually test booting to the operating system, since if the commands are wrong you can just use the grub command line to run the correct ones by hand).

CesarB
+2  A: 

The best way to do this is to install GRUB in a "sane" environment - the one it has to run later on. Grub has to find it configuration files on the right partition etc. pp. and whatever made your approach fail, chances are that something went mangled up because you tried to set it up from outside the target environment (which I don't want to say would not work in general).

Here's the deal to do a clean GRUB installation:

Mount the root partition of the target disk:

mount /dev/hdcX /mnt/new

Make dev/ there working:

mount --bind /dev/ /mnt/new/dev

Also include the new boot partition:

mount /dev/hdc0 /mnt/new/boot

Don't worry - /dev/hdcX won't get mangled up; directorys are just temporarily shadowed.

Now use chroot to work in the target environment:

chroot /mnt/new

Inside chroot, use grub-install, which provides a clean installation:

grub-install /dev/hdc

Shut the system down and try booting from the new hard drive.

Note #1: The method I described is also the best suited to restore a GRUB installation which was destroyed e.g. by Windows install.

Note #2: The best way to clone a hard drive is not to copy over files, but instead use dd (or even cat) to copy over the whole partition. This can also be done over the network, piping to netcat (or using g(un)zip in the middle, for less bandwidth usage).

ypnos