9ae3a8
From 75ad257a1d23dcbde364ad736770d1bd01f157b6 Mon Sep 17 00:00:00 2001
9ae3a8
Message-Id: <75ad257a1d23dcbde364ad736770d1bd01f157b6.1387385974.git.minovotn@redhat.com>
9ae3a8
In-Reply-To: <7cba796b16325e96d01e65bd265b4073ab103157.1387385974.git.minovotn@redhat.com>
9ae3a8
References: <7cba796b16325e96d01e65bd265b4073ab103157.1387385974.git.minovotn@redhat.com>
9ae3a8
From: Markus Armbruster <armbru@redhat.com>
9ae3a8
Date: Tue, 17 Dec 2013 06:46:36 +0100
9ae3a8
Subject: [PATCH 2/5] QMP: Forward-port __com.redhat_drive_add from RHEL-6
9ae3a8
9ae3a8
RH-Author: Markus Armbruster <armbru@redhat.com>
9ae3a8
Message-id: <1387262799-10350-4-git-send-email-armbru@redhat.com>
9ae3a8
Patchwork-id: 56294
9ae3a8
O-Subject: [PATCH v2 3/6] QMP: Forward-port __com.redhat_drive_add from RHEL-6
9ae3a8
Bugzilla: 889051
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
9ae3a8
9ae3a8
From: Markus Armbruster <armbru@redhat.com>
9ae3a8
9ae3a8
Code taken from RHEL-6 as of qemu-kvm-0.12.1.2-2.418.el6, backported
9ae3a8
and fixed up as follows:
9ae3a8
9ae3a8
* Update simple_drive_add() for commit 4e89978 "qemu-option:
9ae3a8
  qemu_opts_from_qdict(): use error_set()".
9ae3a8
9ae3a8
* Update simple_drive_add() for commit 2d0d283 "Support default block
9ae3a8
  interfaces per QEMUMachine".
9ae3a8
9ae3a8
* Add comment explaining drive_init() error reporting hacks to
9ae3a8
  simple_drive_add().
9ae3a8
9ae3a8
* qemu-monitor.hx has been split into qmp-commands.hx and
9ae3a8
  hmp-commands.hx.  Copy the QMP parts to qmp-commands.hx.  Clean up
9ae3a8
  second example slightly.
9ae3a8
9ae3a8
* Trailing whitespace cleaned up.
9ae3a8
9ae3a8
Signed-off-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
---
9ae3a8
 device-hotplug.c          | 73 +++++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 include/sysemu/blockdev.h |  2 ++
9ae3a8
 qmp-commands.hx           | 46 +++++++++++++++++++++++++++++
9ae3a8
 3 files changed, 121 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Michal Novotny <minovotn@redhat.com>
9ae3a8
---
9ae3a8
 device-hotplug.c          | 73 +++++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 include/sysemu/blockdev.h |  2 ++
9ae3a8
 qmp-commands.hx           | 46 +++++++++++++++++++++++++++++
9ae3a8
 3 files changed, 121 insertions(+)
9ae3a8
9ae3a8
diff --git a/device-hotplug.c b/device-hotplug.c
9ae3a8
index 103d34a..20f6b7f 100644
9ae3a8
--- a/device-hotplug.c
9ae3a8
+++ b/device-hotplug.c
9ae3a8
@@ -77,3 +77,76 @@ err:
9ae3a8
         drive_put_ref(dinfo);
9ae3a8
     }
9ae3a8
 }
