Blob Blame Raw
From 75ad257a1d23dcbde364ad736770d1bd01f157b6 Mon Sep 17 00:00:00 2001
Message-Id: <75ad257a1d23dcbde364ad736770d1bd01f157b6.1387385974.git.minovotn@redhat.com>
In-Reply-To: <7cba796b16325e96d01e65bd265b4073ab103157.1387385974.git.minovotn@redhat.com>
References: <7cba796b16325e96d01e65bd265b4073ab103157.1387385974.git.minovotn@redhat.com>
From: Markus Armbruster <armbru@redhat.com>
Date: Tue, 17 Dec 2013 06:46:36 +0100
Subject: [PATCH 2/5] QMP: Forward-port __com.redhat_drive_add from RHEL-6

RH-Author: Markus Armbruster <armbru@redhat.com>
Message-id: <1387262799-10350-4-git-send-email-armbru@redhat.com>
Patchwork-id: 56294
O-Subject: [PATCH v2 3/6] QMP: Forward-port __com.redhat_drive_add from RHEL-6
Bugzilla: 889051
RH-Acked-by: Fam Zheng <famz@redhat.com>
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>

From: Markus Armbruster <armbru@redhat.com>

Code taken from RHEL-6 as of qemu-kvm-0.12.1.2-2.418.el6, backported
and fixed up as follows:

* Update simple_drive_add() for commit 4e89978 "qemu-option:
  qemu_opts_from_qdict(): use error_set()".

* Update simple_drive_add() for commit 2d0d283 "Support default block
  interfaces per QEMUMachine".

* Add comment explaining drive_init() error reporting hacks to
  simple_drive_add().

* qemu-monitor.hx has been split into qmp-commands.hx and
  hmp-commands.hx.  Copy the QMP parts to qmp-commands.hx.  Clean up
  second example slightly.

* Trailing whitespace cleaned up.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 device-hotplug.c          | 73 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/blockdev.h |  2 ++
 qmp-commands.hx           | 46 +++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 device-hotplug.c          | 73 +++++++++++++++++++++++++++++++++++++++++++++++
 include/sysemu/blockdev.h |  2 ++
 qmp-commands.hx           | 46 +++++++++++++++++++++++++++++
 3 files changed, 121 insertions(+)

diff --git a/device-hotplug.c b/device-hotplug.c
index 103d34a..20f6b7f 100644
--- a/device-hotplug.c
+++ b/device-hotplug.c
@@ -77,3 +77,76 @@ err:
         drive_put_ref(dinfo);
     }
 }
+
+static void check_parm(const char *key, QObject *obj, void *opaque)
+{
+    static const char *valid_keys[] = {
+        "id", "cyls", "heads", "secs", "trans", "media", "snapshot",
+        "file", "cache", "aio", "format", "serial", "rerror", "werror",
+        "readonly", "copy-on-read",
+#ifdef CONFIG_BLOCK_IO_THROTTLING
+        "bps", "bps_rd", "bps_wr", "iops", "iops_rd", "iops_wr",
+#endif
+        NULL
+
+    };
+    int *stopped = opaque;
+    const char **p;
+
+    if (*stopped) {
+        return;
+    }
+
+    for (p = valid_keys; *p; p++) {
+        if (!strcmp(key, *p)) {
+            return;
+        }
+    }
+
+    qerror_report(QERR_INVALID_PARAMETER, key);
+    *stopped = 1;
+}
+
+int simple_drive_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
+{
+    int stopped;
+    Error *local_err = NULL;
+    QemuOpts *opts;
+    DriveInfo *dinfo;
+
+    if (!qdict_haskey(qdict, "id")) {
+        qerror_report(QERR_MISSING_PARAMETER, "id");
+        return -1;
+    }
+
+    stopped = 0;
+    qdict_iter(qdict, check_parm, &stopped);
+    if (stopped) {
+        return -1;
+    }
+
+    opts = qemu_opts_from_qdict(&qemu_drive_opts, qdict, &local_err);
+    if (!opts) {
+        qerror_report_err(local_err);
+        error_free(local_err);
+        return -1;
+    }
+    qemu_opt_set(opts, "if", "none");
+    dinfo = drive_init(opts, current_machine->block_default_type);
+    if (!dinfo) {
+        /*
+         * drive_init() reports some errors with qerror_report_err(),
+         * and some with error_report().  The latter vanish without
+         * trace in monitor_vprintf().  See also the rather optimistic
+         * upstream commit 74ee59a.  Emit a generic error here.  If a
+         * prior error from qerror_report_err() is pending, it'll get
+         * ignored.
+         */
+        qerror_report(QERR_DEVICE_INIT_FAILED,
+                      qemu_opts_id(opts));
+        qemu_opts_del(opts);
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/include/sysemu/blockdev.h b/include/sysemu/blockdev.h
index 1082091..74e1cea 100644
--- a/include/sysemu/blockdev.h
+++ b/include/sysemu/blockdev.h
@@ -67,4 +67,6 @@ void qmp_change_blockdev(const char *device, const char *filename,
                          bool has_format, const char *format, Error **errp);
 void do_commit(Monitor *mon, const QDict *qdict);
 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data);
+
+int simple_drive_add(Monitor *mon, const QDict *qdict, QObject **ret_data);
 #endif
diff --git a/qmp-commands.hx b/qmp-commands.hx
index 0ae9204..9e742d5 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -106,6 +106,52 @@ Example:
 Note: The "force" argument defaults to false.
 
 EQMP
+    {
+        .name       = RFQDN_REDHAT "drive_add",
+        .args_type  = "simple-drive:O",
+        .params     = "id=name,[file=file][,format=f][,media=d]...",
+        .help       = "Create a drive similar to -device if=none.",
+	.user_print = monitor_user_noop,
+        .mhandler.cmd_new = simple_drive_add,
+    },
+
+SQMP
+__com.redhat_drive_add
+----------------------
+
+Create a drive similar to -device if=none.
+
+Arguments:
+
+- "id": Drive ID, must be unique (json-string)
+- "file": Disk image (json-string, optional)
+- "format": Disk format (json-string, optional)
+- "aio": How to perform asynchronous disk I/O (json-string, optional)
+- "cache": Host cache use policy (json-string, optional)
+- "cyls", "heads", "secs": Disk geometry (json-int, optional)
+- "trans": BIOS translation mode (json-string, optional)
+- "media": Media type (json-string, optional)
+- "readonly": Open image read-only (json-bool, optional)
+- "rerror": What to do on read error (json-string, optional)
+- "werror": What to do on write error (json-string, optional)
+- "serial": Drive serial number (json-string, optional)
+- "snapshot": Enable snapshot mode (json-bool, optional)
+- "copy-on-read": Enable copy-on-read mode (json-bool, optional)
+
+Example:
+
+1. Add a drive without medium:
+
+-> { "execute": "__com.redhat_drive_add", "arguments": { "id": "foo" } }
+<- {"return": {}}
+
+2. Add a drive with medium:
+
+-> { "execute": "__com.redhat_drive_add",
+     "arguments": { "id": "bar", "file": "tmp.qcow2", "format": "qcow2" } }
+<- {"return": {}}
+
+EQMP
 
     {
         .name       = RFQDN_REDHAT "drive_del",
-- 
1.7.11.7