[libvirt] [PATCH 2/2] util: storage: Parse 'lun' for iSCSI protocol from JSON as string or number

Peter Krempa posted 2 patches 7 years, 3 months ago
[libvirt] [PATCH 2/2] util: storage: Parse 'lun' for iSCSI protocol from JSON as string or number
Posted by Peter Krempa 7 years, 3 months ago
While the QEMU QAPI schema describes 'lun' as a number, the code dealing
with JSON strings does not strictly adhere to this schema and thus
formats the number back as a string. Use the new helper to retrieve both
possibilities.

Note that the formatting code is okay and qemu will accept it as an int.

Tweak also one of the test stringst to verify that both formats work
with libvirt.

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1540290
---
 src/util/virstoragefile.c | 13 +++++--------
 tests/virstoragetest.c    |  2 +-
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 5780180a94..5f661c956c 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -2976,10 +2976,9 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSourcePtr src,
     const char *transport = virJSONValueObjectGetString(json, "transport");
     const char *portal = virJSONValueObjectGetString(json, "portal");
     const char *target = virJSONValueObjectGetString(json, "target");
+    const char *lun = virJSONValueObjectGetStringOrNumber(json, "lun");
     const char *uri;
     char *port;
-    unsigned int lun = 0;
-    char *fulltarget = NULL;
     int ret = -1;

     /* legacy URI based syntax passed via 'filename' option */
@@ -2990,6 +2989,9 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSourcePtr src,
     src->type = VIR_STORAGE_TYPE_NETWORK;
     src->protocol = VIR_STORAGE_NET_PROTOCOL_ISCSI;

+    if (!lun)
+        lun = "0";
+
     if (VIR_ALLOC(src->hosts) < 0)
         goto cleanup;

@@ -3026,17 +3028,12 @@ virStorageSourceParseBackingJSONiSCSI(virStorageSourcePtr src,
         *port = '\0';
     }

-    ignore_value(virJSONValueObjectGetNumberUint(json, "lun", &lun));
-
-    if (virAsprintf(&fulltarget, "%s/%u", target, lun) < 0)
+    if (virAsprintf(&src->path, "%s/%s", target, lun) < 0)
         goto cleanup;

-    VIR_STEAL_PTR(src->path, fulltarget);
-
     ret = 0;

  cleanup:
-    VIR_FREE(fulltarget);
     return ret;
 }

diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
index 7a0d4a8260..1dc7608cee 100644
--- a/tests/virstoragetest.c
+++ b/tests/virstoragetest.c
@@ -1572,7 +1572,7 @@ mymain(void)
                                        "\"transport\":\"tcp\","
                                        "\"portal\":\"test.org:1234\","
                                        "\"target\":\"iqn.2016-12.com.virttest:emulated-iscsi-noauth.target\","
-                                       "\"lun\":6"
+                                       "\"lun\":\"6\""
                                       "}"
                             "}",
                        "<source protocol='iscsi' name='iqn.2016-12.com.virttest:emulated-iscsi-noauth.target/6'>\n"
-- 
2.15.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 2/2] util: storage: Parse 'lun' for iSCSI protocol from JSON as string or number
Posted by Jiri Denemark 7 years, 3 months ago
On Wed, Jan 31, 2018 at 12:07:32 +0100, Peter Krempa wrote:
> While the QEMU QAPI schema describes 'lun' as a number, the code dealing
> with JSON strings does not strictly adhere to this schema and thus
> formats the number back as a string. Use the new helper to retrieve both
> possibilities.
> 
> Note that the formatting code is okay and qemu will accept it as an int.
> 
> Tweak also one of the test stringst to verify that both formats work

s/stringst/strings/

> with libvirt.
> 
> Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1540290
> ---
>  src/util/virstoragefile.c | 13 +++++--------
>  tests/virstoragetest.c    |  2 +-
>  2 files changed, 6 insertions(+), 9 deletions(-)

ACK

Jirka

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list