Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mimic Additional work on ceph-volume to add some choose_disk capabilities #24782

Merged
merged 6 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/ceph-volume/ceph_volume/tests/util/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,30 @@ def test_is_member_blkid(self, device_info, label):

assert disk.is_member is True

def test_reject_removable_device(self, device_info):
data = {"/dev/sdb": {"removable": 1}}
device_info(devices=data)
disk = device.Device("/dev/sdb")
assert not disk.is_valid

def test_accept_non_removable_device(self, device_info):
data = {"/dev/sdb": {"removable": 0}}
device_info(devices=data)
disk = device.Device("/dev/sdb")
assert disk.is_valid

def test_reject_readonly_device(self, device_info):
data = {"/dev/cdrom": {"ro": 1}}
device_info(devices=data)
disk = device.Device("/dev/cdrom")
assert not disk.is_valid

def test_accept_non_readonly_device(self, device_info):
data = {"/dev/sda": {"ro": 0}}
device_info(devices=data)
disk = device.Device("/dev/sda")
assert disk.is_valid

@pytest.mark.parametrize("label", ceph_partlabels)
def test_is_member_lsblk(self, device_info, label):
lsblk = {"PARTLABEL": label}
Expand Down
13 changes: 0 additions & 13 deletions src/ceph-volume/ceph_volume/tests/util/test_disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,6 @@ def test_sda_block_is_found(self, tmpfile, tmpdir):
assert result[dev_sda_path]['model'] == ''
assert result[dev_sda_path]['partitions'] == {}

def test_sda_is_removable_gets_skipped(self, tmpfile, tmpdir):
block_path, dev_path, mapper_path = self.setup_paths(tmpdir)
dev_sda_path = os.path.join(dev_path, 'sda')
block_sda_path = os.path.join(block_path, 'sda')
os.makedirs(block_sda_path)
os.makedirs(dev_sda_path)

tmpfile('removable', contents='1', directory=block_sda_path)
result = disk.get_devices(
_sys_block_path=block_path,
_dev_path=dev_path,
_mapper_path=mapper_path)
assert result == {}

def test_dm_device_is_not_used(self, monkeypatch, tmpdir):
# the link to the mapper is used instead
Expand Down
19 changes: 19 additions & 0 deletions src/ceph-volume/ceph_volume/util/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def __init__(self, path):
self.sys_api = {}
self._exists = None
self._is_lvm_member = None
self._valid = False
self._rejected_reasons = []
self._parse()
self.is_valid

def _parse(self):
# start with lvm since it can use an absolute or relative path
Expand Down Expand Up @@ -130,6 +133,22 @@ def used_by_ceph(self):
return any(osd_ids)


@property
def is_valid(self):
def reject_device(item, value, reason):
try:
if self.sys_api[item] == value:
self._rejected_reasons.append(reason)
except KeyError:
pass
reject_device('removable', 1, 'removable')
reject_device('ro', 1, 'read-only')
reject_device('locked', 1, 'locked')

self._valid = len(self._rejected_reasons) == 0
return self._valid


class CephDiskDevice(object):
"""
Detect devices that have been created by ceph-disk, report their type
Expand Down
35 changes: 30 additions & 5 deletions src/ceph-volume/ceph_volume/util/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,28 @@ def is_mapper_device(device_name):
return device_name.startswith(('/dev/mapper', '/dev/dm-'))


def is_locked_raw_device(disk_path):
"""
A device can be locked by a third party software like a database.
To detect that case, the device is opened in Read/Write and exclusive mode
"""
open_flags = (os.O_RDWR | os.O_EXCL)
open_mode = 0
fd = None

try:
fd = os.open(disk_path, open_flags, open_mode)
except OSError:
return 1

try:
os.close(fd)
except OSError:
return 1

return 0


def get_devices(_sys_block_path='/sys/block', _dev_path='/dev', _mapper_path='/dev/mapper'):
"""
Captures all available devices from /sys/block/, including its partitions,
Expand Down Expand Up @@ -694,12 +716,12 @@ def get_devices(_sys_block_path='/sys/block', _dev_path='/dev', _mapper_path='/d
if lvm.is_lv(diskname):
continue

# If the device reports itself as 'removable', get it excluded
metadata['removable'] = get_file_contents(os.path.join(sysdir, 'removable'))
if metadata['removable'] == '1':
continue
# Is the device read-only ?
metadata['ro'] = get_file_contents(os.path.join(sysdir, 'ro'))


for key in ['vendor', 'model', 'sas_address', 'sas_device_handle']:
for key in ['vendor', 'model', 'rev', 'sas_address', 'sas_device_handle']:
metadata[key] = get_file_contents(sysdir + "/device/" + key)

for key in ['sectors', 'size']:
Expand All @@ -710,7 +732,9 @@ def get_devices(_sys_block_path='/sys/block', _dev_path='/dev', _mapper_path='/d

metadata['partitions'] = get_partitions_facts(sysdir)

metadata['rotational'] = get_file_contents(sysdir + "/queue/rotational")
for key in ['rotational', 'nr_requests']:
metadata[key] = get_file_contents(sysdir + "/queue/" + key)

metadata['scheduler_mode'] = ""
scheduler = get_file_contents(sysdir + "/queue/scheduler")
if scheduler is not None:
Expand All @@ -727,6 +751,7 @@ def get_devices(_sys_block_path='/sys/block', _dev_path='/dev', _mapper_path='/d
metadata['human_readable_size'] = human_readable_size(float(size) * 512)
metadata['size'] = float(size) * 512
metadata['path'] = diskname
metadata['locked'] = is_locked_raw_device(metadata['path'])

device_facts[diskname] = metadata
return device_facts