From: mlarkin Date: Thu, 2 May 2024 15:46:10 +0000 (+0000) Subject: vmctl(8): Add 'vmctl status -r' X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=986b002c40dbe3ca91f8b84f0066a3f37a55585e;p=openbsd vmctl(8): Add 'vmctl status -r' The -r option can be used to limit the output of 'vmctl status' to only running VMs. This is useful for machines that have a large number of stopped VMs, as the running ones are printed at the top by default and previously required scrolling back to see the list of running VMs, and/or using 'grep RUNNING'. There is no change for users not using -r. ok dv --- diff --git a/usr.sbin/vmctl/main.c b/usr.sbin/vmctl/main.c index 6621169dc5e..2b951ea4f9f 100644 --- a/usr.sbin/vmctl/main.c +++ b/usr.sbin/vmctl/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.76 2024/04/10 13:03:41 dv Exp $ */ +/* $OpenBSD: main.c,v 1.77 2024/05/02 15:46:10 mlarkin Exp $ */ /* * Copyright (c) 2015 Reyk Floeter @@ -45,6 +45,7 @@ static const char *socket_name = SOCKET_NAME; static int ctl_sock = -1; static int tty_autoconnect = 0; +int stat_rflag; __dead void usage(void); __dead void ctl_usage(struct ctl_command *); @@ -82,7 +83,7 @@ struct ctl_command ctl_commands[] = { { "start", CMD_START, ctl_start, "[-cL] [-B device] [-b path] [-d disk] [-i count]\n" "\t\t[-m size] [-n switch] [-r path] [-t name] id | name", 1}, - { "status", CMD_STATUS, ctl_status, "[id]" }, + { "status", CMD_STATUS, ctl_status, "[-r] [id]" }, { "stop", CMD_STOP, ctl_stop, "[-fw] [id | -a]" }, { "unpause", CMD_UNPAUSE, ctl_unpause, "id" }, { "wait", CMD_WAITFOR, ctl_waitfor, "id" }, @@ -744,10 +745,25 @@ ctl_convert(const char *srcfile, const char *dstfile, int dsttype, size_t dstsiz int ctl_status(struct parse_result *res, int argc, char *argv[]) { - if (argc == 2) { - if (parse_vmid(res, argv[1], 0) == -1) - errx(1, "invalid id: %s", argv[1]); - } else if (argc > 2) + char ch; + + while ((ch = getopt(argc, argv, "r")) != -1) { + switch (ch) { + case 'r': + stat_rflag = 1; + break; + default: + ctl_usage(res->ctl); + /* NOTREACHED */ + } + } + argc -= optind; + argv += optind; + + if (argc == 1) { + if (parse_vmid(res, argv[0], 0) == -1) + errx(1, "invalid id: %s", argv[0]); + } else if (argc > 1) ctl_usage(res->ctl); return (vmmaction(res)); diff --git a/usr.sbin/vmctl/vmctl.8 b/usr.sbin/vmctl/vmctl.8 index 146ef3da8d8..607816cc51b 100644 --- a/usr.sbin/vmctl/vmctl.8 +++ b/usr.sbin/vmctl/vmctl.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: vmctl.8,v 1.76 2024/02/16 01:48:06 jsg Exp $ +.\" $OpenBSD: vmctl.8,v 1.77 2024/05/02 15:46:10 mlarkin Exp $ .\" .\" Copyright (c) 2015-2024 Mike Larkin .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: February 16 2024 $ +.Dd $Mdocdate: May 2 2024 $ .Dt VMCTL 8 .Os .Sh NAME @@ -245,9 +245,12 @@ The instance will inherit settings from the parent VM, except for exclusive options such as disk, interface lladdr, and interface names. .El -.It Cm status Op Ar id +.It Cm status Oo Fl r Oc Op Ar id List VMs running on the host, optionally listing just the selected VM .Ar id . +If the +.Fl r +flag is present, the output will only contain running VMs. .It Cm stop Oo Fl fw Oc Oo Fl a | Ar id Oc Stop (terminate) a VM defined by the specified VM .Ar id diff --git a/usr.sbin/vmctl/vmctl.c b/usr.sbin/vmctl/vmctl.c index ba05a7322bb..e08f032c04e 100644 --- a/usr.sbin/vmctl/vmctl.c +++ b/usr.sbin/vmctl/vmctl.c @@ -1,4 +1,4 @@ -/* $OpenBSD: vmctl.c,v 1.89 2023/11/09 12:26:08 dv Exp $ */ +/* $OpenBSD: vmctl.c,v 1.90 2024/05/02 15:46:10 mlarkin Exp $ */ /* * Copyright (c) 2014 Mike Larkin @@ -779,6 +779,7 @@ print_vm_info(struct vmop_info_result *list, size_t ct) char user[16], group[16]; const char *name; int running; + extern int stat_rflag; printf("%5s %5s %5s %7s %7s %7s %12s %8s %s\n", "ID", "PID", "VCPUS", "MAXMEM", "CURMEM", "TTY", "OWNER", "STATE", "NAME"); @@ -787,6 +788,8 @@ print_vm_info(struct vmop_info_result *list, size_t ct) vmi = &list[i]; vir = &vmi->vir_info; running = (vir->vir_creator_pid != 0 && vir->vir_id != 0); + if (!running && stat_rflag) + continue; if (check_info_id(vir->vir_name, vir->vir_id)) { /* get user name */ name = user_from_uid(vmi->vir_uid, 1);