Provide a mapping of node to LVs a given instance owns.

Signed-off-by: Ilias Tsitsimpis <[email protected]>
---
 lib/config.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/lib/config.py b/lib/config.py
index 6bc726b..d28372e 100644
--- a/lib/config.py
+++ b/lib/config.py
@@ -405,6 +405,61 @@ class ConfigWriter(object):
     """
     return self._UnlockedGetInstanceSecondaryNodes(instance)
 
+  # pylint: disable=R0201
+  def _UnlockedGetInstanceLVsByNode(self, instance, lvmap=None):
+    """Provide a mapping of node to LVs a given instance owns.
+
+    This function is for internal use, when the config lock is already held.
+
+    """
+    def _MapLVsByNode(lvmap, devs, node_uuid):
+      """Recursively helper function."""
+      if not node_uuid in lvmap:
+        lvmap[node_uuid] = []
+
+      for dev in devs:
+        if dev.dev_type == constants.DT_PLAIN:
+          lvmap[node_uuid].append(dev.logical_id[0] + "/" + dev.logical_id[1])
+
+        elif dev.dev_type in constants.DTS_DRBD:
+          if dev.children:
+            _MapLVsByNode(lvmap, dev.children, dev.logical_id[0])
+            _MapLVsByNode(lvmap, dev.children, dev.logical_id[1])
+
+        elif dev.children:
+          _MapLVsByNode(lvmap, devs.children, node_uuid)
+
+    if lvmap is None:
+      lvmap = {}
+      ret = lvmap
+    else:
+      ret = None
+
+    node_uuid = instance.primary_node
+    devs = instance.disks
+    _MapLVsByNode(lvmap, devs, node_uuid)
+    return ret
+
+  @_ConfigSync(shared=1)
+  def GetInstanceLVsByNode(self, instance, lvmap=None):
+    """Provide a mapping of node to LVs a given instance owns.
+
+    This function figures out what logical volums should belong on
+    which nodes, recursing through a device tree.
+
+    @type instance: L{objects.Instance}
+    @param instance: The instance we want to compute the LVsByNode for
+    @type lvmap: dict
+    @param lvmap: optional dictionary to receive the
+        'node' : ['lv', ...] data.
+    @return: None if lvmap arg is given, otherwise, a dictionary of
+        the form { 'node_uuid' : ['volume1', 'volume2', ...], ... };
+        volumeN is of the form "vg_name/lv_name", compatible with
+        GetVolumeList()
+
+    """
+    return self._UnlockedGetInstanceLVsByNode(instance, lvmap=lvmap)
+
   @_ConfigSync(shared=1)
   def GetGroupDiskParams(self, group):
     """Get the disk params populated with inherit chain.
-- 
1.9.1

Reply via email to