~junaidali/charms/trusty/plumgrid-director/pg-restart

« back to all changes in this revision

Viewing changes to hooks/charmhelpers-pl/contrib/storage/linux/utils.py

  • Committer: bbaqar at plumgrid
  • Date: 2015-07-29 18:07:31 UTC
  • Revision ID: bbaqar@plumgrid.com-20150729180731-ioynar8x3u5pxytc
Addressed reviews by Charmers

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
 
import os
18
 
import re
19
 
from stat import S_ISBLK
20
 
 
21
 
from subprocess import (
22
 
    check_call,
23
 
    check_output,
24
 
    call
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
 
    '''
34
 
    if not os.path.exists(path):
35
 
        return False
36
 
    return S_ISBLK(os.stat(path).st_mode)
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
 
    '''
46
 
    # sometimes sgdisk exits non-zero; this is OK, dd will clean up
47
 
    call(['sgdisk', '--zap-all', '--mbrtogpt',
48
 
          '--clear', block_device])
49
 
    dev_end = check_output(['blockdev', '--getsz',
50
 
                            block_device]).decode('UTF-8')
51
 
    gpt_end = int(dev_end.split()[0]) - 100
52
 
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
53
 
                'bs=1M', 'count=1'])
54
 
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
55
 
                'bs=512', 'count=100', 'seek=%s' % (gpt_end)])
56
 
 
57
 
 
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
 
    '''
66
 
    is_partition = bool(re.search(r".*[0-9]+\b", device))
67
 
    out = check_output(['mount']).decode('UTF-8')
68
 
    if is_partition:
69
 
        return bool(re.search(device + r"\b", out))
70
 
    return bool(re.search(device + r"[0-9]+\b", out))