Linux GRUB – How to Install GRUB on a New Drive

grub-legacylinuxvirtualbox

I have a virtual box running with CentOS.

I have attached a new virtual disk to the existing CentOS VM and I'm now trying to install GRUB on this newly attached disk.

Later, I will bring up a second VM with a newly prepared bootable hard disk with a custom root filesystem and kernel.

I have tried the following steps:

  • Attached a new virtual disk to the existing working CentOS machine.
  • Created a new partition with fdisk /dev/sdb. While partitioning, I chose the options primary partition, partition number 1 and other default options.
  • Formatted the disk with mkfs.ext3 /dev/sdb1.
  • Mounted the disk to /media/new_drive.
  • Installed GRUB grub-install /dev/sdb1 --root-directory=/media/new_drive/.

After this, the second VM with the newly prepared hard disk didn't boot; I got the error: could not read from the boot medium. It seems the MBR is not updated after grub-install, but I can see GRUB installed under /boot/grub on the new drive.

But the worst thing is, it has corrupted my existing CentOS GRUB: The CentOS VM hangs showing a black screen with the only text being GRUB.

Why does grub-install /dev/sdb1 not modify the MBR of sdb1? Is this not the right way to install GRUB on new drive?

Best Answer

I'm not a grub2 expert (sorry) but try adding --skip-fs-probe to your grub-install line, I have found this prevents creation of /boot/grub/device.map which can cause booting to a grub prompt. I think that without this parameter grub-install, instead of doing what you tell it, thinks it is cleverer than you, and may do something different.

Another thing is to be sure you are using the right grub-install (i.e. for grub2 and not for original grub). This isn't a problem if you are inside Centos but with SystemRecoveryCD both versions are available and so you have to use grub2-install. I learned the hard way...

And as @wurtel pointed out (kudos), you should specify a drive not a partition. Grub2 installs in sector 0 of the whole disk drive, and this 'stub' is what runs at boot time, but it needs to know whereabouts on the disk it should install the files for the next stage of booting - this is what the --root-directory parameter is for. (I think.)

Reading man grub-install and googling I see that --root-directory is not really meant to be used for grub2 versions 1.99++, though it does work in my experience. You are meant to use --boot-directory and refer to the actual boot directory, so this would give you:

grub-install /dev/sdb --skip-fs-probe --boot-directory=/media/new_drive/boot
Related Question