~james-page/charms/trusty/cinder/lp1516085-stable

71 by Liam Young
[gnuoy,trivial] Pre-release charmhelper sync
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
35.1.1 by Christopher Glass
Updating charm-helpers from lp:charm-helpers revision 153
17
import os
18
import re
23 by Adam Gandelman
Merging python-redux and havana work.
19
from stat import S_ISBLK
20
21
from subprocess import (
27.2.37 by James Page
Revert local changes, use better disk cleaning
22
    check_call,
23
    check_output,
24
    call
23 by Adam Gandelman
Merging python-redux and havana work.
25
)
26
27
28
def is_block_device(path):
29
    '''
30
    Confirm device at path is a valid block device node.
31
32
    :returns: boolean: True if path is a block device, False if not.
33
    '''
35.1.1 by Christopher Glass
Updating charm-helpers from lp:charm-helpers revision 153
34
    if not os.path.exists(path):
35
        return False
36
    return S_ISBLK(os.stat(path).st_mode)
23 by Adam Gandelman
Merging python-redux and havana work.
37
38
39
def zap_disk(block_device):
40
    '''
41
    Clear a block device of partition table. Relies on sgdisk, which is
42
    installed as pat of the 'gdisk' package in Ubuntu.
43
44
    :param block_device: str: Full path of block device to clean.
45
    '''
27.2.37 by James Page
Revert local changes, use better disk cleaning
46
    # sometimes sgdisk exits non-zero; this is OK, dd will clean up
47
    call(['sgdisk', '--zap-all', '--mbrtogpt',
48
          '--clear', block_device])
56.2.2 by Corey Bryant
Sync charm-helpers.
49
    dev_end = check_output(['blockdev', '--getsz',
50
                            block_device]).decode('UTF-8')
27.2.37 by James Page
Revert local changes, use better disk cleaning
51
    gpt_end = int(dev_end.split()[0]) - 100
35.1.1 by Christopher Glass
Updating charm-helpers from lp:charm-helpers revision 153
52
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
27.2.37 by James Page
Revert local changes, use better disk cleaning
53
                'bs=1M', 'count=1'])
35.1.1 by Christopher Glass
Updating charm-helpers from lp:charm-helpers revision 153
54
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
55
                'bs=512', 'count=100', 'seek=%s' % (gpt_end)])
56
37.1.1 by Edward Hope-Morley
[hopem,r=] synced charmhelpers to get fix for bug 1337266
57
35.1.1 by Christopher Glass
Updating charm-helpers from lp:charm-helpers revision 153
58
def is_device_mounted(device):
59
    '''Given a device path, return True if that device is mounted, and False
60
    if it isn't.
61
62
    :param device: str: Full path of the device to check.
63
    :returns: boolean: True if the path represents a mounted device, False if
64
        it doesn't.
65
    '''
40.2.1 by Hui Xiang
Support cinder for IPv6.
66
    is_partition = bool(re.search(r".*[0-9]+\b", device))
56.2.2 by Corey Bryant
Sync charm-helpers.
67
    out = check_output(['mount']).decode('UTF-8')
40.2.1 by Hui Xiang
Support cinder for IPv6.
68
    if is_partition:
69
        return bool(re.search(device + r"\b", out))
97 by james.page at ubuntu
[gnuoy] 15.07 Charm release
70
    return bool(re.search(device + r"[0-9]*\b", out))