aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Bogorodskiy <novel@FreeBSD.org>2025-12-05 18:45:03 +0000
committerRoman Bogorodskiy <novel@FreeBSD.org>2025-12-13 09:27:00 +0000
commit148111d3775eb159d71a36e3a8b4c5c1bf53392c (patch)
tree10cb258a43be9cd9fa41e8694959dfe6cfdec76a
parent611bbb190ff593e0e424e311575d7e978e623659 (diff)
bhyvectl: improve options error handling
Currently, it is possible to execute bhyvectl(8) with mutually exclusive options, such as "--destroy" and "--suspend", and it will print out obscure errors, e.g.: bhyvectl --suspend=/var/run/vms/my_vm --destroy --vm my_vm connect() failed: Connection refused Address that by failing early if mutually exclusive options were specified. Additionally, move the BHYVE_SNAPSHOT block before the errors are printed, so its errors are also displayed. Approved by: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D54092
-rw-r--r--usr.sbin/bhyvectl/bhyvectl.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/usr.sbin/bhyvectl/bhyvectl.c b/usr.sbin/bhyvectl/bhyvectl.c
index 41055678ac30..9e845ac6b531 100644
--- a/usr.sbin/bhyvectl/bhyvectl.c
+++ b/usr.sbin/bhyvectl/bhyvectl.c
@@ -326,7 +326,7 @@ int
main(int argc, char *argv[])
{
char *vmname;
- int error, ch, vcpuid;
+ int action_opts, error, ch, vcpuid;
struct vm_run vmrun;
struct vmctx *ctx;
struct vcpu *vcpu;
@@ -338,6 +338,7 @@ main(int argc, char *argv[])
opts = setup_options();
+ action_opts = 0;
vcpuid = 0;
vmname = NULL;
progname = basename(argv[0]);
@@ -388,6 +389,14 @@ main(int argc, char *argv[])
if (vmname == NULL)
usage(opts);
+ action_opts = create + destroy + force_reset + force_poweroff;
+ if (checkpoint_file)
+ action_opts++;
+
+ if (action_opts > 1) {
+ fprintf(stderr, "mutually exclusive actions specified\n");
+ exit(1);
+ }
ctx = vm_openf(vmname, create ? VMMAPI_OPEN_CREATE : 0);
if (ctx == NULL) {
@@ -508,17 +517,17 @@ main(int argc, char *argv[])
if (!error && force_poweroff)
error = vm_suspend(ctx, VM_SUSPEND_POWEROFF);
+#ifdef BHYVE_SNAPSHOT
+ if (!error && checkpoint_file)
+ error = snapshot_request(vmname, checkpoint_file, vm_suspend_opt);
+#endif
+
if (error)
printf("errno = %d\n", errno);
if (!error && destroy)
vm_destroy(ctx);
-#ifdef BHYVE_SNAPSHOT
- if (!error && checkpoint_file)
- error = snapshot_request(vmname, checkpoint_file, vm_suspend_opt);
-#endif
-
free(opts);
exit(error);
}