2017-12-15 06:34:11 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
# $FreeBSD$
|
|
|
|
|
|
|
|
#
|
|
|
|
# Installs/updates the necessary boot blocks for the desired boot environment
|
|
|
|
#
|
|
|
|
# Lightly tested.. Intended to be installed, but until it matures, it will just
|
|
|
|
# be a boot tool for regression testing.
|
|
|
|
|
|
|
|
# insert code here to guess what you have -- yikes!
|
|
|
|
|
|
|
|
die() {
|
|
|
|
echo $*
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2017-12-15 23:16:27 +00:00
|
|
|
doit() {
|
|
|
|
echo $*
|
|
|
|
eval $*
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:34:11 +00:00
|
|
|
find-part() {
|
|
|
|
dev=$1
|
|
|
|
part=$2
|
|
|
|
|
|
|
|
gpart show $dev | tail +2 | awk '$4 == "'$part'" { print $3; }'
|
|
|
|
}
|
|
|
|
|
2018-07-11 21:53:04 +00:00
|
|
|
make_esp() {
|
|
|
|
local dev dst mntpt
|
|
|
|
|
2017-12-15 06:34:11 +00:00
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
2018-07-12 17:22:25 +00:00
|
|
|
newfs_msdos -a 32 ${dev}
|
2018-07-11 21:53:04 +00:00
|
|
|
mntpt=$(mktemp -d /tmp/stand-test.XXXXXX)
|
2018-07-12 17:22:25 +00:00
|
|
|
mount -t msdos ${dev} ${mntpt}
|
2018-07-11 21:53:04 +00:00
|
|
|
mkdir -p ${mntpt}/efi/boot
|
|
|
|
cp ${dst}/boot/loader.efi ${mntpt}/efi/boot/bootx64.efi
|
|
|
|
umount ${mntpt}
|
|
|
|
rmdir ${mntpt}
|
|
|
|
}
|
|
|
|
|
|
|
|
make_esp_mbr() {
|
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
|
|
|
s=$(find-part $dev "!239")
|
|
|
|
if [ -z "$s" ] ; then
|
2018-07-13 16:33:21 +00:00
|
|
|
s=$(find-part $dev "efi")
|
|
|
|
if [ -z "$s" ] ; then
|
|
|
|
die "No ESP slice found"
|
|
|
|
fi
|
2017-12-15 06:34:11 +00:00
|
|
|
fi
|
2018-07-11 21:53:04 +00:00
|
|
|
make_esp /dev/${dev}s${s} ${dst}
|
2017-12-15 06:34:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 21:53:04 +00:00
|
|
|
make_esp_gpt() {
|
2018-06-17 05:55:31 +00:00
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
|
|
|
idx=$(find-part $dev "efi")
|
|
|
|
if [ -z "$idx" ] ; then
|
|
|
|
die "No ESP partition found"
|
|
|
|
fi
|
2018-07-11 21:53:04 +00:00
|
|
|
make_esp /dev/${dev}p${idx} ${dst}
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_gpt_ufs_legacy() {
|
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
|
|
|
idx=$(find-part $dev "freebsd-boot")
|
|
|
|
if [ -z "$idx" ] ; then
|
|
|
|
die "No freebsd-boot partition found"
|
|
|
|
fi
|
|
|
|
doit gpart bootcode -b ${gpt0} -p ${gpt2} -i $idx $dev
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_gpt_ufs_uefi() {
|
|
|
|
make_esp_gpt $1 $2
|
2018-06-17 05:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_gpt_ufs_both() {
|
|
|
|
boot_nogeli_gpt_ufs_legacy $1 $2 $3
|
|
|
|
boot_nogeli_gpt_ufs_uefi $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_gpt_zfs_legacy() {
|
2017-12-15 06:34:11 +00:00
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
|
|
|
idx=$(find-part $dev "freebsd-boot")
|
|
|
|
if [ -z "$idx" ] ; then
|
|
|
|
die "No freebsd-boot partition found"
|
|
|
|
fi
|
2018-06-17 05:55:31 +00:00
|
|
|
doit gpart bootcode -b ${gpt0} -p ${gptzfs2} -i $idx $dev
|
2017-12-15 06:34:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 17:49:43 +00:00
|
|
|
boot_nogeli_gpt_zfs_uefi() {
|
2018-07-11 21:53:04 +00:00
|
|
|
make_esp_gpt $1 $2
|
2018-06-09 17:49:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_gpt_zfs_both() {
|
2018-06-17 05:55:31 +00:00
|
|
|
boot_nogeli_gpt_zfs_legacy $1 $2 $3
|
|
|
|
boot_nogeli_gpt_zfs_uefi $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_mbr_ufs_legacy() {
|
2018-06-09 17:49:43 +00:00
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
2018-06-17 05:55:31 +00:00
|
|
|
doit gpart bootcode -b ${mbr0} ${dev}
|
|
|
|
s=$(find-part $dev "freebsd")
|
|
|
|
if [ -z "$s" ] ; then
|
|
|
|
die "No freebsd slice found"
|
2018-06-09 17:49:43 +00:00
|
|
|
fi
|
2018-06-17 05:55:31 +00:00
|
|
|
doit gpart bootcode -p ${mbr2} ${dev}s${s}
|
|
|
|
}
|
2018-06-09 17:49:43 +00:00
|
|
|
|
2018-06-17 05:55:31 +00:00
|
|
|
boot_nogeli_mbr_ufs_uefi() {
|
2018-07-11 21:53:04 +00:00
|
|
|
make_esp_mbr $1 $2
|
2018-06-17 05:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_mbr_ufs_both() {
|
|
|
|
boot_nogeli_mbr_ufs_legacy $1 $2 $3
|
|
|
|
boot_nogeli_mbr_ufs_uefi $1 $2 $3
|
2018-06-09 17:49:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 06:34:11 +00:00
|
|
|
boot_nogeli_mbr_zfs_legacy() {
|
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
|
|
|
# search to find the BSD slice
|
2018-06-17 05:55:31 +00:00
|
|
|
s=$(find-part $dev "freebsd")
|
2017-12-15 06:34:11 +00:00
|
|
|
if [ -z "$s" ] ; then
|
2018-06-17 05:55:31 +00:00
|
|
|
die "No BSD slice found"
|
|
|
|
fi
|
|
|
|
idx=$(find-part ${dev}s${s} "freebsd-zfs")
|
|
|
|
if [ -z "$idx" ] ; then
|
2017-12-15 06:34:11 +00:00
|
|
|
die "No freebsd-zfs slice found"
|
|
|
|
fi
|
|
|
|
# search to find the freebsd-zfs partition within the slice
|
|
|
|
# Or just assume it is 'a' because it has to be since it fails otherwise
|
2018-06-17 05:55:31 +00:00
|
|
|
doit gpart bootcode -b ${dst}/boot/mbr ${dev}
|
2017-12-15 06:34:11 +00:00
|
|
|
dd if=${dst}/boot/zfsboot of=/tmp/zfsboot1 count=1
|
2018-06-17 05:55:31 +00:00
|
|
|
doit gpart bootcode -b /tmp/zfsboot1 ${dev}s${s} # Put boot1 into the start of part
|
2017-12-15 06:44:11 +00:00
|
|
|
sysctl kern.geom.debugflags=0x10 # Put boot2 into ZFS boot slot
|
2018-06-17 05:55:31 +00:00
|
|
|
doit dd if=${dst}/boot/zfsboot of=/dev/${dev}s${s}a skip=1 seek=1024
|
2017-12-15 06:34:11 +00:00
|
|
|
sysctl kern.geom.debugflags=0x0
|
|
|
|
}
|
|
|
|
|
2018-06-17 05:55:31 +00:00
|
|
|
boot_nogeli_mbr_zfs_uefi() {
|
2018-07-13 16:33:21 +00:00
|
|
|
make_esp_mbr $1 $2
|
2018-06-17 05:55:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
boot_nogeli_mbr_zfs_both() {
|
|
|
|
boot_nogeli_mbr_zfs_legacy $1 $2 $3
|
|
|
|
boot_nogeli_mbr_zfs_uefi $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_gpt_ufs_legacy() {
|
|
|
|
boot_nogeli_gpt_ufs_legacy $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_gpt_ufs_uefi() {
|
|
|
|
boot_nogeli_gpt_ufs_uefi $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_gpt_ufs_both() {
|
|
|
|
boot_nogeli_gpt_ufs_both $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_gpt_zfs_legacy() {
|
|
|
|
boot_nogeli_gpt_zfs_legacy $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_gpt_zfs_uefi() {
|
|
|
|
boot_nogeli_gpt_zfs_uefi $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_gpt_zfs_both() {
|
|
|
|
boot_nogeli_gpt_zfs_both $1 $2 $3
|
|
|
|
}
|
|
|
|
|
|
|
|
# GELI+MBR is not a valid configuration
|
|
|
|
boot_geli_mbr_ufs_legacy() {
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_mbr_ufs_uefi() {
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_mbr_ufs_both() {
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_mbr_zfs_legacy() {
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_mbr_zfs_uefi() {
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
boot_geli_mbr_zfs_both() {
|
|
|
|
exit 1
|
2017-12-15 06:34:11 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 00:18:21 +00:00
|
|
|
boot_nogeli_vtoc8_ufs_ofw() {
|
|
|
|
dev=$1
|
|
|
|
dst=$2
|
|
|
|
|
|
|
|
# For non-native builds, ensure that geom_part(4) supports VTOC8.
|
|
|
|
kldload geom_part_vtoc8.ko
|
|
|
|
doit gpart bootcode -p ${vtoc8} ${dev}
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:34:11 +00:00
|
|
|
DESTDIR=/
|
|
|
|
|
|
|
|
# Note: we really don't support geli boot in this script yet.
|
|
|
|
geli=nogeli
|
|
|
|
|
|
|
|
while getopts "b:d:f:g:o:s:" opt; do
|
|
|
|
case "$opt" in
|
|
|
|
b)
|
|
|
|
bios=${OPTARG}
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
DESTDIR=${OPTARG}
|
|
|
|
;;
|
|
|
|
f)
|
|
|
|
fs=${OPTARG}
|
|
|
|
;;
|
|
|
|
g)
|
|
|
|
case ${OPTARG} in
|
|
|
|
[Yy][Ee][Ss]|geli) geli=geli ;;
|
|
|
|
*) geli=nogeli ;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
o)
|
|
|
|
opts=${OPTARG}
|
|
|
|
;;
|
|
|
|
s)
|
|
|
|
scheme=${OPTARG}
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
dev=$1
|
|
|
|
|
|
|
|
# For gpt, we need to install pmbr as the primary boot loader
|
|
|
|
# it knows about
|
|
|
|
gpt0=${DESTDIR}/boot/pmbr
|
|
|
|
gpt2=${DESTDIR}/boot/gptboot
|
|
|
|
gptzfs2=${DESTDIR}/boot/gptzfsboot
|
|
|
|
|
2018-06-17 05:55:31 +00:00
|
|
|
# For MBR, we have lots of choices, but select mbr, boot0 has issues with UEFI
|
|
|
|
mbr0=${DESTDIR}/boot/mbr
|
2017-12-15 06:34:11 +00:00
|
|
|
mbr2=${DESTDIR}/boot/boot
|
|
|
|
|
2018-02-05 00:18:21 +00:00
|
|
|
# VTOC8
|
|
|
|
vtoc8=${DESTDIR}/boot/boot1
|
|
|
|
|
2017-12-15 06:34:11 +00:00
|
|
|
# sanity check here
|
|
|
|
|
|
|
|
eval boot_${geli}_${scheme}_${fs}_${bios} $dev $DESTDIR $opts || echo "Unsupported boot env: ${geli}-${scheme}-${fs}-${bios}"
|