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