All of lore.kernel.org
 help / color / mirror / Atom feed
From: Elliott Mitchell <ehem+xen@m5p.com>
To: xen-devel@lists.xenproject.org
Cc: Ian Jackson <iwj@xenproject.org>
Cc: Wei Liu <wl@xen.org>
Cc: Anthony PERARD <anthony.perard@citrix.com>
Subject: [WIP PATCH 15/16] WIP: tools/xl: Implement output format option for "vm-list" command
Date: Fri, 18 Dec 2020 23:23:36 -0800	[thread overview]
Message-ID: <fc15fc9a52552b8218ad5b8def88b6cf903c4a24.1608663694.git.ehem+xen@m5p.com> (raw)
In-Reply-To: <cover.1608663694.git.ehem+xen@m5p.com>

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 <ehem+xen@m5p.com>
---
 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;
 }
 
-- 


-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         ehem+sigmsg@m5p.com  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445





  parent reply	other threads:[~2021-01-04 22:53 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-22 19:01 [WIP PATCH 00/16] Addition of formatting options to `xl list` subcommands Elliott Mitchell
2020-12-09 22:34 ` [WIP PATCH 14/16] WIP: tools/xl: Enhance "list" command Elliott Mitchell
2020-12-09 22:34 ` [WIP PATCH 07/16] tools/xl: Sort list command options Elliott Mitchell
2020-12-09 22:45 ` [WIP PATCH 06/16] tools/xl: Split list commands off of xl_info.c Elliott Mitchell
2020-12-10 23:09 ` [WIP PATCH 09/16] WIP: tools/xl: Implement generalized output formatting for `xl list` Elliott Mitchell
2020-12-10 23:09 ` [WIP PATCH 10/16] WIP: tools/xl: Implement output format option Elliott Mitchell
2020-12-10 23:09 ` [WIP PATCH 08/16] tools/xl: Fix potential deallocation bug Elliott Mitchell
2020-12-12  6:18 ` [WIP PATCH 11/16] WIP: tools/xl: Replace most of list_domains with use of format() Elliott Mitchell
2020-12-13  4:42 ` [WIP PATCH 12/16] WIP: UNTESTED: tools/xl: Replace remaining options with format() Elliott Mitchell
2020-12-13  5:14 ` [WIP PATCH 13/16] WIP: tools/xl: Purge list_domains() Elliott Mitchell
2020-12-18  1:42 ` [WIP PATCH 04/16] tools/xl: Rename printf_info()/list_domains_details() to dump_by_...() Elliott Mitchell
2020-12-18  1:42 ` [WIP PATCH 05/16] tools/xl: Merge down debug/dry-run section of create_domain() Elliott Mitchell
2020-12-18 21:32 ` [WIP PATCH 03/16] tools/xl: Mark libxl_domain_config * arg of printf_info_*() const Elliott Mitchell
2020-12-18 21:37 ` [WIP PATCH 01/16] tools/libxl: Mark pointer args of many functions constant Elliott Mitchell
2020-12-18 22:45 ` [WIP PATCH 02/16] tools/libxl: Tiny optimization of libxl__mac_is_default() Elliott Mitchell
2020-12-19  7:23 ` Elliott Mitchell [this message]
2020-12-20  7:43 ` [WIP PATCH 16/16] WIP: tools/xl: Enhance "vm-list" command Elliott Mitchell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fc15fc9a52552b8218ad5b8def88b6cf903c4a24.1608663694.git.ehem+xen@m5p.com \
    --to=ehem+xen@m5p.com \
    --cc=iwj@xenproject.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.