From 673cd9c454ca068e070ae42153ff0c77b4cfda6b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Nussbaumer?= <rn@google.com>
Date: Thu, 23 Sep 2010 13:23:51 +0200
Subject: [PATCH] Migrate call from backend._GetVGInfo to
 bdev.LogicalVolume.GetVGInfo
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch removes duplicate code found in backend which also needs to
get VG infos. To make it simpler we moved to bdev.LogicalVolume.GetVGInfo.

Signed-off-by: RenΓ© Nussbaumer <rn@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 lib/backend.py | 52 +++++++++-----------------------------------------
 lib/bdev.py    | 12 +++++++-----
 2 files changed, 16 insertions(+), 48 deletions(-)

diff --git a/lib/backend.py b/lib/backend.py
index ab013366d..ac5f93b4a 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -439,9 +439,15 @@ def GetNodeInfo(vgname, hypervisor_type):
 
   """
   outputarray = {}
-  vginfo = _GetVGInfo(vgname)
-  outputarray['vg_size'] = vginfo['vg_size']
-  outputarray['vg_free'] = vginfo['vg_free']
+
+  vginfo = bdev.LogicalVolume.GetVGInfo([vgname])
+  vg_free = vg_size = None
+  if vginfo:
+    vg_free = int(round(vginfo[0][0], 0))
+    vg_size = int(round(vginfo[0][1], 0))
+
+  outputarray['vg_size'] = vg_size
+  outputarray['vg_free'] = vg_free
 
   hyper = hypervisor.GetHypervisor(hypervisor_type)
   hyp_info = hyper.GetNodeInfo()
@@ -930,46 +936,6 @@ def RunRenameInstance(instance, old_name, debug):
           " log file:\n%s", result.fail_reason, "\n".join(lines), log=False)
 
 
-def _GetVGInfo(vg_name):
-  """Get information about the volume group.
-
-  @type vg_name: str
-  @param vg_name: the volume group which we query
-  @rtype: dict
-  @return:
-    A dictionary with the following keys:
-      - C{vg_size} is the total size of the volume group in MiB
-      - C{vg_free} is the free size of the volume group in MiB
-      - C{pv_count} are the number of physical disks in that VG
-
-    If an error occurs during gathering of data, we return the same dict
-    with keys all set to None.
-
-  """
-  retdic = dict.fromkeys(["vg_size", "vg_free", "pv_count"])
-
-  retval = utils.RunCmd(["vgs", "-ovg_size,vg_free,pv_count", "--noheadings",
-                         "--nosuffix", "--units=m", "--separator=:", vg_name])
-
-  if retval.failed:
-    logging.error("volume group %s not present", vg_name)
-    return retdic
-  valarr = retval.stdout.strip().rstrip(':').split(':')
-  if len(valarr) == 3:
-    try:
-      retdic = {
-        "vg_size": int(round(float(valarr[0]), 0)),
-        "vg_free": int(round(float(valarr[1]), 0)),
-        "pv_count": int(valarr[2]),
-        }
-    except (TypeError, ValueError), err:
-      logging.exception("Fail to parse vgs output: %s", err)
-  else:
-    logging.error("vgs output has the wrong number of fields (expected"
-                  " three): %s", str(valarr))
-  return retdic
-
-
 def _GetBlockDevSymlinkPath(instance_name, idx):
   return utils.PathJoin(constants.DISK_LINKS_DIR,
                         "%s:%d" % (instance_name, idx))
diff --git a/lib/bdev.py b/lib/bdev.py
index 335288584..1378a2ff7 100644
--- a/lib/bdev.py
+++ b/lib/bdev.py
@@ -488,24 +488,26 @@ class LogicalVolume(BlockDev):
     @param filter_readonly: whether to skip over readonly VGs
 
     @rtype: list
-    @return: list of tuples (free_space, name) with free_space in mebibytes
+    @return: list of tuples (free_space, total_size, name) with free_space in
+             MiB
 
     """
     try:
-      info = cls._GetVolumeInfo("vgs", ["vg_name", "vg_free", "vg_attr"])
+      info = cls._GetVolumeInfo("vgs", ["vg_name", "vg_free", "vg_attr",
+                                        "vg_size"])
     except errors.GenericError, err:
       logging.error("Can't get VG information: %s", err)
       return None
 
     data = []
-    for vg_name, vg_free, vg_attr in info:
+    for vg_name, vg_free, vg_attr, vg_size in info:
       # (possibly) skip over vgs which are not writable
       if filter_readonly and vg_attr[0] == "r":
         continue
       # (possibly) skip over vgs which are not in the right volume group(s)
       if vg_names and vg_name not in vg_names:
         continue
-      data.append((float(vg_free), vg_name))
+      data.append((float(vg_free), float(vg_size), vg_name))
 
     return data
 
@@ -702,7 +704,7 @@ class LogicalVolume(BlockDev):
     vg_info = self.GetVGInfo([self._vg_name])
     if not vg_info:
       _ThrowError("Can't compute VG info for vg %s", self._vg_name)
-    free_size, _ = vg_info[0]
+    free_size, _, _ = vg_info[0]
     if free_size < size:
       _ThrowError("Not enough free space: required %s,"
                   " available %s", size, free_size)
-- 
GitLab