summaryrefslogtreecommitdiff
path: root/cloudinit/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'cloudinit/util.py')
-rw-r--r--cloudinit/util.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py
index d594b611..32c19ba2 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -90,6 +90,9 @@ def encode_text(text, encoding='utf-8'):
return text
return text.encode(encoding)
+# Path for DMI Data
+DMI_SYS_PATH = "/sys/class/dmi/id"
+
class ProcessExecutionError(IOError):
@@ -2049,3 +2052,61 @@ def human2bytes(size):
raise ValueError("'%s': cannot be negative" % size_in)
return int(num * mpliers[mplier])
+
+
+def _read_dmi_syspath(key):
+ """
+ Reads dmi data with from /sys/class/dmi/id
+ """
+
+ dmi_key = "{}/{}".format(DMI_SYS_PATH, key)
+ LOG.debug("querying dmi data {}".format(dmi_key))
+ try:
+ if not os.path.exists(dmi_key):
+ LOG.debug("did not find {}".format(dmi_key))
+ return None
+
+ key_data = load_file(dmi_key)
+ if not key_data:
+ LOG.debug("{} did not return any data".format(key))
+ return None
+
+ LOG.debug("dmi data {} returned {}".format(dmi_key, key_data))
+ return key_data.strip()
+
+ except Exception as e:
+ logexc(LOG, "failed read of {}".format(dmi_key), e)
+ return None
+
+
+def _call_dmidecode(key, dmidecode_path):
+ """
+ Calls out to dmidecode to get the data out. This is mostly for supporting
+ OS's without /sys/class/dmi/id support.
+ """
+ try:
+ cmd = [dmidecode_path, "--string", key]
+ (result, _err) = subp(cmd)
+ LOG.debug("dmidecode returned '{}' for '{}'".format(result, key))
+ return result
+ except OSError as _err:
+ LOG.debug('failed dmidecode cmd: {}\n{}'.format(cmd, _err.message))
+ return None
+
+
+def read_dmi_data(key):
+ """
+ Wrapper for reading DMI data. This tries to determine whether the DMI
+ Data can be read directly, otherwise it will fallback to using dmidecode.
+ """
+ if os.path.exists(DMI_SYS_PATH):
+ return _read_dmi_syspath(key)
+
+ dmidecode_path = which('dmidecode')
+ if dmidecode_path:
+ return _call_dmidecode(key, dmidecode_path)
+
+ LOG.warn("did not find either path {} or dmidecode command".format(
+ DMI_SYS_PATH))
+
+ return None