[libvirt] [Resending] [PATCH 2/2] Allow x86 to fetch sysinfo from /proc/cpuinfo when dmidecode is absent.

Prerna prerna at linux.vnet.ibm.com
Fri Feb 17 07:30:22 UTC 2012


The last patch was mangled by my mailer, resending.

From: Prerna Saxena <prerna at linux.vnet.ibm.com>
Date: Thu, 16 Feb 2012 15:33:43 +0530
Subject: [PATCH 2/2] Sysinfo : Allow x86 to fetch sysinfo from /proc/cpuinfo
 in the event 'dmidecode' is absent in the system.

Until now, libvirt on x86 flags an error message if dmidecode is not
found. With this patch, the following is a sample output on x86 when
dmidecode is absent:

virsh # sysinfo
<sysinfo type='smbios'>
  <processor>
    <entry name='socket_destination'>0</entry>
    <entry name='type'>Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz</entry>
    <entry name='family'>6</entry>
    <entry name='manufacturer'>GenuineIntel</entry>
  </processor>
  <processor>
    <entry name='socket_destination'>1</entry>
    <entry name='type'>Intel(R) Xeon(R) CPU           X5570  @ 2.93GHz</entry>
    <entry name='family'>6</entry>
    <entry name='manufacturer'>GenuineIntel</entry>
  </processor>
  ... (listing for all online CPUs)
</sysinfo>

---
 src/util/sysinfo.c |   95 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 91 insertions(+), 4 deletions(-)

diff --git a/src/util/sysinfo.c b/src/util/sysinfo.c
index 0161b22..de78d23 100644
--- a/src/util/sysinfo.c
+++ b/src/util/sysinfo.c
@@ -596,6 +596,96 @@ no_memory:
     return -1;
 }
 
+/* If a call to 'dmidecode' fails,
+ * extract basic sysinfo from /proc/cpuinfo */
+
+static int
+virSysinfoParseCPUInfoProcessor(const char *base, virSysinfoDefPtr ret)
+{
+    char *cur, *eol, *tmp_base;
+    virSysinfoProcessorDefPtr processor;
+
+    while((tmp_base = strstr(base, "processor")) != NULL) {
+        base = tmp_base;
+        eol = strchr(base, '\n');
+        cur = strchr(base, ':') + 1;
+
+        if (VIR_EXPAND_N(ret->processor, ret->nprocessor, 1) < 0) {
+            goto no_memory;
+        }
+
+        processor = &ret->processor[ret->nprocessor - 1];
+        if ((eol) &&
+            ((processor->processor_socket_destination =
+                               strndup(cur, eol - cur)) == NULL))
+            goto no_memory;
+        virSkipSpaces(&(processor->processor_socket_destination));
+
+        if ((cur = strstr(base, "vendor_id")) != NULL) {
+            cur = strchr(cur, ':') + 1;
+            eol = strchr(cur, '\n');
+            if ((eol) &&
+                ((processor->processor_manufacturer =
+                                  strndup(cur, eol - cur)) == NULL))
+                goto no_memory;
+            virSkipSpaces(&(processor->processor_manufacturer));
+        }
+
+        if ((cur = strstr(base, "cpu family")) != NULL) {
+            cur = strchr(cur, ':') + 1;
+            eol = strchr(cur, '\n');
+            if ((eol) &&
+                ((processor->processor_family = strndup(cur, eol - cur))
+		                                               == NULL))
+                goto no_memory;
+            virSkipSpaces(&(processor->processor_family));
+        }
+
+        if ((cur = strstr(base, "model name")) != NULL) {
+            cur = strchr(cur, ':') + 1;
+            eol = strchr(cur, '\n');
+            if ((eol) &&
+                ((processor->processor_type = strndup(cur, eol - cur))
+		                                             == NULL))
+                goto no_memory;
+            virSkipSpaces(&(processor->processor_type));
+        }
+
+        base = cur;
+    }
+
+    return 0;
+
+no_memory:
+    return -1;
+}
+
+static virSysinfoDefPtr
+virCPUInfoSysinfoRead(void) {
+    virSysinfoDefPtr ret = NULL;
+    char *outbuf = NULL;
+
+    if (VIR_ALLOC(ret) < 0)
+        goto no_memory;
+
+    if(virFileReadAll(CPUINFO, 20000, &outbuf) < 0) {
+        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
+                             _("Failed to open %s"), CPUINFO);
+        return NULL;
+    }
+
+    ret->nprocessor = 0;
+    ret->processor = NULL;
+    if (virSysinfoParseCPUInfoProcessor(outbuf, ret) < 0)
+        goto no_memory;
+
+    return ret;
+
+no_memory:
+    VIR_FREE(outbuf);
+    return NULL;
+}
+
 virSysinfoDefPtr
 virSysinfoRead(void) {
     char *path;
@@ -605,10 +695,7 @@ virSysinfoRead(void) {
 
     path = virFindFileInPath(SYSINFO_SMBIOS_DECODER);
     if (path == NULL) {
-        virSmbiosReportError(VIR_ERR_INTERNAL_ERROR,
-                             _("Failed to find path for %s binary"),
-                             SYSINFO_SMBIOS_DECODER);
-        return NULL;
+        return virCPUInfoSysinfoRead();
     }
 
     cmd = virCommandNewArgList(path, "-q", "-t", "0,1,4,17", NULL);
-- 
1.7.7



-- 
Prerna Saxena

Linux Technology Centre,
IBM Systems and Technology Lab,
Bangalore, India




More information about the libvir-list mailing list