libxl: add "xl qemu-monitor-command"
authorJuergen Gross <jgross@suse.com>
Tue, 6 Sep 2016 10:51:06 +0000 (12:51 +0200)
committerWei Liu <wei.liu2@citrix.com>
Thu, 15 Sep 2016 09:48:55 +0000 (10:48 +0100)
Add a new xl command "qemu-monitor-command" to issue arbitrary commands
to a domain's device model. Syntax is:

xl qemu-monitor-command <domain> <command>

The command is issued via qmp human-monitor-command command. Any
information returned by the command is printed to stdout.

Signed-off-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Dario Faggioli <dario.faggioli@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
docs/man/xl.pod.1.in
tools/libxl/libxl.h
tools/libxl/libxl_colo_qdisk.c
tools/libxl/libxl_internal.h
tools/libxl/libxl_qmp.c
tools/libxl/xl.h
tools/libxl/xl_cmdimpl.c
tools/libxl/xl_cmdtable.c

index 1adf3223fd2a71be928fb9817a35782070530e65..a2be541436d9665e6ed4dfea0da8ae1b8675d32d 100644 (file)
@@ -1516,6 +1516,42 @@ List pass-through usb devices for a domain.
 
 =back
 
+=head1 DEVICE-MODEL CONTROL
+
+=over 4
+
+=item B<qemu-monitor-command> I<domain-id> I<command>
+
+Issue a monitor command to the device model of the domain specified by
+I<domain-id>. I<command> can be any valid command qemu understands. This
+can be e.g. used to add non-standard devices or devices with non-standard
+parameters to a domain. The output of the command is printed to stdout.
+
+B<Warning:> This qemu monitor access is provided for convenience when
+debugging, troubleshooting, and experimenting.  Its use is not
+supported by the Xen Project.
+
+Specifically, not all information printed by the qemu monitor will
+necessarily be accurate or complete, because in a Xen system qemu
+does not have a complete view of the guest.
+
+Furthermore, modifying the guest's setup via the qemu monitor may
+conflict with the Xen toolstack's assumptions.  Resulting problems
+may include, but are not limited to: guest crashes; toolstack error
+messages; inability to migrate the guest; and security
+vulnerabilities which are not covered by the Xen Project security
+response policy.
+
+B<EXAMPLE>
+
+Obtain information of USB devices connected as such via the device model
+(only!) to a domain:
+
+ xl qemu-monitor-command vm1 'info usb'
+  Device 0.2, Port 5, Speed 480 Mb/s, Product Mass Storage
+
+=back
+
 =head1 TMEM
 
 =over 4
index e4c25c4d112b6575e2095d0ca3d82be60dd9fa75..7cfa540282f63b092145af4ce15eba786d6f79f1 100644 (file)
  */
 #define LIBXL_HAVE_BUILD_ID 1
 
+/*
+ * LIBXL_HAVE_QEMU_MONITOR_COMMAND indiactes the availability of the
+ * libxl_qemu_monitor_command() function.
+ */
+#define LIBXL_HAVE_QEMU_MONITOR_COMMAND 1
+
 /*
  * libxl ABI compatibility
  *
@@ -2152,6 +2158,14 @@ void libxl_psr_cat_info_list_free(libxl_psr_cat_info *list, int nr);
 int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec);
 int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock);
 
+/*
+ * Issue a qmp monitor command to the device model of the specified domain.
+ * The function returns the output of the command in a new allocated buffer
+ * via output.
+ */
+int libxl_qemu_monitor_command(libxl_ctx *ctx, uint32_t domid,
+                               const char *command_line, char **output);
+
 #include <libxl_event.h>
 
 #endif /* LIBXL_H */
index c23b81bae85bf7a4702c5ceca1bfb37b6edcc0f0..d271d1fa02fdb02fd97e4a8f61d959fa77d6bfc9 100644 (file)
@@ -173,7 +173,7 @@ static void colo_qdisk_save_preresume(libxl__egc *egc,
                     "file.driver=nbd,file.host=%s,file.port=%d,"
                     "file.export=%s,node-name=%s,if=none",
                     host, port, export_name, node);
-    ret = libxl__qmp_hmp(gc, domid, cmd);
+    ret = libxl__qmp_hmp(gc, domid, cmd, NULL);
     if (ret)
         rc = ERROR_FAIL;
 
index 05a930825ebed955ce1c405c19757fbcb1ffac74..7510031d7e6b402d710c00875f1657c8832eed38 100644 (file)
@@ -1821,7 +1821,8 @@ _hidden int libxl__qmp_x_blockdev_change(libxl__gc *gc, int domid,
                                          const char *parant,
                                          const char *child, const char *node);
 /* run a hmp command in qmp mode */
