Skip to content

Instantly share code, notes, and snippets.

@gamecreature
Last active January 23, 2025 10:50
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.
Installs the FreeBSD Bootloader on all efi an freebsd-boot partitions. When installing the freebsd-boot partition it also installs the MBR. Without --commit nothings happens, only the commands are shown.
#!/bin/sh
emit() {
if [ "$arg_commit" = "1" ]; then
#echo " | $@"
echo " | $@"
$@ ## << make this the main command or run with `eval $@`
else
echo " | $@"
(exit 0)
fi
}
update_efi_partition() {
name="$1"
index="$2"
mountpoint="/boot/efi"
echo "* Update efi ${name}p${index} (device ${name})"
emit umount $mountpoint
emit mount -t msdosfs "/dev/${name}p${index}" $mountpoint
if [ $? -ne 0 ]; then
echo " ** ERROR - Cannot mount the efi partition"
echo " ** please check if the $mountpoint can be mounted"
exit
fi
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 $name $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 $arg_device); 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
}
show_partitions() {
echo "Current Partition Information $arg_device"
echo "------------------------------------------"
gpart show $arg_device
echo ""
}
usage() {
echo "Usage: $0 [--commit] [--all] device..."
echo ""
echo "Installs the FreeBSD Bootloader on all efi an freebsd-boot partitions."
echo "When installing the freebsd-boot partition it also installs the MBR."
echo "Without --commit nothings happens, only the commands are shown."
echo ""
echo "--all: use for all devices"
echo "--commit: apply the changes to the device(s)"
echo "--help: show this usage information"
echo ""
}
arg_commit=0
arg_all=0
arg_device=''
# Loop through all provided arguments
while [ $# -gt 0 ]; do
case "$1" in
--commit)
arg_commit="1"
shift
;;
--all)
arg_all="1"
shift
;;
--help)
usage
exit 0
;;
#--) # End of options
# shift
# break
# ;;
*) # Unknown option
# when it starts with a dash show usage information
if [ "${1#-}" != "$1" ]; then
echo "Error: Unknown option: $1"
echo ""
usage
exit 1
else
if [ ! -z "$arg_device" ]; then
echo "Error: Only one (or --all) device is support"
echo ""
usage
exit 1
fi
arg_device="$1"
shift
fi
;;
esac
done
# make sure a device or --all is selected
if ! [ "$arg_all" == "1" ] ; then
if [ -z "$arg_device" ]; then
echo "Error: Please specify a device or use the option --all"
echo ""
usage
exit 1
fi
fi
if [ $arg_commit == "1" ]; then
echo "Updating Partitions"
echo "-------------------"
echo
show_partitions
update_partitions
echo ""
echo "Done!"
else
echo "Update the following Actions?"
echo "-----------------------------"
echo
show_partitions
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

@gamecreature
Copy link
Author

New version support device name on the command line.

Usage: ./install_bootloader.sh [--commit] [--all] device...

Installs the FreeBSD Bootloader on all efi an freebsd-boot partitions.
When installing the freebsd-boot partition it also installs the MBR.
Without --commit nothings happens, only the commands are shown.

--all: use for all devices
--commit: apply the changes to the device(s)
--help: show this usage information

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