imgutils estimate_size fixups

count directories in estimate
msdos blocksize is 2048
round_to_blocksize rounds 0 -> blocksize
pass graft to estimate_size so we don't repeat overhead
This commit is contained in:
Will Woods 2011-05-11 17:53:02 -04:00
parent 8f45d48562
commit 72a3958a38

View File

@ -129,25 +129,28 @@ def do_grafts(grafts, dest, preserve=True):
copytree(filename, join(dest, imgpath), preserve) copytree(filename, join(dest, imgpath), preserve)
def round_to_blocks(size, blocksize): def round_to_blocks(size, blocksize):
'''Round the size of a file to the size of the blocks it would use''' '''If size isn't a multiple of blocksize, round up to the next multiple'''
diff = size % blocksize diff = size % blocksize
if diff: if diff or not size:
size += blocksize - diff size += blocksize - diff
return size return size
def estimate_size(rootdir, fstype=None, blocksize=4096, overhead=1024): def estimate_size(rootdir, graft={}, fstype=None, blocksize=4096, overhead=1024):
if not rootdir:
return 0
getsize = lambda f: os.lstat(f).st_size getsize = lambda f: os.lstat(f).st_size
if fstype == "btrfs": if fstype == "btrfs":
overhead = 64*1024 # don't worry, it's all sparse overhead = 64*1024 # don't worry, it's all sparse
if fstype in ("vfat", "msdos"): if fstype in ("vfat", "msdos"):
overhead = 128 overhead = 128
blocksize = 2048
getsize = lambda f: os.stat(f).st_size # no symlinks, count as copies getsize = lambda f: os.stat(f).st_size # no symlinks, count as copies
total = overhead*blocksize total = overhead*blocksize
for root, dirs, files in os.walk(rootdir): dirlist = graft.values()
for f in files: if rootdir:
total += round_to_blocks(getsize(join(root,f)), blocksize) dirlist.append(rootdir)
for root in dirlist:
for top, dirs, files in os.walk(root):
for f in files + dirs:
total += round_to_blocks(getsize(join(top,f)), blocksize)
if fstype == "btrfs": if fstype == "btrfs":
total = max(256*1024*1024, total) # btrfs minimum size: 256MB total = max(256*1024*1024, total) # btrfs minimum size: 256MB
return total return total
@ -193,9 +196,7 @@ def mkfsimage(fstype, rootdir, outfile, size=None, mkfsargs=[], mountargs="", gr
Will raise CalledProcessError if something goes wrong.''' Will raise CalledProcessError if something goes wrong.'''
preserve = (fstype not in ("msdos", "vfat")) preserve = (fstype not in ("msdos", "vfat"))
if not size: if not size:
size = estimate_size(rootdir, fstype) size = estimate_size(rootdir, graft, fstype)
for f in graft.values():
size += estimate_size(f, fstype)
with LoopDev(outfile, size) as loopdev: with LoopDev(outfile, size) as loopdev:
check_call(["mkfs.%s" % fstype] + mkfsargs + [loopdev], check_call(["mkfs.%s" % fstype] + mkfsargs + [loopdev],
stdout=PIPE, stderr=PIPE) stdout=PIPE, stderr=PIPE)