=== modified file 'linaro_image_tools/media_create/partitions.py'
--- linaro_image_tools/media_create/partitions.py	2011-07-28 01:24:21 +0000
+++ linaro_image_tools/media_create/partitions.py	2011-12-22 08:30:57 +0000
@@ -42,6 +42,9 @@
 CYLINDER_SIZE = HEADS * SECTORS * SECTOR_SIZE
 DBUS_PROPERTIES = 'org.freedesktop.DBus.Properties'
 UDISKS = "org.freedesktop.UDisks"
+# Max number of attempts to sleep (total sleep time in seconds =
+# 1+2+...+MAX_TTS)
+MAX_TTS = 10
 
 
 def setup_android_partitions(board_config, media, image_size, bootfs_label,
@@ -428,14 +431,32 @@
     """
     # This could be simpler but UDisks doesn't make it easy for us:
     # https://bugs.freedesktop.org/show_bug.cgi?id=33113.
-    for dev_file in glob.glob("%s?*" % device):
-        device_path = _get_udisks_device_path(dev_file)
-        udisks_dev = dbus.SystemBus().get_object(UDISKS, device_path)
-        part_number = udisks_dev.Get(
-            device_path, 'PartitionNumber', dbus_interface=DBUS_PROPERTIES)
-        if part_number == partition:
-            return str(udisks_dev.Get(
-                device_path, 'DeviceFile', dbus_interface=DBUS_PROPERTIES))
+    time_to_sleep = 1
+    dev_files = glob.glob("%s?*" % device)
+    i = 0
+    while i < len(dev_files):
+        dev_file = dev_files[i]
+        try:
+            device_path = _get_udisks_device_path(dev_file)
+            partition_str = _get_udisks_device_file(device_path, partition)
+            if partition_str:
+                return partition_str
+            i += 1
+        except dbus.exceptions.DBusException, e:
+            if time_to_sleep > MAX_TTS:
+                print "We've waited long enough..."
+                raise
+            print "*" * 60
+            print "UDisks doesn't know about %s: %s" % (dev_file, e)
+            bus = dbus.SystemBus()
+            manager = dbus.Interface(
+                bus.get_object(UDISKS, "/org/freedesktop/UDisks"), UDISKS)
+            print "This is what UDisks know about: %s" % (
+                manager.EnumerateDevices())
+            print "Sleeping for %d seconds" % time_to_sleep
+            time.sleep(time_to_sleep)
+            time_to_sleep += 1
+            print "*" * 60
     return None
 
 
@@ -447,6 +468,16 @@
     return udisks.get_dbus_method('FindDeviceByDeviceFile')(device)
 
 
+def _get_udisks_device_file(path, part):
+    """Return the UNIX special device file for the given partition."""
+    udisks_dev = dbus.SystemBus().get_object(UDISKS, path)
+    part_number = udisks_dev.Get(
+        path, 'PartitionNumber', dbus_interface=DBUS_PROPERTIES)
+    if part_number == part:
+        return str(udisks_dev.Get(
+            path, 'DeviceFile', dbus_interface=DBUS_PROPERTIES))
+
+
 def convert_size_to_bytes(size):
     """Convert a size string in Kbytes, Mbytes or Gbytes to bytes."""
     unit = size[-1].upper()

=== modified file 'linaro_image_tools/media_create/tests/test_media_create.py'
--- linaro_image_tools/media_create/tests/test_media_create.py	2011-12-19 13:51:23 +0000
+++ linaro_image_tools/media_create/tests/test_media_create.py	2011-12-22 14:36:11 +0000
@@ -30,6 +30,7 @@
 import types
 import struct
 import tarfile
+import dbus
 
 from StringIO import StringIO
 from testtools import TestCase
@@ -92,6 +93,7 @@
     setup_partitions,
     get_uuid,
     _parse_blkid_output,
+    _get_device_file_for_partition_number,
     )
 from linaro_image_tools.media_create.rootfs import (
     append_to_fstab,
@@ -2210,6 +2212,71 @@
              '%s mkfs.ext3 %s -L root' % (sudo_args, rootfs_dev)],
             popen_fixture.mock.commands_executed)
 
+    def test_get_device_file_for_partition_number_raises_DBusException(self):
+	def mock_get_udisks_device_path(d):
+	    raise dbus.exceptions.DBusException
+
+        self.useFixture(MockSomethingFixture(
+            partitions, '_get_udisks_device_path',
+            mock_get_udisks_device_path))
+
+        tmpfile = self.createTempFileAsFixture()
+	partition = board_configs['beagle'].mmc_part_offset
+
+        self.useFixture(MockSomethingFixture(
+            glob, 'glob',
+            lambda pathname: ['%s%d' % (tmpfile, partition)]))
+
+        self.useFixture(MockSomethingFixture(
+            sys, 'stdout', open('/dev/null', 'w')))
+
+        media = Media(tmpfile)
+        media.is_block_device = True
+        self.assertRaises(dbus.exceptions.DBusException,
+	    _get_device_file_for_partition_number,
+            media.path, partition)
+
+    def test_get_device_file_for_partition_number(self):
+	class Namespace: pass
+	ns = Namespace()
+	ns.count = 0
+
+	def mock_get_udisks_device_path(dev):
+	    ns.count += 1
+	    if ns.count < 5:
+		raise dbus.exceptions.DBusException
+	    else:
+		return '/abc/123'
+
+	def mock_get_udisks_device_file(dev, part):
+	    if ns.count < 5:
+		raise dbus.exceptions.DBusException
+	    else:
+		return '/abc/123'
+
+        self.useFixture(MockSomethingFixture(
+            partitions, '_get_udisks_device_path',
+            mock_get_udisks_device_path))
+
+        self.useFixture(MockSomethingFixture(
+            partitions, '_get_udisks_device_file',
+            mock_get_udisks_device_file))
+
+        tmpfile = self.createTempFileAsFixture()
+	partition = board_configs['beagle'].mmc_part_offset
+
+        self.useFixture(MockSomethingFixture(
+            glob, 'glob',
+            lambda pathname: ['%s%d' % (tmpfile, partition)]))
+
+        self.useFixture(MockSomethingFixture(
+            sys, 'stdout', open('/dev/null', 'w')))
+
+        media = Media(tmpfile)
+        media.is_block_device = True
+        self.assertIsNotNone(_get_device_file_for_partition_number(
+            media.path, partition))
+
 
 class TestException(Exception):
     """Just a test exception."""

