render / rpms / libvirt

Forked from rpms/libvirt 7 months ago
Clone
25aafc
From 227bf0ec28c4ace3b90d383393a9e93c3c0c727c Mon Sep 17 00:00:00 2001
25aafc
Message-Id: <227bf0ec28c4ace3b90d383393a9e93c3c0c727c@dist-git>
25aafc
From: Michal Privoznik <mprivozn@redhat.com>
25aafc
Date: Tue, 8 Oct 2019 14:14:20 +0200
25aafc
Subject: [PATCH] RHEL: virscsi: Support TAPEs in virSCSIDeviceGetDevName()
25aafc
25aafc
If the SCSI device we want to get /dev node name for is TAPE
25aafc
device we need to look at 'tape' symlink in the sysfs dir
25aafc
corresponding to the device.
25aafc
25aafc
https://bugzilla.redhat.com/show_bug.cgi?id=1754241
25aafc
25aafc
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
25aafc
Message-Id: <3c722bc2e0627583f26111441f37027c256878c7.1570536610.git.mprivozn@redhat.com>
25aafc
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
25aafc
---
25aafc
 src/util/virscsi.c                          | 37 ++++++++++++++++++++
25aafc
 tests/virscsidata/2-0-0-0/model             |  1 +
25aafc
 tests/virscsidata/2-0-0-0/scsi_tape/st0/dev |  1 +
25aafc
 tests/virscsidata/2-0-0-0/sg3/dev           |  1 +
25aafc
 tests/virscsidata/2-0-0-0/tape              |  1 +
25aafc
 tests/virscsidata/2-0-0-0/type              |  1 +
25aafc
 tests/virscsidata/2-0-0-0/vendor            |  1 +
25aafc
 tests/virscsidata/sg3                       |  0
25aafc
 tests/virscsitest.c                         | 38 ++++++++++++++++++---
25aafc
 9 files changed, 76 insertions(+), 5 deletions(-)
25aafc
 create mode 100644 tests/virscsidata/2-0-0-0/model
25aafc
 create mode 100644 tests/virscsidata/2-0-0-0/scsi_tape/st0/dev
25aafc
 create mode 100644 tests/virscsidata/2-0-0-0/sg3/dev
25aafc
 create mode 120000 tests/virscsidata/2-0-0-0/tape
25aafc
 create mode 100644 tests/virscsidata/2-0-0-0/type
25aafc
 create mode 100644 tests/virscsidata/2-0-0-0/vendor
25aafc
 create mode 100644 tests/virscsidata/sg3
25aafc
25aafc
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
25aafc
index af908107d9..6c3fd8a562 100644
25aafc
--- a/src/util/virscsi.c
25aafc
+++ b/src/util/virscsi.c
25aafc
@@ -42,6 +42,7 @@
25aafc
 #include "virutil.h"
25aafc
 #include "virstring.h"
25aafc
 #include "virerror.h"
25aafc
+#include "dirname.h"
25aafc
 
25aafc
 #define SYSFS_SCSI_DEVICES "/sys/bus/scsi/devices"
25aafc
 
25aafc
@@ -249,6 +250,39 @@ virSCSIDeviceGetDevNameBlock(const char *prefix,
25aafc
 }
25aafc
 
25aafc
 
