From 784421c739200306d2ed07d2c8d0939b1af7f71e Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Tue, 30 Oct 2012 13:58:14 -0500
Subject: [PATCH 05/12] Fix detection of partitioned md devices. (#866519)

---
 pyanaconda/storage/devicelibs/mdraid.py |   19 ++++++++++++++++++-
 pyanaconda/storage/devices.py           |    3 +++
 pyanaconda/storage/devicetree.py        |   17 +++++++++--------
 pyanaconda/storage/udev.py              |    4 +---
 4 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/pyanaconda/storage/devicelibs/mdraid.py b/pyanaconda/storage/devicelibs/mdraid.py
index 432a24e..ee6a465 100644
--- a/pyanaconda/storage/devicelibs/mdraid.py
+++ b/pyanaconda/storage/devicelibs/mdraid.py
@@ -259,9 +259,26 @@ def mdexamine(device):
 def md_node_from_name(name):
     named_path = "/dev/md/" + name
     try:
-        # /dev/mapper/ nodes are usually symlinks to /dev/dm-N
         node = os.path.basename(os.readlink(named_path))
     except OSError as e:
         raise MDRaidError("md_node_from_name failed: %s" % e)
     else:
         return node
+
+def name_from_md_node(node):
+    md_dir = "/dev/md"
+    name = None
+    # It's sad, but it's what we've got.
+    for link in os.listdir(md_dir):
+        full_path = "%s/%s" % (md_dir, link)
+        md_name = os.path.basename(os.readlink(full_path))
+        log.debug("link: %s -> %s" % (link, os.readlink(full_path)))
+        if md_name == node:
+            name = link
+            break
+
+    if not name:
+        raise MDRaidError("name_from_md_node(%s) failed" % node)
+
+    log.debug("returning %s" % name)
+    return name
diff --git a/pyanaconda/storage/devices.py b/pyanaconda/storage/devices.py
index 1c56f95..4134aed 100644
--- a/pyanaconda/storage/devices.py
+++ b/pyanaconda/storage/devices.py
@@ -148,6 +148,9 @@ def devicePathToName(devicePath):
     if name.startswith("mapper/"):
         name = name[7:]
 
+    if name.startswith("md/"):
+        name = name[3:]
+
     return name
 
 
diff --git a/pyanaconda/storage/devicetree.py b/pyanaconda/storage/devicetree.py
index 85d5c61..d30b8f0 100644
--- a/pyanaconda/storage/devicetree.py
+++ b/pyanaconda/storage/devicetree.py
@@ -730,7 +730,7 @@ class DeviceTree(object):
         return device
 
     def addUdevMDDevice(self, info):
-        name = udev_device_get_name(info)
+        name = udev_device_get_md_name(info)
         log_method_call(self, name=name)
         uuid = udev_device_get_uuid(info)
         sysfs_path = udev_device_get_sysfs_path(info)
@@ -791,6 +791,9 @@ class DeviceTree(object):
         if disk is None:
             disk_name = os.path.basename(os.path.dirname(sysfs_path))
             disk_name = disk_name.replace('!','/')
+            if disk_name.startswith("md"):
+                disk_name = devicelibs.mdraid.name_from_md_node(disk_name)
+
             disk = self.getDeviceByName(disk_name)
 
         if disk is None:
@@ -887,16 +890,14 @@ class DeviceTree(object):
             kwargs["identifier"] = udev_device_get_fcoe_identifier(info)
             log.info("%s is an fcoe disk" % name)
         elif udev_device_get_md_container(info):
+            name = udev_device_get_md_name(info)
             diskType = MDRaidArrayDevice
             parentPath = udev_device_get_md_container(info)
-            if os.path.islink(parentPath):
-                parentPath = os.path.join(os.path.dirname(parentPath),
-                                          os.readlink(parentPath))
-
-            parentName = devicePathToName(os.path.normpath(parentPath))
+            parentName = devicePathToName(parentPath)
             container = self.getDeviceByName(parentName)
             if not container:
-                container_sysfs = "/class/block/" + parentName
+                parentSysName = devicelibs.mdraid.md_node_from_name(parentName)
+                container_sysfs = "/class/block/" + parentSysName
                 container_info = udev_get_block_device(container_sysfs)
                 if not container_info:
                     log.error("failed to find md container %s at %s"
@@ -1401,7 +1402,7 @@ class DeviceTree(object):
                     continue
 
                 if dev_uuid == md_uuid and dev_level == md_level:
-                    md_name = udev_device_get_name(dev)
+                    md_name = udev_device_get_md_name(dev)
                     md_metadata = dev.get("MD_METADATA")
                     break
 
diff --git a/pyanaconda/storage/udev.py b/pyanaconda/storage/udev.py
index 4076203..0510184 100644
--- a/pyanaconda/storage/udev.py
+++ b/pyanaconda/storage/udev.py
@@ -132,9 +132,7 @@ def udev_get_block_device(sysfs_path):
 # udev database entries.
 def udev_device_get_name(udev_info):
     """ Return the best name for a device based on the udev db data. """
-    if "MD_DEVNAME" in udev_info:
-        name = udev_info["MD_DEVNAME"]
-    elif "DM_NAME" in udev_info:
+    if "DM_NAME" in udev_info:
         name = udev_info["DM_NAME"]
     else:
         name = udev_info["name"]
-- 
1.7.7.6

