Solved How to recover a freebsd-boot partition on UEFI

I accidentally deleted the freebsd-boot partition do to some Windows installation shenanigans and a misclick. All other partitions are intact, but I can't boot in to my system

1726669033030.png


I tried creating a UEFI FAT32 partition in its place. Mounted both UFS and the UEFI partiton and copying the loader.efi partition from UFS to UEFI partitions EFI/FreeBSD directory, which didn't work.

this is my current fdisk output for the SSD in which freebsd is installed on

Disk /dev/sda: 465.76 GiB, 500107862016 bytes, 976773168 sectors
Disk model: Samsung SSD 860
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: BD81C6ED-3C44-11EF-A015-18C04D044F8E

Device Start End Sectors Size Type
/dev/sda1 532520 968884255 968351736 461.7G FreeBSD UFS
/dev/sda2 968884256 976773127 7888872 3.8G FreeBSD swap
/dev/sda3 2048 532519 530472 259M EFI System

Partition table entries are not in disk order.
 
Concerning the freebsd-boot partition, you can do that easily with gpart from a FreeBSD console, but with Linux, it may be difficult to impossible. I can't say.

The EFI partition should work if you have put the correct loader name in the correct path and you set the machine to start on UEFI.
 
Just gpart add the efi partition, format it with an msdos fs. Then create the EFI/BOOT subdirectory. Copy the boot record into it.

You can name the directories anything you want as your UEFI BIOS will "remember" the boot file location.

The FreeBSD documentation is here.

I used this procedure to clone (copy) my complete FreeBSD from my old MBR legacy BIOS laptop to my new UEFI HP laptop. The script I used is below.

sh:
#!/bin/sh --
set -e -x

gpart create -s gpt nda0

gpart add -t efi -s 260M nda0
newfs_msdos -F 32 -c 1 /dev/nda0p1
mount -t msdosfs /dev/nda0p1 /mnt
mkdir -p /mnt/EFI/BOOT
# cp /boot/loader.efi /mnt/EFI/BOOT/BOOTX64.efi
cp /boot/gptboot.efi /mnt/EFI/BOOT/BOOTX64.efi
cd /
umount /mnt
USED=1

gpart add -t freebsd-ufs -s 4194304 nda0
gpart set -a bootme -i 2 nda0
newfs -Uj -L Sroot /dev/nda0p2
tunefs -N enable -t enable /dev/nda0p2
mount /dev/nda0p2 /mnt
cd /mnt
dump 0Laf - /dev/ufs/Hroot | restore xf -
cd /
umount /mnt

# ndap3
gpart add -t freebsd-swap -s 32g nda0
USED=$((USED+32))

gpart add -t freebsd-ufs -s 4194304 nda0
newfs -Uj -L Svar /dev/nda0p4
tunefs -N enable -t enable /dev/nda0p4
mount /dev/nda0p4 /mnt
cd /mnt
dump 0Laf - /dev/ufs/Hvar | restore xf -
cd /
umount /mnt
# USED= accounted for

gpart add -t freebsd-ufs -s 4194304 nda0
newfs -Uj -L Svtmp /dev/nda0p5
tunefs -N enable -t enable /dev/nda0p5
mount /dev/nda0p5 /mnt
cd /mnt
dump 0Laf - /dev/ufs/Hvtmp | restore xf -
cd /
umount /mnt
# USED= accounted for

gpart add -t freebsd-ufs -s 41943040 nda0
newfs -Uj -L Susr /dev/nda0p6
tunefs -N enable -t enable /dev/nda0p6
mount /dev/nda0p6 /mnt
cd /mnt
dump 0Laf - /dev/ufs/Husr | restore xf -
cd /
umount /mnt
USED=$((USED+20-3))

# For real:
ZPOOL_SIZE=$((789-$USED))
echo zpool size=$ZPOOL_SIZE

gpart add -t freebsd-zfs -s 1619001336 nda0

