a41c76
From c481bcacd1f515d2e93036dc452a25e9ff06f7ae Mon Sep 17 00:00:00 2001
a41c76
Message-Id: <c481bcacd1f515d2e93036dc452a25e9ff06f7ae@dist-git>
073345
From: Michal Privoznik <mprivozn@redhat.com>
a41c76
Date: Fri, 6 Mar 2020 15:52:22 +0100
073345
Subject: [PATCH] RHEL: virscsi: Support TAPEs in virSCSIDeviceGetDevName()
073345
073345
If the SCSI device we want to get /dev node name for is TAPE
073345
device we need to look at 'tape' symlink in the sysfs dir
073345
corresponding to the device.
073345
a41c76
https://bugzilla.redhat.com/show_bug.cgi?id=1808390
073345
073345
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
073345
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
a41c76
Message-Id: <20200306145226.1610708-3-abologna@redhat.com>
073345
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
073345
---
a41c76
 src/util/virscsi.c                          | 28 +++++++++++++++
073345
 tests/virscsidata/2-0-0-0/model             |  1 +
073345
 tests/virscsidata/2-0-0-0/scsi_tape/st0/dev |  1 +
073345
 tests/virscsidata/2-0-0-0/sg3/dev           |  1 +
073345
 tests/virscsidata/2-0-0-0/tape              |  1 +
073345
 tests/virscsidata/2-0-0-0/type              |  1 +
073345
 tests/virscsidata/2-0-0-0/vendor            |  1 +
073345
 tests/virscsidata/sg3                       |  0
073345
 tests/virscsitest.c                         | 38 ++++++++++++++++++---
a41c76
 9 files changed, 67 insertions(+), 5 deletions(-)
073345
 create mode 100644 tests/virscsidata/2-0-0-0/model
073345
 create mode 100644 tests/virscsidata/2-0-0-0/scsi_tape/st0/dev
073345
 create mode 100644 tests/virscsidata/2-0-0-0/sg3/dev
073345
 create mode 120000 tests/virscsidata/2-0-0-0/tape
073345
 create mode 100644 tests/virscsidata/2-0-0-0/type
073345
 create mode 100644 tests/virscsidata/2-0-0-0/vendor
073345
 create mode 100644 tests/virscsidata/sg3
073345
073345
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
a41c76
index c40857977f..57958c06ea 100644
073345
--- a/src/util/virscsi.c
073345
+++ b/src/util/virscsi.c
a41c76
@@ -238,6 +238,31 @@ virSCSIDeviceGetDevNameBlock(const char *prefix,
073345
 }
073345
 
073345
 
073345
+static char *
073345
+virSCSIDeviceGetDevNameTape(const char *prefix,
073345
+                            unsigned int adapter,
073345
+                            unsigned int bus,
073345
+                            unsigned int target,
073345
+                            unsigned long long unit)
073345
+{
a41c76
+    g_autofree char *path = NULL;
a41c76
+    g_autofree char *resolvedPath = NULL;
a41c76
+    g_autoptr(GError) err = NULL;
073345
+
a41c76
+    path = g_strdup_printf("%s/%d:%u:%u:%llu/tape",
a41c76
+                           prefix, adapter, bus, target, unit);
073345
+
a41c76
+    if (!(resolvedPath = g_file_read_link(path, &err))) {
a41c76
+        virReportError(VIR_ERR_SYSTEM_ERROR,
a41c76
+                       _("Unable to read link: %s"),
a41c76
+                       err->message);
a41c76
+        return NULL;
073345
+    }
073345
+
a41c76
+    return g_path_get_basename(resolvedPath);
073345
+}
073345
+
073345
+
073345
 /* Returns device name (e.g. "sdc") on success, or NULL
073345
  * on failure.
073345
  */
a41c76
@@ -266,6 +291,9 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
073345
         break;
073345
 
073345
     case VIR_SCSI_DEVICE_TYPE_TAPE:
073345
+        name = virSCSIDeviceGetDevNameTape(prefix, adapter_id, bus, target, unit);
073345
+        break;
073345
+
073345
     case VIR_SCSI_DEVICE_TYPE_PRINTER:
073345
     case VIR_SCSI_DEVICE_TYPE_PROCESSOR:
073345
     case VIR_SCSI_DEVICE_TYPE_WORM:
