set_lsb_release: enable batch modification

When setting a field, this script mounts the image, unmounts, mounts
it, then writes the field, then unmounts it.  When setting 4 or 5
keys at once, this is quite a waste.

Tweak it so we only mount it once, and we can set multiple keys in
a single call by looping over the input args.

BUG=None
TEST=`./signing_unittests.py` passes
BRANCH=None

Change-Id: Id7dc4e8ef58113cc4632721851fcab04ef1e69eb
Reviewed-on: https://gerrit.chromium.org/gerrit/42601
Reviewed-by: Ryan Cui <rcui@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/scripts/image_signing/set_lsb_release.sh b/scripts/image_signing/set_lsb_release.sh
index 5d859d8..9d0addd 100755
--- a/scripts/image_signing/set_lsb_release.sh
+++ b/scripts/image_signing/set_lsb_release.sh
@@ -24,12 +24,9 @@
 main() {
   set -e
 
-  local image=$1
-  local key=$2
-  local value=$3
-  if [ $# -ne 1 ] && [ $# -ne 3 ]; then
+  if [[ $(( $# % 2 )) -eq 0 ]]; then
     cat <<EOF
-Usage: $PROG <image.bin> [<key> <value>]
+Usage: $PROG <image.bin> [<key> <value> [<key> <value> ...]]
 
 Examples:
 
@@ -47,15 +44,28 @@
     exit 1
   fi
 
+  local image=$1
+  shift
   local rootfs=$(make_temp_dir)
-  mount_image_partition_ro "$image" 3 "$rootfs"
-  if [ -n "$key" ]; then
-    sudo umount "$rootfs"
-    mount_image_partition "$image" 3 "$rootfs"
-    set_lsb_release_keyval "$rootfs" "$key" "$value"
-    touch "$image"  # Updates the image modification time.
+
+  # If there are no key/value pairs to process, we don't need write access.
+  if [[ $# -eq 0 ]]; then
+    mount_image_partition_ro "${image}" 3 "${rootfs}"
+  else
+    mount_image_partition "${image}" 3 "${rootfs}"
+    touch "${image}"  # Updates the image modification time.
   fi
-  cat "$rootfs/etc/lsb-release"
+
+  # Process all the key/value pairs.
+  local key value
+  while [[ $# -ne 0 ]]; do
+    key=$1 value=$2
+    shift 2
+    set_lsb_release_keyval "${rootfs}" "${key}" "${value}"
+  done
+
+  # Dump the final state.
+  cat "${rootfs}/etc/lsb-release"
 }
 
 main "$@"