1
# Copyright 2014-2015 Canonical Limited.
3
# This file is part of charm-helpers.
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.
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.
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/>.
19
from stat import S_ISBLK
21
from subprocess import (
28
def is_block_device(path):
30
Confirm device at path is a valid block device node.
32
:returns: boolean: True if path is a block device, False if not.
34
if not os.path.exists(path):
36
return S_ISBLK(os.stat(path).st_mode)
39
def zap_disk(block_device):
41
Clear a block device of partition table. Relies on sgdisk, which is
42
installed as pat of the 'gdisk' package in Ubuntu.
44
:param block_device: str: Full path of block device to clean.
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),
55
check_call(['dd', 'if=/dev/zero', 'of=%s' % (block_device),
56
'bs=512', 'count=100', 'seek=%s' % (gpt_end)])
59
def is_device_mounted(device):
60
'''Given a device path, return True if that device is mounted, and False
63
:param device: str: Full path of the device to check.
64
:returns: boolean: True if the path represents a mounted device, False if
67
is_partition = bool(re.search(r".*[0-9]+\b", device))
68
out = check_output(['mount']).decode('UTF-8')
70
return bool(re.search(device + r"\b", out))
71
return bool(re.search(device + r"[0-9]*\b", out))