termina_build_image: fix file system shrink.

Warn users to install fdupes when it's not available.
Also, resize2fs doesn't work for me on 1K block size and man page of resize2fs
reads "The minimum size of the filesystem as estimated by resize2fs may be
incorrect,  especially for filesystems with 1k and 2k blocksizes.", so just
begin with the 110% of disk usage of root fs.

BUG=None
TEST=manual - run termina_build_image

Change-Id: I54c8a6e342f6e6d4c1ed5c0c27d72f81b19f9013
Reviewed-on: https://chromium-review.googlesource.com/1544864
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Lepton Wu <lepton@chromium.org>
Reviewed-by: Stephen Barber <smbarber@chromium.org>
diff --git a/termina_build_image b/termina_build_image
index e34f348..b55cc18 100755
--- a/termina_build_image
+++ b/termina_build_image
@@ -95,6 +95,9 @@
   local dir=$1
 
   local line prev_line=""
+  if [ -z $(command -v fdupes) ]; then
+    warn "Please install fdupes for removing dups"
+  fi
   while read line; do
     # Treat the first file as the original.
     if [[ -n "${prev_line}" && -n "${line}" ]]; then
@@ -160,8 +163,9 @@
     sudo mksquashfs "${rootfs}" "${rootfs_img}" -comp lzo
     ;;
   ext4)
-    # Start with 410MB, then shrink.
-    local image_size=410
+    # Start with 110% rootfs size, then shrink.
+    local image_size=$(sudo du -sxm "${rootfs}" | awk '{print $1}')
+    let image_size=image_size*11/10
     truncate --size "${image_size}M" "${rootfs_img}"
     /sbin/mkfs.ext4 -F -m 0 -i 16384 -b 4096 -O "^has_journal" "${rootfs_img}"
     local rootfs_mnt="$(mktemp -d)"