From b262fb9fd91c93a337e862579c65671373187910 Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Fri, 28 Mar 2014 16:21:09 -0500
Subject: [PATCH 3/5] Use LVMSnapshotDevice when populating the devicetree.

---
 blivet/devicetree.py | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)

diff --git a/blivet/devicetree.py b/blivet/devicetree.py
index 1fe7d4b..931ba15 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -28,7 +28,7 @@ import pprint
 import copy
 
 from .errors import CryptoError, DeviceError, DeviceTreeError, DiskLabelCommitError, DMError, FSError, InvalidDiskLabelError, LUKSError, MDRaidError, StorageError
-from .devices import BTRFSDevice, BTRFSSubVolumeDevice, BTRFSVolumeDevice, DASDDevice, DMDevice, DMLinearDevice, DMRaidArrayDevice, DiskDevice, FcoeDiskDevice, FileDevice, LoopDevice, LUKSDevice, LVMLogicalVolumeDevice, LVMThinLogicalVolumeDevice, LVMThinPoolDevice, LVMVolumeGroupDevice, MDRaidArrayDevice, MultipathDevice, NoDevice, OpticalDevice, PartitionDevice, ZFCPDiskDevice, devicePathToName, iScsiDiskDevice
+from .devices import BTRFSDevice, BTRFSSubVolumeDevice, BTRFSVolumeDevice, DASDDevice, DMDevice, DMLinearDevice, DMRaidArrayDevice, DiskDevice, FcoeDiskDevice, FileDevice, LoopDevice, LUKSDevice, LVMLogicalVolumeDevice, LVMSnapShotDevice, LVMThinLogicalVolumeDevice, LVMThinSnapShotDevice, LVMThinPoolDevice, LVMVolumeGroupDevice, MDRaidArrayDevice, MultipathDevice, NoDevice, OpticalDevice, PartitionDevice, ZFCPDiskDevice, devicePathToName, iScsiDiskDevice
 from .deviceaction import ActionCreateDevice, ActionDestroyDevice, action_type_from_string, action_object_from_string
 from . import formats
 from .formats import getFormat
@@ -1354,6 +1354,7 @@ class DeviceTree(object):
 
             lv_class = LVMLogicalVolumeDevice
             lv_parents = [vg_device]
+            lv_kwargs = {}
             name = "%s-%s" % (vg_name, lv_name)
 
             if self.getDeviceByName(name):
@@ -1370,17 +1371,16 @@ class DeviceTree(object):
                     return
 
                 if origin_name.endswith("_vorigin]"):
-                    log.info("snapshot volume '%s' has vorigin", name)
-                    vg_device.voriginSnapshots[lv_name] = lv_size
-                    return
+                    lv_kwargs["vorigin"] = True
+                    origin = None
+                else:
+                    origin_device_name = "%s-%s" % (vg_name, origin_name)
+                    addRequiredLV(origin_device_name,
+                                  "failed to locate origin lv")
+                    origin = self.getDeviceByName(origin_device_name)
 
-                origin_device_name = "%s-%s" % (vg_name, origin_name)
-                addRequiredLV(origin_device_name, "failed to locate origin lv")
-                origin = self.getDeviceByName(origin_device_name)
-                log.debug("adding %s to %s snapshot total", lv_size,
-                                                            origin.name)
-                origin.snapshotSpace += lv_size
-                return
+                lv_kwargs["origin"] = origin
+                lv_class = LVMSnapShotDevice
             elif lv_attr[0] == 'v':
                 # skip vorigins
                 return
@@ -1417,7 +1417,17 @@ class DeviceTree(object):
                 pool_name = lvm.thinlvpoolname(vg_name, lv_name)
                 pool_device_name = "%s-%s" % (vg_name, pool_name)
                 addRequiredLV(pool_device_name, "failed to look up thin pool")
-                lv_class = LVMThinLogicalVolumeDevice
+
+                origin_name = lvm.lvorigin(vg_name, lv_name)
+                if origin_name:
+                    origin_device_name = "%s-%s" % (vg_name, origin_name)
+                    addRequiredLV(origin_device_name, "failed to locate origin lv")
+                    origin = self.getDeviceByName(origin_device_name)
+                    lv_kwargs["origin"] = origin
+                    lv_class = LVMThinSnapShotDevice
+                else:
+                    lv_class = LVMThinLogicalVolumeDevice
+
                 lv_parents = [self.getDeviceByName(pool_device_name)]
             elif lv_name.endswith(']'):
                 # Internal LVM2 device
@@ -1429,7 +1439,7 @@ class DeviceTree(object):
             if lv_dev is None:
                 lv_device = lv_class(lv_name, parents=lv_parents,
                                      uuid=lv_uuid, size=lv_size,segType=lv_type,
-                                     exists=True)
+                                     exists=True, **lv_kwargs)
                 self._addDevice(lv_device)
                 if flags.installer_mode:
                     lv_device.setup()
-- 
1.9.0

