[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[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 <ehem+xen@xxxxxxx>
---
 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@xxxxxxx  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445






 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.