From fa39c7e385cf629ebc232ea2bcb30d33501feb3c Mon Sep 17 00:00:00 2001
From: Nikos Skalkotos <skalkoto@grnet.gr>
Date: Fri, 4 Jan 2013 14:06:53 +0200
Subject: [PATCH] Truncate the image before fs creation

---
 image_creator/bundle_volume.py | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/image_creator/bundle_volume.py b/image_creator/bundle_volume.py
index e86d613..53c62f6 100644
--- a/image_creator/bundle_volume.py
+++ b/image_creator/bundle_volume.py
@@ -418,9 +418,17 @@ class BundleVolume(object):
 
         end_sector = self._shrink_partitions(image)
 
+        size = (end_sector + 1) * self.disk.device.sectorSize
+
+        # Truncate image to the new size.
+        fd = os.open(image, os.O_RDWR)
+        try:
+            os.ftruncate(fd, size)
+        finally:
+            os.close(fd)
+
         # Check if the available space is enough to host the image
         dirname = os.path.dirname(image)
-        size = (end_sector + 1) * self.disk.device.sectorSize
         self.out.output("Examining available space in %s ..." % dirname, False)
         stat = os.statvfs(dirname)
         available = stat.f_bavail * stat.f_frsize
@@ -431,15 +439,6 @@ class BundleVolume(object):
 
         self._create_filesystems(image)
 
-        # Truncate image to the new size. I counldn't find a better way to do
-        # this. It seems that python's high level functions work in a different
-        # way.
-        fd = os.open(image, os.O_RDWR)
-        try:
-            os.ftruncate(fd, size)
-        finally:
-            os.close(fd)
-
         return image
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
-- 
GitLab