~james-page/charms/trusty/ceph-radosgw/embedded-webserver

« back to all changes in this revision

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

  • Committer: james.page at ubuntu
  • Date: 2014-05-21 10:00:22 UTC
  • mfrom: (18.1.1 ceph-radosgw)
  • Revision ID: james.page@ubuntu.com-20140521100022-2ry1kgp8henahpa8
[tribaal,r=james-page,t=james-page]

Resync helpers to pickup fixes for apt lock races and better block device detection and handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from os import stat
 
1
import os
 
2
import re
2
3
from stat import S_ISBLK
3
4
 
4
5
from subprocess import (
5
 
    check_call
 
6
    check_call,
 
7
    check_output,
 
8
    call
6
9
)
7
10
 
8
11
 
12
15
 
13
16
    :returns: boolean: True if path is a block device, False if not.
14
17
    '''
15
 
    return S_ISBLK(stat(path).st_mode)
 
18
    if not os.path.exists(path):
 
19
        return False
 
20
    return S_ISBLK(os.stat(path).st_mode)
16
21
 
17
22
 
18
23
def zap_disk(block_device):
22
27
 
23
28
    :param block_device: str: Full path of block device to clean.
24
29
    '''
25
 
    check_call(['sgdisk', '--zap-all', '--clear',
26
 
                '--mbrtogpt', block_device])
 
30
    # sometimes sgdisk exits non-zero; this is OK, dd will clean up
 
31
    call(['sgdisk', '--zap-all', '--mbrtogpt',
 
32
          '--clear', block_device])
 
33
    dev_end = check_output(['blockdev', '--getsz', block_device])
 
34
    gpt_end = int(dev_end.split()[0]) - 100
 
35
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
 
36
                'bs=1M', 'count=1'])
 
37
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
 
38
                'bs=512', 'count=100', 'seek=%s' % (gpt_end)])
 
39
 
 
40
def is_device_mounted(device):
 
41
    '''Given a device path, return True if that device is mounted, and False
 
42
    if it isn't.
 
43
 
 
44
    :param device: str: Full path of the device to check.
 
45
    :returns: boolean: True if the path represents a mounted device, False if
 
46
        it doesn't.
 
47
    '''
 
48
    out = check_output(['mount'])
 
49
    return bool(re.search(device + r"[0-9]+\b", out))