build_image: reclain empty block with fstrim

The current method - writing a file with 0s until the root partition is full and
then deleting it - is not working. Using the script below, ~50% of the blocks
are reported as non 0s.
fstrim is able to reclaim those blocks, > 98% of free blocks are 0s.

BUG=chromium:780291
TEST=Check code is ran:
/mnt/host/source/src/build/images/eve/R64-10090.0.2017_11_02_1153-a1/rootfs:
349.6 MiB (366538752 bytes) trimmed

Check most of the empty blocks are empty with the following sequence:
Load the root partition
sudo losetup -o 163577856 -f image.bin
List all the free blocks:
sudo debugfs -R 'ffb 10000000000' /dev/loop2 | sed 's/Free blocks found: //' > /tmp/all_fb.txt
Count the number of zero'd free blocks:
zero=0
for b in $(cat /tmp/all_fb.txt); doi
  if [ $(sudo dd if=image.bin skip=$(( b + 39936 )) count=1 bs=4K status=none| cksum | cut -d ' ' ^C 1) -eq 3018728591 ] ; then
    : $(( zero += 1 ))
  fi
done
echo $zero

// 39936 * 4096 == 163577856 the usual location of the root partition.
// 3018728591 cksum of a 4K sectors full of 0s

Old image: 50223 out of 90619 are zero'd
New image: 99821 out of 100255 are zero'd

Change-Id: Iba5c86862e52482722e8cb66452bc39989d5223d
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/751809
Reviewed-by: Mike Frysinger <vapier@chromium.org>
1 file changed