0a7476
From 0fffd7d7d2767d8f03407c2c61da6baf0ac181c5 Mon Sep 17 00:00:00 2001
0a7476
Message-Id: <0fffd7d7d2767d8f03407c2c61da6baf0ac181c5@dist-git>
bba56f
From: John Ferlan <jferlan@redhat.com>
bba56f
Date: Tue, 12 Mar 2019 13:55:58 -0400
bba56f
Subject: [PATCH] util: Introduce virStorageFileGetNPIVKey
bba56f
MIME-Version: 1.0
bba56f
Content-Type: text/plain; charset=UTF-8
bba56f
Content-Transfer-Encoding: 8bit
bba56f
bba56f
https://bugzilla.redhat.com/show_bug.cgi?id=1687715 (7.6.z)
bba56f
https://bugzilla.redhat.com/show_bug.cgi?id=1657468 (7.7.0)
bba56f
bba56f
The vHBA/NPIV LUNs created via the udev processing of the
bba56f
VPORT_CREATE command end up using the same serial value
bba56f
as seen/generated by the /lib/udev/scsi_id as returned
bba56f
during virStorageFileGetSCSIKey. Therefore, in order to
bba56f
generate a unique enough key to be used when adding the
bba56f
LUN as a volume during virStoragePoolObjAddVol a more
bba56f
unique key needs to be generated for an NPIV volume.
bba56f
bba56f
The problem is illustrated by the following example, where
bba56f
scsi_host5 is a vHBA used with the following LUNs:
bba56f
bba56f
$ lsscsi -tg
bba56f
...
bba56f
[5:0:4:0]    disk    fc:0x5006016844602198,0x101f00  /dev/sdh   /dev/sg23
bba56f
[5:0:5:0]    disk    fc:0x5006016044602198,0x102000  /dev/sdi   /dev/sg24
bba56f
...
bba56f
bba56f
Calling virStorageFileGetSCSIKey would return:
bba56f
bba56f
/lib/udev/scsi_id --device /dev/sdh --whitelisted --replace-whitespace /dev/sdh
bba56f
350060160c460219850060160c4602198
bba56f
/lib/udev/scsi_id --device /dev/sdh --whitelisted --replace-whitespace /dev/sdi
bba56f
350060160c460219850060160c4602198
bba56f
bba56f
Note that althrough /dev/sdh and /dev/sdi are separate LUNs, they
bba56f
end up with the same serial number used for the vol->key value.
bba56f
When virStoragePoolFCRefreshThread calls virStoragePoolObjAddVol
bba56f
the second LUN fails to be added with the following message
bba56f
getting logged:
bba56f
bba56f
    virHashAddOrUpdateEntry:341 : internal error: Duplicate key
bba56f
bba56f
To resolve this, virStorageFileGetNPIVKey will use a similar call
bba56f
sequence as virStorageFileGetSCSIKey, except that it will add the
bba56f
"--export" option to the call. This results in more detailed output
bba56f
which needs to be parsed in order to formulate a unique enough key
bba56f
to be used. In order to be unique enough, the returned value will
bba56f
concatenate the target port as returned in the "ID_TARGET_PORT"
bba56f
field from the command to the "ID_SERIAL" value.
bba56f
bba56f
Signed-off-by: John Ferlan <jferlan@redhat.com>
bba56f
ACKed-by: Michal Privoznik <mprivozn@redhat.com>
bba56f
Reviewed-by: Ján Tomko <jtomko@redhat.com>
bba56f
(cherry picked from commit 5f9e211c9319311fc296cb91ca8e330917ac9494)
bba56f
bba56f
Handled build failure of not having VIR_AUTOFREE in the next-7.6 sources,
bba56f
by not using VIR_AUTOFREE and using VIR_FREE(outbuf) explicitly.
bba56f
bba56f
Signed-off-by: John Ferlan <jferlan@redhat.com>
bba56f
Message-Id: <20190312175559.13583-4-jferlan@redhat.com>
bba56f
Reviewed-by: Ján Tomko <jtomko@redhat.com>
bba56f
---
bba56f
 src/libvirt_private.syms  |  1 +
bba56f
 src/util/virstoragefile.c | 80 +++++++++++++++++++++++++++++++++++++++
bba56f
 src/util/virstoragefile.h |  2 +
bba56f
 3 files changed, 83 insertions(+)
bba56f
bba56f
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
bba56f
index 86846f3b08..636891eabd 100644
bba56f
--- a/src/libvirt_private.syms
bba56f
+++ b/src/libvirt_private.syms
bba56f
@@ -2820,6 +2820,7 @@ virStorageFileGetMetadata;
bba56f
 virStorageFileGetMetadataFromBuf;
bba56f
 virStorageFileGetMetadataFromFD;
bba56f
 virStorageFileGetMetadataInternal;
bba56f
+virStorageFileGetNPIVKey;
bba56f
 virStorageFileGetRelativeBackingPath;
bba56f
 virStorageFileGetSCSIKey;
bba56f
 virStorageFileGetUniqueIdentifier;
