logically there is no reason why resize2fsToMinimal should require an
argument of the size in blocks of the filesystem that is passed in as
it's first argument, when that can be calculated by pointing dumpe2fs at
that first argument.
diff -Naur livecd.2.remove_ignoredeleted_option/creator/livecd-creator livecd.3.resize2fsToMinimal_implicitsize/creator/livecd-creator
--- livecd.2.remove_ignoredeleted_option/creator/livecd-creator 2007-09-17 16:50:59.000000000 +0000
+++ livecd.3.resize2fsToMinimal_implicitsize/creator/livecd-creator 2007-09-17 17:09:57.000000000 +0000
@@ -930,6 +930,21 @@
shutil.move("%s/data/os.img" %(self.build_dir,),
"%s/out/ext3fs.img" %(self.build_dir,))
+ def parseField(self, output, field):
+ for line in output.split("\n"):
+ if line.startswith(field + ":"):
+ return line[len(field) + 1:].strip()
+
+ raise KeyError("Failed to find field '%s' in output" % field)
+
+ def getBlockCountOfExt2FS(self, filesystem):
+ output = subprocess.Popen(['/sbin/dumpe2fs', '-h', filesystem],
+ stdout=subprocess.PIPE,
+ stderr=open('/dev/null', 'w')
+ ).communicate()[0]
+
+ return int(self.parseField(output, "Block count"))
+
def resize2fs(self, image, n_blocks):
dev_null = os.open("/dev/null", os.O_WRONLY)
try:
@@ -943,9 +958,9 @@
# resize2fs doesn't have any kind of minimal setting, so use
# a binary search to get it to minimal size.
#
- def resize2fsToMinimal(self, image, n_blocks):
+ def resize2fsToMinimal(self, image):
bot = 0
- top = n_blocks
+ top = self.getBlockCountOfExt2FS(image)
while top != (bot + 1):
t = bot + ((top - bot) / 2)
@@ -968,7 +983,7 @@
n_blocks = os.stat(image)[stat.ST_SIZE] / self.blocksize
- min_blocks = self.resize2fsToMinimal(image, n_blocks)
+ min_blocks = self.resize2fsToMinimal(image)
# truncate the unused excess portion of the sparse file
fd = os.open(image, os.O_WRONLY )