From patchwork Sat Dec 19 07:23:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliott Mitchell X-Patchwork-Id: 11997683 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.3 required=3.0 tests=BAYES_00,DATE_IN_PAST_96_XX, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87A27C433DB for ; Mon, 4 Jan 2021 22:53:09 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4DA3F2074B for ; Mon, 4 Jan 2021 22:53:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4DA3F2074B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=m5p.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from list by lists.xenproject.org with outflank-mailman.61577.108469 (Exim 4.92) (envelope-from ) id 1kwYij-0001d0-48; Mon, 04 Jan 2021 22:53:01 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 61577.108469; Mon, 04 Jan 2021 22:53:01 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kwYij-0001ct-0j; Mon, 04 Jan 2021 22:53:01 +0000 Received: by outflank-mailman (input) for mailman id 61577; Mon, 04 Jan 2021 22:52:59 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kwYih-0001cF-9d for xen-devel@lists.xenproject.org; Mon, 04 Jan 2021 22:52:59 +0000 Received: from mailhost.m5p.com (unknown [74.104.188.4]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id fbc95469-5e97-4dbe-a668-db3856b1b067; Mon, 04 Jan 2021 22:52:58 +0000 (UTC) Received: from m5p.com (mailhost.m5p.com [IPv6:2001:470:1f07:15ff:0:0:0:f7]) by mailhost.m5p.com (8.15.2/8.15.2) with ESMTPS id 104MqnT7071512 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Mon, 4 Jan 2021 17:52:55 -0500 (EST) (envelope-from ehem@m5p.com) Received: (from ehem@localhost) by m5p.com (8.15.2/8.15.2/Submit) id 104MqnL5071511; Mon, 4 Jan 2021 14:52:49 -0800 (PST) (envelope-from ehem) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: fbc95469-5e97-4dbe-a668-db3856b1b067 Message-Id: In-Reply-To: References: From: Elliott Mitchell To: xen-devel@lists.xenproject.org Cc: Ian Jackson Cc: Wei Liu Cc: Anthony PERARD Date: Fri, 18 Dec 2020 23:23:36 -0800 Subject: [WIP PATCH 15/16] WIP: tools/xl: Implement output format option for "vm-list" command While the "vm-list" subcommand has far fewer fields than the "list" subcommand, one might still desire to list a subset of the fields. Signed-off-by: Elliott Mitchell --- tools/xl/xl_cmdtable.c | 7 ++++- tools/xl/xl_list.c | 66 +++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/tools/xl/xl_cmdtable.c b/tools/xl/xl_cmdtable.c index 91c2026bc8..c083566989 100644 --- a/tools/xl/xl_cmdtable.c +++ b/tools/xl/xl_cmdtable.c @@ -256,7 +256,12 @@ struct cmd_spec cmd_table[] = { { "vm-list", &main_vm_list, 0, 0, "List guest domains, excluding dom0, stubdoms, etc.", - "", + "[options]\n", + "-F, --format Specify output format string\n" + " Similar to printf(3) formatting, conversion characters are:\n" + " %i Domain Id (%o, %x, and %X allow octal and hexadecimal)\n" + " %n Domain name\n" + " %u UUID\n" }, { "info", &main_info, 0, 0, diff --git a/tools/xl/xl_list.c b/tools/xl/xl_list.c index 49ff2acaad..58809aa10b 100644 --- a/tools/xl/xl_list.c +++ b/tools/xl/xl_list.c @@ -310,6 +310,7 @@ static void format(const format_table_t fmttab, const char *fmt, } static const libxl_dominfo *_discard; +static const libxl_vminfo *_discard_vm; const format_table_t formats = { ['A' - 'A'] = {"NODE Affinity", "", format_node}, @@ -340,6 +341,20 @@ const format_table_t formats = { (int *)&_discard->vcpu_online - (int *)_discard, {.i = 0}}, ['x' - 'A'] = {"ID", "x", format_normal, (int *)&_discard->domid - (int *)_discard, {.i = 0}}, +}, formats_vm = { + ['X' - 'A'] = {"ID", "X", format_normal, + (int *)&_discard_vm->domid - (int *)_discard_vm, {.i = 0}}, + ['i' - 'A'] = {"ID", "d", format_normal, + (int *)&_discard_vm->domid - (int *)_discard_vm, {.i = 0}}, + ['n' - 'A'] = {" name", "s", format_allocstr, + (uint32_t *)&_discard_vm->domid - (uint32_t *)_discard_vm, + {.xlfunc = libxl_domid_to_name}}, + ['o' - 'A'] = {"ID", "o", format_normal, + (int *)&_discard_vm->domid - (int *)_discard_vm, {.i = 0}}, + ['u' - 'A'] = {"UUID ", "", format_uuid, + (char *)&_discard_vm->uuid - (char *)_discard_vm}, + ['x' - 'A'] = {"ID", "x", format_normal, + (int *)&_discard_vm->domid - (int *)_discard_vm, {.i = 0}}, }; static char *build_list_domain_format(bool verbose, bool context, bool claim, @@ -368,29 +383,6 @@ static char *build_list_domain_format(bool verbose, bool context, bool claim, } -static void list_vm(void) -{ - libxl_vminfo *info; - char *domname; - int nb_vm, i; - - info = libxl_list_vm(ctx, &nb_vm); - - if (!info) { - fprintf(stderr, "libxl_list_vm failed.\n"); - exit(EXIT_FAILURE); - } - printf("UUID ID name\n"); - for (i = 0; i < nb_vm; i++) { - domname = libxl_domid_to_name(ctx, info[i].domid); - printf(LIBXL_UUID_FMT " %d %-30s\n", LIBXL_UUID_BYTES(info[i].uuid), - info[i].domid, domname); - free(domname); - } - libxl_vminfo_list_free(info, nb_vm); -} - - int main_list(int argc, char **argv) { int opt; @@ -507,13 +499,35 @@ int main_list(int argc, char **argv) int main_vm_list(int argc, char **argv) { + const char *formatstr = "%u %5i %n\n"; int opt; + static const struct option opts[] = { + {"format", 0, 0, 'F'}, + }; - SWITCH_FOREACH_OPT(opt, "", NULL, "vm-list", 0) { - /* No options */ + libxl_vminfo *info; + int nb_vm, i; + + SWITCH_FOREACH_OPT(opt, "F:", opts, "vm-list", 0) { + case 'F': + formatstr = optarg; + break; + } + + info = libxl_list_vm(ctx, &nb_vm); + + if (!info) { + fprintf(stderr, "libxl_list_vm failed.\n"); + exit(EXIT_FAILURE); } - list_vm(); + format(formats_vm, formatstr, NULL); + + for (i = 0; i < nb_vm; i++) + format(formats_vm, formatstr, info + i); + + libxl_vminfo_list_free(info, nb_vm); + return EXIT_SUCCESS; }