[blivet:f21-branch 14/14] Canonicalize MD_UUID* values in udev.py (#1147087)

mulhern amulhern at redhat.com
Mon Sep 29 17:23:01 UTC 2014


Resolves: fed#1147087

Sometimes, MD_UUID* are obtained, not from mdraid methods, but
from udev methods. Since these did not pass through the mdraid methods,
they are not canonicalized, leading to uncanonicalized forms being used for
lookup. Now, they are canonicalized on lookup in blivet.util.py. Since
the canonicalize_UUID method is shared, it is moved up into blivet.utils.py.

Signed-off-by: mulhern <amulhern at redhat.com>
---
 blivet/devicelibs/mdraid.py | 19 +------------------
 blivet/udev.py              |  5 +++--
 blivet/util.py              | 20 ++++++++++++++++++++
 3 files changed, 24 insertions(+), 20 deletions(-)

diff --git a/blivet/devicelibs/mdraid.py b/blivet/devicelibs/mdraid.py
index f77ef0a..f4ed875 100644
--- a/blivet/devicelibs/mdraid.py
+++ b/blivet/devicelibs/mdraid.py
@@ -22,7 +22,6 @@
 
 import os
 import re
-import uuid
 
 from .. import util
 from ..errors import MDRaidError
@@ -248,22 +247,6 @@ def mddeactivate(device):
     except MDRaidError as msg:
         raise MDRaidError("mddeactivate failed for %s: %s" % (device, msg))
 
-def canonicalize_UUID(a_uuid):
-    """ Converts uuids to canonical form.
-
-        :param str a_uuid: the UUID
-
-        :returns: a canonicalized UUID
-        :rtype: str
-
-        mdadm's UUIDs are actual 128 bit uuids, but it formats them strangely.
-        This converts the uuids to canonical form.
-        Example:
-            mdadm UUID: '3386ff85:f5012621:4a435f06:1eb47236'
-        canonical UUID: '3386ff85-f501-2621-4a43-5f061eb47236'
-    """
-    return str(uuid.UUID(a_uuid.replace(':', '')))
-
 def process_UUIDS(info, UUID_keys):
     """ Extract and convert expected UUIDs to canonical form.
         Reassign canonicalized UUIDs to corresponding keys.
@@ -276,7 +259,7 @@ def process_UUIDS(info, UUID_keys):
             # extract mdadm UUID, e.g., '3386ff85:f5012621:4a435f06:1eb47236'
             the_uuid = re.match(r"(([a-f0-9]){8}:){3}([a-f0-9]){8}", v)
 
-            info[k] = canonicalize_UUID(the_uuid.group())
+            info[k] = util.canonicalize_UUID(the_uuid.group())
         except (ValueError, AttributeError) as e:
             # the unlikely event that mdadm's UUIDs change their format
             log.warning('uuid value %s could not be canonicalized: %s', v, e)
diff --git a/blivet/udev.py b/blivet/udev.py
index 57cf0a8..4f12d5f 100644
--- a/blivet/udev.py
+++ b/blivet/udev.py
@@ -411,7 +411,7 @@ def device_get_md_uuid(info):
     # Value for MD_UUID known to be obtained from:
     #  * pyudev/libudev
     #  * mdraid/mdadm (all numeric metadata versions and container default)
-    return info["MD_UUID"]
+    return util.canonicalize_UUID(info["MD_UUID"])
 
 def device_get_md_container(info):
     """
@@ -456,7 +456,8 @@ def device_get_md_device_uuid(info):
     # Value for MD_UUID known to be obtained from:
     #  * pyudev/libudev
     #  * mdraid/mdadm (only 1.x metadata versions)
-    return info.get('MD_DEV_UUID')
+    md_device_uuid = info.get('MD_DEV_UUID')
+    return util.canonicalize_UUID(md_device_uuid) if md_device_uuid else None
 
 def device_get_vg_name(info):
     return info['LVM2_VG_NAME']
diff --git a/blivet/util.py b/blivet/util.py
index 1e28ddc..d89fee6 100644
--- a/blivet/util.py
+++ b/blivet/util.py
@@ -8,6 +8,7 @@ import subprocess
 import re
 import sys
 import tempfile
+import uuid
 from decimal import Decimal
 
 import six
@@ -361,6 +362,25 @@ class ObjectID(object):
         self.id = self._newid_gen() # pylint: disable=attribute-defined-outside-init
         return self
 
+def canonicalize_UUID(a_uuid):
+    """ Converts uuids to canonical form.
+
+        :param str a_uuid: the UUID
+
+        :returns: a canonicalized UUID
+        :rtype: str
+
+        mdadm's UUIDs are actual 128 bit uuids, but it formats them strangely.
+        This converts the uuids to canonical form.
+        Example:
+            mdadm UUID: '3386ff85:f5012621:4a435f06:1eb47236'
+        canonical UUID: '3386ff85-f501-2621-4a43-5f061eb47236'
+
+        If the UUID is already in canonical form, the conversion
+        is equivalent to the identity.
+    """
+    return str(uuid.UUID(a_uuid.replace(':', '')))
+
 ##
 ## Convenience functions for examples and tests
 ##
-- 
1.9.3



More information about the anaconda-patches mailing list