25aafc
+static char *
25aafc
+virSCSIDeviceGetDevNameTape(const char *prefix,
25aafc
+                            unsigned int adapter,
25aafc
+                            unsigned int bus,
25aafc
+                            unsigned int target,
25aafc
+                            unsigned long long unit)
25aafc
+{
25aafc
+    char *path = NULL;
25aafc
+    char *resolvedPath = NULL;
25aafc
+    char *name = NULL;
25aafc
+
25aafc
+    if (virAsprintf(&path,
25aafc
+                    "%s/%d:%u:%u:%llu/tape",
25aafc
+                    prefix, adapter, bus, target, unit) < 0)
25aafc
+        return NULL;
25aafc
+
25aafc
+    if (virFileReadLink(path, &resolvedPath) < 0) {
25aafc
+        virReportSystemError(errno,
25aafc
+                             _("Unable to read link: %s"),
25aafc
+                             path);
25aafc
+        goto cleanup;
25aafc
+    }
25aafc
+
25aafc
+    if (VIR_STRDUP(name, last_component(resolvedPath)) < 0)
25aafc
+        goto cleanup;
25aafc
+
25aafc
+ cleanup:
25aafc
+    VIR_FREE(resolvedPath);
25aafc
+    VIR_FREE(path);
25aafc
+    return name;
25aafc
+}
25aafc
+
25aafc
+
25aafc
 /* Returns device name (e.g. "sdc") on success, or NULL
25aafc
  * on failure.
25aafc
  */
25aafc
@@ -277,6 +311,9 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
25aafc
         break;
25aafc
 
25aafc
     case VIR_SCSI_DEVICE_TYPE_TAPE:
25aafc
+        name = virSCSIDeviceGetDevNameTape(prefix, adapter_id, bus, target, unit);
25aafc
+        break;
25aafc
+
25aafc
     case VIR_SCSI_DEVICE_TYPE_PRINTER:
25aafc
     case VIR_SCSI_DEVICE_TYPE_PROCESSOR:
25aafc
     case VIR_SCSI_DEVICE_TYPE_WORM:
25aafc
diff --git a/tests/virscsidata/2-0-0-0/model b/tests/virscsidata/2-0-0-0/model
25aafc
new file mode 100644
25aafc
index 0000000000..d2ab4715c3
25aafc
--- /dev/null
25aafc
+++ b/tests/virscsidata/2-0-0-0/model
25aafc
@@ -0,0 +1 @@
25aafc
+scsi_debug
25aafc
diff --git a/tests/virscsidata/2-0-0-0/scsi_tape/st0/dev b/tests/virscsidata/2-0-0-0/scsi_tape/st0/dev
25aafc
new file mode 100644
25aafc
index 0000000000..3dd777e840
25aafc
--- /dev/null
25aafc
+++ b/tests/virscsidata/2-0-0-0/scsi_tape/st0/dev
25aafc
@@ -0,0 +1 @@
25aafc
+9:0
25aafc
diff --git a/tests/virscsidata/2-0-0-0/sg3/dev b/tests/virscsidata/2-0-0-0/sg3/dev
25aafc
new file mode 100644
25aafc
index 0000000000..b369a59b3e
25aafc
--- /dev/null
25aafc
+++ b/tests/virscsidata/2-0-0-0/sg3/dev
25aafc
@@ -0,0 +1 @@
25aafc
+21:3
25aafc
diff --git a/tests/virscsidata/2-0-0-0/tape b/tests/virscsidata/2-0-0-0/tape
25aafc
new file mode 120000
25aafc
index 0000000000..6ca7f77539
25aafc
--- /dev/null
25aafc
+++ b/tests/virscsidata/2-0-0-0/tape
25aafc
@@ -0,0 +1 @@
25aafc
+scsi_tape/st0
25aafc
\ No newline at end of file
25aafc
diff --git a/tests/virscsidata/2-0-0-0/type b/tests/virscsidata/2-0-0-0/type
25aafc
new file mode 100644
25aafc
index 0000000000..d00491fd7e
25aafc
--- /dev/null
25aafc
+++ b/tests/virscsidata/2-0-0-0/type
25aafc
@@ -0,0 +1 @@
25aafc
+1
25aafc
diff --git a/tests/virscsidata/2-0-0-0/vendor b/tests/virscsidata/2-0-0-0/vendor
25aafc
new file mode 100644
25aafc
index 0000000000..9b075671ea
25aafc
--- /dev/null
25aafc
+++ b/tests/virscsidata/2-0-0-0/vendor
25aafc
@@ -0,0 +1 @@
25aafc
+Linux
25aafc
diff --git a/tests/virscsidata/sg3 b/tests/virscsidata/sg3
25aafc
new file mode 100644
25aafc
index 0000000000..e69de29bb2
25aafc
diff --git a/tests/virscsitest.c b/tests/virscsitest.c
25aafc
index 1215adbfab..880fa22ca8 100644
25aafc
--- a/tests/virscsitest.c
25aafc
+++ b/tests/virscsitest.c
25aafc
@@ -36,18 +36,34 @@ VIR_LOG_INIT("tests.scsitest");
25aafc
 static const char *abs_top_srcdir;
