[PATCH v2 7/7] virsysinfo: Parse OEM strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Setting OEM strings for a domain was introduced in
v4.1.0-rc1~315. However, any application that wanted to use them
(e.g. to point to an URL where a config file is stored) had to
'dmidecode -u --oem-string N' (where N is index of the string).
Well, we can expose them under our <sysinfo/> XML and if the
domain is running Libvirt inside it can be obtained using
virConnectGetSysinfo() API.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/util/virsysinfo.c               | 102 +++++++++++++++++++++++++++-
 tests/sysinfodata/x86sysinfo.data   |   6 ++
 tests/sysinfodata/x86sysinfo.expect |   8 +++
 tests/sysinfotest.c                 |  27 ++++++--
 4 files changed, 138 insertions(+), 5 deletions(-)

diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c
index a26c27e83e..09e32df6a9 100644
--- a/src/util/virsysinfo.c
+++ b/src/util/virsysinfo.c
@@ -915,6 +915,103 @@ virSysinfoParseX86Chassis(const char *base,
 }
 
 
+static int
+virSysinfoDMIDecodeOEMString(size_t i,
+                             char **str)
+{
+    g_autofree char *err = NULL;
+    g_autoptr(virCommand) cmd = virCommandNewArgList(DMIDECODE, "--dump",
+                                                     "--oem-string", NULL);
+    virCommandAddArgFormat(cmd, "%zu", i);
+    virCommandSetOutputBuffer(cmd, str);
+    virCommandSetErrorBuffer(cmd, &err);
+
+    if (virCommandRun(cmd, NULL) < 0)
+        return -1;
+
+    /* Unfortunately, dmidecode returns 0 even if OEM String index is out
+     * of bounds, but it prints an error message in that case. Check stderr
+     * and return success/failure accordingly. */
+
+    if (err && *err != '\0')
+        return -1;
+
+    return 0;
+}
+
+
+static int
+virSysinfoParseOEMStrings(const char *base,
+                          virSysinfoOEMStringsDefPtr *stringsRet)
+{
+    virSysinfoOEMStringsDefPtr strings = NULL;
+    size_t i = 1;
+    int ret = -1;
+    const char *cur;
+
+    if (!(cur = strstr(base, "OEM Strings")))
+        return 0;
+
+    if (VIR_ALLOC(strings) < 0)
+        return -1;
+
+    while ((cur = strstr(cur, "String "))) {
+        char *eol;
+
+        cur += 7;
+
+        if (!(eol = strchr(cur, '\n'))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Malformed output of dmidecode"));
+            goto cleanup;
+        }
+
+        while (g_ascii_isdigit(*cur))
+            cur++;
+
+        if (*cur != ':') {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("Malformed output of dmidecode"));
+            goto cleanup;
+        }
+
+        cur += 2;
+
+        virSkipSpacesBackwards(cur, &eol);
+        if (!eol)
+            continue;
+
+        if (VIR_EXPAND_N(strings->values, strings->nvalues, 1) < 0)
+            goto cleanup;
+
+        /* If OEM String contains newline, dmidecode escapes it as a dot.
+         * If this is the case then run dmidecode again to get raw string.
+         * Unfortunately, we can't dinstinguish betwen dot an new line at
+         * this level. */
+        if (memchr(cur, '.', eol - cur)) {
+            char *str;
+
+            if (virSysinfoDMIDecodeOEMString(i, &str) < 0)
+                goto cleanup;
+
+            strings->values[strings->nvalues - 1] = g_steal_pointer(&str);
+        } else {
+            strings->values[strings->nvalues - 1] = g_strndup(cur, eol - cur);
+        }
+
+        i++;
+        cur = eol;
+    }
+
+    *stringsRet = g_steal_pointer(&strings);
+    ret = 0;
+
+ cleanup:
+    virSysinfoOEMStringsDefFree(strings);
+    return ret;
+}
+
+
 static int
 virSysinfoParseX86Processor(const char *base, virSysinfoDefPtr ret)
 {
@@ -1119,7 +1216,7 @@ virSysinfoReadDMI(void)
     g_autofree char *outbuf = NULL;
     g_autoptr(virCommand) cmd = NULL;
 
-    cmd = virCommandNewArgList(DMIDECODE, "-q", "-t", "0,1,2,3,4,17", NULL);
+    cmd = virCommandNewArgList(DMIDECODE, "-q", "-t", "0,1,2,3,4,11,17", NULL);
     virCommandSetOutputBuffer(cmd, &outbuf);
     if (virCommandRun(cmd, NULL) < 0)
         return NULL;
@@ -1141,6 +1238,9 @@ virSysinfoReadDMI(void)
     if (virSysinfoParseX86Chassis(outbuf, &ret->chassis) < 0)
         return NULL;
 
+    if (virSysinfoParseOEMStrings(outbuf, &ret->oemStrings) < 0)
+        return NULL;
+
     ret->nprocessor = 0;
     ret->processor = NULL;
     if (virSysinfoParseX86Processor(outbuf, ret) < 0)
diff --git a/tests/sysinfodata/x86sysinfo.data b/tests/sysinfodata/x86sysinfo.data
index 426261041d..3f0b654e4b 100644
--- a/tests/sysinfodata/x86sysinfo.data
+++ b/tests/sysinfodata/x86sysinfo.data
@@ -81,3 +81,9 @@ Memory Device
 	Serial Number: 29057112
 	Asset Tag: 0839
 	Part Number: IMSH2GS13A1F1C-10F
+
+OEM Strings
+        String 1: Hello
+        String 2: World
+        String 3: Ha ha ha try parsing\n.      String 3: this correctly.      String 4:then
+        String 4: This is, more tricky value=escaped
diff --git a/tests/sysinfodata/x86sysinfo.expect b/tests/sysinfodata/x86sysinfo.expect
index fcdd790cbd..05add8f031 100644
--- a/tests/sysinfodata/x86sysinfo.expect
+++ b/tests/sysinfodata/x86sysinfo.expect
@@ -50,4 +50,12 @@
     <entry name='serial_number'>29057112</entry>
     <entry name='part_number'>IMSH2GS13A1F1C-10F</entry>
   </memory_device>
+  <oemStrings>
+    <entry>Hello</entry>
+    <entry>World</entry>
+    <entry>Ha ha ha try parsing\n
+      String 3: this correctly
+      String 4:then</entry>
+    <entry>This is, more tricky value=escaped</entry>
+  </oemStrings>
 </sysinfo>
diff --git a/tests/sysinfotest.c b/tests/sysinfotest.c
index 10d24b823a..3b418955d0 100644
--- a/tests/sysinfotest.c
+++ b/tests/sysinfotest.c
@@ -56,10 +56,29 @@ testDMIDecodeDryRun(const char *const*args G_GNUC_UNUSED,
 {
     const char *sysinfo = opaque;
 
-    if (virFileReadAll(sysinfo, 10 * 1024 * 1024, output) < 0) {
-        *error = g_strdup(virGetLastErrorMessage());
-        *status = EXIT_FAILURE;
-        return;
+    if (STREQ_NULLABLE(args[1], "--dump") &&
+        STREQ_NULLABLE(args[2], "--oem-string")) {
+        if (!args[3]) {
+            *error = g_strdup("dmidecode: option '--oem-string' requires an argument");
+            *status = EXIT_FAILURE;
+            return;
+        }
+
+        if (STREQ(args[3], "3")) {
+            *output = g_strdup("Ha ha ha try parsing\\n\n"
+                               "      String 3: this correctly\n"
+                               "      String 4:then");
+        } else {
+            *error = g_strdup_printf("No OEM string number %s", args[3]);
+            *status = EXIT_FAILURE;
+            return;
+        }
+    } else {
+        if (virFileReadAll(sysinfo, 10 * 1024 * 1024, output) < 0) {
+            *error = g_strdup(virGetLastErrorMessage());
+            *status = EXIT_FAILURE;
+            return;
+        }
     }
 
     *error = g_strdup("");
-- 
2.26.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux