Determining parameters of the cache automagically is hard, but if users tell use they want (some of) their LVs cached with all the required parameters for the cache, we can quite easily create cached LVs instead of plain ones. Doing so requires us to change our calculations a bit, but not in any hard or too invasive way.
Related: rhbz#1120421 Signed-off-by: Vratislav Podzimek vpodzime@redhat.com --- blivet/deviceaction.py | 10 +++++++- blivet/devicelibs/lvm.py | 43 ++++++++++++++++++++++++++++++++ blivet/devices/lvm.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++-- blivet/partitioning.py | 4 +++ 4 files changed, 118 insertions(+), 3 deletions(-)
diff --git a/blivet/deviceaction.py b/blivet/deviceaction.py index 70966e2..cf75a27 100644 --- a/blivet/deviceaction.py +++ b/blivet/deviceaction.py @@ -26,7 +26,7 @@ from . import util from . import udev from .util import get_current_entropy from .devices import StorageDevice -from .devices import PartitionDevice +from .devices import PartitionDevice, LVMLogicalVolumeDevice from .formats import getFormat, luks from .storage_log import log_exception_info from parted import partitionFlag, PARTITION_LBA @@ -316,6 +316,14 @@ class ActionCreateDevice(DeviceAction): otherNum = action.device.partedPartition.number if selfNum > otherNum: rc = True + elif (action.isCreate and action.isDevice and + isinstance(self.device, LVMLogicalVolumeDevice) and + isinstance(action.device, LVMLogicalVolumeDevice) and + self.device.vg == action.device.vg): + # create cached LVs before non-cached LVs so that fast cache space + # is not taken by non-cached LVs + if not self.device.cached and action.device.cached: + rc = True elif (action.isAdd and action.container == self.container): rc = True
diff --git a/blivet/devicelibs/lvm.py b/blivet/devicelibs/lvm.py index c1fbe90..883a3f0 100644 --- a/blivet/devicelibs/lvm.py +++ b/blivet/devicelibs/lvm.py @@ -46,6 +46,8 @@ LVM_THINP_MAX_METADATA_SIZE = Size("16 GiB") LVM_THINP_MIN_CHUNK_SIZE = Size("64 KiB") LVM_THINP_MAX_CHUNK_SIZE = Size("1 GiB")
+LVM_MIN_CACHE_MD_SIZE = Size("8 MiB") + RAID_levels = raid.RAIDLevels(["raid0", "raid1", "linear"])
ThPoolProfile = namedtuple("ThPoolProfile", ["name", "desc"]) @@ -639,3 +641,44 @@ def cachepoolname(vg_name, lv_name): # exactly the same as thin pool name return thinlvpoolname(vg_name, lv_name)
+def cachepool_default_md_size(cache_size): + # according to lvmcache(7) + return max(cache_size / 1000, LVM_MIN_CACHE_MD_SIZE) + +def cachepool_create(vg_name, lv_name, data_size, md_size, mode, pvs): + """Create a cache pool LV with the given parameters""" + + args = ["lvcreate", "--type", "cache-pool", "-L", "%dm" % data_size.convertTo(spec="MiB"), + "--poolmetadatasize", "%dm" % md_size.convertTo(spec="MiB"), + "-n", lv_name] + if mode: + args.extend(["--cachemode", mode]) + args.append(vg_name) + args.extend(pvs) + + try: + lvm(args) + except LVMError as msg: + raise LVMError("Failed to create cache pool '%s-%s': %s" % (vg_name, lv_name, msg)) + +def lvcreate_cached(vg_name, lv_name, lv_size, cache_data_size, cache_md_size, + mode, slow_pvs, fast_pvs): + """Create a cached LV with the given parameters""" + + # create the cache pool first (so that it gets allocated on the fast PVs) + cachepool_create(vg_name, lv_name+"_cache", cache_data_size, cache_md_size, mode, fast_pvs) + + # create the LV itself on all PVs, preferring the slow ones + lvcreate(vg_name, lv_name, lv_size, slow_pvs + fast_pvs) + + vg_lv_name = lambda lv_name: "%s/%s" % (vg_name, lv_name) + + # attach the cache pool to the LV + args = ["lvconvert", "--type", "cache", "--cachepool", vg_lv_name(lv_name+"_cache"), vg_lv_name(lv_name)] + try: + lvm(args) + except LVMError as msg: + msg = "Failed to attach cache pool '%s' to the LV '%s': %s" % (vg_lv_name(lv_name+"_cache"), + vg_lv_name(lv_name), + msg) + raise LVMError(msg) diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py index 356446c..7c9813e 100644 --- a/blivet/devices/lvm.py +++ b/blivet/devices/lvm.py @@ -461,7 +461,7 @@ class LVMLogicalVolumeDevice(DMDevice): def __init__(self, name, parents=None, size=None, uuid=None, copies=1, logSize=0, segType=None, fmt=None, exists=False, sysfsPath='', - grow=None, maxsize=None, percent=None): + grow=None, maxsize=None, percent=None, cacheRequest=None): """ :param name: the device name (generally a device node's basename) :type name: str @@ -495,6 +495,8 @@ class LVMLogicalVolumeDevice(DMDevice): :type maxsize: :class:`~.size.Size` :keyword percent -- percent of VG space to take :type percent: int + :keyword cacheRequest: parameters of the requested cache (if any) + :type cacheRequest: :class:`LVMCacheRequest`
""" if self.__class__.__name__ == "LVMLogicalVolumeDevice": @@ -529,6 +531,11 @@ class LVMLogicalVolumeDevice(DMDevice): self.req_size = self._size self.req_percent = util.numeric_type(percent)
+ if cacheRequest: + self._cache = LVMCache(self, cacheRequest.size, exists=False, + fast_pv_names=cacheRequest.fast_devs_names, + mode=cacheRequest.mode) + # here we go with the circular references self.parents[0]._addLogVol(self)
@@ -683,7 +690,34 @@ class LVMLogicalVolumeDevice(DMDevice): """ Create the device. """ log_method_call(self, self.name, status=self.status) # should we use --zero for safety's sake? - lvm.lvcreate(self.vg.name, self._name, self.size) + if self.cached: + # prepare the list of fast PV devices + fast_pvs = [] + for pv in self.cache.fast_pv_names: + # make sure we have the full device paths + if not pv.startswith("/dev/"): + fast_pvs.append("/dev/%s" % pv) + else: + fast_pvs.append(pv) + + # get the list of all fast PV devices used in the VG so that we can + # consider the rest to be slow PVs and generate a list of them + all_fast_pvs_names = set() + for lv in self.vg.lvs: + if lv.cached and lv.cache.fast_pv_names: + all_fast_pvs_names |= set(lv.cache.fast_pv_names) + slow_pvs = [pv.path for pv in self.vg.pvs if pv.name not in all_fast_pvs_names] + + # TODO: allow specification of metadata size + # for now, we just make the metadata+data parts take the requested cache space together + md_size = lvm.cachepool_default_md_size(self.cache.size) + data_size = self.cache.size - md_size + + lvm.lvcreate_cached(self.vg.name, self._name, self.size, + data_size, md_size, self.cache.mode, + slow_pvs, fast_pvs) + else: + lvm.lvcreate(self.vg.name, self._name, self.size)
def _preDestroy(self): StorageDevice._preDestroy(self) @@ -1298,3 +1332,29 @@ class LVMCache(object): def mode(self): return self._mode
+class LVMCacheRequest(object): + """Class representing the LVM cache creation request""" + def __init__(self, size, fast_pv_names, mode=None): + """ + :param size: requested size of the cache + :type size: :class:`~.size.Size` + :param fast_pv_names: PVs to allocate the cache on/from + :type fast_pv_names: list of str + :param str mode: requested mode for the cache (``None`` means the default is used) + + """ + self._size = size + self._fast_pvs = fast_pv_names + self._mode = mode or "writethrough" + + @property + def size(self): + return self._size + + @property + def fast_devs_names(self): + return self._fast_pvs + + @property + def mode(self): + return self._mode diff --git a/blivet/partitioning.py b/blivet/partitioning.py index c96dbe1..9fd1819 100644 --- a/blivet/partitioning.py +++ b/blivet/partitioning.py @@ -1728,6 +1728,10 @@ class VGChunk(Chunk): if max_raid_disks: self.pool -= 5 * max_raid_disks
+ if req.device.cached: + # cached LV -> reserve space for the cache + self.pool -= int(self.vg.align(req.device.cache.size, roundup=True) / self.vg.peSize) + super(VGChunk, self).addRequest(req)
def lengthToSize(self, length):