From: Loic Dachary Date: Tue, 5 Jan 2016 16:33:45 +0000 (+0100) Subject: ceph-disk: list accepts absolute dev names X-Git-Tag: v9.2.1~32^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=399b7c5409d9801aa7f38533dd8b7c19c726a594;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 (cherry picked from commit 591d581c84cfd72d7c655ac88b0911a318b96e95) Conflicts: src/ceph-disk: as part of the implementation of deactivate / destroy in master, the prototype of list_device was changed to take a list of paths instead of the all arguments (args). --- diff --git a/src/ceph-disk b/src/ceph-disk index 71531f22ac98..902effffa22e 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 @@ -529,21 +529,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): @@ -2772,9 +2768,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']): @@ -2854,8 +2850,8 @@ def list_dev(dev, uuid_map, journal_map): return info -def list_devices(args): - partmap = list_all_partitions(args.path) +def list_devices(): + partmap = list_all_partitions() uuid_map = {} journal_map = {} @@ -2915,11 +2911,25 @@ def list_devices(args): return devices def main_list(args): - devices = list_devices(args) + 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 a150dd324104..2d1d612fbc91 100644 --- a/src/test/python/ceph-disk/tests/test_ceph_disk.py +++ b/src/test/python/ceph-disk/tests/test_ceph_disk.py @@ -235,6 +235,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"