073345
diff --git a/tests/virscsidata/2-0-0-0/model b/tests/virscsidata/2-0-0-0/model
073345
new file mode 100644
073345
index 0000000000..d2ab4715c3
073345
--- /dev/null
073345
+++ b/tests/virscsidata/2-0-0-0/model
073345
@@ -0,0 +1 @@
073345
+scsi_debug
073345
diff --git a/tests/virscsidata/2-0-0-0/scsi_tape/st0/dev b/tests/virscsidata/2-0-0-0/scsi_tape/st0/dev
073345
new file mode 100644
073345
index 0000000000..3dd777e840
073345
--- /dev/null
073345
+++ b/tests/virscsidata/2-0-0-0/scsi_tape/st0/dev
073345
@@ -0,0 +1 @@
073345
+9:0
073345
diff --git a/tests/virscsidata/2-0-0-0/sg3/dev b/tests/virscsidata/2-0-0-0/sg3/dev
073345
new file mode 100644
073345
index 0000000000..b369a59b3e
073345
--- /dev/null
073345
+++ b/tests/virscsidata/2-0-0-0/sg3/dev
073345
@@ -0,0 +1 @@
073345
+21:3
073345
diff --git a/tests/virscsidata/2-0-0-0/tape b/tests/virscsidata/2-0-0-0/tape
073345
new file mode 120000
073345
index 0000000000..6ca7f77539
073345
--- /dev/null
073345
+++ b/tests/virscsidata/2-0-0-0/tape
073345
@@ -0,0 +1 @@
073345
+scsi_tape/st0
073345
\ No newline at end of file
073345
diff --git a/tests/virscsidata/2-0-0-0/type b/tests/virscsidata/2-0-0-0/type
073345
new file mode 100644
073345
index 0000000000..d00491fd7e
073345
--- /dev/null
073345
+++ b/tests/virscsidata/2-0-0-0/type
073345
@@ -0,0 +1 @@
073345
+1
073345
diff --git a/tests/virscsidata/2-0-0-0/vendor b/tests/virscsidata/2-0-0-0/vendor
073345
new file mode 100644
073345
index 0000000000..9b075671ea
073345
--- /dev/null
073345
+++ b/tests/virscsidata/2-0-0-0/vendor
073345
@@ -0,0 +1 @@
073345
+Linux
073345
diff --git a/tests/virscsidata/sg3 b/tests/virscsidata/sg3
073345
new file mode 100644
073345
index 0000000000..e69de29bb2
073345
diff --git a/tests/virscsitest.c b/tests/virscsitest.c
a41c76
index d5a0da4753..e501d6d041 100644
073345
--- a/tests/virscsitest.c
073345
+++ b/tests/virscsitest.c
a41c76
@@ -32,18 +32,34 @@ VIR_LOG_INIT("tests.scsitest");
a41c76
 
073345
 static char *virscsi_prefix;
073345
 
073345
+typedef struct {
073345
+    const char *adapter;
073345
+    unsigned int bus;
073345
+    unsigned int target;
073345
+    unsigned int unit;
073345
+    const char *expectedName;
073345
+} testGetDevNameData;
073345
+
073345
 static int
a41c76
-test1(const void *data G_GNUC_UNUSED)
073345
+testGetDevName(const void *opaque)
073345
 {
073345
+    const testGetDevNameData *data = opaque;
073345
     char *name = NULL;
073345
     int ret = -1;
073345
 
073345
     if (!(name = virSCSIDeviceGetDevName(virscsi_prefix,
073345
-                                         "scsi_host1", 0, 0, 0)))
073345
+                                         data->adapter,
073345
+                                         data->bus,
073345
+                                         data->target,
073345
+                                         data->unit)))
073345
         return -1;
073345
 
073345
-    if (STRNEQ(name, "sdh"))
073345
+    if (STRNEQ(name, data->expectedName)) {
073345
+        fprintf(stderr,
073345
+                "SCSI dev name mismatch, expected %s got %s",
073345
+                data->expectedName, name);
073345
         goto cleanup;
073345
+    }
073345
 
073345
     ret = 0;
073345
  cleanup:
a41c76
@@ -212,15 +228,27 @@ mymain(void)
073345
 
073345
     CREATE_SYMLINK("0-0-0-0", "0:0:0:0");
073345
     CREATE_SYMLINK("1-0-0-0", "1:0:0:0");
073345
+    CREATE_SYMLINK("2-0-0-0", "2:0:0:0");
073345
     CREATE_SYMLINK("sg0", "sg0");
073345
+    CREATE_SYMLINK("sg3", "sg3");
073345
     CREATE_SYMLINK("sg8", "sg8");
073345
 
073345
     VIR_FREE(virscsi_prefix);
a41c76
 
a41c76
     virscsi_prefix = g_strdup(tmpdir);
073345
 
073345
-    if (virTestRun("test1", test1, NULL) < 0)
073345
-        ret = -1;
073345
+#define TEST_GET_DEV_NAME(adapter, bus, target, unit, expectedName) \
073345
+    do { \
073345
+        testGetDevNameData data = {adapter, bus, target, unit, expectedName}; \
073345
+        if (virTestRun("test getDevname " expectedName, \
073345
+                       testGetDevName, &data) < 0) \
073345
+            ret = -1; \
073345
+    } while (0)
073345
+
073345
+    TEST_GET_DEV_NAME("scsi_host0", 0, 0, 0, "sda");
073345
+    TEST_GET_DEV_NAME("scsi_host1", 0, 0, 0, "sdh");
073345
+    TEST_GET_DEV_NAME("scsi_host2", 0, 0, 0, "st0");
073345
+
073345
     if (virTestRun("test2", test2, NULL) < 0)
073345
         ret = -1;
073345
 
073345
-- 
073345
2.25.1
073345