9ae3a8
+
9ae3a8
+static void check_parm(const char *key, QObject *obj, void *opaque)
9ae3a8
+{
9ae3a8
+    static const char *valid_keys[] = {
9ae3a8
+        "id", "cyls", "heads", "secs", "trans", "media", "snapshot",
9ae3a8
+        "file", "cache", "aio", "format", "serial", "rerror", "werror",
9ae3a8
+        "readonly", "copy-on-read",
9ae3a8
+#ifdef CONFIG_BLOCK_IO_THROTTLING
9ae3a8
+        "bps", "bps_rd", "bps_wr", "iops", "iops_rd", "iops_wr",
9ae3a8
+#endif
9ae3a8
+        NULL
9ae3a8
+
9ae3a8
+    };
9ae3a8
+    int *stopped = opaque;
9ae3a8
+    const char **p;
9ae3a8
+
9ae3a8
+    if (*stopped) {
9ae3a8
+        return;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    for (p = valid_keys; *p; p++) {
9ae3a8
+        if (!strcmp(key, *p)) {
9ae3a8
+            return;
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    qerror_report(QERR_INVALID_PARAMETER, key);
9ae3a8
+    *stopped = 1;
9ae3a8
+}
9ae3a8
+
9ae3a8
+int simple_drive_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
9ae3a8
+{
9ae3a8
+    int stopped;
9ae3a8
+    Error *local_err = NULL;
9ae3a8
+    QemuOpts *opts;
9ae3a8
+    DriveInfo *dinfo;
9ae3a8
+
9ae3a8
+    if (!qdict_haskey(qdict, "id")) {
9ae3a8
+        qerror_report(QERR_MISSING_PARAMETER, "id");
9ae3a8
+        return -1;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    stopped = 0;
9ae3a8
+    qdict_iter(qdict, check_parm, &stopped);
9ae3a8
+    if (stopped) {
9ae3a8
+        return -1;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    opts = qemu_opts_from_qdict(&qemu_drive_opts, qdict, &local_err);
9ae3a8
+    if (!opts) {
9ae3a8
+        qerror_report_err(local_err);
9ae3a8
+        error_free(local_err);
9ae3a8
+        return -1;
9ae3a8
+    }
9ae3a8
+    qemu_opt_set(opts, "if", "none");
9ae3a8
+    dinfo = drive_init(opts, current_machine->block_default_type);
9ae3a8
+    if (!dinfo) {
9ae3a8
+        /*
9ae3a8
+         * drive_init() reports some errors with qerror_report_err(),
9ae3a8
+         * and some with error_report().  The latter vanish without
9ae3a8
+         * trace in monitor_vprintf().  See also the rather optimistic
9ae3a8
+         * upstream commit 74ee59a.  Emit a generic error here.  If a
9ae3a8
+         * prior error from qerror_report_err() is pending, it'll get
9ae3a8
+         * ignored.
9ae3a8
+         */
9ae3a8
+        qerror_report(QERR_DEVICE_INIT_FAILED,
9ae3a8
+                      qemu_opts_id(opts));
9ae3a8
+        qemu_opts_del(opts);
9ae3a8
+        return -1;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    return 0;
9ae3a8
+}
9ae3a8
diff --git a/include/sysemu/blockdev.h b/include/sysemu/blockdev.h
9ae3a8
index 1082091..74e1cea 100644
9ae3a8
--- a/include/sysemu/blockdev.h
9ae3a8
+++ b/include/sysemu/blockdev.h
9ae3a8
@@ -67,4 +67,6 @@ void qmp_change_blockdev(const char *device, const char *filename,
9ae3a8
                          bool has_format, const char *format, Error **errp);
9ae3a8
 void do_commit(Monitor *mon, const QDict *qdict);
9ae3a8
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
9ae3a8
+
9ae3a8
+int simple_drive_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
9ae3a8
 #endif
9ae3a8
diff --git a/qmp-commands.hx b/qmp-commands.hx
9ae3a8
index 0ae9204..9e742d5 100644
9ae3a8
--- a/qmp-commands.hx
9ae3a8
+++ b/qmp-commands.hx
9ae3a8
@@ -106,6 +106,52 @@ Example:
9ae3a8
 Note: The "force" argument defaults to false.
9ae3a8
 
9ae3a8
 EQMP
9ae3a8
+    {
9ae3a8
+        .name       = RFQDN_REDHAT "drive_add",
9ae3a8
+        .args_type  = "simple-drive:O",
9ae3a8
+        .params     = "id=name,[file=file][,format=f][,media=d]...",
9ae3a8
+        .help       = "Create a drive similar to -device if=none.",
9ae3a8
+	.user_print = monitor_user_noop,
9ae3a8
+        .mhandler.cmd_new = simple_drive_add,
9ae3a8
+    },
9ae3a8
+
9ae3a8
+SQMP
9ae3a8
+__com.redhat_drive_add
9ae3a8
+----------------------
9ae3a8
+
9ae3a8
+Create a drive similar to -device if=none.
9ae3a8
+
9ae3a8
+Arguments:
9ae3a8
+
9ae3a8
+- "id": Drive ID, must be unique (json-string)
9ae3a8
+- "file": Disk image (json-string, optional)
9ae3a8
+- "format": Disk format (json-string, optional)
9ae3a8
+- "aio": How to perform asynchronous disk I/O (json-string, optional)
9ae3a8
+- "cache": Host cache use policy (json-string, optional)
9ae3a8
+- "cyls", "heads", "secs": Disk geometry (json-int, optional)
9ae3a8
+- "trans": BIOS translation mode (json-string, optional)
9ae3a8
+- "media": Media type (json-string, optional)
9ae3a8
+- "readonly": Open image read-only (json-bool, optional)
9ae3a8
+- "rerror": What to do on read error (json-string, optional)
9ae3a8
+- "werror": What to do on write error (json-string, optional)
9ae3a8
+- "serial": Drive serial number (json-string, optional)
9ae3a8
+- "snapshot": Enable snapshot mode (json-bool, optional)
9ae3a8
+- "copy-on-read": Enable copy-on-read mode (json-bool, optional)
9ae3a8
+
9ae3a8
+Example:
9ae3a8
+
9ae3a8
+1. Add a drive without medium:
9ae3a8
+
9ae3a8
+-> { "execute": "__com.redhat_drive_add", "arguments": { "id": "foo" } }
9ae3a8
+<- {"return": {}}
9ae3a8
+
9ae3a8
+2. Add a drive with medium:
9ae3a8
+
9ae3a8
+-> { "execute": "__com.redhat_drive_add",
9ae3a8
+     "arguments": { "id": "bar", "file": "tmp.qcow2", "format": "qcow2" } }
9ae3a8
+<- {"return": {}}
9ae3a8
+
9ae3a8
+EQMP
9ae3a8
 
9ae3a8
     {
9ae3a8
         .name       = RFQDN_REDHAT "drive_del",
9ae3a8
-- 
9ae3a8
1.7.11.7
9ae3a8