#!/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
import json
import logging
import os
-import os.path
import platform
import re
import subprocess
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):
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:
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']):
return info
-def list_devices(path):
- partmap = list_all_partitions(path)
+def list_devices():
+ partmap = list_all_partitions()
uuid_map = {}
journal_map = {}
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
'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"