From 7293bd852419590d43550cdeb5985bec4ff31bbb Mon Sep 17 00:00:00 2001 From: Gilad Arnold Date: Tue, 14 Feb 2012 09:46:21 -0800 Subject: [PATCH] image_to_usb.sh: warn before copying to non-existent /dev/sdX device. Without this fix, when the script is invoked with (say) --to=/dev/sdk but /dev/sdk is not a valid block device, we will interpret it as if the user intended to copy the image to a file of the same name. However, it is more likely that the user specified the wrong device name. Hence, this fix catches this type of error and will fail the operation with a corresponding error message. Also includes minor changes to printed messages. BUG=None TEST=Ran the script with different values of --to Change-Id: Ie060ff2be77b8c065ba95f635455049e034d4bbc Reviewed-on: https://gerrit.chromium.org/gerrit/15822 Reviewed-by: Gilad Arnold Tested-by: Gilad Arnold Commit-Ready: Gilad Arnold --- image_to_usb.sh | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/image_to_usb.sh b/image_to_usb.sh index ab61816b4b..7113832af3 100755 --- a/image_to_usb.sh +++ b/image_to_usb.sh @@ -93,6 +93,17 @@ function get_disk_string() { echo "${product_string}, ${disk_size}" } +# Prompt for user confirmation. Default is no, which will gracefully terminate +# the script. +function are_you_sure() { + local sure + read -p "Are you sure (y/N)? " sure + if [ "${sure}" != "y" ]; then + echo "Ok, better safe than sorry." + exit + fi +} + # Prohibit mutually exclusive factory/install flags. if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} -a \ @@ -288,9 +299,9 @@ fi if [ -b "${FLAGS_to}" ]; then # Output to a block device (i.e., a real USB key / SD card), so need sudo dd if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then - echo "Copying image ${SRC_IMAGE} to ${FLAGS_to}..." + echo "Copying image ${SRC_IMAGE} to device ${FLAGS_to}..." else - echo "Installing image ${SRC_IMAGE} to ${FLAGS_to}..." + echo "Installing image ${SRC_IMAGE} to device ${FLAGS_to}..." fi # Warn if it looks like they supplied a partition as the destination. @@ -310,12 +321,7 @@ if [ -b "${FLAGS_to}" ]; then warning_str="${warning_str}, which does not appear to be a USB/MMC disk!" fi warn "${warning_str}" - read -p "Are you sure (y/N)? " SURE - SURE="${SURE:0:1}" # Get just the first character - if [ "${SURE}" != "y" ]; then - echo "Ok, better safe than sorry." - exit - fi + are_you_sure fi mount_list=$(mount | grep ^"${FLAGS_to}" | awk '{print $1}') @@ -346,10 +352,13 @@ if [ -b "${FLAGS_to}" ]; then --payload_image="${SRC_IMAGE}" \ --dst="${FLAGS_to}" fi +elif [[ "${FLAGS_to}" == /dev/* ]]; then + # Did the user attempt to write to a non-existent block device? + die "Target device ${FLAGS_to} does not exist" else # Output to a file, so just make a copy. if [ "${SRC_IMAGE}" != "${FLAGS_to}" ]; then - echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." + echo "Copying image ${SRC_IMAGE} to file ${FLAGS_to}..." ${COMMON_PV_CAT} "${SRC_IMAGE}" >"${FLAGS_to}" fi