Skip to content

Commit

Permalink
Rework the restart command.
Browse files Browse the repository at this point in the history
Change how to the `restart` command works to provide a better
control for the user over the result of the operation.  This also
removes the need for the dedicated `resume` command, which can be
now expressed in terms of `restart`.
  • Loading branch information
pgj committed Jan 30, 2022
1 parent 08ab7bd commit 9be9049
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 47 deletions.
2 changes: 1 addition & 1 deletion devd/wifibox.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
notify 11 {
match "system" "ACPI";
match "subsystem" "Resume";
action "/etc/rc.resume acpi $notify && %%PREFIX%%/sbin/wifibox resume";
action "/etc/rc.resume acpi $notify && %%PREFIX%%/sbin/wifibox restart vmm";
};
48 changes: 33 additions & 15 deletions man/wifibox.8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.Dd January 15, 2022
.Dd January 30, 2022
.Dt WIFIBOX 8
.Os
.Sh NAME
Expand All @@ -11,11 +11,12 @@
.Cm stop
.Nm
.Cm restart
.Oo
.Cm guest | Cm netif | Cm console | Cm vmm
.Oc
.Nm
.Cm status
.Nm
.Cm resume
.Nm
.Cm console
.Nm
.Cm version
Expand Down Expand Up @@ -172,24 +173,41 @@ The
interface is destroyed and the guest is detached from the configured
PCI wireless network device. After that, the FreeBSD driver is free
to take over the device.
.It Cm restart
.It Cm restart Oo Cm guest | Cm netif | Cm console | Cm vmm Oc
Restart
.Nm ,
which is a sequential composition of the
which is the sequential composition of the
.Cm stop
and
.Cm start
commands.
commands by default. It is possible to specify a target, therefore
limit the scope of this operation in different ways.
.Bl -tag -width "console"
.It Cm guest
Restart the guest only, maintain the console device and the networking
interface. This is recommended for applying system-level updates to
the guest.
.It Cm netif
Recreate the networking interface and restart the guest. That latter
is required because the networking interface is bound to the virtual
machine that runs the guest.
.It Cm console
Recreate the null-modem device and restart the guest. That latter is
required because the null-modem device is bound to the virtual machine
that runs the guest. This is recommended when the console support is
turned on in the configuration.
.It Cm vmm
Restart the guest while reloading the
.Xr vmm 4
kernel module, maintain the console device and the networking
interface. This is a workaround for the guest to recover from a state
where the wireless device becomes unresponsive after the ACPI resume
event.
.El
.It Cm status
Check and display if
.Nm
is still running.
.It Cm resume
Apply a workaround for the guest to recover from a state where the
wireless device becomes unresponsive after the ACPI resume event.
This involves reloading the
.Xr vmm 4
kernel module and restarting the guest.
.It Cm console
Attach to the running guest with
.Xr cu 1
Expand Down Expand Up @@ -219,7 +237,7 @@ executed command, and their amount depends on the configured level of
logging. The log files of the guest are exported to the host and they
are made available under the
.Pa /var/run/wifibox/appliance/log
directory. There it is recommend to check the
directory. There it is recommended to check the
.Pa /var/run/wifibox/appliance/log/dmesg
file for messages related to the boot sequence, such as driver
initialization, and the
Expand All @@ -246,14 +264,14 @@ supports only a single wireless network device at a time, and it has
to be PCI one. It cannot be launched multiple times.
.Pp
The
.Cm resume
.Cm restart vmm
command should be used with caution, because it may crash the system
when it has not been a sleep state. Hence it is best to use in
combination with
.Xr devd 8 .
.Pp
The
.Cm resume
.Cm restart vmm
command will not probably work on systems where other
.Xr bhyve 8
guests are running in parallel as
Expand Down
118 changes: 87 additions & 31 deletions sbin/wifibox
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ wifibox_start() {
local _pid

log info "Begin: wifibox start"

${PRINTF} "Launching wifibox..."
assert_vm_can_run
_pid="$(get_vm_pid)"

Expand All @@ -476,8 +478,6 @@ wifibox_start() {
exit 1
fi

${PRINTF} "Launching wifibox"

create_bridge
show_progress

Expand All @@ -487,17 +487,15 @@ wifibox_start() {
vm_start
show_progress

# Give some more time for the guest to boot up.
${SLEEP} 2 2>&1 | capture_output debug sleep
${ECHO} "OK"
log info "End: wifibox start"
}

wifibox_stop() {
log info "Begin: wifibox stop"
assert_vm_runs

${PRINTF} "Stopping wifibox"
${PRINTF} "Stopping wifibox..."
assert_vm_runs

vm_stop
show_progress
Expand All @@ -514,41 +512,85 @@ wifibox_stop() {

# This is a workaround to recover from the unfortunate state of the
# wireless device after resume.
kick_vmm() {
log debug "Unloading vmm.ko"
reload_vmm() {
log info "Unloading vmm.ko"

if ! (${KLDUNLOAD} vmm 2>&1 | capture_output debug kldunload); then
log error "Some other bhyve guests might be running, vmm.ko could not be unloaded"
exit 127
fi

log debug "Reloading vmm.ko"
log info "Reloading vmm.ko"
${KLDLOAD} "${VMM_KO}" 2>&1 | capture_output debug kldload
}

wifibox_resume() {
log info "Begin: wifibox resume"
assert_vm_runs
has_flag() {
local _flags="$1"
local _flag="$2"

${PRINTF} "Resuming wifibox"
${ECHO} "${_flags}" | ${GREP} -Fq "${_flag}"
}

vm_stop
show_progress
wifibox_restart() {
local _target="$1"
local _restart

${SLEEP} 1
kick_vmm
show_progress
log info "Begin: wifibox restart"

vm_start
if [ -n "$2" ]; then
log error "Too many parameters"
exit 1
fi

${ECHO} "OK"
log info "End: wifibox resume"
}
case ${_target} in
guest) _restart="G";;
""|netif) _restart="GN";;
console) _restart="GM";;
vmm) _restart="GV";;
*) log error "Unknown target: ${_target}"
exit 1;;
esac

wifibox_restart() {
log info "Begin: wifibox restart"
wifibox_stop
wifibox_start
log debug "restart=${_restart}"
${PRINTF} "Restarting wifibox..."

if has_flag "${_restart}" "G"; then
assert_vm_runs
vm_stop
show_progress
fi

if has_flag "${_restart}" "M"; then
destroy_nmdm
show_progress
fi

if has_flag "${_restart}" "N"; then
destroy_bridge
show_progress
fi

if has_flag "${_restart}" "V"; then
reload_vmm
show_progress
fi

if has_flag "${_restart}" "N"; then
create_bridge
show_progress
fi

if has_flag "${_restart}" "M"; then
create_nmdm
show_progress
fi

if has_flag "${_restart}" "G"; then
vm_start
show_progress
fi

${ECHO} "OK"
log info "End: wifibox restart"
}

Expand Down Expand Up @@ -597,18 +639,32 @@ wifibox_version() {
log info "End: wifibox version"
}

wifibox_usage() {
${CAT} <<EOF
USAGE: ${SCRIPT} ...
start
stop
restart [guest|netif|console|vmm]
status
console
version
EOF
exit 1
}

# Do not execute the main program when sourced.
[ "$0" != "${SCRIPT}" ] && return 0

log debug "Program started as $0"
log debug "Program started as $0, with arguments: $@"
command="$1"
shift

case "$1" in
case ${command} in
start) wifibox_start;;
stop) wifibox_stop;;
resume) wifibox_resume;;
restart) wifibox_restart;;
restart) wifibox_restart "$@";;
status) wifibox_status;;
console) wifibox_console;;
version) wifibox_version;;
*) ${ECHO} "USAGE: ${SCRIPT} start | stop | restart | resume | status | console | version" ;;
*) wifibox_usage;;
esac

0 comments on commit 9be9049

Please sign in to comment.