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