Skip to content

Commit 375cee8

Browse files
committed
Replace grub-bhyve for direct UEFI boot.
It is simpler and easier to boot the Linux guest via EFI. This makes it possible to abandon using `grub-bhyve` for preloading and enable a more secure setup (which is compatible with hardened systems). With that change, a dedicated EFI System Partition (ESP) is searched as a separate disk image under `GUEST_ROOT`. Note that is a non-backward-compatible, breaking change, because it will not possible to boot guests via GRUB any more. Albeit it will be still possible to build the ESP with GRUB if that is still preferred.
1 parent cdf15fd commit 375cee8

2 files changed

Lines changed: 28 additions & 49 deletions

File tree

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ possible:
6666
15.0-RELEASE. Later versions will also probably work, but your
6767
mileage may vary.
6868

69-
- [`grub2-bhyve`] or the corresponding `sysutils/grub2-bhyve` FreeBSD
70-
package, so the Linux guest could be booted via GRUB 2.
69+
- [`bhyve` UEFI firmware] or the corresponding
70+
`sysutils/bhyve-firmware` FreeBSD package, so the Linux guest could
71+
be booted via EFI.
7172

7273
- [`socat`] or the respective `net/socat` FreeBSD package, through
7374
which control sockets for `wpa_supplicant(8)` and `hostapd(8)` could
@@ -92,30 +93,32 @@ mostly recommended for development and testing.
9293
```console
9394
# make install \
9495
PREFIX=<prefix> \
95-
LOCALBASE=<prefix of the grub2-bhyve and socat packages> \
96-
GUEST_ROOT=<guest disk image location> \
96+
LOCALBASE=<prefix of the bhyve-firmware and socat packages> \
97+
GUEST_ROOT=<guest disk images location> \
9798
GUEST_MAN=<guest manual page location> \
9899
RECOVERY_METHOD=<method to use on suspend and resume> \
99100
DEVD_FIX=<add extra devd.conf(5) configuration to handle suspend>
100101
```
101102

102103
By default, `PREFIX` is set to `/usr/local`. In addition to that, it
103104
is possible to set the `LOCALBASE` variable to tell if the prefix
104-
under which the `grub-bhyve` and `socat` utilities were installed is
105-
different.
105+
under which the `bhyve-firmware` data files and the `socat` utility
106+
were installed is different.
106107

107108
The `GUEST_ROOT` variable should point to the directory that houses
108109
the files related to the guest. Note that these are not part of the
109110
repository and should be installed individually. For example, such
110111
files could be installed from the [`freebsd-wifibox-alpine`]
111112
repository.
112113

113-
- GRUB is going to be configured according to the contents of
114-
`grub.cfg`, and then the system is booted from the virtual disk
115-
image whose contents should be stored as `disk.img`.
114+
- `esp.img` should hold the EFI System Partition (ESP), which is a
115+
FAT12 file system where either the Linux kernel itself (wrapped in
116+
the [EFI stub]) or some other boot loader (e.g. GRUB, Syslinux) is
117+
stored, alongside the kernel, its configuration and data files.
116118

117-
- When needed, `device.map` could also be placed there to teach GRUB
118-
about the virtual disk image.
119+
- `root.img` is the contents of the root file system, which is made
120+
available for booting as the secondary disk drive, e.g. under
121+
`/dev/vdb`, once the kernel has been loaded.
119122

120123
The `RECOVERY_METHOD` variable can be used to tell in which way
121124
Wifibox should be revived on a suspend/resume pair of events.
@@ -192,6 +195,7 @@ configuration added here!
192195
[`freebsd-wifibox-port`]: https://github.com/pgj/freebsd-wifibox-port
193196
[`freebsd-wifibox-alpine`]: https://github.com/pgj/freebsd-wifibox-alpine
194197
[`net/wpa_supplicant_gui`]: https://cgit.freebsd.org/ports/tree/net/wpa_supplicant_gui
195-
[`grub2-bhyve`]: https://github.com/grehan-freebsd/grub2-bhyve
198+
[`bhyve` UEFI firmware]: https://wiki.freebsd.org/bhyve/UEFI
196199
[`socat`]: http://www.dest-unreach.org/socat/
200+
[EFI stub]: https://docs.kernel.org/admin-guide/efi-stub.html
197201
[article]: https://github.com/pgj/freebsd-wifibox/releases/download/freebsd-journal-2024-06/freebsd-journal-wifibox.pdf

sbin/wifibox

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ PREFIX=%%PREFIX%%
66
LOCALBASE=%%LOCALBASE%%
77
VERSION=%%VERSION%%
88
GUEST_ROOT=%%GUEST_ROOT%%
9-
SCRIPT=${PREFIX}/sbin/wifibox
9+
SCRIPT="${PREFIX}"/sbin/wifibox
10+
BOOTROOM="${LOCALBASE}"/share/uefi-firmware/BHYVE_UEFI.fd
1011
LOGDIR=/var/log
1112
RUNDIR=/var/run/wifibox
1213
CONFDIR=${PREFIX}/etc/wifibox
@@ -51,10 +52,10 @@ KERNEL_PATH=$(${SYSCTL} -n kern.module_path | ${SED} -E 's/^([^;]*);.*/\1/')
5152
: "${LS:=/bin/ls}"
5253
: "${SEQ:=/usr/bin/seq}"
5354

