summaryrefslogtreecommitdiff
path: root/python/example.py
blob: 67bb7e4206cdbc6a4d4b2aaa63143d48fd0ace79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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()