~brad-marshall/charms/trusty/apache2-wsgi/fix-haproxy-relations

« back to all changes in this revision

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

  • Committer: Robin Winslow
  • Date: 2014-05-27 14:00:44 UTC
  • Revision ID: robin.winslow@canonical.com-20140527140044-8rpmb3wx4djzwa83
Add all files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from os import stat
 
2
from stat import S_ISBLK
 
3
 
 
4
from subprocess import (
 
5
    check_call,
 
6
    check_output,
 
7
    call
 
8
)
 
9
 
 
10
 
 
11
def is_block_device(path):
 
12
    '''
 
13
    Confirm device at path is a valid block device node.
 
14
 
 
15
    :returns: boolean: True if path is a block device, False if not.
 
16
    '''
 
17
    return S_ISBLK(stat(path).st_mode)
 
18
 
 
19
 
 
20
def zap_disk(block_device):
 
21
    '''
 
22
    Clear a block device of partition table. Relies on sgdisk, which is
 
23
    installed as pat of the 'gdisk' package in Ubuntu.
 
24
 
 
25
    :param block_device: str: Full path of block device to clean.
 
26
    '''
 
27
    # sometimes sgdisk exits non-zero; this is OK, dd will clean up
 
28
    call(['sgdisk', '--zap-all', '--mbrtogpt',
 
29
          '--clear', block_device])
 
30
    dev_end = check_output(['blockdev', '--getsz', block_device])
 
31
    gpt_end = int(dev_end.split()[0]) - 100
 
32
    check_call(['dd', 'if=/dev/zero', 'of=%s'%(block_device),
 
33
                'bs=1M', 'count=1'])
 
34
    check_call(['dd', 'if=/dev/zero', 'of=%s'%(block_device),
 
35
                'bs=512', 'count=100', 'seek=%s'%(gpt_end)])