]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk: list accepts absolute dev names
authorLoic Dachary <ldachary@redhat.com>
Tue, 5 Jan 2016 16:33:45 +0000 (17:33 +0100)
committerLoic Dachary <ldachary@redhat.com>
Wed, 6 Jan 2016 10:13:16 +0000 (11:13 +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>
src/ceph-disk
src/test/python/ceph-disk/tests/test_ceph_disk.py

index 1e163f175147dd9102b0e06cb79de7ff29146252..ad655e627d6ee4f806e6561293dbef03e2fadbff 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
@@ -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
 
index b0c818c6d82eff3718dbab950b7a26f74e7a567f..1ec4ed1d81e42f3bd0cab67713bb5920143d2a16 100644 (file)
@@ -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"