scripts: Build DLC images during the build_image phase.

Previously, the DLC images were created at the build_packages phase.
With this change, the build_packages will only build the DLC packages,
and the DLC images will be created at the build_images phase.
The change in common.sh is to exclude the DLC files from the rootfs
during emerge to the image and DUT, but ideally, the DLC files should
not be included in tha tarball during the build_package phase at all.

BUG=chromium:1050646
TEST=build_packages, build test image, cros flash local image.

Cq-Depend: chromium:2067295
Change-Id: Ifb827e101d9a1d65a1d1f5b2c6c58b226d240a7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosutils/+/2067939
Tested-by: Andrew Lassalle <andrewlassalle@chromium.org>
Commit-Queue: Andrew Lassalle <andrewlassalle@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
diff --git a/build_image b/build_image
index f926a5d..3fd2e3a 100755
--- a/build_image
+++ b/build_image
@@ -213,7 +213,7 @@
 # Move the completed image to the output_root.
 move_image "${BUILD_DIR}" "${OUTPUT_DIR}"
 
-# Move DLC images to the output_root directory.
+# Copy DLC images to the output_root directory.
 build_dlc --sysroot="${BOARD_ROOT}" --install-root-dir="${OUTPUT_DIR}/dlc"
 
 # Create a named symlink.
diff --git a/build_library/base_image_util.sh b/build_library/base_image_util.sh
index 1f82b08..5696760 100755
--- a/build_library/base_image_util.sh
+++ b/build_library/base_image_util.sh
@@ -450,6 +450,9 @@
     create_dev_install_lists "${root_fs_dir}"
   fi
 
+  # Generate DLCs and copy their metadata to the rootfs.
+  build_dlc --sysroot="${BOARD_ROOT}" --rootfs="${root_fs_dir}"
+
   restore_fs_contexts "${BOARD_ROOT}" "${root_fs_dir}" "${stateful_fs_dir}"
 
   # Move the bootable kernel images out of the /boot directory to save
diff --git a/build_library/test_image_util.sh b/build_library/test_image_util.sh
index 2fbcab7..a90768f 100755
--- a/build_library/test_image_util.sh
+++ b/build_library/test_image_util.sh
@@ -44,9 +44,9 @@
 
   # Move DLC images to the /var/cache directory. The dlc-images directory will
   # hold DLC(s) that dlcservice will leverage for testing/provisioning.
-  sudo build_dlc --sysroot="${BOARD_ROOT}" \
+  build_dlc --sysroot="${BOARD_ROOT}" \
     --install-root-dir="${root_fs_dir}/var/cache/dlc-images" \
-    --preload
+    --preload --rootfs="${root_fs_dir}"
 
   unmount_image
   trap - EXIT
diff --git a/common.sh b/common.sh
index 5c8bf5e..50394e2 100644
--- a/common.sh
+++ b/common.sh
@@ -391,6 +391,7 @@
   *.proto
   */.keep*
   /build/libexec/tast
+  /build/rootfs/dlc
   /build/share
   /etc/init.d
   /etc/runlevels