Solved How to restore boot loader?

It's important to know if your bootcode is UEFI or BIOS.

Code:
% gpart show ada0
=>       40  937703008  ada0  GPT  (447G)
         40     409600     1  efi  (200M)
     409640  928628736     2  freebsd-ufs  (443G)
  929038376    8388608     3  freebsd-swap  (4.0G)
  937426984     276064        - free -  (135M)

If the first partition is 'efi', like above, you have UEFI bootcode in first partition and MBR is clear/not used.
If it were 'freebsd-boot', than it is BIOS bootcode inside that partition and an entry inside the Master Boot Record/sector 0 as well.
Size of first partition doesn't really matter.
Examples assume you use GPT partitioning.

Create UEFI bootcode:
gpart bootcode -p /boot/boot1.efi -i1 ada0

Create BIOS bootcode:
gpart bootcode -b /boot/pmbr -p /boot/gptboot-i1 ada0
 
It's important to know if your bootcode is UEFI or BIOS.

Code:
% gpart show ada0
=>       40  937703008  ada0  GPT  (447G)
         40     409600     1  efi  (200M)
     409640  928628736     2  freebsd-ufs  (443G)
  929038376    8388608     3  freebsd-swap  (4.0G)
  937426984     276064        - free -  (135M)

If the first partition is 'efi', like above, you have UEFI bootcode in first partition and MBR is clear/not used.
If it were 'freebsd-boot', than it is BIOS bootcode inside that partition and an entry inside the Master Boot Record/sector 0 as well.
Size of first partition doesn't really matter.
Examples assume you use GPT partitioning.

Create UEFI bootcode:
gpart bootcode -p /boot/boot1.efi -i1 ada0

Create BIOS bootcode:
gpart bootcode -b /boot/pmbr -p /boot/gptboot-i1 ada0

Thank you friend.
So this would be done from a live disk?
What about zfs with raid?
 
No, you can modify bootcode at all time, no need for a live disk or single user mode.

For zfs:
To create UEFI bootcode to boot zfs, you use the same bootcode/command as above:
gpart bootcode -p /boot/boot1.efi -i1 ada0

To create BIOS bootcode to boot zfs:
gpart bootcode -b pmbr -p /boot/gptzfsboot -i1 ada0

Remember it is allways a good idea to install bootcode on every disk in a pool.
 
Remember it is allways a good idea to install bootcode on every disk in a pool.

Slight correction / adding specificity. It's important to install bootcode on every disk in a bootable pool (aka root-on-zfs setups). There's no need to have bootcode installed on drives that are never, ever meant to be booted from.

For example, all our storage servers at work have 2 ZFS pools:
  1. a system pool that's bootable (usually just a mirrored vdev on SSDs)
  2. a storage pool made up of dozens (90 in the biggest ones) of drives
The SSDs in the system pool include bootcode; the harddrives in the storage pool don't.
 
It's important to know if your bootcode is UEFI or BIOS.

Code:
% gpart show ada0
=>       40  937703008  ada0  GPT  (447G)
         40     409600     1  efi  (200M)
     409640  928628736     2  freebsd-ufs  (443G)
  929038376    8388608     3  freebsd-swap  (4.0G)
  937426984     276064        - free -  (135M)

If the first partition is 'efi', like above, you have UEFI bootcode in first partition and MBR is clear/not used.
If it were 'freebsd-boot', than it is BIOS bootcode inside that partition and an entry inside the Master Boot Record/sector 0 as well.
Size of first partition doesn't really matter.
Examples assume you use GPT partitioning.

Create UEFI bootcode:
gpart bootcode -p /boot/boot1.efi -i1 ada0

Create BIOS bootcode:
gpart bootcode -b /boot/pmbr -p /boot/gptboot-i1 ada0

I'm trying this now, with
gpart bootcode -p /boot/boot1.efi -i1 nvd0

and I get an error like this:
gpart: /dev/nvd0p1: Operation not permitted

Any ideas what to do? Google results aren't leading anywhere for me.
 
I've not done UEFI stuff, but I don't think that command is the correct one for UEFI.
If I knew what it should be I would tell you but I don't.
 
Do I have to use a rescue / install disk ? I'm still getting Operation not permitted. I read somewhere (above) that it wasn't required to boot up the install medium.
I'm doing this:
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 nvd0

gpart show -p

=> 40 976773088 nvd0 GPT (466G)
40 532480 nvd0p1 efi (260M)
532520 2008 - free - (1.0M)
534528 67108864 nvd0p2 freebsd-swap (32G)
67643392 909129728 nvd0p3 freebsd-zfs (434G)
976773120 8 - free - (4.0K)

EDIT: I don't know what happened -- I used efibootmgr improperly and deleted the freebsd efi. Went through all the mentioned steps, kept getting Operation not permitted. Finally had to shutdown laptop because I had to leave work for the day. Did a reboot to see if it would boot, and it wouldn't. So when home, I put in the freebsd install img on usb, and I got the GELI prompt for my encryption password. (I didn't get that when rebooting at work). So I tried all the same steps after booting into "Live CD". Still got Operation not permitted.
I then rebooted and pulled out the freebsd usb, just to see if I'd get the GELI prompt again. I did, and everything is working normally.
 
You tried to put a BIOS bootcode in a EFI partition. This last is probabled mounted on /boot/efi, hence the operation not permitted.

As I said, read well the howto. I can't explain better.
 
gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i 1 nvd0

You tried to put a BIOS bootcode in a EFI partition. This last is probabled mounted on /boot/efi, hence the operation not permitted.

As I said, read well the howto. I can't explain better.

Yes, it looks like you're right. Below is the line I needed for a 13.1-RELEASE with efi boot code on a GPT scheme. I'm sure I tried that as well yesterday, and it probably is why my system is working now. I must have run that line and not noticed that it executed without complaints.

Important note: if you started with a 13.0-RELEASE installation, the partition where the efi loader resides is mounted on /boot/efi. In this case, you cannot mount the partition on /mnt. mount will answer "Device is busy".

So, the procedure becomes:
cp /boot/loader.efi /boot/efi/efi/boot/bootx64.efi

BTW, here is my efibootmgr output. "FreeBSD" is still gone, but I have NVMe0 which is apparently the one in use (plus symbol):

Code:
Boot to FW : false
BootCurrent: 0019
Timeout    : 0 seconds
BootOrder  : 0000, 0001, 0019, 0010, 0011, 0012, 0013, 001B, 0017, 0018, 001A, 001C, 001D, 001E, 0005
 Boot0000* Windows Boot Manager
 Boot0001* ubuntu
+Boot0019* NVMe0
 Boot0010  Setup
 Boot0011  Boot Menu
 Boot0012  Diagnostic Splash Screen
 Boot0013  Lenovo Diagnostics
 Boot001B* USB HDD
 Boot0017* USB CD
 Boot0018* USB FDD
 Boot001A* ATA HDD0
 Boot001C* PCI LAN
 Boot001D  Other CD
 Boot001E  Other HDD
 Boot0005* Linux-Firmware-Updater
 
BTW, here is my efibootmgr output. "FreeBSD" is still gone,
Code:
     The efibootmgr program can be used to create new EFI boot variables.  The
     following command may be used to create a new boot method, using the EFI
     partition mounted under /boot/efi, mark the method active, using the
     given loader and label the method "FreeBSD-11":

           efibootmgr -a -c -l /boot/efi/EFI/freebsd/loader.efi -L FreeBSD-11
From efibootmgr(8).
 
Back
Top