Currently, function sorted inside the python was not suitable for
storage, for example, if ls -1d /dev/sd* gets result like this
after split('\n'):
['/dev/sda', '/dev/sda1', '/dev/sda2', '/dev/sda3',
 '/dev/sdaa', '/dev/sdab','/dev/sdac', '/dev/sdad', '/dev/sdae',
 '/dev/sdaf', '/dev/sdag', '/dev/sdah', '/dev/sdai', '/dev/sdaj',
 '/dev/sdak', '/dev/sdal', '/dev/sdam', '/dev/sdan', '/dev/sdao',
 '/dev/sdap', '/dev/sdaq', '/dev/sdar', '/dev/sdas', '/dev/sdb',
 '/dev/sdb1', '/dev/sdb2', '/dev/sdb3', '/dev/sdc', '/dev/sdd',
 '/dev/sde', '/dev/sdf', '/dev/sdg', '/dev/sdg1', '/dev/sdg2',
 '/dev/sdg3', '/dev/sdh', '/dev/sdh1', '/dev/sdh2', '/dev/sdh3',
 '/dev/sdi', '/dev/sdj', '/dev/sdk', '/dev/sdl', '/dev/sdm',
 '/dev/sdn', '/dev/sdo', '/dev/sdp', '/dev/sdq', '/dev/sdr',
 '/dev/sds', '/dev/sdt', '/dev/sdu', '/dev/sdv', '/dev/sdw',
 '/dev/sdx', '/dev/sdy', '/dev/sdz']

it will make no any change if just use sorted. As the default,
it will choose last 9 disk:
['/dev/sdr', '/dev/sds', '/dev/sdt', '/dev/sdu', '/dev/sdv', '/dev/sdw',
 '/dev/sdx', '/dev/sdy', '/dev/sdz']

Sometimes this is very dangerous, for lots of them may be the disk contains
lots of important data. Actually, maybe you just use scsi_debug just create:
['/dev/sdak','/dev/sdal', '/dev/sdam', '/dev/sdan', '/dev/sdao',
 '/dev/sdap', '/dev/sdaq', '/dev/sdar', '/dev/sdas']

But select the wrong disks as mentioned above.

To solve this problem, add param cmp to sorted and get the right order.

Signed-off-by: Mike Qiu <[email protected]>
---
 virttest/storage.py | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/virttest/storage.py b/virttest/storage.py
index 35958e6..dfbda5d 100644
--- a/virttest/storage.py
+++ b/virttest/storage.py
@@ -67,13 +67,35 @@ def get_image_filename(params, root_dir):
            image_format -- the format of the image (qcow2, raw etc)
     @raise VMDeviceError: When no matching disk found (in indirect method).
     """
+    def sort_cmp(x, y):
+        """
+        This function used for sort to suit for this test, first sort by len
+        then by value.
+        """
+        has_digit_x = re.findall('[vhs]d[a-z]*[\d]+', x)
+        has_digit_y = re.findall('[vhs]d[a-z]*[\d]+', y)
+
+        if not has_digit_x and  not has_digit_y:
+            if len(x) > len(y):
+                return 1
+            elif len(x) < len(y):
+                return -1
+        if len(x) == len(y):
+            if has_digit_x and has_digit_y:
+                return cmp(x, y)
+            elif has_digit_x:
+                return -1
+            elif has_digit_y:
+                return 1
+        return cmp(x, y)
+
     image_name = params.get("image_name", "image")
     indirect_image_select = params.get("indirect_image_select")
     if indirect_image_select:
         re_name = image_name
         indirect_image_select = int(indirect_image_select)
         matching_images = utils.system_output("ls -1d %s" % re_name)
-        matching_images = sorted(matching_images.split('\n'))
+        matching_images = sorted(matching_images.split('\n'), cmp = sort_cmp)
         if matching_images[-1] == '':
             matching_images = matching_images[:-1]
         try:
-- 
1.7.11.7

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to