package_to_container: generate chrome extension manifest

package_to_container will now produce a manifest that can be
used to identify the container when packaged into a CRX file.

BUG=chromium:644347
TEST=check for manifest.json in the output directory, try to
  mount using mount_extension_image on a DUT and ensure it
  finds the container extension

Change-Id: I7f5b01fa13de54218b9d4dc20774168bc875ee70
Reviewed-on: https://chromium-review.googlesource.com/471786
Commit-Ready: Eric Caruso <ejcaruso@chromium.org>
Tested-by: Eric Caruso <ejcaruso@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
diff --git a/package_to_container b/package_to_container
index 06a9e72..3fa165d 100755
--- a/package_to_container
+++ b/package_to_container
@@ -36,6 +36,7 @@
 * imageloader.json: imageloader manifest which contains hashes
     of image.squash and table
 * imageloader.sig.2: ECDSA signature of imageloader.json
+* manifest.json: chrome extension manifest
 
 If the --minor_version flag is given a negative number, it is ignored.
 "
@@ -79,6 +80,25 @@
   sign_manifest "${output}"
 }
 
+# Generate the manifest for a chrome extension.
+generate_crx_manifest() {
+  local container_name="$1"
+  local pkg_version="$2"
+  local manifest="${output}/manifest.json"
+
+  (
+    cd "${output}"
+    printf '{\n'
+    printf '"manifest_version": 2,\n'
+    printf '"name": "%s",\n' "${container_name}"
+    printf '"version": "%s"\n' "${pkg_version}"
+    printf '}\n'
+  ) >"${manifest}"
+
+  # Sanity check the generated manifest.
+  python -mjson.tool <"${manifest}" >/dev/null
+}
+
 # Sign the specified disk image using verity so we can load it with dm-verity
 # at runtime.  We don't allow algorithm selection -- sha256 should be good
 # enough for everyone! :)
@@ -153,6 +173,9 @@
   info "Generating manifest ..."
   generate_manifest "${version}" "${output}"
 
+  info "Generating extension manifest ..."
+  generate_crx_manifest "${container_name}" "${version}"
+
   info "Cleaning up ... "
   sudo rm -rf "${IMAGEDIR}"
 }