~smoser/ubuntu/quantal/cloud-init/sru

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Author: Scott Moser <smoser@brickies.net>
Bug: https://launchpad.net/bugs/1100545
Applied-Upstream: revno 760
Description: DataSourceConfigDrive: consider CD rom as valid config-drive source.
 previously, there was an attempt in the config drive source to limit
 the source device to a "full block device" rather than a partition.
 .
 This was done by a simplistic approach of checking that the last 
 character of the name was not a number.  That was filtering out
 CD-rom devices (sr0).
 .
 Now, we have a bit more sophisticated approach to that same problem.
 We filter out block devices that have a 'partition' entry in
 /sys/class/block/DEVICE_NAME/partition .
--- a/cloudinit/sources/DataSourceConfigDrive.py
+++ b/cloudinit/sources/DataSourceConfigDrive.py
@@ -200,7 +200,7 @@ def find_candidate_devs():
     combined = (by_label + [d for d in by_fstype if d not in by_label])
 
     # We are looking for block device (sda, not sda1), ignore partitions
-    combined = [d for d in combined if d[-1] not in "0123456789"]
+    combined = [d for d in combined if not util.is_partition(d)]
 
     return combined
 
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1533,3 +1533,10 @@ def keyval_str_to_dict(kvstring):
             val = True
         ret[key] = val
     return ret
+
+
+def is_partition(device):
+    if device.startswith("/dev/"):
+        device = device[5:]
+
+    return os.path.isfile("/sys/class/block/%s/partition" % device)
--- a/tests/unittests/test_datasource/test_configdrive.py
+++ b/tests/unittests/test_datasource/test_configdrive.py
@@ -136,19 +136,25 @@ class TestConfigDriveDataSource(TestCase
                           ds.read_config_drive_dir, my_d)
 
     def test_find_candidates(self):
-        devs_with_answers = {
-            "TYPE=vfat": [],
-            "TYPE=iso9660": ["/dev/vdb"],
-            "LABEL=config-2": ["/dev/vdb"],
-        }
+        devs_with_answers = {}
 
         def my_devs_with(criteria):
             return devs_with_answers[criteria]
 
+        def my_is_partition(dev):
+            return dev[-1] in "0123456789" and not dev.startswith("sr")
+
         try:
             orig_find_devs_with = util.find_devs_with
             util.find_devs_with = my_devs_with
 
+            orig_is_partition = util.is_partition
+            util.is_partition = my_is_partition
+
+            devs_with_answers = {"TYPE=vfat": [],
+                "TYPE=iso9660": ["/dev/vdb"],
+                "LABEL=config-2": ["/dev/vdb"],
+            }
             self.assertEqual(["/dev/vdb"], ds.find_candidate_devs())
 
             # add a vfat item
@@ -164,6 +170,7 @@ class TestConfigDriveDataSource(TestCase
 
         finally:
             util.find_devs_with = orig_find_devs_with
+            util.is_partition = orig_is_partition
 
     def test_pubkeys_v2(self):
         """Verify that public-keys work in config-drive-v2."""