From: Vratislav Podzimek <vpodzime(a)redhat.com>
We already support various segment types for LVs to some extent. However, we
need to get a better picture of how much space on such LVs' PVs is required. We
already have a code for that, so let's just use it.
This unfortunately requires the LVPVSpec to have read-write attributes/fields
and thus it cannot be a namedtuple. We should probably come up with some
"read-write namedtuple" thing for cases like this in the future.
---
blivet/devices/lvm.py | 20 +++++++++++++++++---
1 file changed, 17 insertions(+), 3 deletions(-)
diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py
index 2242214..2e3b8d8 100644
--- a/blivet/devices/lvm.py
+++ b/blivet/devices/lvm.py
@@ -73,9 +73,11 @@ def get_internal_lv_class(lv_attr):
return None
-LVPVSpec = namedtuple("LVPVSpec", ["pv", "size"])
-""" A namedtuple class for specifying how much space on a PV should be allocated for some LV """
-
+class LVPVSpec(object):
+ """ Class for specifying how much space on a PV should be allocated for some LV """
+ def __init__(self, pv, size):
+ self.pv = pv
+ self.size = size
PVFreeInfo = namedtuple("PVFreeInfo", ["pv", "size", "free"])
""" A namedtuple class holding the information about PV's (usable) size and free space """
@@ -602,6 +604,9 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
self.uuid = uuid
self.seg_type = seg_type or "linear"
+ self._raid_level = None
+ if self.seg_type in (level.name for level in lvm.raid_levels):
+ self._raid_level = lvm.raid_levels.raid_level(self.seg_type)
self.req_grow = None
self.req_max_size = Size(0)
@@ -638,12 +643,21 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
set(spec.pv for spec in self._pv_specs).difference(set(self.vg.parents))]
msg = "invalid destination PV(s) %s for LV %s" % (missing, self.name)
raise ValueError(msg)
+ if self._pv_specs:
+ self._assign_pv_space()
# check that we got parents as expected and add this device to them now
# that it is fully-initialized
self._check_parents()
self._add_to_parents()
+ def _assign_pv_space(self):
+ if self.seg_type == "linear" or not self._raid_level:
+ # nothing to do for non-RAID (including striped) LVs here
+ return
+ for spec in self._pv_specs:
+ spec.size = self._raid_level.get_base_member_size(self.size, len(self._pv_specs))
+
def _check_parents(self):
"""Check that this device has parents as expected"""
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/9f224e6fd9e60b50db30afb57db306…
From: Vratislav Podzimek <vpodzime(a)redhat.com>
If we want to support other types of LV than linear we need to know how much
space we have in each PV. For example a 1GiB RAID1 LV requires not only 2GiB
total space in the VG with 2 PVs, but at the same time it requires 1GiB space on
each of the PVs.
---
blivet/devices/lvm.py | 80 +++++++++++++++++++++++++++++++++++++++----------
blivet/formats/lvmpv.py | 22 ++++++++++++++
2 files changed, 87 insertions(+), 15 deletions(-)
diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py
index de40fd6..2242214 100644
--- a/blivet/devices/lvm.py
+++ b/blivet/devices/lvm.py
@@ -77,6 +77,10 @@ def get_internal_lv_class(lv_attr):
""" A namedtuple class for specifying how much space on a PV should be allocated for some LV """
+PVFreeInfo = namedtuple("PVFreeInfo", ["pv", "size", "free"])
+""" A namedtuple class holding the information about PV's (usable) size and free space """
+
+
class LVMVolumeGroupDevice(ContainerDevice):
""" An LVM Volume Group """
@@ -128,22 +132,22 @@ def __init__(self, name, parents=None, size=None, free=None,
self.pv_count = util.numeric_type(pv_count)
if exists and not pv_count:
self._complete = True
+ self.pe_size = util.numeric_type(pe_size)
+ self.pe_count = util.numeric_type(pe_count)
+ self.pe_free = util.numeric_type(pe_free)
+
+ # TODO: validate pe_size if given
+ if not self.pe_size:
+ self.pe_size = lvm.LVM_PE_SIZE
super(LVMVolumeGroupDevice, self).__init__(name, parents=parents,
uuid=uuid, size=size,
exists=exists, sysfs_path=sysfs_path)
self.free = util.numeric_type(free)
- self.pe_size = util.numeric_type(pe_size)
- self.pe_count = util.numeric_type(pe_count)
- self.pe_free = util.numeric_type(pe_free)
self._reserved_percent = 0
self._reserved_space = Size(0)
- # TODO: validate pe_size if given
- if not self.pe_size:
- self.pe_size = lvm.LVM_PE_SIZE
-
if not self.exists:
self.pv_count = len(self.parents)
@@ -304,6 +308,17 @@ def _add_log_vol(self, lv):
if origin:
origin.snapshots.append(lv)
+ # PV space accounting
+ pv_sizes = lv.pv_space_used
+ if pv_sizes:
+ for size_spec in pv_sizes:
+ # check that we have enough space in the PVs for the LV and
+ # account for it
+ if size_spec.pv.format.free < size_spec.size:
+ msg = "not enough space in the '%s' PV for the '%s' LV's extents" % (size_spec.pv.name, lv.name)
+ raise errors.DeviceError(msg)
+ size_spec.pv.format.free -= size_spec.size
+
def _remove_log_vol(self, lv):
""" Remove an LV from this VG. """
if lv not in self.lvs:
@@ -316,6 +331,12 @@ def _remove_log_vol(self, lv):
if origin:
origin.snapshots.remove(lv)
+ # PV space accounting
+ pv_sizes = lv.pv_space_used
+ if pv_sizes:
+ for size_spec in pv_sizes:
+ size_spec.pv.format.free += size_spec.size
+
def _add_parent(self, member):
super(LVMVolumeGroupDevice, self)._add_parent(member)
@@ -323,6 +344,10 @@ def _add_parent(self, member):
len(self.parents) + 1 == self.pv_count):
self._complete = True
+ # this PV object is just being added so it has all its space available
+ # (adding LVs will eat that space later)
+ member.format.free = self._get_pv_usable_space(member)
+
def _remove_parent(self, member):
# XXX It would be nice to raise an exception if removing this member
# would not leave enough space, but the devicefactory relies on it
@@ -331,6 +356,7 @@ def _remove_parent(self, member):
# Maybe remove_member could be a wrapper with the checks and the
# devicefactory could call the _ versions to bypass the checks.
super(LVMVolumeGroupDevice, self)._remove_parent(member)
+ member.format.free = None
# We can't rely on lvm to tell us about our size, free space, &c
# since we could have modifications queued, unless the VG and all of
@@ -358,6 +384,12 @@ def reserved_space(self):
return self.align(reserved, roundup=True)
+ def _get_pv_usable_space(self, pv):
+ if isinstance(pv, MDRaidArrayDevice):
+ return self.align(pv.size - 2 * pv.format.pe_start)
+ else:
+ return self.align(pv.size - pv.format.pe_start)
+
@property
def lvm_metadata_space(self):
""" The amount of the space LVM metadata cost us in this VG's PVs """
@@ -372,10 +404,7 @@ def lvm_metadata_space(self):
# class once it exists
diff = Size(0)
for pv in self.pvs:
- if isinstance(pv, MDRaidArrayDevice):
- diff += pv.size - self.align(pv.size - 2 * pv.format.pe_start)
- else:
- diff += pv.size - self.align(pv.size - pv.format.pe_start)
+ diff += pv.size - self._get_pv_usable_space(pv)
return diff
@@ -422,6 +451,16 @@ def free_extents(self):
# TODO: just ask lvm if is_modified returns False
return int(self.free_space / self.pe_size)
+ @property
+ def pv_free_info(self):
+ """
+ :returns: information about sizes and free space in this VG's PVs
+ :rtype: list of PVFreeInfo
+
+ """
+ return [PVFreeInfo(pv, self._get_pv_usable_space(pv.size), pv.format.free)
+ for pv in self.pvs]
+
def align(self, size, roundup=False):
""" Align a size to a multiple of physical extent size. """
size = util.numeric_type(size)
@@ -576,10 +615,6 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
self.req_size = self._size
self.req_percent = util.numeric_type(percent)
- # check that we got parents as expected and add this device to them
- self._check_parents()
- self._add_to_parents()
-
self._metadata_size = Size(0)
self._internal_lvs = []
self._cache = None
@@ -589,6 +624,7 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
fast_pvs=cache_request.fast_devs, mode=cache_request.mode)
self._pv_specs = []
+ pvs = pvs or []
for pv_spec in pvs:
if isinstance(pv_spec, LVPVSpec):
self._pv_specs.append(pv_spec)
@@ -603,6 +639,11 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
msg = "invalid destination PV(s) %s for LV %s" % (missing, self.name)
raise ValueError(msg)
+ # check that we got parents as expected and add this device to them now
+ # that it is fully-initialized
+ self._check_parents()
+ self._add_to_parents()
+
def _check_parents(self):
"""Check that this device has parents as expected"""
@@ -705,6 +746,15 @@ def vg_space_used(self):
return (self.vg.align(self.size, roundup=True) * self.copies
+ self.log_size + self.metadata_size + cache_size)
+ @property
+ def pv_space_used(self):
+ """
+ :returns: space occupied by this LV on its VG's PVs (if we have and idea)
+ :rtype: list of LVPVSpec
+
+ """
+ return self._pv_specs
+
def _set_format(self, fmt):
super(LVMLogicalVolumeDevice, self)._set_format(fmt)
for snapshot in (s for s in self.snapshots if not s.exists):
diff --git a/blivet/formats/lvmpv.py b/blivet/formats/lvmpv.py
index 65bab91..f0031ef 100644
--- a/blivet/formats/lvmpv.py
+++ b/blivet/formats/lvmpv.py
@@ -33,6 +33,7 @@
from ..tasks import availability
from ..i18n import N_
from ..size import Size
+from ..errors import PhysicalVolumeError
from . import DeviceFormat, register_device_format
import logging
@@ -66,6 +67,8 @@ def __init__(self, **kwargs):
:type pe_start: :class:`~.size.Size`
:keyword data_alignment: data alignment (for non-existent PVs)
:type data_alignment: :class:`~.size.Size`
+ :keyword free: free space in the PV
+ :type free: :class:`~.size.Size`
.. note::
@@ -81,6 +84,7 @@ def __init__(self, **kwargs):
self.vg_uuid = kwargs.get("vg_uuid")
self.pe_start = kwargs.get("pe_start", lvm.LVM_PE_START)
self.data_alignment = kwargs.get("data_alignment", Size(0))
+ self._free = kwargs.get("free") # None means unknown
self.inconsistent_vg = False
@@ -143,4 +147,22 @@ def status(self):
return (self.exists and self.vg_name and
os.path.isdir("/dev/%s" % self.vg_name))
+ @property
+ def free(self):
+ """ Information about the free space in this PV """
+ if self._free is None:
+ if self.exists:
+ # we don't have any actual value, but the PV exists and is
+ # active, we should try to determine it
+ pv_info = blockdev.lvm.pvinfo(self.device)
+ self._free = Size(pv_info.pv_free)
+ else:
+ raise PhysicalVolumeError("Unknown free space information for the PV '%s'" % self.device)
+
+ return self._free
+
+ @free.setter
+ def free(self, value):
+ self._free = value
+
register_device_format(LVMPhysicalVolume)
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/e668c30c4e534c6d927fafeafac21d…
From: Vratislav Podzimek <vpodzime(a)redhat.com>
DM maps cannot be removed while they are in use so in order to make them
removable, we need to teardown all devices "built" on top of them first. Since
we don't support using disk images together with real disks we can safely just
tear all devices down. This makes our examples (and possibly some tests) better
clean after themselves.
---
blivet/devicetree.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/blivet/devicetree.py b/blivet/devicetree.py
index fe10aa5..953d977 100644
--- a/blivet/devicetree.py
+++ b/blivet/devicetree.py
@@ -1048,6 +1048,9 @@ def setup_disk_images(self):
def teardown_disk_images(self):
""" Tear down any disk image stacks. """
+ # teardown all devices first so that there's nothing active running on
+ # top of disk images when we try to tear those down
+ self.teardown_all()
self._populator.teardown_disk_images()
@property
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/1111ba89d6f11f75c3c6b545955df0…
From: Vratislav Podzimek <vpodzime(a)redhat.com>
It's just more common these days and the definition is easier to find and
recognize in the sources.
---
blivet/devices/device.py | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/blivet/devices/device.py b/blivet/devices/device.py
index 48ea554..62637aa 100644
--- a/blivet/devices/device.py
+++ b/blivet/devices/device.py
@@ -148,18 +148,18 @@ def _init_parent_list(self):
for parent in list(self._parents):
self._parents.remove(parent)
- def _set_parent_list(self, parents):
+ @property
+ def parents(self):
+ """ Devices upon which this device is built """
+ return self._parents
+
+ @parents.setter
+ def parents(self, parents):
""" Set this instance's parent list. """
self._init_parent_list()
for parent in parents:
self._parents.append(parent)
- def _get_parent_list(self):
- return self._parents
-
- parents = property(_get_parent_list, _set_parent_list,
- doc="devices upon which this device is built")
-
@property
def dict(self):
d = {"type": self.type, "name": self.name,
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/24799edea1a46b6ded5cc0ead13e64…
From: Vratislav Podzimek <vpodzime(a)redhat.com>
This really only takes care about passing the type down to libblockdev and thus
lvcreate. We need to a lot more to actually fully support various types of LVs.
---
blivet/devices/lvm.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py
index 159e3ef..de40fd6 100644
--- a/blivet/devices/lvm.py
+++ b/blivet/devices/lvm.py
@@ -841,7 +841,9 @@ def _create(self):
# TODO: specify sizes together with PVs once LVM and libblockdev support it
pvs = [spec.pv.path for spec in self._pv_specs]
pvs = pvs or None
- blockdev.lvm.lvcreate(self.vg.name, self._name, self.size, pv_list=pvs)
+
+ blockdev.lvm.lvcreate(self.vg.name, self._name, self.size,
+ type=self.seg_type, pv_list=pvs)
else:
mode = blockdev.lvm.cache_get_mode_from_str(self.cache.mode)
# prepare the list of fast PV devices
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/d96d2a807ae4f871813ee38886bfb5…
From: Vratislav Podzimek <vpodzime(a)redhat.com>
This allows for more precise configuration of how the LVM setup should look
like. We already have some rudimentary support for specifying PVs for caches
which is a must, but it's useful for all LVs in general.
Thanks dlehman(a)redhat.com for pieces of code and ideas for this patch!
---
blivet/devices/lvm.py | 48 +++++++++++++++++++++++++++++++++++++++---------
1 file changed, 39 insertions(+), 9 deletions(-)
diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py
index 350e423..159e3ef 100644
--- a/blivet/devices/lvm.py
+++ b/blivet/devices/lvm.py
@@ -27,6 +27,7 @@
import re
import os
import time
+from collections import namedtuple
import gi
gi.require_version("BlockDev", "1.0")
@@ -72,6 +73,10 @@ def get_internal_lv_class(lv_attr):
return None
+LVPVSpec = namedtuple("LVPVSpec", ["pv", "size"])
+""" A namedtuple class for specifying how much space on a PV should be allocated for some LV """
+
+
class LVMVolumeGroupDevice(ContainerDevice):
""" An LVM Volume Group """
@@ -512,7 +517,7 @@ class LVMLogicalVolumeDevice(DMDevice):
def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
fmt=None, exists=False, sysfs_path='', grow=None, maxsize=None,
- percent=None, cache_request=None):
+ percent=None, cache_request=None, pvs=None):
"""
:param name: the device name (generally a device node's basename)
:type name: str
@@ -544,6 +549,8 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
:type percent: int
:keyword cache_request: parameters of requested cache (if any)
:type cache_request: :class:`~.devices.lvm.LVMCacheRequest`
+ :keyword pvs: list of PVs to allocate extents from (size could be specified for each PV)
+ :type pvs: list of :class:`~.devices.StorageDevice` or :class:`LVPVSpec` objects (tuples)
"""
@@ -581,6 +588,21 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
self._cache = LVMCache(self, size=cache_request.size, exists=False,
fast_pvs=cache_request.fast_devs, mode=cache_request.mode)
+ self._pv_specs = []
+ for pv_spec in pvs:
+ if isinstance(pv_spec, LVPVSpec):
+ self._pv_specs.append(pv_spec)
+ elif isinstance(pv_spec, StorageDevice):
+ self._pv_specs.append(LVPVSpec(pv_spec, Size(0)))
+ else:
+ raise ValueError("Invalid PV spec '%s' for the '%s' LV" % (pv_spec, self.name))
+ # Make sure any destination PVs are actually PVs in this VG
+ if not set(spec.pv for spec in self._pv_specs).issubset(set(self.vg.parents)):
+ missing = [r.name for r in
+ set(spec.pv for spec in self._pv_specs).difference(set(self.vg.parents))]
+ msg = "invalid destination PV(s) %s for LV %s" % (missing, self.name)
+ raise ValueError(msg)
+
def _check_parents(self):
"""Check that this device has parents as expected"""
@@ -816,7 +838,10 @@ def _create(self):
# should we use --zero for safety's sake?
if not self.cache:
# just a plain LV
- blockdev.lvm.lvcreate(self.vg.name, self._name, self.size)
+ # TODO: specify sizes together with PVs once LVM and libblockdev support it
+ pvs = [spec.pv.path for spec in self._pv_specs]
+ pvs = pvs or None
+ blockdev.lvm.lvcreate(self.vg.name, self._name, self.size, pv_list=pvs)
else:
mode = blockdev.lvm.cache_get_mode_from_str(self.cache.mode)
# prepare the list of fast PV devices
@@ -828,13 +853,17 @@ def _create(self):
else:
fast_pvs.append(pv_name)
- # 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_pvs:
- all_fast_pvs_names |= set(pv.name for pv in lv.cache.fast_pvs)
- slow_pvs = [pv.path for pv in self.vg.pvs if pv.name not in all_fast_pvs_names]
+ if self._pv_specs:
+ # (slow) PVs specified for this LV
+ slow_pvs = [spec.pv.path for spec in self._pv_specs]
+ else:
+ # 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_pvs:
+ all_fast_pvs_names |= set(pv.name for pv in lv.cache.fast_pvs)
+ slow_pvs = [pv.path for pv in self.vg.pvs if pv.name not in all_fast_pvs_names]
# VG name, LV name, data size, cache size, metadata size, mode, flags, slow PVs, fast PVs
# XXX: we need to pass slow_pvs+fast_pvs as slow PVs because parts
@@ -1573,6 +1602,7 @@ def _create(self):
else:
profile_name = None
# TODO: chunk size, data/metadata split --> profile
+ # TODO: allow for specification of PVs
blockdev.lvm.thpoolcreate(self.vg.name, self.lvname, self.size,
md_size=self.metadata_size,
chunk_size=self.chunk_size,
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/6e2a6875f300ba70d84cc400323abb…
This adds an elementary support of non-linear LVs to Blivet. The first patch is unrelated, but also useful. Tests will follow later.
--
To view this pull request on github, visit https://github.com/rhinstaller/blivet/pull/286
From: Vratislav Podzimek <vpodzime(a)redhat.com>
RAID LVs have/need one extent big internal meta data LVs. Instead of bothering
the user code with this, let's just make the RAID LVs one extent smaller than
requested and use that space for the meta data.
---
blivet/devices/lvm.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/blivet/devices/lvm.py b/blivet/devices/lvm.py
index 166f5f6..ad10ba0 100644
--- a/blivet/devices/lvm.py
+++ b/blivet/devices/lvm.py
@@ -644,8 +644,10 @@ def __init__(self, name, parents=None, size=None, uuid=None, seg_type=None,
self.req_percent = util.numeric_type(percent)
if not self.exists and self.seg_type.startswith("raid"):
- # RAID LVs create one extent big internal metadata LVs
+ # RAID LVs create one extent big internal metadata LVs so make sure
+ # we reserve space for it
self._metadata_size = self.vg.pe_size
+ self._size -= self._metadata_size
else:
self._metadata_size = Size(0)
self._internal_lvs = []
--
To view this commit on github, visit https://github.com/rhinstaller/blivet/commit/d8fcfc4cb7159f696cb1c9831d3dbe…