]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk: list accepts absolute dev names 6880/head
authorLoic Dachary <ldachary@redhat.com>
Tue, 5 Jan 2016 16:33:45 +0000 (17:33 +0100)
committerLoic Dachary <ldachary@redhat.com>
Thu, 7 Jan 2016 14:33:18 +0000 (15:33 +0100)
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 <loic@dachary.org>
(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).

src/ceph-disk
src/test/python/ceph-disk/tests/test_ceph_disk.py

index 71531f22ac98a0d530b8cf418a852d00e5c65337..902effffa22ee040a4ba115cac33b3e38383e981 100755 (executable)
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 #
+# Copyright (C) 2015 Red Hat <contact@redhat.com>
 # Copyright (C) 2014 Inktank <info@inktank.com>
 # Copyright (C) 2014 Cloudwatt <libre.licensing@cloudwatt.com>
 # 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
 
index a150dd324104000c67ded688a6995fba5bffff1e..2d1d612fbc9150a779d19d05cbafd650fe487681 100644 (file)
@@ -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"