image_to_vm.sh: use dd conv=sparse to copy partitions.

After writing lots of data to the output block device, the kernel will
sometimes hang in close.  See also http://b/135292499.

When the destination is not a regular file, cp ignores --sparse.  Since
the copies are to partitions in a loopback block device that was freshly
created, there is no need to write the zero blocks to it.

BUG=chromium:957712
TEST=manual

Change-Id: I884eb143d9cc2303e7855e78995a6c836b095f2b
Reviewed-on: https://chromium-review.googlesource.com/1661975
Tested-by: LaMont Jones <lamontjones@chromium.org>
Commit-Ready: LaMont Jones <lamontjones@chromium.org>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/image_to_vm.sh b/image_to_vm.sh
index e759d90..6a223d3 100755
--- a/image_to_vm.sh
+++ b/image_to_vm.sh
@@ -191,10 +191,15 @@
 DST_ESP="${DST_DEV}"p12
 
 # Copy into the partition parts of the file.
-sudo dd if="${SRC_ROOTFS}" of="${DST_ROOTFS}" status=progress bs=16M
-sudo dd if="${TEMP_STATE}" of="${DST_STATE}"  status=progress bs=16M
-sudo dd if="${SRC_ESP}"    of="${DST_ESP}"    status=progress bs=16M
-sudo dd if="${SRC_OEM}"    of="${DST_OEM}"    status=progress bs=16M
+# When copying to (or from) a non-regular file, cp ignores --sparse. Since we
+# have created a collection of empty partitions for the new image above, we can
+# use 'dd conv=sparse' to both speed up the copy, and (apparently) avoid
+# b/135292499.  See also crbug.com/957712.  This only works because we know that
+# the destination partition is all zeros.
+sudo dd if="${SRC_ROOTFS}" of="${DST_ROOTFS}" conv=sparse bs=2M
+sudo dd if="${TEMP_STATE}" of="${DST_STATE}"  conv=sparse bs=2M
+sudo dd if="${SRC_ESP}"    of="${DST_ESP}"    conv=sparse bs=2M
+sudo dd if="${SRC_OEM}"    of="${DST_OEM}"    conv=sparse bs=2M
 sync
 
 TEMP_MNT=$(mktemp -d)
@@ -226,8 +231,10 @@
 
 # cros_make_image_bootable made the kernel in slot A recovery signed. We want
 # it to be normally signed like the one in slot B, so copy B into A.
+# Because cros_make_image_bootable overwrote p2 above, we cannot do a sparse
+# copy.
 IMAGE_DEV=$(loopback_partscan "${TEMP_IMG}")
-sudo dd if=${IMAGE_DEV}p4 of=${IMAGE_DEV}p2 status=progress bs=16M
+sudo dd if=${IMAGE_DEV}p4 of=${IMAGE_DEV}p2 bs=2M
 sync
 
 trap 'die_err_trap' INT TERM EXIT