~james-page/charms/trusty/swift-proxy/trunk

« back to all changes in this revision

Viewing changes to charmhelpers/contrib/storage/linux/utils.py

  • Committer: James Page
  • Date: 2016-01-07 11:47:03 UTC
  • mfrom: (130.1.2 stable.remote)
  • Revision ID: james.page@ubuntu.com-20160107114703-hajxe7wjt4ooeu19
Resync helpers

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
    # https://github.com/ceph/ceph/commit/fdd7f8d83afa25c4e09aaedd90ab93f3b64a677b
 
47
    # sometimes sgdisk exits non-zero; this is OK, dd will clean up
 
48
    call(['sgdisk', '--zap-all', '--', block_device])
 
49
    call(['sgdisk', '--clear', '--mbrtogpt', '--', block_device])
 
50
    dev_end = check_output(['blockdev', '--getsz',
 
51
                            block_device]).decode('UTF-8')
 
52
    gpt_end = int(dev_end.split()[0]) - 100
 
53
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
 
54
                'bs=1M', 'count=1'])
 
55
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
 
56
                'bs=512', 'count=100', 'seek=%s' % (gpt_end)])
 
57
 
 
58
 
 
59
def is_device_mounted(device):
 
60
    '''Given a device path, return True if that device is mounted, and False
 
61
    if it isn't.
 
62
 
 
63
    :param device: str: Full path of the device to check.
 
64
    :returns: boolean: True if the path represents a mounted device, False if
 
65
        it doesn't.
 
66
    '''
 
67
    is_partition = bool(re.search(r".*[0-9]+\b", device))
 
68
    out = check_output(['mount']).decode('UTF-8')
 
69
    if is_partition:
 
70
        return bool(re.search(device + r"\b", out))
 
71
    return bool(re.search(device + r"[0-9]*\b", out))