]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk: refactor list_[all_]partitions
authorSage Weil <sage@inktank.com>
Wed, 3 Jul 2013 17:55:36 +0000 (10:55 -0700)
committerSage Weil <sage@inktank.com>
Tue, 16 Jul 2013 22:51:44 +0000 (15:51 -0700)
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>
src/ceph-disk

index 3f7f9d53bac66e27385748859044c830a6c3ff32..4ad4ccc5d9a1c70243b69d61ce5a0720becc5ca5 100755 (executable)
@@ -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)