image_signing: convert_recovery_to_ssd.sh: rewrite to be better

This converts the script in one commit as nothing uses it directly,
so the chances of it breaking overall build is low.
- Convert to common.sh for more helpers
- Convert echo to info
- Convert to loopback devices to speed things up
- Fix quoting in a few places
- Drop cgpt usage since we use loopback partitions everywhere now

BRANCH=None
BUG=chromium:714598
TEST=running on an image still works

Change-Id: I6608db77792502f35522a6f793ccd800fdd6af4e
Reviewed-on: https://chromium-review.googlesource.com/505482
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: David Riley <davidriley@chromium.org>
diff --git a/scripts/image_signing/convert_recovery_to_ssd.sh b/scripts/image_signing/convert_recovery_to_ssd.sh
index 200efee..bd6c96d 100755
--- a/scripts/image_signing/convert_recovery_to_ssd.sh
+++ b/scripts/image_signing/convert_recovery_to_ssd.sh
@@ -1,18 +1,14 @@
-#!/bin/bash 
-
+#!/bin/bash
 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-# Script to convert a recovery image into an SSD image. Changes are made in-
-# place.
-
 usage() {
   cat <<EOF
-Usage: $PROG <image> [--force] [--cgpt=/path/to/cgpt]
+Usage: $PROG <image> [--force]
 
 In-place converts recovery <image> into an SSD image. With --force, does not
-ask for confirmation from the user. Use --cgpt= to specify cgpt binary location.
+ask for confirmation from the user.
 
 EOF
   exit 1
@@ -25,13 +21,10 @@
   shift
 fi
 
-for arg in $*; do
+for arg in "$@"; do
   case "$arg" in
   --force)
-    IS_FORCE=$arg
-    ;;
-  --cgpt=*)
-    GPT=${arg#--cgpt=}
+    IS_FORCE=${arg}
     ;;
   *)
     usage
@@ -40,43 +33,39 @@
 done
 
 # Load common constants (and use GPT if set above) and variables.
-. "$(dirname "$0")/common_minimal.sh"
-
-type -P $GPT &>/dev/null ||
-  { echo "cgpt tool must be in the path or specified via --cgpt"; exit 1; }
+. "$(dirname "$0")/common.sh"
 
 # Abort on errors.
 set -e
 
 if [ "${IS_FORCE}" != "--force" ]; then
   echo "This will modify ${IMAGE} in-place and convert it into an SSD image."
-  read -p "Are you sure you want to continue (y/N)?" SURE
+  read -p "Are you sure you want to continue (y/N)? " SURE
   SURE="${SURE:0:1}"
   [ "${SURE}" != "y" ] && exit 1
 fi
 
-kerna_offset=$(partoffset ${IMAGE} 2)
-kernb_offset=$(partoffset ${IMAGE} 4)
-# Kernel partition sizes should be the same.
-kern_size=$(partsize ${IMAGE} 2)
+loopdev=$(loopback_partscan "${IMAGE}")
+loop_kerna="${loopdev}p2"
+loop_kernb="${loopdev}p4"
 
 # Move Kernel B to Kernel A.
-kernb=$(make_temp_file)
-echo "Replacing Kernel partition A with Kernel partition B"
-extract_image_partition ${IMAGE} 4 ${kernb}
-replace_image_partition ${IMAGE} 2 ${kernb}
+info "Replacing Kernel partition A with Kernel partition B"
+sudo cp "${loop_kernb}" "${loop_kerna}"
 
 # Overwrite the vblock.
+info "Overwriting kernel partition A vblock with SSD vblock"
 stateful_dir=$(make_temp_dir)
 tmp_vblock=$(make_temp_file)
-mount_image_partition_ro ${IMAGE} 1 ${stateful_dir}
-sudo cp ${stateful_dir}/vmlinuz_hd.vblock ${tmp_vblock}
+sudo mount -o ro "${loopdev}p1" "${stateful_dir}"
+sudo cp "${stateful_dir}/vmlinuz_hd.vblock" "${tmp_vblock}"
 # Unmount before overwriting image to avoid sync issues.
-sudo umount ${stateful_dir}
-echo "Overwriting kernel partition A vblock with SSD vblock"
-sudo dd if=${tmp_vblock} of=${IMAGE} seek=${kerna_offset} bs=512 conv=notrunc
+sudo umount "${stateful_dir}"
+sudo dd if="${tmp_vblock}" of="${loop_kerna}" bs=512 conv=notrunc
 
 # Zero out Kernel B partition.
-echo "Zeroing out Kernel partition B"
-sudo dd if=/dev/zero of=${IMAGE} seek=${kernb_offset} bs=512 count=${kern_size} conv=notrunc
-echo "${IMAGE} was converted to an SSD image."
+info "Zeroing out Kernel partition B"
+# This will throw a "disk is full" error, so ignore it.
+sudo cp /dev/zero "${loop_kernb}" 2>/dev/null || :
+
+info "${IMAGE} was converted to an SSD image."