diff --git a/README.rst b/README.rst index bf11617..2b914f4 100644 --- a/README.rst +++ b/README.rst @@ -3,10 +3,11 @@ Introduction lvm2py is a ctypes based binding for lvm's liblvm2app api. There are some limitations to what it can do compared to the command line options available. For example, the -resize of Physical and Logical volumes is not implemented in the api yet. Still, -contributions are planned in the near future. This is the first release, tested using -LVM 2.2, please report any bugs or suggestions to my github repo in the links below or -by e-mail: rq.sysadmin@gmail.com +resize of Physical and Logical volumes is not implemented in the api yet. + +As of February 2019, there were two people who had forked and patched +the upstream repo at https://github.com/xzased/lvm2py and this branch +just combines their minimal changes. Downloads ========= diff --git a/lvm2py/lv.py b/lvm2py/lv.py index 36407a1..45c1ddd 100644 --- a/lvm2py/lv.py +++ b/lvm2py/lv.py @@ -13,9 +13,9 @@ #You should have received a copy of the GNU General Public License #along with lvm2py. If not, see . -from conversion import * -from exception import * -from util import * +from .conversion import * +from .exception import * +from .util import * class LogicalVolume(object): @@ -53,6 +53,7 @@ def __init__(self, vg, lvh=None, name=None): self.__vg.open() self.__lvh = lvm_lv_from_name(vg.handle, name) if not bool(self.__lvh): + self.__vg.close() raise HandleError("Failed to initialize LV Handle.") self.__uuid = lvm_lv_get_uuid(self.__lvh) self.__vg.close() @@ -179,4 +180,4 @@ def deactivate(self): d = lvm_lv_deactivate(self.handle) self.close() if d != 0: - raise CommitError("Failed to deactivate LV.") \ No newline at end of file + raise CommitError("Failed to deactivate LV.") diff --git a/lvm2py/lvm.py b/lvm2py/lvm.py index fcedb5d..a5ed4d7 100644 --- a/lvm2py/lvm.py +++ b/lvm2py/lvm.py @@ -14,10 +14,10 @@ #along with lvm2py. If not, see . from ctypes import cast, c_ulonglong, c_ulong -from conversion import * -from exception import * -from util import * -from vg import VolumeGroup +from .conversion import * +from .exception import * +from .util import * +from .vg import VolumeGroup import os @@ -183,18 +183,22 @@ def create_vg(self, name, devices): for device in devices: if not os.path.exists(device): self._destroy_vg(vgh) + self.close() raise ValueError("%s does not exist." % device) ext = lvm_vg_extend(vgh, device) if ext != 0: self._destroy_vg(vgh) + self.close() raise CommitError("Failed to extend Volume Group.") try: self._commit_vg(vgh) except CommitError: self._destroy_vg(vgh) + self.close() raise CommitError("Failed to add %s to VolumeGroup." % device) self._close_vg(vgh) vg = VolumeGroup(self, name) + self.close() return vg def remove_vg(self, vg): @@ -263,4 +267,4 @@ def vgscan(self): for name in vgnames: vginst = self.get_vg(name) vg_list.append(vginst) - return vg_list \ No newline at end of file + return vg_list diff --git a/lvm2py/pv.py b/lvm2py/pv.py index d995bef..dea3a13 100644 --- a/lvm2py/pv.py +++ b/lvm2py/pv.py @@ -13,9 +13,9 @@ #You should have received a copy of the GNU General Public License #along with lvm2py. If not, see . -from conversion import * -from exception import * -from util import * +from .conversion import * +from .exception import * +from .util import * # Physical volume handling should not be needed anymore. Only physical volumes # bound to a vg contain useful information. Therefore the creation, @@ -171,4 +171,4 @@ def free(self, units="MiB"): self.open() size = lvm_pv_get_free(self.handle) self.close() - return size_convert(size, units) \ No newline at end of file + return size_convert(size, units) diff --git a/lvm2py/vg.py b/lvm2py/vg.py index 336b15b..8853297 100644 --- a/lvm2py/vg.py +++ b/lvm2py/vg.py @@ -15,11 +15,11 @@ from ctypes import cast, c_ulonglong, c_ulong import os -from conversion import * -from exception import * -from util import * -from pv import PhysicalVolume -from lv import LogicalVolume +from .conversion import * +from .exception import * +from .util import * +from .pv import PhysicalVolume +from .lv import LogicalVolume class VolumeGroup(object): @@ -48,21 +48,25 @@ class VolumeGroup(object): To create a new volume group use the LVM method create_vg. """ - def __init__(self, handle, name, mode="r"): + def __init__(self, handle, name, mode=b"r"): self.__name = name self.__vgh = None + if isinstance(mode, str): + mode = mode.encode() self.__mode = mode self.__lvm = handle # verify we can open this vg in the desired mode handle.open() - vgh = lvm_vg_open(handle.handle, name, mode) - if not bool(vgh): - raise HandleError("Failed to initialize VG Handle.") - # Close the handle so we can proceed - cl = lvm_vg_close(vgh) - if cl != 0: - raise HandleError("Failed to close VG handle after init check.") - handle.close() + try: + vgh = lvm_vg_open(handle.handle, name, self.mode) + if not bool(vgh): + raise HandleError("Failed to initialize VG Handle.") + # Close the handle so we can proceed + cl = lvm_vg_close(vgh) + if cl != 0: + raise HandleError("Failed to close VG handle after init check.") + finally: + handle.close() def open(self): """ @@ -398,6 +402,7 @@ def pvscan(self): pv_list = [] pv_handles = lvm_vg_list_pvs(self.handle) if not bool(pv_handles): + self.close() return pv_list pvh = dm_list_first(pv_handles) while pvh: @@ -430,6 +435,7 @@ def lvscan(self): lv_list = [] lv_handles = lvm_vg_list_lvs(self.handle) if not bool(lv_handles): + self.close() return lv_list lvh = dm_list_first(lv_handles) while lvh: @@ -568,4 +574,4 @@ def set_extent_size(self, length, units): self._commit() self.close() if ext != 0: - raise CommitError("Failed to set extent size.") \ No newline at end of file + raise CommitError("Failed to set extent size.") diff --git a/setup.py b/setup.py index 50af13d..5da381d 100644 --- a/setup.py +++ b/setup.py @@ -8,5 +8,6 @@ author='Ruben Quinones', author_email='rq.sysadmin@gmail.com', url='http://github.com/xzased/lvm2py', + python_requires='>=3.5' packages=['lvm2py'], )