# Two FreeBSD installations and EFI



## aragats (Feb 6, 2020)

Goofy question: How to add an EFI entry for a second FreeBSD installation?
I have two FreeBSD installation on the same disk partitioned this way:
1. EFI
2. FreeBSD 12.1
3. FreeBSD 13.0


----------



## memreflect (Feb 6, 2020)

The script below is a variation of what is used in bsdinstall's bootconfig script (revision 356740) as you already know which partition is the EFI System Partition (ESP) and don't need to create a new filesystem for it, and you are dealing with multiple installations instead of just one:

```
#!/bin/sh
set -x

# Change this to ada1, ada2, etc. if necessary.
# If using MBR instead of GPT, you'll need to adjust all mount commands
# to use slices (ada0s1a) rather than partitions (ada0p1).
# The addefi function calls at the end will need to be fixed as well.
disk="ada0"
dosmnt=$(mktemp -d /tmp/stand-test.XXXXXX)

# usage: addefi $version $partition_num
addefi() {
    test "$#" -lt 2 && exit 1
    mount "/dev/${disk}p$2" /mnt
    p="/${dosmnt}/EFI/freebsd-$1"
    mkdir -p "$p"
    cp /mnt/boot/loader.efi "$p"
    efibootmgr --create --label "FreeBSD $v" --loader "$p"/loader.efi >/dev/null
    efibootmgr --activate $(efibootmgr | grep "FreeBSD $v" | sed 's/.Boot\(....\).*$/\1/')
    umount /mnt
}

mount -t msdosfs "/dev/${disk}p1" "${dosmnt}"
addefi 13.0 3
addefi 12.1 2
umount "${dosmnt}"
```

You may want to fix the boot order as well (see the `-o` option of efibootmgr(8) and the example(s) using it).  I reversed the order specifically because the boot order will automatically default to the last activated EFI entry.

Edit: Fixed the efibootmgr activation line that didn't work.


----------



## aragats (Feb 6, 2020)

Thanks, I already tried all those `efibootmg` commands manually, they do create another entry in EFI, but how would loader.efi know which partition to load FreeBSD from?


----------



## tingo (Feb 6, 2020)

Ah, now you are asking the right question! If you look at uefi(8), you will see that it states:
"boot1.efi searches partitions of type freebsd-ufs and freebsd-zfs for loader.efi.  The search begins with partitions on the device from which boot1.efi was loaded, and continues with other available partitions.  If both freebsd-ufs and freebsd-zfs partitions exist on the same device the freebsd-zfs partition is preferred.  boot1.efi then loads and executes loader.efi."
and
"loader.efi loads and boots the kernel, as described in loader(8)."

In other words: boot1.efi (well, the current implementation of it anyway) will _always_ boot from the first freebsd-[zfs|ufs] partition it finds, and you can't change that. 
If you want to boot from a different partition, you must manually enter the bootloader and input commands to change which device it boots from (set currdev=...). Every time you boot.

Quite a change (a POLA violation, IMHO) from the old gptboot loader, where you could use gpart(8) flags (bootme, bootonce) to control and change the boot order.


----------



## Phishfry (Feb 6, 2020)

What about the hardware aspect.
Since the BIOS will read the ESP it should see both installations. So you should be able to pick your boot order from the BIOS.


----------



## aragats (Feb 6, 2020)

Thanks tingo !


tingo said:


> will _always_ boot from the first freebsd-[zfs|ufs] partition it finds


That means the only way to force the automatic boot is changing partitions' indices, right?



Phishfry said:


> Since the BIOS will read the ESP it should see both installations


The BIOS sees EFI, and reads its entry. Any entry for FreeBSD on the same disk will cause the loader booting from the first found partition ― as tingo explained.
So, booting with a different from EFI scheme may work, but that requires re-organizing of everything.


----------



## aragats (Feb 7, 2020)

More experiments:
1. Changing partition index is not easy since there is no such function in gpart(). So, the partition has to be deleted and recreated with a different index, and that works for changing the boot partition.
2. The attribute "bootme" is ignored, maybe this should be filed as a bug report/feature request, since it's a real deficiency.
3. Changing the partition type doesn't affect the functionality: boot1.efi is smart enough to ignore e.g. "linux-data" type set on "freebsd-ufs" partition.


----------



## tingo (Feb 8, 2020)

and there is a bug report with patches already (but I haven't tested it) https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=207940


----------

