From 390522345603b03f36f2bef23ba03bebf8516887 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Fri, 7 Mar 2014 16:43:36 -0600
Subject: [PATCH 1/6] Add backend functions for container member set
 management.

---
 blivet/devicelibs/btrfs.py  | 12 ++++++++++--
 blivet/devicelibs/lvm.py    | 28 +++++++++++++++++++++++-----
 blivet/devicelibs/mdraid.py | 18 ++++++++++++++----
 3 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/blivet/devicelibs/btrfs.py b/blivet/devicelibs/btrfs.py
index bca1242..f68fce0 100644
--- a/blivet/devicelibs/btrfs.py
+++ b/blivet/devicelibs/btrfs.py
@@ -78,9 +78,17 @@ def create_volume(devices, label=None, data=None, metadata=None):
 
 # destroy is handled using wipefs
 
-# add device
+def add(mountpoint, devices):
+    if not os.path.ismount(mountpoint):
+        raise ValueError("volume not mounted")
+
+    return btrfs(["device", "add"] + devices + [mountpoint])
+
+def remove(mountpoint, devices):
+    if not os.path.ismount(mountpoint):
+        raise ValueError("volume not mounted")
 
-# remove device
+    return btrfs(["device", "delete"] + devices + [mountpoint])
 
 def create_subvolume(mountpoint, name):
     """Create a subvolume named name below mountpoint mountpoint."""
diff --git a/blivet/devicelibs/lvm.py b/blivet/devicelibs/lvm.py
index 46ae660..f172fef 100644
--- a/blivet/devicelibs/lvm.py
+++ b/blivet/devicelibs/lvm.py
@@ -258,6 +258,15 @@ def pvscan(device):
     except LVMError as msg:
         raise LVMError("pvscan failed for %s: %s" % (device, msg))
 
+def pvmove(source, dest):
+    """ Move physical extents from one PV to another. """
+    args = ["pvmove"] + _getConfigArgs() + [source, dest]
+
+    try:
+        lvm(args)
+    except LVMError as msg:
+        raise LVMError("pvmove failed for %s->%s: %s" % (source, dest, msg))
+
 def pvinfo(device):
     """
         If the PV was created with '--metadacopies 0', lvm will do some
@@ -334,16 +343,16 @@ def vgdeactivate(vg_name):
     except LVMError as msg:
         raise LVMError("vgdeactivate failed for %s: %s" % (vg_name, msg))
 
-def vgreduce(vg_name, pv_list, rm=False):
+def vgreduce(vg_name, pv_list, missing=False):
     """ Reduce a VG.
 
-    rm -> with RemoveMissing option.
-    Use pv_list when rm=False, otherwise ignore pv_list and call vgreduce with
-    the --removemissing option.
+    missing -> with RemoveMissing option.
+    Use pv_list when missing=False, otherwise ignore pv_list and call vgreduce
+    with the --removemissing option.
     """
     args = ["vgreduce"]
     args.extend(_getConfigArgs())
-    if rm:
+    if missing:
         args.extend(["--removemissing", "--force", vg_name])
     else:
         args.extend([vg_name] + pv_list)
@@ -353,6 +362,15 @@ def vgreduce(vg_name, pv_list, rm=False):
     except LVMError as msg:
         raise LVMError("vgreduce failed for %s: %s" % (vg_name, msg))
 
+def vgextend(vg_name, pv_list):
+    """ Add PV(s) to a VG. """
+    args = ["vgextend"] + _getConfigArgs() + [vg_name] + pv_list
+
+    try:
+        lvm(args)
+    except LVMError as msg:
+        raise LVMError("vgextend failed for %s: %s" % (vg_name, msg))
+
 def vginfo(vg_name):
     args = ["vgs", "--noheadings", "--nosuffix"] + \
             ["--units", "m"] + \
diff --git a/blivet/devicelibs/mdraid.py b/blivet/devicelibs/mdraid.py
index b5bf688..8c3156f 100644
--- a/blivet/devicelibs/mdraid.py
+++ b/blivet/devicelibs/mdraid.py
@@ -134,14 +134,24 @@ def mddestroy(device):
     except MDRaidError as msg:
         raise MDRaidError("mddestroy failed for %s: %s" % (device, msg))
 
-def mdadd(device):
-    args = ["--incremental", "--quiet"]
-    args.append(device)
+def mdadd(array, devices, incremental=False):
+    if incremental:
+        args = ["--incremental", "--quiet"]
+    else:
+        args = [array, "--add"]
+
+    args.extend(devices)
 
     try:
         mdadm(args)
     except MDRaidError as msg:
-        raise MDRaidError("mdadd failed for %s: %s" % (device, msg))
+        raise MDRaidError("mdadd failed for %s: %s" % (devices, msg))
+
+def mdremove(array, devices):
+    try:
+        mdadm([array, "--remove"] + devices)
+    except MDRaidError as msg:
+        raise MDRaidError("mdremove failed for %s: %s" % (devices, msg))
 
 def mdactivate(device, members=[], uuid=None):
     """Assemble devices given by members into a single device.
-- 
1.8.5.3

