958e1b
From 48398d9b96fa334e8c0e626d99cf2df8f4a5c664 Mon Sep 17 00:00:00 2001
958e1b
From: Kevin Wolf <kwolf@redhat.com>
958e1b
Date: Thu, 23 Oct 2014 10:10:11 +0200
958e1b
Subject: [PATCH 08/19] block: Catch duplicate IDs in bdrv_new()
958e1b
958e1b
Message-id: <1414059011-15516-9-git-send-email-kwolf@redhat.com>
958e1b
Patchwork-id: 61840
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH v2 8/8] block: Catch duplicate IDs in bdrv_new()
958e1b
Bugzilla: 1088176
958e1b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
958e1b
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
958e1b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
958e1b
958e1b
Since commit f298d071, block devices added with blockdev-add don't have
958e1b
a QemuOpts around in dinfo->opts. Consequently, we can't rely any more
958e1b
on QemuOpts catching duplicate IDs for block devices.
958e1b
958e1b
This patch adds a new check for duplicate IDs to bdrv_new(), and moves
958e1b
the existing check that the ID isn't already taken for a node-name there
958e1b
as well.
958e1b
958e1b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
958e1b
Reviewed-by: Eric Blake <eblake@redhat.com>
958e1b
(cherry picked from commit f2d953ec31eeeb3029ca915a55938c538a14efa8)
958e1b
958e1b
In RHEL 7, node names don't exist yet, so only check the ID.
958e1b
958e1b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 block.c                    |  6 ++++++
958e1b
 tests/qemu-iotests/087     | 33 +++++++++++++++++++++++++++++++++
958e1b
 tests/qemu-iotests/087.out | 13 +++++++++++++
958e1b
 3 files changed, 52 insertions(+)
958e1b
958e1b
diff --git a/block.c b/block.c
958e1b
index eb0810e..d7b6376 100644
958e1b
--- a/block.c
958e1b
+++ b/block.c
958e1b
@@ -304,6 +304,12 @@ BlockDriverState *bdrv_new(const char *device_name, Error **errp)
958e1b
 {
958e1b
     BlockDriverState *bs;
958e1b
 
958e1b
+    if (bdrv_find(device_name)) {
958e1b
+        error_setg(errp, "Device with id '%s' already exists",
958e1b
+                   device_name);
958e1b
+        return NULL;
958e1b
+    }
958e1b
+
958e1b
     bs = g_malloc0(sizeof(BlockDriverState));
958e1b
     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
958e1b
     if (device_name[0] != '\0') {
958e1b
diff --git a/tests/qemu-iotests/087 b/tests/qemu-iotests/087
958e1b
index 53b6c43..f2fb3f4 100755
958e1b
--- a/tests/qemu-iotests/087
958e1b
+++ b/tests/qemu-iotests/087
958e1b
@@ -73,6 +73,39 @@ run_qemu <
958e1b
 EOF
958e1b
 
958e1b
 echo
958e1b
+echo === Duplicate ID ===
958e1b
+echo
958e1b
+
958e1b
+run_qemu <
958e1b
+{ "execute": "qmp_capabilities" }
958e1b
+{ "execute": "blockdev-add",
958e1b
+  "arguments": {
958e1b
+      "options": {
958e1b
+        "driver": "$IMGFMT",
958e1b
+        "id": "disk",
958e1b
+        "file": {
958e1b
+            "driver": "file",
958e1b
+            "filename": "$TEST_IMG"
958e1b
+        }
958e1b
+      }
958e1b
+    }
958e1b
+  }
958e1b
+{ "execute": "blockdev-add",
958e1b
+  "arguments": {
958e1b
+      "options": {
958e1b
+        "driver": "$IMGFMT",
958e1b
+        "id": "disk",
958e1b
+        "file": {
958e1b
+            "driver": "file",
958e1b
+            "filename": "$TEST_IMG"
958e1b
+        }
958e1b
+      }
958e1b
+    }
958e1b
+  }
958e1b
+{ "execute": "quit" }
958e1b
+EOF
958e1b
+
958e1b
+echo
958e1b
 echo === aio=native without O_DIRECT ===
958e1b
 echo
958e1b
 
958e1b
diff --git a/tests/qemu-iotests/087.out b/tests/qemu-iotests/087.out
958e1b
index b871032..19e0228 100644
958e1b
--- a/tests/qemu-iotests/087.out
958e1b
+++ b/tests/qemu-iotests/087.out
958e1b
@@ -13,6 +13,19 @@ QMP_VERSION
958e1b
 {"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "DEVICE_TRAY_MOVED", "data": {"device": "floppy0", "tray-open": true}}
958e1b
 
958e1b
 
958e1b
+=== Duplicate ID ===
958e1b
+
958e1b
+Testing:
958e1b
+QMP_VERSION
958e1b
+{"return": {}}
958e1b
+{"return": {}}
958e1b
+{"error": {"class": "GenericError", "desc": "Device with id 'disk' already exists"}}
958e1b
+{"return": {}}
958e1b
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "SHUTDOWN"}
958e1b
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "DEVICE_TRAY_MOVED", "data": {"device": "ide1-cd0", "tray-open": true}}
958e1b
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "DEVICE_TRAY_MOVED", "data": {"device": "floppy0", "tray-open": true}}
958e1b
+
958e1b
+
958e1b
 === aio=native without O_DIRECT ===
958e1b
 
958e1b
 Testing:
958e1b
-- 
958e1b
1.8.3.1
958e1b