From: Loic Dachary Date: Tue, 5 Jan 2016 16:33:45 +0000 (+0100) Subject: ceph-disk: list accepts absolute dev names X-Git-Tag: v10.0.3~126^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=591d581c84cfd72d7c655ac88b0911a318b96e95;p=ceph.git ceph-disk: list accepts absolute dev names The ceph-disk list subcommand now accepts /dev/sda as well as sda. The filtering is done on the full list of devices instead of restricting the number of devices explored. Always obtaining the full list of devices makes things simpler when trying to match a dmcrypted device to the corresponding raw device. Signed-off-by: Loic Dachary --- diff --git a/src/ceph-disk b/src/ceph-disk index 1e163f175147..ad655e627d6e 100755 --- a/src/ceph-disk +++ b/src/ceph-disk @@ -1,5 +1,6 @@ #!/usr/bin/env python # +# Copyright (C) 2015 Red Hat # Copyright (C) 2014 Inktank # Copyright (C) 2014 Cloudwatt # Copyright (C) 2014 Catalyst.net Ltd @@ -23,7 +24,6 @@ import fcntl import json import logging import os -import os.path import platform import re import subprocess @@ -555,21 +555,17 @@ def get_partition_dev(dev, pnum): raise Error('partition %d for %s does not appear to exist' % (pnum, dev)) -def list_all_partitions(names): +def list_all_partitions(): """ Return a list of devices and partitions """ - if names: - names = map(lambda x: re.sub('^/dev/', '', x), names) - else: - names = os.listdir('/sys/block') + names = os.listdir('/sys/block') dev_part_list = {} for name in names: - LOG.debug("list_all_partitions: " + name) # /dev/fd0 may hang http://tracker.ceph.com/issues/6827 if re.match(r'^fd\d$', name): continue - dev_part_list[name] = list_partitions(os.path.join('/dev', name)) + dev_part_list[name] = list_partitions(get_dev_path(name)) return dev_part_list def list_partitions(dev): @@ -2680,7 +2676,7 @@ def main_deactivate_locked(args): path = args.path target_dev = None dmcrypt = False - devices = list_devices([]) + devices = list_devices() # list all devices and found we need for device in devices: @@ -3194,9 +3190,9 @@ def list_format_dev_plain(dev, devices=[], prefix=''): desc.append('mounted on %s' % dev['mount']) return '%s%s %s' % (prefix, dev['path'], ', '.join(desc)) -def list_format_plain(devices): +def list_format_plain(devices, selected_devices): lines = [] - for device in devices: + for device in selected_devices: if device.get('partitions'): lines.append('%s :' % device['path']) for p in sorted(device['partitions']): @@ -3276,8 +3272,8 @@ def list_dev(dev, uuid_map, journal_map): return info -def list_devices(path): - partmap = list_all_partitions(path) +def list_devices(): + partmap = list_all_partitions() uuid_map = {} journal_map = {} @@ -3337,11 +3333,25 @@ def list_devices(path): return devices def main_list(args): - devices = list_devices(args.path) + devices = list_devices() + if args.path: + paths = [] + for path in args.path: + if os.path.exists(path): + paths.append(os.path.realpath(path)) + else: + paths.append(path) + selected_devices = [] + for device in devices: + for path in paths: + if re.search(path + '$', device['path']): + selected_devices.append(device) + else: + selected_devices = devices if args.format == 'json': - print json.dumps(devices) + print json.dumps(selected_devices) else: - output = list_format_plain(devices) + output = list_format_plain(devices, selected_devices) if output: print output diff --git a/src/test/python/ceph-disk/tests/test_ceph_disk.py b/src/test/python/ceph-disk/tests/test_ceph_disk.py index b0c818c6d82e..1ec4ed1d81e4 100644 --- a/src/test/python/ceph-disk/tests/test_ceph_disk.py +++ b/src/test/python/ceph-disk/tests/test_ceph_disk.py @@ -243,6 +243,38 @@ class TestCephDisk(object): 'magic': ceph_disk.CEPH_OSD_ONDISK_MAGIC, 'state': 'prepared'} == desc + @patch('os.path.exists') + def test_list_paths_to_names(self, m_exists): + + def exists(path): + return path in ( + '/sys/block/sda', + '/sys/block/sdb', + '/sys/block/cciss!c0d0', + '/sys/block/cciss!c0d1', + '/sys/block/cciss!c0d2', + ) + + m_exists.side_effect = exists + paths = [ + '/dev/sda', + '/dev/cciss/c0d0', + 'cciss!c0d1', + 'cciss/c0d2', + 'sdb', + ] + expected = [ + 'sda', + 'cciss!c0d0', + 'cciss!c0d1', + 'cciss!c0d2', + 'sdb', + ] + assert expected == ceph_disk.list_paths_to_names(paths) + with pytest.raises(ceph_disk.Error) as excinfo: + ceph_disk.list_paths_to_names(['unknown']) + assert 'unknown' in excinfo.value.message + def test_list_all_partitions(self): partition_uuid = "56244cf5-83ef-4984-888a-2d8b8e0e04b2" disk = "Xda"