build_image: right-sizing at least some spare inode

To address problems creating real tiny filesystem placeholders,
make sure extra spare inodes margin is greater than 0.
For initial estimate we add 4% of total we request at least 8.
For second pass estimate we add 0.2% of total we request at least 1.

We bumped up the margin for zie on the second pass to 0.3% as the
value was too close on one of the builds.

Test: build
Bug: 122328872
Change-Id: I41707bb6fcc8bbfbdda143a9ce62446cad9c1533
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index 6aaff6b..ad0432d 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -76,7 +76,12 @@
   cmd = ["find", path, "-print"]
   output = common.RunAndCheckOutput(cmd, verbose=False)
   # increase by > 4% as number of files and directories is not whole picture.
-  return output.count('\n') * 25 // 24
+  inodes = output.count('\n')
+  spare_inodes = inodes * 4 // 100
+  min_spare_inodes = 8
+  if spare_inodes < min_spare_inodes:
+    spare_inodes = min_spare_inodes
+  return inodes + spare_inodes
 
 
 def GetFilesystemCharacteristics(image_path, sparse_image=True):
@@ -436,8 +441,8 @@
         size -= free_size
         size += reserved_size
         if reserved_size == 0:
-          # add .2% margin
-          size = size * 1002 // 1000
+          # add .3% margin
+          size = size * 1003 // 1000
         # Use a minimum size, otherwise we will fail to calculate an AVB footer
         # or fail to construct an ext4 image.
         size = max(size, 256 * 1024)
@@ -448,8 +453,12 @@
       extfs_inode_count = prop_dict["extfs_inode_count"]
       inodes = int(fs_dict.get("Inode count", extfs_inode_count))
       inodes -= int(fs_dict.get("Free inodes", "0"))
-      # add .2% margin
-      inodes = inodes * 1002 // 1000
+      # add .2% margin or 1 inode, whichever is greater
+      spare_inodes = inodes * 2 // 1000
+      min_spare_inodes = 1
+      if spare_inodes < min_spare_inodes:
+        spare_inodes = min_spare_inodes
+      inodes += spare_inodes
       prop_dict["extfs_inode_count"] = str(inodes)
       prop_dict["partition_size"] = str(size)
       logger.info(