I have a (non-UEFI) Gentoo Linux system, and I am preparing a hard drive for another system. I have prepared partitions for the new system, and now I want to make it bootable. I remembed that in legacy GRUB, I used these commands:

# grub
grub> root (hd0,0)
grub> setup (hd1)

This made a bootsector on current (hd1), which pointed to (hd0). Then, when I booted the system, the new disk became the first device (i.e. hd0) and everything worked.

The only thing I was able to find for GRUB2 is grub2-install /dev/sdb, which installs the MBR pointing to UUID of some current volume, which of course fails when I move the disk to a new machine.

So, how do I make a bootable HDD with GRUB2?


Just found out that as GRUB2 uses UUIDs for partition ID, all you have to do is mount target /boot partition somewhere (let's say /mnt/new-boot) and then tell that to GRUB using --boot-directory:

grub2-install --boot-directory=/mnt/new-boot /dev/sdb

Just doing grub-install won't be enough, you will have to update-grub or grub-mkconfig unless you have your own initial grub.cfg. I have to run this from a chroot jail for it to work. Here is a snippet adapted from a script I wrote to do this (altered form is untested):

# assumes /dev/sdb1 is the target root and
# /boot doesn't have to mounted separately
mount /dev/sdb1 /mnt

grub-install --boot-directory=/mnt/boot /dev/sdb

mount --bind /proc /mnt/proc
mount --bind /sys /mnt/sys
mount --bind /dev /mnt/dev
chroot /mnt update-grub
umount /mnt/proc /mnt/sys /mnt/dev

update-grub may be Debian specific, according to the manual it is just a stub for:

grub-mkconfig -o /boot/grub/grub.cfg
  • I already had a working grub.cfg, but it's good to know this for future reference. – che Jan 13 '14 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.