# For testing:
# gpart add -t freebsd-zfs nda0
# mirror zpool separately
echo Remember to mirror zpool separately

echo Remember to edit fstab
 
create an efi / esp partition and format it to fat 32
create this structure

/efi
/efi/freebsd
/efi/freebsd/loader.efi
/efi/boot
/efi/boot/bootx64.efi

bootx64.efi is the same file as loader.efi
 
create an efi / esp partition and format it to fat 32
create this structure

/efi
/efi/freebsd
/efi/freebsd/loader.efi
/efi/boot
/efi/boot/bootx64.efi

bootx64.efi is the same file as loader.efi
You don't need both loaders. Pick one. And the name doesn't matter as long as your BIOS can see it. It will "remember" it.
 
I think all the howto in this thread should be readily available in
/usr/src/UPDATING and come to think of it even in
/usr/ports/UPDATING because if I recall the UEFI EFI boot methods did not exist until
comparatively recently and there should be a resource to find it without having to be online. Maybe as
a seperate section/header within the first or both files.
 
You can name the directories anything you want as your UEFI BIOS will "remember" the boot file location.
This is not true on VirtualBox.

First, I deactivated the boot entry of /efi/freebsd/loader.efi. After that, it booted on /efi/boot/bootx64.efi.
Then, I renamed /efi/boot/bootx64.efi to /efi/boot/other.efi. Now, it can't boot.
/efi/boot/bootx64.efi is the default name for amd64 arch. No mistake is allowed there unless the EFI var had correctly set with efibootmgr.
 
create an efi / esp partition and format it to fat 32
create this structure

/efi
/efi/freebsd
/efi/freebsd/loader.efi
/efi/boot
/efi/boot/bootx64.efi

bootx64.efi is the same file as loader.efi

I successfully create and formatted the efi partition, copied the loader.efi file in EFI partition in EFI/BOOT as BOOTX64.efi

It successfully boots in the standard freebsd loader, but when Multi-user is selected it gets stuck (And i can not interact with the shell at /bin/sh screen). I only have the image, if I need the full boot logs how do I access them?

fhshfkds.jpg
 
Boot using it into LiveCD. The bootable USB should be the same version as the system that you are trying to repair.
Then from the LiveCD show me the output of

gpart show
 
Thank yall, I fixed it successfully!

This part was important
I successfully create and formatted the efi partition, copied the loader.efi file in EFI partition in EFI/BOOT as BOOTX64.efi

I could not do this on my Linux install and Linux live-environment, but on FreeBSD live-environment it worked. And that was I needed to mount the freebsd-ufs partition and edits the /etc/fstab file in
vi change the /boot/efi mountpoint from /dev/ada0p1 (my previous efi partition location) to /dev/ada0p3
 
Ok create the new filesystem on Index 3 on ada0 (ada0p3) but before format it double check that ada0p3 is the efi partition using
gpart show -p

Then run


newfs_msdos -F 32 -c 1 /dev/ada0p3
mkdir /tmp/ESP
mount -t msdosfs /dev/ada0p3 /tmp/ESP
mkdir -p /tmp/ESP/EFI/BOOT
cp /boot/loader.efi /tmp/ESP/EFI/BOOT/BOOTx64.efi


Before unmount the ESP check the UEFI boot order using

efibootmgr

or

efibootmgr -v

then you can remove the old boot records from the UEFI and create a new one using

efibootmgr -B -b <boot number>

example: efibootmgr -B -b 5 will delete Boot0005
then you can create a new boot record using

efibootmgr --create --activate --label "FreeBSD" --loader /tmp/ESP/EFI/BOOT/BOOTx64.efi


After that you can unmout the ESP partition

umount /tmp/ESP

and reboot

shutdown -r now


Edit:
As your partition indexes are bit messy you may need to mount the UFS and fix your /etc/fstab so ada0p1 is your UFS and ada0p2 is your SWAP
 
Back
Top