BmapFlasher: error out if the image does not fit the block device
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 6 Nov 2012 13:21:30 +0000 (15:21 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 8 Nov 2012 08:10:39 +0000 (10:10 +0200)
I've got a bug report that the flasher tries to flash even if the block device
is too small. Let's fix this.

Change-Id: I4e7742d362fca8b6a2885b318a60fbeeb5ef1a9d
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptools/BmapFlasher.py

index bd67167..a657a85 100644 (file)
@@ -247,6 +247,22 @@ class BmapFlasher:
                 image_size = os.fstat(self._f_image.fileno()).st_size
                 self._initialize_sizes(image_size)
 
+        # If we are writing to a real block device and the image size is known,
+        # check that the image fits the block device.
+        if self.target_is_block_device and self.bmap_image_size:
+            try:
+                bdev_size = os.lseek(self._f_bdev.fileno(), 0, os.SEEK_END)
+                os.lseek(self._f_bdev.fileno(), 0, os.SEEK_SET)
+            except OSError as err:
+                raise Error("cannot seed block device '%s': %s " \
+                            % (self._bdev_path, err.strerror))
+
+            if bdev_size < self.bmap_image_size:
+                raise Error("the image file '%s' has size %s and it will not " \
+                            "fit the block device '%s' which has %s capacity" \
+                            % (self._image_path, self.bmap_image_size_human,
+                               self._bdev_path, human_size(bdev_size)))
+
     def __del__(self):
         """ The class destructor which closes the opened files. """