How to install GRUB on a new drive?
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
This is how I moved a Debian installation consisting of a boot partition /boot
and a root partition /
to a new drive and made it bootable using GNU GRUB:
Clone partitions
- Using the GParted live CD, create the boot and root partition on the new drive.
- Using a root console in GParted, mount the old boot partition (let's say it's
/dev/sda1
) and the new partition (/dev/sdb1
):mount /dev/sda1 /mnt/oldBoot && mount /dev/sdb1 /mnt/newBoot
- Copy the data from the old boot partition to the new one:
cp -afv /mnt/oldBoot /mnt/newBoot
. Explanation ofcp -afv
:a
stands for "archive" which means:- don't dereference links
- copy recursively (like
-R
) - preserve all attributes (timestamp, owner, permission)
f
: force, if an existing destination file cannot be opened, remove it and try againv
: verbose, explain what is being done
- This will generate some output on your console showing you which file is currently copied and whether
cp
makes progress - Mount and copy the files from your old root partition to the new one:
- For safety:
umount /mnt/oldBoot && umount /mnt/newBoot
- Mount the old and the new root partition (assuming it's
/dev/sda2
and/dev/sdb2
):mount /dev/sda2 /mnt/oldRoot && mount /dev/sdb2 /mnt/newRoot
- Copy the data of the old root partition to the new root partition:
cp -afv /mnt/oldRoot /mnt/newRoot
- For safety:
- Edit the filesystem table that defines which partitions are mounted on boot (I'll use Vim for that):
vi /etc/fstab
You'll notice that your old partitions are referenced here. Use the UUIDs of the new partitions instead
You can temporarily insert all UUIDs into fstab for easier copy and pasting with
:r !blkid
Use the UUIDs of your new partitions to change the entries in fstab. They should look something like this:
`# <file system> <mount point> <type> <options> <dump> <pass> # Root partition UUID=76fd1ffd-fb96-4ab4-be1a-42f8e9223983 / ext4 errors=remount-ro 0 1 # Boot partition UUID=e560e29e-8752-4b83-b1ee-4b86c0009f0b /boot ext2 defaults 0 2`
- Remove the output of
blkid
from fstab that you inserted earlier with:r !blkid
- Remove the output of
Install GRUB
Mount the virtual filesystems of the GParted live CD:
mount --bind /dev /mnt/newRoot/dev mount --bind /proc /mnt/newRoot/proc mount --bind /sys /mnt/newRoot/sys
Make the GRUB utilites of the GParted live CD available to the root partition:
mount --bind /usr/ /mnt/newRoot/usr
Mount the boot partition onto the root partition since GRUB will store its configuration in
/boot
:mount /dev/sdb1 /mnt/newRoot/boot
Use
chroot /mnt/newRoot
to make the new root partition temporarily the root of the filesystemCreate a GRUB config file at
/boot/grub/grub.cfg
usingupdate-grub2
Install GRUB on the new drive:
grub-install /dev/sdb
. You mustn't specify a partition number hereReturn to the filesystem of the GParted live CD:
exit
Unmount partitions:
umount /mnt/newRoot/*
Shutdown machine
If you have multiple drives attached, make sure the newer drive comes first in the boot order
Start machine
These instructions are inspired by those of oaktreepeak.com.
Alternatively, you can give Clonezilla a try to achieve the same.