Reinstalling GRUB2 over an existing (and un-bootable) CentOS 7 box

Every now and then, you might need to “recover” from your un-bootable CentOS. I’m not referring to serious and/or hardware problems, like broken HDD or corrupted filesystem. I’m referring to changes in “geometry” of your HDD/partitions, changes in partitions UUIDs, switch from MBR to GPT, etc. etc..

In other words, I’m referring to system changes that even if not impacting your data (that are still there, inside your HDD partitions), led you to an “unbootable” system.

Well, in such a scenario, it’s really easy to boot your system with a live-cd like systemrescuecd or clonezilla-live. Once booted, you can easily mount your “old” system within some mount-point within your live-os filesystem, with something like:

mkdir /mnt/temp 
mount /dev/xvda2 /mnt/temp/
mount /dev/xvda1 /mnt/temp/boot 
mount --bind /proc /mnt/temp/proc
mount --bind /sys /mnt/temp/sys
mount --bind /dev /mnt/temp/dev

(btw: here my old system had a /dev/xda1 partition mounted as /boot and a /dev/xvda2 mounted as /)

So, now, you have all of your previous filesystem under /mnt/temp and you’re free to check, get and modify whatever you want.

As you’re here ’cause your system is un-bootable, chances are high that you want to reinstall GRUB2 (BTW: we’re under a CentOS 7 box, so we have GRUB2 as the bootloader).

Should your system be an Ubuntu, you have the nice update-grub tool: just launch it and… it quickly and automatically goes through all the required steps.

But unfortunately, there is no update-grub in CentOS.

After some search, I finally found the official alternative for CentOS: grub2-mkconfig. With it, things are as easy as:

grub2-mkconfig -o /etc/grub2.conf

As you can see:

  1. such a command refer to a grub2.cfg stored within /etc and…. as for the above mounts, our grub2.cfg is in /mnt/temp/etc/grub2.cfg.
  2. the grub2-mkconfig executable is the one whose full-path is relative to the “old” filesystem hierarchy

and so you might wonder: “how can I access the my old filesystem, as if it was “/“, as currently it’s mounted under /mnt/temp?”. The answer is simple: just use “chroot”. A simple:

chroot /mnt/temp /bin/bash

will move your “root” filesystem pointer to the current “/mnt/temp” and, once moved, just launch the “/bin/bash“.

Right after such command, you’ll have a “bash prompt”, that believes to live in your “old” filesystem, and so both the grub2-mkconfig and /etc/grub2.conf will be properly recognized.

Hope this help!

P.S.: credits for some of above concepts goes to this question and answer



Leave a Reply

Your email address will not be published. Required fields are marked *