bba56f
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
bba56f
index 56d38b467e..52c9dc0e1a 100644
bba56f
--- a/src/util/virstoragefile.c
bba56f
+++ b/src/util/virstoragefile.c
bba56f
@@ -1503,6 +1503,86 @@ int virStorageFileGetSCSIKey(const char *path,
bba56f
 #endif
bba56f
 
bba56f
 
bba56f
+#ifdef WITH_UDEV
bba56f
+/* virStorageFileGetNPIVKey
bba56f
+ * @path: Path to the NPIV device
bba56f
+ * @key: Unique key to be returned
bba56f
+ *
bba56f
+ * Using a udev specific function, query the @path to get and return a
bba56f
+ * unique @key for the caller to use. Unlike the GetSCSIKey method, an
bba56f
+ * NPIV LUN is uniquely identified by its ID_TARGET_PORT value.
bba56f
+ *
bba56f
+ * Returns:
bba56f
+ *     0 On success, with the @key filled in or @key=NULL if the
bba56f
+ *       returned output string didn't have the data we need to
bba56f
+ *       formulate a unique key value
bba56f
+ *    -1 When WITH_UDEV is undefined and a system error is reported
bba56f
+ *    -2 When WITH_UDEV is defined, but calling virCommandRun fails
bba56f
+ */
bba56f
+# define ID_SERIAL "ID_SERIAL="
bba56f
+# define ID_TARGET_PORT "ID_TARGET_PORT="
bba56f
+int
bba56f
+virStorageFileGetNPIVKey(const char *path,
bba56f
+                         char **key)
bba56f
+{
bba56f
+    int status;
bba56f
+    char *outbuf = NULL;
bba56f
+    const char *serial;
bba56f
+    const char *port;
bba56f
+    virCommandPtr cmd = virCommandNewArgList("/lib/udev/scsi_id",
bba56f
+                                             "--replace-whitespace",
bba56f
+                                             "--whitelisted",
bba56f
+                                             "--export",
bba56f
+                                             "--device", path,
bba56f
+                                             NULL
bba56f
+                                             );
bba56f
+    int ret = -2;
bba56f
+
bba56f
+    *key = NULL;
bba56f
+
bba56f
+    /* Run the program and capture its output */
bba56f
+    virCommandSetOutputBuffer(cmd, &outbuf);
bba56f
+    if (virCommandRun(cmd, &status) < 0)
bba56f
+        goto cleanup;
bba56f
+
bba56f
+    /* Explicitly check status == 0, rather than passing NULL
bba56f
+     * to virCommandRun because we don't want to raise an actual
bba56f
+     * error in this scenario, just return a NULL key.
bba56f
+     */
bba56f
+    if (status == 0 && *outbuf &&
bba56f
+        (serial = strstr(outbuf, ID_SERIAL)) &&
bba56f
+        (port = strstr(outbuf, ID_TARGET_PORT))) {
bba56f
+        char *tmp;
bba56f
+
bba56f
+        serial += strlen(ID_SERIAL);
bba56f
+        port += strlen(ID_TARGET_PORT);
bba56f
+
bba56f
+        if ((tmp = strchr(serial, '\n')))
bba56f
+            *tmp = '\0';
bba56f
+
bba56f
+        if ((tmp = strchr(port, '\n')))
bba56f
+            *tmp = '\0';
bba56f
+
bba56f
+        if (*serial != '\0' && *port != '\0')
bba56f
+            ignore_value(virAsprintf(key, "%s_PORT%s", serial, port));
bba56f
+    }
bba56f
+
bba56f
+    ret = 0;
bba56f
+
bba56f
+ cleanup:
bba56f
+    virCommandFree(cmd);
bba56f
+    VIR_FREE(outbuf);
bba56f
+
bba56f
+    return ret;
bba56f
+}
bba56f
+#else
bba56f
+int virStorageFileGetNPIVKey(const char *path,
bba56f
+                             char **key ATTRIBUTE_UNUSED)
bba56f
+{
bba56f
+    return -1;
bba56f
+}
bba56f
+#endif
bba56f
+
bba56f
 /**
bba56f
  * virStorageFileParseBackingStoreStr:
bba56f
  * @str: backing store specifier string to parse
bba56f
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
bba56f
index bbb0b8b3c1..7d28dcfe65 100644
bba56f
--- a/src/util/virstoragefile.h
bba56f
+++ b/src/util/virstoragefile.h
bba56f
@@ -386,6 +386,8 @@ int virStorageFileGetLVMKey(const char *path,
bba56f
 int virStorageFileGetSCSIKey(const char *path,
bba56f
                              char **key,
bba56f
                              bool ignoreError);
bba56f
+int virStorageFileGetNPIVKey(const char *path,
bba56f
+                             char **key);
bba56f
 
bba56f
 void virStorageAuthDefFree(virStorageAuthDefPtr def);
bba56f
 virStorageAuthDefPtr virStorageAuthDefCopy(const virStorageAuthDef *src);
bba56f
-- 
bba56f
2.21.0
bba56f