25aafc
 static char *virscsi_prefix;
25aafc
 
25aafc
+typedef struct {
25aafc
+    const char *adapter;
25aafc
+    unsigned int bus;
25aafc
+    unsigned int target;
25aafc
+    unsigned int unit;
25aafc
+    const char *expectedName;
25aafc
+} testGetDevNameData;
25aafc
+
25aafc
 static int
25aafc
-test1(const void *data ATTRIBUTE_UNUSED)
25aafc
+testGetDevName(const void *opaque)
25aafc
 {
25aafc
+    const testGetDevNameData *data = opaque;
25aafc
     char *name = NULL;
25aafc
     int ret = -1;
25aafc
 
25aafc
     if (!(name = virSCSIDeviceGetDevName(virscsi_prefix,
25aafc
-                                         "scsi_host1", 0, 0, 0)))
25aafc
+                                         data->adapter,
25aafc
+                                         data->bus,
25aafc
+                                         data->target,
25aafc
+                                         data->unit)))
25aafc
         return -1;
25aafc
 
25aafc
-    if (STRNEQ(name, "sdh"))
25aafc
+    if (STRNEQ(name, data->expectedName)) {
25aafc
+        fprintf(stderr,
25aafc
+                "SCSI dev name mismatch, expected %s got %s",
25aafc
+                data->expectedName, name);
25aafc
         goto cleanup;
25aafc
+    }
25aafc
 
25aafc
     ret = 0;
25aafc
  cleanup:
25aafc
@@ -225,7 +241,9 @@ mymain(void)
25aafc
 
25aafc
     CREATE_SYMLINK("0-0-0-0", "0:0:0:0");
25aafc
     CREATE_SYMLINK("1-0-0-0", "1:0:0:0");
25aafc
+    CREATE_SYMLINK("2-0-0-0", "2:0:0:0");
25aafc
     CREATE_SYMLINK("sg0", "sg0");
25aafc
+    CREATE_SYMLINK("sg3", "sg3");
25aafc
     CREATE_SYMLINK("sg8", "sg8");
25aafc
 
25aafc
     VIR_FREE(virscsi_prefix);
25aafc
@@ -235,8 +253,18 @@ mymain(void)
25aafc
         goto cleanup;
25aafc
     }
25aafc
 
25aafc
-    if (virTestRun("test1", test1, NULL) < 0)
25aafc
-        ret = -1;
25aafc
+#define TEST_GET_DEV_NAME(adapter, bus, target, unit, expectedName) \
25aafc
+    do { \
25aafc
+        testGetDevNameData data = {adapter, bus, target, unit, expectedName}; \
25aafc
+        if (virTestRun("test getDevname " expectedName, \
25aafc
+                       testGetDevName, &data) < 0) \
25aafc
+            ret = -1; \
25aafc
+    } while (0)
25aafc
+
25aafc
+    TEST_GET_DEV_NAME("scsi_host0", 0, 0, 0, "sda");
25aafc
+    TEST_GET_DEV_NAME("scsi_host1", 0, 0, 0, "sdh");
25aafc
+    TEST_GET_DEV_NAME("scsi_host2", 0, 0, 0, "st0");
25aafc
+
25aafc
     if (virTestRun("test2", test2, NULL) < 0)
25aafc
         ret = -1;
25aafc
 
25aafc
-- 
25aafc
2.23.0
25aafc