Notes on fixing a broken Gentoo grub install…

Last night I switched HDs on my Gentoo laptop. In the process of doing this, I reset the bios… this was a bad idea, as I had used efibootmgr to setup my UEFI firmware to boot Linux.

Now, at this point the reasonable thing to do is boot from a usb stick, chroot into the Gentoo install and either re-run efibootmgr or install grub… I didn’t have a usb stick/other installation media handy… I did however have a SSD with Debian installed. My laptop only has 1 SATA bay however (both the Gentoo install and the Debian install are on SATA drives). These notes document what I did, in case I need to do this again in the future…

So… I decided to boot into Debian. Create a ramdisk with a working Debian install, chroot to this. Then swap in the Gentoo SSD, chroot to the Gentoo install and install grub. This post documents my notes on this process.

First boot into Debian, then as root create a ramdisk.

swapoff
mount -t tmpfs -o size=2048m none /mnt/ramdisk

I probably could have used debbootstrap or something else… but I decided to copy everything I needed across manually. First I setup the dev,sys and proc directories:

cd /mnt/ramdisk
mkdir dev
mkdir sys
mkdir proc
mount --rbind /dev ./dev
mount --rbind /sys ./sys
mount --rbind /proc ./proc

Now copy across files needed for a more or less working system:

cp -r /etc .
cp -r /bin .
cp -r /sbin .
mkdir usr
cp -r /usr/bin ./usr
cp -r /usr/sbin ./usr
cp -r /lib64 .
cp -r /lib .

In order to setup grub, I also needed the vfat module and 437 codepage modules. It’s easier to load them now…

modprobe vfat
modprobe nls_cp437

Next we pivot_root to use our new ramdisk… I guess chrooting will work too…

mkdir /tmp/ramdisk/old_root
pivot_root /tmp/ramdisk /tmp/ramdisk/old_root

You can also chroot into the ramdisk… but if you exit and re-login you should be in the new root (you might need to restart login…).

I now pulled out the Debian SATA drive (there’s probably a way to umount it cleanly… I didn’t… this is bad don’t do it). And inserted the Gentoo drive.

Mount the gentoo drive somewhere and chroot into it:

mkdir gentoo;mount /dev/sdb5 ./gentoo
cd gentoo
mount --rbind /dev ./dev
mount --rbind /sys ./sys
mount --rbind /proc ./proc
chroot gentoo

Within the gentoo install you should be able to install grub, or do whatever else you want to do. For me the grub install commands are:

mount /boot
grub-install /dev/sda --target=x86_64-efi --efi-directory=/boot --removable
grub-mkconfig -o /boot/grub/grub.cfg

My laptop (Lenovo x230) doesn’t boot unless I used –removable.