54-
: "${GRUB_BHYVE:=${LOCALBASE}/sbin/grub-bhyve}"
5555
: "${SOCAT:=${LOCALBASE}/bin/socat}"
5656

57-
DISK_IMAGE="${GUEST_ROOT}/disk.img"
57+
ESP_IMAGE="${GUEST_ROOT}/esp.img"
58+
ROOT_IMAGE="${GUEST_ROOT}/root.img"
5859

5960
NMDM_DEVICE=/dev/nmdm-wifibox
6061
NMDM_A="${NMDM_DEVICE}.1A"
@@ -588,7 +589,6 @@ assert_value_yesno() {
588589
# shellcheck disable=SC2086
589590
vm_manager() {
590591
local _max_vmm_cpus
591-
local _nmdm_grub_bhyve
592592
local _nmdm_bhyve
593593
local _passthru_bhyve
594594
local _tap_bhyve
@@ -598,10 +598,6 @@ vm_manager() {
598598
local _app_conf_ptr="${CONFDIR}/app_config"
599599
local _app_conf_mode
600600
local _app_conf_bhyve
601-
local _grub_bhyve_args
602-
local _grub_bhyve_exit_code
603-
local _grub_device_map="${GUEST_ROOT}/device.map"
604-
local _grub_cfg="${GUEST_ROOT}/grub.cfg"
605601
local _nice_priority
606602
local _bhyve_args
607603
local _bhyve_devs
@@ -619,7 +615,6 @@ vm_manager() {
619615

620616
if [ "${console}" = "yes" ]; then
621617
assert_nmdm_loaded
622-
_nmdm_grub_bhyve="-c ${NMDM_A}"
623618
_nmdm_bhyve="-l com1,${NMDM_A}"
624619
log info "Guest console is configured to use"
625620
else
@@ -661,21 +656,22 @@ vm_manager() {
661656
exit 5
662657
fi
663658

664-
log info "Launching guest ${WIFIBOX_VM} from ${GUEST_ROOT} with grub-bhyve"
659+
log info "Launching guest ${WIFIBOX_VM} from ${GUEST_ROOT} via UEFI"
665660

666-
if [ ! -f "${_grub_cfg}" ]; then
667-
log error "${_grub_cfg} could not be found, guest cannot be started"
661+
if [ ! -f "${ESP_IMAGE}" ]; then
662+
log error "${ESP_IMAGE} could not be found, guest cannot be started"
668663
quit_daemonization
669664
exit 4
670665
fi
671666

672-
if [ ! -f "${DISK_IMAGE}" ]; then
673-
log error "${DISK_IMAGE} could not be found, guest cannot be started"
667+
if [ ! -f "${ROOT_IMAGE}" ]; then
668+
log error "${ROOT_IMAGE} could not be found, guest cannot be started"
674669
quit_daemonization
675670
exit 4
676671
fi
677672

678-
_bhyve_devs="virtio-blk,${DISK_IMAGE}"
673+
_bhyve_devs="${_bhyve_devs} virtio-blk,${ESP_IMAGE}"
674+
_bhyve_devs="${_bhyve_devs} virtio-blk,${ROOT_IMAGE}"
679675
_bhyve_devs="${_bhyve_devs} virtio-9p,config=${CONFDIR}/appliance,ro"
680676
_bhyve_devs="${_bhyve_devs} virtio-9p,var=${RUNDIR}/appliance"
681677
_app_conf=$(${READLINK} -f "%Y" ${_app_conf_ptr})
@@ -697,28 +693,6 @@ vm_manager() {
697693
fi
698694

699695
log debug "Devices: ${_bhyve_devs}"
700-
701-
_grub_bhyve_args=""
702-
_grub_bhyve_args="${_grub_bhyve_args} -S -M ${memory}"
703-
_grub_bhyve_args="${_grub_bhyve_args} -r host ${_nmdm_grub_bhyve}"
704-
[ -f "${_grub_device_map}" ] \
705-
&& _grub_bhyve_args="${_grub_bhyve_args} -m ${_grub_device_map}"
706-
_grub_bhyve_args="${_grub_bhyve_args} -d ${GUEST_ROOT} ${WIFIBOX_VM}"
707-
708-
log debug "Arguments: ${_grub_bhyve_args}"
709-
${GRUB_BHYVE} ${_grub_bhyve_args} 2>&1 | capture_output debug grub-bhyve
710-
_grub_bhyve_exit_code="$?"
711-
712-
if [ "${_grub_bhyve_exit_code}" -ne "0" ]; then
713-
destroy_vm
714-
715-
log debug "exit_code=${_grub_bhyve_exit_code}"
716-
log info "grub-bhyve failed to start, signaling restart after 5 seconds"
717-
718-
${SLEEP} 5 2>&1 | capture_output debug sleep
719-
exit 1
720-
fi
721-
722696
log info "Launching guest ${WIFIBOX_VM} from ${GUEST_ROOT} with bhyve"
723697

724698
_bhyve_args=""
@@ -727,6 +701,7 @@ vm_manager() {
727701
_bhyve_args="${_bhyve_args} ${_nmdm_bhyve}"
728702
_bhyve_args="${_bhyve_args} -s 0,hostbridge"
729703
_bhyve_args="${_bhyve_args} -s 31,lpc"
704+
_bhyve_args="${_bhyve_args} -l bootrom,${BOOTROOM}"
730705
_slot=0
731706

732707
for dev in ${_bhyve_devs}; do

0 commit comments

Comments
 (0)