diff options
authorRoss Burton <>2021-03-23 16:37:21 (GMT)
committerRichard Purdie <>2021-03-28 21:28:26 (GMT)
commit3743234dab23e4068e1d1fde9e1fa0bc09fdb182 (patch)
parente9929301d408962061ed6a94730a600fba1274d7 (diff)
lib/oe/utils: add directory size function
For the purpose of image construction using du on a rootfs directory isn't entirely satisfactory. Bare "du" will report the actual disk usage so file systems which can compress the data will report less than the actual space required. Using "du --apparent-size" will report the actual space used, but as this simply sums the bytes used for content across an entire file system can result in significant under-reporting due to block size overhead. Attempt to solve these problems by implementing our own function to calculate how large a rootfs will be. This function handles hardlinks correctly but rounds up all sizes to multiples of the block size (currently, 4KB is the hard-coded block size). (From OE-Core rev: 6ca53ad7b26ee2b4e6d2c121c6f6d6eed7f6b56f) Signed-off-by: Ross Burton <> Signed-off-by: Richard Purdie <>
1 files changed, 31 insertions, 0 deletions
diff --git a/meta/lib/oe/ b/meta/lib/oe/
index 9a2187e..a84039f 100644
--- a/meta/lib/oe/
+++ b/meta/lib/oe/
@@ -536,3 +536,34 @@ class ImageQAFailed(Exception):
536def sh_quote(string): 536def sh_quote(string):
537 import shlex 537 import shlex
538 return shlex.quote(string) 538 return shlex.quote(string)
540def directory_size(root, blocksize=4096):
541 """
542 Calculate the size of the directory, taking into account hard links,
543 rounding up every size to multiples of the blocksize.
544 """
545 def roundup(size):
546 """
547 Round the size up to the nearest multiple of the block size.
548 """
549 import math
550 return math.ceil(size / blocksize) * blocksize
552 def getsize(filename):
553 """
554 Get the size of the filename, not following symlinks, taking into
555 account hard links.
556 """
557 stat = os.lstat(filename)
558 if stat.st_ino not in inodes:
559 inodes.add(stat.st_ino)
560 return stat.st_size
561 else:
562 return 0
564 inodes = set()
565 total = 0
566 for root, dirs, files in os.walk(root):
567 total += sum(roundup(getsize(os.path.join(root, name))) for name in files)
568 total += roundup(getsize(root))
569 return total