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 <garnold@chromium.org>
Tested-by: Gilad Arnold <garnold@chromium.org>
Commit-Ready: Gilad Arnold <garnold@chromium.org>
This commit is contained in:
Gilad Arnold 2012-02-14 09:46:21 -08:00 committed by Gerrit
parent dd59d7e9a6
commit 7293bd8524

View File

@ -93,6 +93,17 @@ function get_disk_string() {
echo "${product_string}, ${disk_size}" 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. # Prohibit mutually exclusive factory/install flags.
if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} -a \ if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} -a \
@ -288,9 +299,9 @@ fi
if [ -b "${FLAGS_to}" ]; then if [ -b "${FLAGS_to}" ]; then
# Output to a block device (i.e., a real USB key / SD card), so need sudo dd # Output to a block device (i.e., a real USB key / SD card), so need sudo dd
if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then 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 else
echo "Installing image ${SRC_IMAGE} to ${FLAGS_to}..." echo "Installing image ${SRC_IMAGE} to device ${FLAGS_to}..."
fi fi
# Warn if it looks like they supplied a partition as the destination. # 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!" warning_str="${warning_str}, which does not appear to be a USB/MMC disk!"
fi fi
warn "${warning_str}" warn "${warning_str}"
read -p "Are you sure (y/N)? " SURE are_you_sure
SURE="${SURE:0:1}" # Get just the first character
if [ "${SURE}" != "y" ]; then
echo "Ok, better safe than sorry."
exit
fi
fi fi
mount_list=$(mount | grep ^"${FLAGS_to}" | awk '{print $1}') mount_list=$(mount | grep ^"${FLAGS_to}" | awk '{print $1}')
@ -346,10 +352,13 @@ if [ -b "${FLAGS_to}" ]; then
--payload_image="${SRC_IMAGE}" \ --payload_image="${SRC_IMAGE}" \
--dst="${FLAGS_to}" --dst="${FLAGS_to}"
fi 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 else
# Output to a file, so just make a copy. # Output to a file, so just make a copy.
if [ "${SRC_IMAGE}" != "${FLAGS_to}" ]; then 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}" ${COMMON_PV_CAT} "${SRC_IMAGE}" >"${FLAGS_to}"
fi fi