~robert-ayres/charms/trusty/contrail-configuration/trunk

« back to all changes in this revision

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

  • Committer: Robert Ayres
  • Date: 2014-09-10 14:03:02 UTC
  • Revision ID: robert.ayres@canonical.com-20140910140302-bqu0wb61an4nhgfa
Initial charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from subprocess import (
 
2
    CalledProcessError,
 
3
    check_call,
 
4
    check_output,
 
5
    Popen,
 
6
    PIPE,
 
7
)
 
8
 
 
9
 
 
10
##################################################
 
11
# LVM helpers.
 
12
##################################################
 
13
def deactivate_lvm_volume_group(block_device):
 
14
    '''
 
15
    Deactivate any volume gruop associated with an LVM physical volume.
 
16
 
 
17
    :param block_device: str: Full path to LVM physical volume
 
18
    '''
 
19
    vg = list_lvm_volume_group(block_device)
 
20
    if vg:
 
21
        cmd = ['vgchange', '-an', vg]
 
22
        check_call(cmd)
 
23
 
 
24
 
 
25
def is_lvm_physical_volume(block_device):
 
26
    '''
 
27
    Determine whether a block device is initialized as an LVM PV.
 
28
 
 
29
    :param block_device: str: Full path of block device to inspect.
 
30
 
 
31
    :returns: boolean: True if block device is a PV, False if not.
 
32
    '''
 
33
    try:
 
34
        check_output(['pvdisplay', block_device])
 
35
        return True
 
36
    except CalledProcessError:
 
37
        return False
 
38
 
 
39
 
 
40
def remove_lvm_physical_volume(block_device):
 
41
    '''
 
42
    Remove LVM PV signatures from a given block device.
 
43
 
 
44
    :param block_device: str: Full path of block device to scrub.
 
45
    '''
 
46
    p = Popen(['pvremove', '-ff', block_device],
 
47
              stdin=PIPE)
 
48
    p.communicate(input='y\n')
 
49
 
 
50
 
 
51
def list_lvm_volume_group(block_device):
 
52
    '''
 
53
    List LVM volume group associated with a given block device.
 
54
 
 
55
    Assumes block device is a valid LVM PV.
 
56
 
 
57
    :param block_device: str: Full path of block device to inspect.
 
58
 
 
59
    :returns: str: Name of volume group associated with block device or None
 
60
    '''
 
61
    vg = None
 
62
    pvd = check_output(['pvdisplay', block_device]).splitlines()
 
63
    for l in pvd:
 
64
        if l.strip().startswith('VG Name'):
 
65
            vg = ' '.join(l.strip().split()[2:])
 
66
    return vg
 
67
 
 
68
 
 
69
def create_lvm_physical_volume(block_device):
 
70
    '''
 
71
    Initialize a block device as an LVM physical volume.
 
72
 
 
73
    :param block_device: str: Full path of block device to initialize.
 
74
 
 
75
    '''
 
76
    check_call(['pvcreate', block_device])
 
77
 
 
78
 
 
79
def create_lvm_volume_group(volume_group, block_device):
 
80
    '''
 
81
    Create an LVM volume group backed by a given block device.
 
82
 
 
83
    Assumes block device has already been initialized as an LVM PV.
 
84
 
 
85
    :param volume_group: str: Name of volume group to create.
 
86
    :block_device: str: Full path of PV-initialized block device.
 
87
    '''
 
88
    check_call(['vgcreate', volume_group, block_device])