Skip to content

Instantly share code, notes, and snippets.

@gamecreature
Created July 23, 2024 05:20
Show Gist options
  • Save gamecreature/9b080c112e08b420be48de43ae10f004 to your computer and use it in GitHub Desktop.
Save gamecreature/9b080c112e08b420be48de43ae10f004 to your computer and use it in GitHub Desktop.
Install the current FreeBSD boot loaders on the correct locations: freebsd-boot partition, efi partition and creates mbr
#!/bin/sh
emit() {
if [ "$commit" = "1" ]; then
#echo " | $@"
echo " | $@"
$@ ## << make this the main command or run with `eval $@`
else
echo " | $@"
fi
}
update_efi_partition() {
name="$1"
index="$2"
mountpoint="/boot/efi"
echo "* Update efi ${name}p${index}"
emit umount $mountpoint
emit mount -t msdosfs "/dev/${name}p${index}" $mountpoint
emit mkdir -p $mountpoint/efi/boot
emit cp -p $mountpoint/efi/boot/BOOTx64.efi $mountpoint/efi/boot/BOOTx64.efi.bak
emit cp -p /boot/loader.efi $mountpoint/efi/boot/BOOTx64.efi
emit umount $mountpoint
echo ""
}
update_boot_partition() {
name="$1"
index="$2"
echo "* Update freebsd-boot $partition $index"
emit gpart bootcode -b /boot/pmbr -p /boot/gptzfsboot -i $index $name
echo ""
}
# => 40 3907029088 nda0 GPT (1.8T)]
# 40 532480 1 efi (260M)]
# 532520 1024 2 freebsd-boot (512K)]
# 533544 984 - free - (492K)]
# 534528 16777216 3 freebsd-swap (8.0G)]
# 17311744 3889717248 4 freebsd-zfs (1.8T)]
# 3907028992 136 - free - (68K)]
# => 40 3907029088 ada0 GPT (1.8T)]
# 40 532480 1 efi (260M)]
# 532520 1024 2 freebsd-boot (512K)]
# 533544 984 - free - (492K)]
# 534528 16777216 3 freebsd-swap (8.0G)]
# 17311744 3889717248 4 freebsd-zfs (1.8T)]
# 3907028992 136 - free - (68K)]
# => 40 3907029088 da0 GPT (1.8T)]
# 40 532480 1 efi (260M)]
# 532520 1024 2 freebsd-boot (512K)]
# 533544 984 - free - (492K)]
# 534528 16777216 3 freebsd-swap (8.0G)]
# 17311744 3889717248 4 freebsd-zfs (1.8T)]
# 3907028992 136 - free - (68K)
update_partitions() {
old_ifs=$IFS
IFS='
'
device=""
for line in $(gpart show); do
IFS=' '
set -- $line
#echo "[$1, $2, $3, $4, $5, $6]"
case "$1" in
# [=>, 40, 3907029088, ada0, GPT, (1.8T)]
"=>")
device="$4"
;;
# [40, 532480, 1, efi, (260M), ]
*)
part_type="$4"
part_index="$3"
case "$part_type" in
"efi")
update_efi_partition $device $part_index
;;
"freebsd-boot")
update_boot_partition $device $part_index
;;
esac
;;
esac
done
IFS=$old_ifs
}
echo "Current Partition Information"
echo "-----------------------------"
gpart show
echo ""
if [ "$1" = "--commit" ]; then
commit="1"
echo "Updating Partitions"
echo "-------------------"
update_partitions
echo ""
echo "Done!"
else
echo "Update the following Actions?"
echo "-----------------------------"
update_partitions
echo ""
echo "Please run with --commit argument to realy write the partition!"
fi
@gamecreature
Copy link
Author

gamecreature commented Jul 23, 2024

This script fetches the partition info for all disks and

  • it installs gptzfsboot on every freebsd-boot partition (and writes mbr)
  • it installs the /boot/loader.efi on every efi partition

Usage:

# view the actions that the script is going to take:
install_bootloader.sh

# check these actions, when ok run with the commit option
install_bootloader.sh --commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment