2012-10-12 05:08:47 +04:00
#
# 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 ) :
2014-09-02 17:42:01 +04:00
print ( ' PV name: ' , pv . getName ( ) , ' ID: ' , pv . getUuid ( ) , ' Size: ' , pv . getSize ( ) )
2012-10-12 05:08:47 +04:00
#Dump some information about a specific volume group
2012-10-16 00:54:19 +04:00
def print_vg ( vg_name ) :
2012-10-12 05:08:47 +04:00
#Open read only
2012-10-16 00:54:19 +04:00
vg = lvm . vgOpen ( vg_name , ' r ' )
2012-10-12 05:08:47 +04:00
2014-09-02 17:42:01 +04:00
print ( ' Volume group: ' , vg_name , ' Size: ' , vg . getSize ( ) )
2012-10-12 05:08:47 +04:00
#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 :
2014-09-02 17:42:01 +04:00
print ( ' LV name: ' , l . getName ( ) , ' ID: ' , l . getUuid ( ) )
2012-10-12 05:08:47 +04:00
else :
2014-09-02 17:42:01 +04:00
print ( ' No logical volumes present! ' )
2012-10-12 05:08:47 +04:00
vg . close ( )
#Returns the name of a vg with space available
2012-10-16 00:54:19 +04:00
def find_vg_with_free_space ( ) :
2012-10-12 05:08:47 +04:00
free_space = 0
rc = None
2012-10-16 00:54:19 +04:00
vg_names = lvm . listVgNames ( )
2012-10-12 05:08:47 +04:00
for v in vg_names :
2012-10-16 00:54:19 +04:00
vg = lvm . vgOpen ( v , ' r ' )
2012-10-12 05:08:47 +04:00
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
2012-10-16 00:54:19 +04:00
def create_delete_logical_volume ( ) :
vg_name = find_vg_with_free_space ( )
2012-10-12 05:08:47 +04:00
2014-09-02 17:42:01 +04:00
print ( ' Using volume group ' , vg_name , ' for example ' )
2012-10-12 05:08:47 +04:00
if vg_name :
2012-10-16 00:54:19 +04:00
vg = lvm . vgOpen ( vg_name , ' w ' )
2012-10-12 05:08:47 +04:00
lv = vg . createLvLinear ( ' python_lvm_ok_to_delete ' , vg . getFreeSize ( ) )
if lv :
2014-09-02 17:42:01 +04:00
print ( ' New lv, id= ' , lv . getUuid ( ) )
2012-10-12 05:08:47 +04:00
#Create a tag
lv . addTag ( ' Demo_tag ' )
#Get the tags
tags = lv . getTags ( )
for t in tags :
#Remove tag
lv . removeTag ( t )
2012-10-16 00:54:19 +04:00
lv . deactivate ( )
2012-10-12 05:08:47 +04:00
#Try to rename
2012-10-16 00:54:19 +04:00
lv . rename ( " python_lvm_renamed " )
2014-09-02 17:42:01 +04:00
print ( ' LV name= ' , lv . getName ( ) )
2012-10-12 05:08:47 +04:00
lv . remove ( )
vg . close ( )
else :
2014-09-02 17:42:01 +04:00
print ( ' No free space available to create demo lv! ' )
2012-10-12 05:08:47 +04:00
if __name__ == ' __main__ ' :
#What version
2014-09-02 17:42:01 +04:00
print ( ' lvm version= ' , lvm . getVersion ( ) )
2012-10-12 05:08:47 +04:00
#Get a list of volume group names
2012-10-16 00:54:19 +04:00
vg_names = lvm . listVgNames ( )
2012-10-12 05:08:47 +04:00
#For each volume group display some information about each of them
for vg_i in vg_names :
2012-10-16 00:54:19 +04:00
print_vg ( vg_i )
2012-10-12 05:08:47 +04:00
#Demo creating a logical volume
2012-10-16 00:54:19 +04:00
create_delete_logical_volume ( )
2012-10-12 05:08:47 +04:00