build_image: print the size of the rootfs

Add the size of the rootfs of the base image to the build log. This
makes it easy to see the rootfs usage from a build log, without having
to download/mount an image and manually check the output of df.

Typical output:

INFO : Usage of the root filesystem:
INFO : Blocks:        Total: 579536     Used: 482893     Free: 96643
INFO : Inodes:        Total: 147456     Used: 13053      Free: 134403
INFO : Size (bytes):  Total: 2373779456 Used: 1977929728 Free: 395849728
INFO : Size (MiB):    Total: 2263       Used: 1886       Free: 377

BUG=chromium:1074024
TEST=run build_image, verify that the output shows the expected message

Change-Id: I57cc30c4488b004189efa2c1929f975f4279d2d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/2164210
Tested-by: Nicolas Norvez <norvez@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/build_library/base_image_util.sh b/build_library/base_image_util.sh
index 1008e61..d1dcc5e 100755
--- a/build_library/base_image_util.sh
+++ b/build_library/base_image_util.sh
@@ -40,6 +40,44 @@
   sudo fstrim -v "${fs_mount_point}"
 }
 
+log_rootfs_usage() {
+  local fs_mount_point="$1"
+  local -i block_size
+  local -i free_blocks total_blocks used_blocks
+  local -i free_bytes total_bytes used_bytes
+  local -i total_nodes free_nodes used_nodes
+  local -i total_mb free_mb used_mb
+
+  block_size="$(stat -f -c "%S" "${fs_mount_point}")"
+
+  total_blocks="$(stat -f -c "%b" "${fs_mount_point}")"
+  free_blocks="$(stat -f -c "%f" "${fs_mount_point}")"
+  used_blocks=$(( total_blocks - free_blocks ))
+
+  total_nodes="$(stat -f -c "%c" "${fs_mount_point}")"
+  free_nodes="$(stat -f -c "%d" "${fs_mount_point}")"
+  used_nodes=$(( total_nodes - free_nodes ))
+
+  total_bytes=$(( total_blocks * block_size ))
+  free_bytes=$(( free_blocks * block_size ))
+  used_bytes=$(( used_blocks * block_size ))
+  total_mb=$(( total_bytes / (1024 * 1024) ))
+  free_mb=$(( free_bytes / (1024 * 1024) ))
+  used_mb=$(( used_bytes / (1024 * 1024) ))
+
+  # Format the printout based on typical values (e.g. total_bytes and used_bytes
+  # are typically 10 digits).
+  info "Usage of the root filesystem:"
+  info "Blocks:\t\tTotal: ${total_blocks}\t\tUsed: ${used_blocks}" \
+    "\t\tFree: ${free_blocks}"
+  info "Inodes:\t\tTotal: ${total_nodes}\t\tUsed: ${used_nodes}" \
+    "\t\tFree: ${free_nodes}"
+  info "Size (bytes):\tTotal: ${total_bytes}\tUsed: ${used_bytes}" \
+    "\tFree: ${free_bytes}"
+  info "Size (MiB):\t\tTotal: ${total_mb}\t\tUsed: ${used_mb}" \
+    "\t\tFree: ${free_mb}"
+}
+
 # create_dev_install_lists updates package lists used by
 # chromeos-base/dev-install
 create_dev_install_lists() {
@@ -210,8 +248,6 @@
   mount_image "${BUILD_DIR}/${image_name}" "${root_fs_dir}" \
     "${stateful_fs_dir}" "${esp_fs_dir}"
 
-  df -h "${root_fs_dir}"
-
   # Create symlinks so that /usr/local/usr based directories are symlinked to
   # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc.
   setup_symlinks_on_root "." \
@@ -487,10 +523,12 @@
         --partition-name 'rootfs') \
      > "${BUILD_DIR}/${image_name}-package-sizes.json"
 
-# Zero rootfs free space to make it more compressible so auto-update
+  # Zero rootfs free space to make it more compressible so auto-update
   # payloads become smaller.
   zero_free_space "${root_fs_dir}"
 
+  log_rootfs_usage "${root_fs_dir}"
+
   unmount_image
   trap - EXIT