~openstack-charmers-archive/charms/trusty/nova-cloud-controller/old-stable

« 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:03:01 UTC
  • mfrom: (75.1.1 nova-cloud-controller)
  • Revision ID: james.page@ubuntu.com-20140521100301-wmm536lpzw9ouam7
[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 (
14
15
 
15
16
    :returns: boolean: True if path is a block device, False if not.
16
17
    '''
17
 
    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)
18
21
 
19
22
 
20
23
def zap_disk(block_device):
29
32
          '--clear', block_device])
30
33
    dev_end = check_output(['blockdev', '--getsz', block_device])
31
34
    gpt_end = int(dev_end.split()[0]) - 100
32
 
    check_call(['dd', 'if=/dev/zero', 'of=%s'%(block_device),
 
35
    check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
33
36
                'bs=1M', 'count=1'])
34
 
    check_call(['dd', 'if=/dev/zero', 'of=%s'%(block_device),
35
 
                'bs=512', 'count=100', 'seek=%s'%(gpt_end)])
 
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))