~ben-kaehne/charms/trusty/folder-swift-sync/backup

« back to all changes in this revision

Viewing changes to lib/charmhelpers/contrib/storage/linux/lvm.py

  • Committer: Ben Kaehne
  • Date: 2016-05-03 07:59:35 UTC
  • Revision ID: ben.kaehne@canonical.com-20160503075935-3121ed9jaj1a8uyl
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014-2015 Canonical Limited.
 
2
#
 
3
# This file is part of charm-helpers.
 
4
#
 
5
# charm-helpers is free software: you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License version 3 as
 
7
# published by the Free Software Foundation.
 
8
#
 
9
# charm-helpers is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU Lesser General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU Lesser General Public License
 
15
# along with charm-helpers.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
from subprocess import (
 
18
    CalledProcessError,
 
19
    check_call,
 
20
    check_output,
 
21
    Popen,
 
22
    PIPE,
 
23
)
 
24
 
 
25
 
 
26
##################################################
 
27
# LVM helpers.
 
28
##################################################
 
29
def deactivate_lvm_volume_group(block_device):
 
30
    '''
 
31
    Deactivate any volume gruop associated with an LVM physical volume.
 
32
 
 
33
    :param block_device: str: Full path to LVM physical volume
 
34
    '''
 
35
    vg = list_lvm_volume_group(block_device)
 
36
    if vg:
 
37
        cmd = ['vgchange', '-an', vg]
 
38
        check_call(cmd)
 
39
 
 
40
 
 
41
def is_lvm_physical_volume(block_device):
 
42
    '''
 
43
    Determine whether a block device is initialized as an LVM PV.
 
44
 
 
45
    :param block_device: str: Full path of block device to inspect.
 
46
 
 
47
    :returns: boolean: True if block device is a PV, False if not.
 
48
    '''
 
49
    try:
 
50
        check_output(['pvdisplay', block_device])
 
51
        return True
 
52
    except CalledProcessError:
 
53
        return False
 
54
 
 
55
 
 
56
def remove_lvm_physical_volume(block_device):
 
57
    '''
 
58
    Remove LVM PV signatures from a given block device.
 
59
 
 
60
    :param block_device: str: Full path of block device to scrub.
 
61
    '''
 
62
    p = Popen(['pvremove', '-ff', block_device],
 
63
              stdin=PIPE)
 
64
    p.communicate(input='y\n')
 
65
 
 
66
 
 
67
def list_lvm_volume_group(block_device):
 
68
    '''
 
69
    List LVM volume group associated with a given block device.
 
70
 
 
71
    Assumes block device is a valid LVM PV.
 
72
 
 
73
    :param block_device: str: Full path of block device to inspect.
 
74
 
 
75
    :returns: str: Name of volume group associated with block device or None
 
76
    '''
 
77
    vg = None
 
78
    pvd = check_output(['pvdisplay', block_device]).splitlines()
 
79
    for l in pvd:
 
80
        l = l.decode('UTF-8')
 
81
        if l.strip().startswith('VG Name'):
 
82
            vg = ' '.join(l.strip().split()[2:])
 
83
    return vg
 
84
 
 
85
 
 
86
def create_lvm_physical_volume(block_device):
 
87
    '''
 
88
    Initialize a block device as an LVM physical volume.
 
89
 
 
90
    :param block_device: str: Full path of block device to initialize.
 
91
 
 
92
    '''
 
93
    check_call(['pvcreate', block_device])
 
94
 
 
95
 
 
96
def create_lvm_volume_group(volume_group, block_device):
 
97
    '''
 
98
    Create an LVM volume group backed by a given block device.
 
99
 
 
100
    Assumes block device has already been initialized as an LVM PV.
 
101
 
 
102
    :param volume_group: str: Name of volume group to create.
 
103
    :block_device: str: Full path of PV-initialized block device.
 
104
    '''
 
105
    check_call(['vgcreate', volume_group, block_device])