From 4fc5227fac085dc36478af913db82c17c2c9ff69 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Wed, 8 Dec 2010 14:22:11 -0800 Subject: [PATCH] Factored out the code to copy an image and modify it for test into mod_image_for_test.sh rather than slightly different versions of the same in image_to_usb.sh and image_to_vm.sh Added a function to get a test image into common.sh Added --inplace option to mod_image_for_test, which is the default, and preserves the original behaviour. But using --noinplace it will now do the copy for you. Found that chromiumos_image.bin appears throughout the scripts, so added it and the test variant to common.sh BUG=chromiumos-10126 TEST=run mod_image_for_test.sh with and without --noinplace run image_to_usb.sh and image_to_vm.sh with both options test on Seaboard that correct image is provided Really we should have automated testing for these scripts Change-Id: I5cfa91792c7fded35e7f4ca8f8f27c6b270817fb Review URL: http://codereview.chromium.org/5271010 --- common.sh | 57 +++++++++++++++++++++++++++++++++++++ image_to_usb.sh | 66 ++++++++++++------------------------------- image_to_vm.sh | 42 ++++++--------------------- mod_image_for_test.sh | 27 +++++++++++++++++- 4 files changed, 110 insertions(+), 82 deletions(-) diff --git a/common.sh b/common.sh index a11f257289..6b55b707dc 100644 --- a/common.sh +++ b/common.sh @@ -14,6 +14,14 @@ # and dpkg-buildpackage NUM_JOBS=`grep -c "^processor" /proc/cpuinfo` +# True if we have the 'pv' utility - also set up COMMON_PV_CAT for convenience +COMMON_PV_OK=1 +COMMON_PV_CAT=pv +pv -V >/dev/null 2>&1 || COMMON_PV_OK=0 +if [ $COMMON_PV_OK -eq 0 ]; then + COMMON_PV_CAT=cat +fi + # Store location of the calling script. TOP_SCRIPT_DIR="${TOP_SCRIPT_DIR:-$(dirname $0)}" @@ -124,6 +132,12 @@ else INSIDE_CHROOT=0 fi + +# Standard filenames +CHROMEOS_IMAGE_NAME="chromiumos_image.bin" +CHROMEOS_TEST_IMAGE_NAME="chromiumos_test_image.bin" + + # Directory locations inside the dev chroot CHROOT_TRUNK_DIR="/home/$USER/trunk" @@ -527,3 +541,46 @@ chroot_hacks_from_outside() { sudo bash -c "echo root ALL=\(ALL\) ALL >> \"${chroot_dir}/etc/sudoers\"" fi } + +# This function converts a chromiumos image into a test image, either +# in place or by copying to a new test image filename first. It honors +# the following flags (see mod_image_for_test.sh) +# +# --factory +# --factory_install +# --force_copy +# +# On entry, pass the directory containing the image, and the image filename +# On exit, it puts the pathname of the resulting test image into +# CHROMEOS_RETURN_VAL +# (yes this is ugly, but perhaps less ugly than the alternatives) +# +# Usage: +# SRC_IMAGE=$(prepare_test_image "directory" "imagefile") +prepare_test_image() { + # If we're asked to modify the image for test, then let's make a copy and + # modify that instead. + # Check for manufacturing image. + local args + + if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then + args="--factory" + fi + + # Check for install shim. + if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then + args="--factory_install" + fi + + # Check for forcing copy of image + if [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ]; then + args="${args} --force_copy" + fi + + # Modify the image for test, creating a new test image + "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} \ + --image="$1/$2" --noinplace ${args} + + # From now on we use the just-created test image + CHROMEOS_RETURN_VAL="$1/${CHROMEOS_TEST_IMAGE_NAME}" +} diff --git a/image_to_usb.sh b/image_to_usb.sh index 19eefb1f63..4b32ff8f88 100755 --- a/image_to_usb.sh +++ b/image_to_usb.sh @@ -1,6 +1,6 @@ #!/bin/bash -# Copyright (c) 2009 The Chromium OS Authors. All rights reserved. +# Copyright (c) 2010 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. @@ -18,7 +18,7 @@ get_default_board # Flags DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built" DEFINE_string from "" \ - "Directory containing chromiumos_image.bin" + "Directory containing ${CHROMEOS_IMAGE_NAME}" DEFINE_string to "/dev/sdX" "${DEFAULT_TO_HELP}" DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y" DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" @@ -31,8 +31,8 @@ DEFINE_boolean factory ${FLAGS_FALSE} \ DEFINE_boolean copy_kernel ${FLAGS_FALSE} \ "Copy the kernel to the fourth partition." DEFINE_boolean test_image "${FLAGS_FALSE}" \ - "Copies normal image to chromiumos_test_image.bin, modifies it for test." -DEFINE_string image_name "chromiumos_image.bin" \ + "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test." +DEFINE_string image_name "${CHROMEOS_IMAGE_NAME}" \ "Base name of the image" i DEFINE_string build_root "/build" \ "The root location for board sysroots." @@ -81,6 +81,8 @@ fi # We have a board name but no image set. Use image at default location if [ -z "${FLAGS_from}" ]; then IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" + + # Get latest image directory FLAGS_from="${IMAGES_DIR}/$(ls -t ${IMAGES_DIR} 2>&-| head -1)" fi @@ -131,9 +133,6 @@ if [ -b "${FLAGS_to}" ]; then fi fi -# Use this image as the source image to copy -SRC_IMAGE="${FLAGS_from}/${FLAGS_image_name}" - STATEFUL_DIR="${FLAGS_from}/stateful_partition" mkdir -p "${STATEFUL_DIR}" @@ -147,41 +146,18 @@ function do_cleanup { fi } - -# If we're asked to modify the image for test, then let's make a copy and -# modify that instead. if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then - if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ - [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then - # Copy it. - echo "Creating test image from original..." - cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin" + # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL + prepare_test_image "${FLAGS_from}" "${FLAGS_image_name}" + SRC_IMAGE="${CHROMEOS_RETURN_VAL}" - # Check for manufacturing image. - if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then - EXTRA_ARGS="--factory" - fi - - # Check for instqall shim. - if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then - EXTRA_ARGS="--factory_install" - fi - - # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we - # really want to modify the image; the user gave their assent already with - # --test-image and the original image is going to be preserved. - "${SCRIPTS_DIR}/mod_image_for_test.sh" --image \ - "${FLAGS_from}/chromiumos_test_image.bin" --board=${FLAGS_board} \ - ${EXTRA_ARGS} --yes - echo "Done with mod_image_for_test." - else - echo "Using cached test image." - fi - SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin" - echo "Source test image is: ${SRC_IMAGE}" + # No need to check with the user, as we are preserving the base image + FLAGS_yes="${FLAGS_TRUE}" +else + # Use the standard image + SRC_IMAGE="${FLAGS_from}/${FLAGS_image_name}" fi - # Let's do it. if [ -b "${FLAGS_to}" ] then @@ -235,15 +211,9 @@ then sleep 3 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then - echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." - if type pv >/dev/null 2>&1; then - # pv displays file size in k=1024 while dd uses k=1000. - # To prevent confusion, we suppress the summary report from dd. - sudo pv -ptreb -B 4m "${SRC_IMAGE}" | - sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer - else - sudo dd if="${SRC_IMAGE}" of="${FLAGS_to}" bs=4M oflag=sync - fi + echo "Copying with dd ${SRC_IMAGE} to ${FLAGS_to}..." + sudo ${COMMON_PV_CAT} "${SRC_IMAGE}" | + sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer sync else if [ ${INSIDE_CHROOT} -ne 1 ]; then @@ -264,7 +234,7 @@ then else # Output to a file, so just make a copy. echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." - cp -f "${SRC_IMAGE}" "${FLAGS_to}" + ${COMMON_PV_CAT} "${SRC_IMAGE}" >"${FLAGS_to}" echo "Done. To copy to a USB drive, do something like:" echo " sudo dd if=${FLAGS_to} of=/dev/sdX bs=4M oflag=sync" diff --git a/image_to_vm.sh b/image_to_vm.sh index 2ff4984607..12f421a95e 100755 --- a/image_to_vm.sh +++ b/image_to_vm.sh @@ -10,6 +10,8 @@ # Load common constants. This should be the first executable line. # The path to common.sh should be relative to your script's location. . "$(dirname "$0")/common.sh" + +# Load functions and constants for chromeos-install . "$(dirname "$0")/chromeos-common.sh" . "$(dirname "$0")/lib/cros_vm_constants.sh" @@ -40,7 +42,7 @@ DEFINE_string state_image "" \ DEFINE_integer statefulfs_size 2048 \ "Stateful partition size in MBs." DEFINE_boolean test_image "${FLAGS_FALSE}" \ - "Copies normal image to chromiumos_test_image.bin, modifies it for test." + "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test." DEFINE_string to "" \ "Destination folder for VM output file(s)" DEFINE_string vbox_disk "${DEFAULT_VBOX_DISK}" \ @@ -84,39 +86,13 @@ if [ -z "${FLAGS_to}" ] ; then FLAGS_to="${FLAGS_from}" fi -# Use this image as the source image to copy -SRC_IMAGE="${FLAGS_from}/chromiumos_image.bin" - -# If we're asked to modify the image for test, then let's make a copy and -# modify that instead. if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then - if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ - [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then - # Copy it. - echo "Creating test image from original..." - cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin" - - # Check for manufacturing image. - if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then - EXTRA_ARGS="--factory" - fi - - # Check for install shim. - if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then - EXTRA_ARGS="--factory_install" - fi - - # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we - # really want to modify the image; the user gave their assent already with - # --test-image and the original image is going to be preserved. - "${SCRIPTS_DIR}/mod_image_for_test.sh" --board=${FLAGS_board} --image \ - "${FLAGS_from}/chromiumos_test_image.bin" ${EXTRA_ARGS} --yes - echo "Done with mod_image_for_test." - else - echo "Using cached test image." - fi - SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin" - echo "Source test image is: ${SRC_IMAGE}" + # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL + prepare_test_image "${FLAGS_from}" "${CHROMEOS_IMAGE_NAME}" + SRC_IMAGE="${CHROMEOS_RETURN_VAL}" +else + # Use the standard image + SRC_IMAGE="${FLAGS_from}/${CHROMEOS_IMAGE_NAME}" fi # Memory units are in MBs diff --git a/mod_image_for_test.sh b/mod_image_for_test.sh index 97aa5e0cc7..52bd99d3a7 100755 --- a/mod_image_for_test.sh +++ b/mod_image_for_test.sh @@ -33,6 +33,12 @@ DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts" y DEFINE_string build_root "/build" \ "The root location for board sysroots." DEFINE_boolean fast ${DEFAULT_FAST} "Call many emerges in parallel" +DEFINE_boolean inplace $FLAGS_TRUE \ + "Modify/overwrite the image ${CHROMEOS_IMAGE_NAME} in place. \ +Otherwise the image will be copied to ${CHROMEOS_TEST_IMAGE_NAME} \ +if needed, and modified there" +DEFINE_boolean force_copy ${FLAGS_FALSE} \ + "Always rebuild test image if --noinplace" # Parse command line @@ -56,7 +62,7 @@ fi # We have a board name but no image set. Use image at default location if [ -z $FLAGS_image ] ; then IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" - FILENAME="chromiumos_image.bin" + FILENAME="${CHROMEOS_IMAGE_NAME}" FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}" fi @@ -155,6 +161,25 @@ install_autotest() { # main process begins here. +IMAGE_DIR="$(dirname "${FLAGS_image}")" + +# Copy the image to a test location if required +if [ ${FLAGS_inplace} -eq ${FLAGS_FALSE} ]; then + TEST_PATHNAME="${IMAGE_DIR}/${CHROMEOS_TEST_IMAGE_NAME}" + if [ ! -f "${TEST_PATHNAME}" ] || \ + [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then + echo "Creating test image from original..." + ${COMMON_PV_CAT} "${FLAGS_image}" >"${TEST_PATHNAME}" \ + || die "Cannot copy ${FLAGS_image} to test image" + FLAGS_image="${TEST_PATHNAME}" + else + echo "Using cached test image" + fi + + # No need to confirm now, since we are not overwriting the main image + FLAGS_yes="$FLAGS_TRUE" +fi + # Make sure this is really what the user wants, before nuking the device if [ $FLAGS_yes -ne $FLAGS_TRUE ]; then read -p "Modifying image ${FLAGS_image} for test; are you sure (y/N)? " SURE