diff options
author | Sage Weil <sage@inktank.com> | 2013-07-03 10:55:36 -0700 |
---|---|---|
committer | Sage Weil <sage@inktank.com> | 2013-07-16 15:51:44 -0700 |
commit | 35d3f2d84808efda3d2ac868afe03e6959d51c03 (patch) | |
tree | d97dd2b3f8a44f2784fca2054672d643510579e8 | |
parent | e0401591e352ea9653e3276d66aebeb41801eeb3 (diff) | |
download | ceph-35d3f2d84808efda3d2ac868afe03e6959d51c03.tar.gz |
ceph-disk: refactor list_[all_]partitions
Make these methods work in terms of device *names*, not paths, and fix up
the only direct list_partitions() caller to do the same.
Signed-off-by: Sage Weil <sage@inktank.com>
-rwxr-xr-x | src/ceph-disk | 51 |
1 files changed, 19 insertions, 32 deletions
diff --git a/src/ceph-disk b/src/ceph-disk index 3f7f9d53bac..4ad4ccc5d9a 100755 --- a/src/ceph-disk +++ b/src/ceph-disk @@ -201,28 +201,6 @@ def maybe_mkdir(*a, **kw): # a device "name" is something like # sdb # cciss!c0d1 -def list_all_partitions(): - """ - Return a list of devices and partitions - """ - dev_part_list = {} - for name in os.listdir('/dev/disk/by-path'): - target = os.readlink(os.path.join('/dev/disk/by-path', name)) - dev = target.split('/')[-1] - #print "name %s target %s dev %s" % (name, target, dev) - (baser) = re.search('(.*)-part\d+$', name) - if baser is not None: - basename = baser.group(1) - #print 'basename %s' % basename - base = os.readlink(os.path.join('/dev/disk/by-path', basename)).split('/')[-1] - if base not in dev_part_list: - dev_part_list[base] = [] - dev_part_list[base].append(dev) - else: - if dev not in dev_part_list: - dev_part_list[dev] = [] - return dev_part_list - def get_dev_name(path): """ get device name from path. e.g., /dev/sda -> sdas, /dev/cciss/c0d1 -> cciss!c0d1 @@ -246,18 +224,25 @@ def get_dev_relpath(name): """ return name.replace('!', '/') -def list_partitions(disk): +def list_all_partitions(): """ - Return a list of partitions on the given device + Return a list of devices and partitions + """ + dev_part_list = {} + for name in os.listdir('/sys/block'): + if not os.path.exists(os.path.join('/sys/block', name, 'device')): + continue + dev_part_list[name] = list_partitions(name) + return dev_part_list + +def list_partitions(basename): + """ + Return a list of partitions on the given device name """ - disk = os.path.realpath(disk) - assert not is_partition(disk) - assert disk.startswith('/dev/') - base = disk.split('/')[-1] partitions = [] - for name in os.listdir(os.path.join('/sys/block', base)): - if name.startswith(base): - partitions.append('/dev/' + name) + for name in os.listdir(os.path.join('/sys/block', basename)): + if name.startswith(basename): + partitions.append(name) return partitions @@ -345,7 +330,9 @@ def verify_not_in_use(dev): if holders: raise Error('Device is in use by a device-mapper mapping (dm-crypt?)' % dev, ','.join(holders)) else: - for partition in list_partitions(dev): + basename = get_dev_name(os.path.realpath(dev)) + for partname in list_partitions(basename): + partition = get_dev_path(partname) if is_mounted(partition): raise Error('Device is mounted', partition) holders = is_held(partition) |