diff options
author | Scott Moser <smoser@ubuntu.com> | 2016-03-10 12:32:46 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2016-03-10 12:32:46 -0500 |
commit | b839ad32b9bf4541583ecbe68a0bd5dd9f12345a (patch) | |
tree | ce5352d0d3a8932f47c69fb991408bff7b92b2c3 | |
parent | 0865e5179f2a803f727e83b5e36681613e63fe8a (diff) | |
download | vyos-cloud-init-b839ad32b9bf4541583ecbe68a0bd5dd9f12345a.tar.gz vyos-cloud-init-b839ad32b9bf4541583ecbe68a0bd5dd9f12345a.zip |
dmi data: fix failure of reading dmi data for unset dmi values
it is not uncommon to find dmi data in /sys full of 'ff'. utf-8
decoding of those would fail, causing warning and stacktrace.
Return '.' instead of \xff. This maps to what dmidecode would return
$ dmidecode --string system-product-name
.................................
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | cloudinit/util.py | 13 | ||||
-rw-r--r-- | tests/unittests/test_util.py | 9 |
3 files changed, 20 insertions, 3 deletions
@@ -88,6 +88,7 @@ - No longer run pollinate in seed_random (LP: #1554152) - groups: add defalt user to 'lxd' group. Create groups listed for a user if they do not exist. (LP: #1539317) + - dmi data: fix failure of reading dmi data for unset dmi values 0.7.6: - open 0.7.6 diff --git a/cloudinit/util.py b/cloudinit/util.py index e7407ea4..1d50edc9 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -2140,13 +2140,20 @@ def _read_dmi_syspath(key): LOG.debug("did not find %s", dmi_key_path) return None - key_data = load_file(dmi_key_path) + key_data = load_file(dmi_key_path, decode=False) if not key_data: LOG.debug("%s did not return any data", dmi_key_path) return None - LOG.debug("dmi data %s returned %s", dmi_key_path, key_data) - return key_data.strip() + # in the event that this is all \xff and a carriage return + # then return '.' in its place. + if key_data == b'\xff' * (len(key_data) - 1) + b'\n': + key_data = b'.' * (len(key_data) - 1) + b'\n' + + str_data = key_data.decode('utf8').strip() + + LOG.debug("dmi data %s returned %s", dmi_key_path, str_data) + return str_data except Exception: logexc(LOG, "failed read of %s", dmi_key_path) diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index 95990165..542e4075 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -385,6 +385,15 @@ class TestReadDMIData(helpers.FilesystemMockingTestCase): self.patch_mapping({}) self.assertEqual(None, util.read_dmi_data('expect-fail')) + def test_dots_returned_instead_of_foxfox(self): + my_len = 32 + dmi_value = b'\xff' * my_len + b'\n' + expected = '.' * my_len + dmi_key = 'system-product-name' + sysfs_key = 'product_name' + self._create_sysfs_file(sysfs_key, dmi_value) + self.assertEqual(expected, util.read_dmi_data(dmi_key)) + class TestMultiLog(helpers.FilesystemMockingTestCase): |