Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=========
Expand Down
9 changes: 5 additions & 4 deletions lvm2py/lv.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#You should have received a copy of the GNU General Public License
#along with lvm2py. If not, see <http://www.gnu.org/licenses/>.

from conversion import *
from exception import *
from util import *
from .conversion import *
from .exception import *
from .util import *


class LogicalVolume(object):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -179,4 +180,4 @@ def deactivate(self):
d = lvm_lv_deactivate(self.handle)
self.close()
if d != 0:
raise CommitError("Failed to deactivate LV.")
raise CommitError("Failed to deactivate LV.")
14 changes: 9 additions & 5 deletions lvm2py/lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
#along with lvm2py. If not, see <http://www.gnu.org/licenses/>.

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


Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -263,4 +267,4 @@ def vgscan(self):
for name in vgnames:
vginst = self.get_vg(name)
vg_list.append(vginst)
return vg_list
return vg_list
8 changes: 4 additions & 4 deletions lvm2py/pv.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#You should have received a copy of the GNU General Public License
#along with lvm2py. If not, see <http://www.gnu.org/licenses/>.

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,
Expand Down Expand Up @@ -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)
return size_convert(size, units)
36 changes: 21 additions & 15 deletions lvm2py/vg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.")
raise CommitError("Failed to set extent size.")
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
)