-_hidden int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line);
+_hidden int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line,
+                           char **out);
 /* close and free the QMP handler */
 _hidden void libxl__qmp_close(libxl__qmp_handler *qmp);
 /* remove the socket file, if the file has already been removed,
index f6748e6e1e6ba2e6c5d155308795c6d8f6aa04ed..f8addf9ba6b79603e8f981022c9864f2937a776f 100644 (file)
@@ -1119,14 +1119,54 @@ int libxl__qmp_x_blockdev_change(libxl__gc *gc, int domid, const char *parent,
     return qmp_run_command(gc, domid, "x-blockdev-change", args, NULL, NULL);
 }
 
-int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line)
+static int hmp_callback(libxl__qmp_handler *qmp,
+                        const libxl__json_object *response,
+                        void *opaque)
+{
+    char **output = opaque;
+    GC_INIT(qmp->ctx);
+    int rc;
+
+    rc = 0;
+    if (!output)
+        goto out;
+
+    *output = NULL;
+
+    if (libxl__json_object_is_string(response)) {
+        *output = libxl__strdup(NOGC, libxl__json_object_get_string(response));
+        goto out;
+    }
+
+    LOG(ERROR, "Response has unexpected format");
+    rc = ERROR_FAIL;
+
+out:
+    GC_FREE;
+    return rc;
+}
+
+int libxl__qmp_hmp(libxl__gc *gc, int domid, const char *command_line,
+                   char **output)
 {
     libxl__json_object *args = NULL;
 
     qmp_parameters_add_string(gc, &args, "command-line", command_line);
 
     return qmp_run_command(gc, domid, "human-monitor-command", args,
-                           NULL, NULL);
+                           hmp_callback, output);
+}
+
+int libxl_qemu_monitor_command(libxl_ctx *ctx, uint32_t domid,
+                               const char *command_line, char **output)
+{
+    GC_INIT(ctx);
+    int rc;
+
+    rc = libxl__qmp_hmp(gc, domid, command_line, output);
+
+    GC_FREE;
+    return rc;
 }
 
 int libxl__qmp_initializations(libxl__gc *gc, uint32_t domid,
index e601ca1dfbd5b74ae6150ee0dfa4a3dcfb1f5244..0a8c8133cfd5d54d2d334508d28a3b52294b5623 100644 (file)
@@ -133,6 +133,7 @@ int main_psr_cmt_show(int argc, char **argv);
 int main_psr_cat_cbm_set(int argc, char **argv);
 int main_psr_cat_show(int argc, char **argv);
 #endif
+int main_qemu_monitor_command(int argc, char **argv);
 
 void help(const char *command);
 
index 7540fb1bfa4ec3d941955c0fd101fa4ba842e621..62237d0acd24142dde35f9052c3a6773c900f06e 100644 (file)
@@ -9536,6 +9536,35 @@ int main_psr_hwinfo(int argc, char **argv)
 
 #endif
 
+int main_qemu_monitor_command(int argc, char **argv)
+{
+    int opt;
+    uint32_t domid;
+    char *cmd;
+    char *output;
+    int ret;
+
+    SWITCH_FOREACH_OPT(opt, "", NULL, "qemu-monitor-command", 2) {
+        /* No options */
+    }
+
+    domid = find_domain(argv[optind]);
+    cmd = argv[optind + 1];
+
+    if (argc - optind > 2) {
+        fprintf(stderr, "Invalid arguments.\n");
+        return EXIT_FAILURE;
+    }
+
+    ret = libxl_qemu_monitor_command(ctx, domid, cmd, &output);
+    if (!ret && output) {
+        printf("%s\n", output);
+        free(output);
+    }
+
+    return ret ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
 /*
  * Local variables:
  * mode: C
index 85c1e0f7bd8a5ef41e528ae9b72944c16945ed86..78786fe2d1e4233699ad679336d4654298ab4457 100644 (file)
@@ -584,6 +584,11 @@ struct cmd_spec cmd_table[] = {
       "List information about all USB controllers and devices for a domain",
       "<Domain>",
     },
+    { "qemu-monitor-command",
+      &main_qemu_monitor_command, 0, 1,
+      "Issue a qemu monitor command to the device model of a domain",
+      "<Domain> <Command>",
+    },
 };
 
 int cmdtable_len = sizeof(cmd_table)/sizeof(struct cmd_spec);