render / rpms / libvirt

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