HOWTO: Restore Grub2

From Sabayon Wiki
Jump to: navigation, search
i18n: en tr

Let's reinstall Grub2 for whatever reason. Maybe you reinstalled Windows and lost Grub2 or you just plain old did something silly and lost Grub2. Remember, in Sabayon we are using Grub2 so it's a bit different than Legacy Grub. It's still pretty painless though and with about 5 minutes of time you will have your Grub2 reinstalled.

Quick Recovery

As outlined in the Gentoo Grub2 wiki page - Recovery section you can simply reinstall Grub2 to the MBR (Master Boot Record), linking to your original /boot location.

So, assuming your root partition is located on /dev/sda3 and you have a separate /boot on /dev/sda1 you would do the following:
(If you do not have a separate boot partition you do not need to do the second mount.)

# mkdir -p /mnt/sabayon/
# mount /dev/sda3 /mnt/sabayon
# mount /dev/sda1 /mnt/sabayon/boot
# grub2-install --root-directory=/mnt/sabayon /dev/sda

This tells Grub2 to install itself to the MBR on the /dev/sda device linking to the config found in /mnt/sabayon/boot.
You can then reboot and Grub2 will be back to normal - there's no need to re-create your config (see below) as it should still be undisturbed in your /boot directory.

Completely Reinstall and Re-Configure Grub2

From the article Chroot from a livecd in the Gentoo Wiki we can learn how to chroot into our system. So boot up the livedvd or other live disk, and get to a terminal, command line or konsole.

Make sure the livedvd you use is for the same architecture as the architecture of the installation on the hard disk, i.e. to chroot to a 64-bit installation (e.g. amd64) you cannot use a 32-bit livedvd (e.g. x86), and vice versa. You will see an error message "chroot: cannot run command `/bin/bash': Exec format error" if you try to chroot from a livedvd of a different architecture.

First a quick note on mounting filesystems. While it's possible to mount filesystems from within the chrooted system, this is not recommended. The reason for this is that the livecd environment won't know about these mounted systems, so if they are forgotten about and left mounted, they will not be unmounted properly when the system shuts down, which could cause damage to the filesystems on those mounts.

Mount the root partition (and, if you have /boot on a separate partition, mount that too) of the installed system. If separate partitions are used for other areas of the system (for example, a separate partition for /var/log) then these will also need to be mounted.

In the following example, /dev/hda1 is the /boot partition and /dev/hda3 is the root partition. Obviously replace those with the device names for your boot partition (if you have one) and your root partition. If your partition names are of the form /dev/sd<letter><number> rather than /dev/hd<letter><number> then obviously use that form instead. Obviously, if you do not have /boot on a separate partition to / (root) then you should omit the mount and umount commands referring to /boot.

Additionally, mount the /dev, /sysfs and /proc filesystems so that they can be used by the chrooted environment.

# mkdir -p /mnt/sabayon/boot
# mount /dev/hda3 /mnt/sabayon
# mount /dev/hda1 /mnt/sabayon/boot
# mount -t proc none /mnt/sabayon/proc
# mount -t sysfs sys /mnt/sabayon/sys
# mount -o bind /dev /mnt/sabayon/dev

So now we are set to enter into our installed system:

# chroot /mnt/sabayon /bin/bash
# env-update
# source /etc/profile
# export PS1="(chroot) $PS1"

You should then end up with the following prompt:

(chroot) #

Grub2 reads the /etc/mtab file to ascertain the filesystems that are currently mounted, so /etc/mtab must be up-to-date. If you do not have a separate boot partition then update /etc/mtab using the following command:

(chroot) # grep -v rootfs /proc/mounts > /etc/mtab

or the following command if you do have a separate boot partition:

(chroot) # cp /proc/mounts /etc/mtab

To install Grub2 with the Grub2 Stage 1 code written to the MBR of hda, use the following command:

(chroot) # grub2-install /dev/hda

or the following command if you want to install Grub2 with the Grub2 Stage 1 code written to the first sector of a separate boot partition hda1 (i.e. no Grub2 code written to the MBR):

(chroot) # grub2-install /dev/hda1

It should do its thing and it should tell you it has finished installing it.

grub2-install does not create/recreate the grub.cfg file (the equivalent to Grub Legacy's grub.conf/menu.lst file), so use the following command to create/recreate the grub.cfg file:

(chroot) # grub-mkconfig -o /boot/grub/grub.cfg

Now you can exit from the chroot environment and unmount the drives:

(chroot) # exit
# umount /mnt/sabayon/boot /mnt/sabayon/dev /mnt/sabayon/proc /mnt/sabayon

Now you can reboot to Grub2.

You can find more information in the article Grub2 in the Gentoo Wiki.