1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-07-30 23:41:55 +03:00

python: Add bindings for liblvm2app.

Use configure --enable-python_bindings to generate them.

Note that the Makefiles do not yet control the owner or permissions of
the two new files on installation.
This commit is contained in:
Alasdair G Kergon
2012-10-12 02:08:47 +01:00
parent 4022fd3e79
commit ee3cfa4184
10 changed files with 2175 additions and 15 deletions

39
python/Makefile.in Normal file
View File

@ -0,0 +1,39 @@
#
# Copyright (C) 2011-2012 Red Hat, Inc.
#
# This file is part of LVM2.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU Lesser General Public License v.2.1.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
python_bindings: .liblvm_built
.liblvm_built: liblvm_python.c
$(PYTHON) setup.py build
touch $@
liblvm_python.c:
$(LN_S) $(srcdir)/liblvm.c $@
include $(top_builddir)/make.tmpl
install_python_bindings: python_bindings
$(PYTHON) setup.py install --skip-build --root $(rootdir)
install_lvm2: install_python_bindings
install: install_lvm2
CLEAN_TARGETS += .liblvm_built liblvm_python.c
DISTCLEAN_DIRS += build
DISTCLEAN_TARGETS += setup.py

125
python/example.py Normal file
View File

@ -0,0 +1,125 @@
#
# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
#
# This file is part of LVM2.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#-----------------------------
# Python example code:
#-----------------------------
import lvm
# Note: This example will create a logical unit, tag it and
# delete it, don't run this on production box!
#Dump information about PV
def print_pv(pv):
print 'PV name: ', pv.getName(), ' ID: ', pv.getUuid(), 'Size: ', pv.getSize()
#Dump some information about a specific volume group
def print_vg(h, vg_name):
#Open read only
vg = h.vgOpen(vg_name, 'r')
print 'Volume group:', vg_name, 'Size: ', vg.getSize()
#Retrieve a list of Physical volumes for this volume group
pv_list = vg.listPVs()
#Print out the physical volumes
for p in pv_list:
print_pv(p)
#Get a list of logical volumes in this volume group
lv_list = vg.listLVs()
if len(lv_list):
for l in lv_list:
print 'LV name: ', l.getName(), ' ID: ', l.getUuid()
else:
print 'No logical volumes present!'
vg.close()
#Returns the name of a vg with space available
def find_vg_with_free_space(h):
free_space = 0
rc = None
vg_names = l.listVgNames()
for v in vg_names:
vg = h.vgOpen(v, 'r')
c_free = vg.getFreeSize()
if c_free > free_space:
free_space = c_free
rc = v
vg.close()
return rc
#Walk through the volume groups and fine one with space in which we can
#create a new logical volume
def create_delete_logical_volume(h):
vg_name = find_vg_with_free_space(h)
print 'Using volume group ', vg_name, ' for example'
if vg_name:
vg = h.vgOpen(vg_name, 'w')
lv = vg.createLvLinear('python_lvm_ok_to_delete', vg.getFreeSize())
if lv:
print 'New lv, id= ', lv.getUuid()
#Create a tag
lv.addTag('Demo_tag')
#Get the tags
tags = lv.getTags()
for t in tags:
#Remove tag
lv.removeTag(t)
#Try to rename
lv.rename("python_lvm_ok_to_be_removed_shortly")
print 'LV name= ', lv.getName()
lv.deactivate()
lv.remove()
vg.close()
else:
print 'No free space available to create demo lv!'
if __name__ == '__main__':
#Create a new LVM instance
l = lvm.Liblvm()
#What version
print 'lvm version=', l.getVersion()
#Get a list of volume group names
vg_names = l.listVgNames()
#For each volume group display some information about each of them
for vg_i in vg_names:
print_vg(l, vg_i)
#Demo creating a logical volume
create_delete_logical_volume(l)
#Close
l.close()

1711
python/liblvm.c Normal file

File diff suppressed because it is too large Load Diff

35
python/setup.py.in Normal file
View File

@ -0,0 +1,35 @@
#
# Copyright (C) 2012 Red Hat, Inc. All rights reserved.
#
# This file is part of LVM2.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from distutils.core import setup, Extension
liblvm = Extension('lvm',
sources = ['liblvm_python.c'],
libraries= ['lvm2app'],
library_dirs= ['@top_builddir@/liblvm'],
include_dirs= ['@top_builddir@/include'])
setup (name='lvm',
version=@LVM_VERSION@,
description='Python bindings for liblvm2',
license="LGPLv2+",
maintainer='LVM2 maintainers',
maintainer_email='linux-lvm@redhat.com',
url='http://sourceware.org/lvm2/',
ext_modules=[liblvm],
)