Thursday, August 9, 2018

Restoring Grub - dual boot FreeBSD and Linux

This note may be specific to my setup:

FreeBSD 11.2 running root on ZFS alongside Linux Mint 18.2
5 gpt partitions:
1. freebsd-boot 128K
2. freebsd-swap 17G
3. freebsd-zfs 63G
4. linux-ext4 137G
5. linux-swap 17G

Notice that there isn't a bios-boot or efi boot.

When I installed Mint, FreeBSD was already installed. The Mint installer happily installed its root system into /dev/sda4 and the grub boot manager into /dev/sda. I had to create a 40_custom file in /etc/grub.d and tell it how to find my freebsd instance, but once that was done and update-grub did its thing, I got dual boot working.

Fast forward a few hours and I thought running this command was a good idea:

gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 ada0

But, this kinda hurt because the FreeBSD boot loader was back in charge and it knows nothing about linux on a gpt partition. So, I was off to figuring out how to restore grub. After a lot of scouring the Internet and some painful re-and-re-re-installations, I figured out how to get it restored.

Thankfully, mint keeps a record of the installation in /var/installation/syslog. By reading this file, I found the magic incantation that was required. --force, an argument that should be self explanatory was the solution...

Here is the procedure that worked:

1. Boot the T430 from the Linux Mint live install media
2. Fire up a Terminal and run these commands

sudo -s                                                         # enter a root shell
mount /dev/sda4 /mnt                                            # mount the linux-ext4 root fs
for i in /dev /dev/pts proc sys; do mount -B $i /mnt/$i; done   # mount dev et al in /mnt for chroot
chroot /mnt                                                     # enter the chroot for the root fs
chmod a-x /mnt/etc/grub.d/30_os_prober                          # don't do additional probing
update-grub                                                     # update grub
grub-install --force /dev/sda                                   # if you don't force, will fail due to gpt
exit                                                            # to exit the chroot
exit                                                            # to exit the root shell
exit                                                            # to exit the user shell

Reboot, and voila! Grub is back.

For reference, here's the 40_custom file (I mount the dataset zroot/ROOT/default as / in FreeBSD:

menuentry "FreeBSD" --class freebsd --class bsd --class os {
insmod zfs
insmod part_gpt
search -s -l zroot
kfreebsd /ROOT/default/@/boot/kernel/kernel
kfreebsd_module_elf /ROOT/default/@/boot/kernel/opensolaris.ko
kfreebsd_module_elf /ROOT/default/@/boot/kernel/zfs.ko
kfreebsd_module /ROOT/default/@/boot/zfs/zpool.cache type=/boot/zfs/zpool.cache
set kFreeBSD.vfs.root.mountfrom=zfs:zroot/ROOT/default
set kFreeBSD.hw.psm.synaptics_support="1"

No comments:

Post a Comment

All comments are moderated. Use discretion.