Make sure the tmp bunble_volume img gets deleted
[snf-image-creator] / image_creator / bundle_volume.py
index afcc6b8..27839fa 100644 (file)
@@ -33,7 +33,6 @@
 
 import os
 import re
-import uuid
 import tempfile
 import time
 from collections import namedtuple
@@ -45,7 +44,6 @@ from image_creator.util import get_command
 from image_creator.util import FatalError
 
 findfs = get_command('findfs')
-truncate = get_command('truncate')
 dd = get_command('dd')
 dmsetup = get_command('dmsetup')
 losetup = get_command('losetup')
@@ -219,7 +217,7 @@ class BundleVolume():
             image_disk.setPartitionGeometry(
                 image_disk.getPartitionBySector(last.start),
                 parted.Constraint(device=image_disk.device),
-                start=last.start, end=last.end)
+                start=last.start, end=part_end)
             image_disk.commit()
 
             # Parted may have changed this for better alignment
@@ -228,7 +226,7 @@ class BundleVolume():
             partitions[-1] = last
 
             # Leave 2048 blocks at the end.
-            new_end = new_size + 2048
+            new_end = part_end + 2048
 
             if last.type == parted.PARTITION_LOGICAL:
                 # Fix the extended partition
@@ -239,6 +237,7 @@ class BundleVolume():
                     ext.geometry.start, end=last.end)
                 image_disk.commit()
 
+        image_dev.destroy()
         return new_end
 
     def _map_partition(self, dev, num, start, end):
@@ -319,7 +318,7 @@ class BundleVolume():
                  '/boot/grub/menu.lst',
                  '/boot/grub/grub.conf']
 
-        orig = dict(map(lambda p: (p.number, blkid( '-s', 'UUID', '-o',
+        orig = dict(map(lambda p: (p.number, blkid('-s', 'UUID', '-o',
             'value', p.path).stdout.strip()), self.disk.partitions))
 
         for f in map(lambda f: target + f, files):
@@ -391,14 +390,16 @@ class BundleVolume():
                 self._unmap_partition(dev)
             losetup('-d', loop)
 
-    def create_image(self):
+    def create_image(self, image):
 
-        image = '/mnt/%s.diskdump' % uuid.uuid4().hex
-
-        disk_size = self.disk.device.getLength() * self.disk.device.sectorSize
+        size = self.disk.device.getLength() * self.disk.device.sectorSize
 
         # Create sparse file to host the image
-        truncate("-s", "%d" % disk_size, image)
+        fd = os.open(image, os.O_WRONLY | os.O_CREAT)
+        try:
+            os.ftruncate(fd, size)
+        finally:
+            os.close(fd)
 
         self._create_partition_table(image)
 
@@ -417,6 +418,15 @@ class BundleVolume():
 
         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 :