diff --git a/build_image b/build_image index 1068d209c4..1109b6eb9d 100755 --- a/build_image +++ b/build_image @@ -89,6 +89,7 @@ OVERLAY_CHROMEOS_DIR="${SRC_ROOT}/third_party/chromiumos-overlay/chromeos" . "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1 . "${BUILD_LIBRARY_DIR}/build_gpt.sh" || exit 1 +. "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1 . "${BUILD_LIBRARY_DIR}/test_image_content.sh" || exit 1 @@ -288,7 +289,7 @@ cleanup_stateful_fs_loop() { sudo umount -d "${STATEFUL_FS_DIR}" } -cleanup() { +loopback_cleanup() { # Disable die on error. set +e @@ -327,18 +328,6 @@ delete_prompt() { fi } -# $1 - Directory where developer rootfs is mounted. -# $2 - Directory where developer stateful_partition is mounted. -# $3 - Directory where the ESP partition is mounted. -mount_gpt_cleanup() { - local rootfs="${1-$ROOT_FS_DIR}" - local statefs="${2-$STATEFUL_FS_DIR}" - local espfs="${3-$ESP_FS_DIR}" - "${SCRIPTS_DIR}/mount_gpt_image.sh" \ - -u -r "${rootfs}" -s "${statefs}" -e "${espfs}" - delete_prompt -} - # Takes no arguments and populates the configuration for # cros_make_image_bootable. create_boot_desc() { @@ -368,11 +357,10 @@ install_dev_packages() { echo "Adding developer packages to ${image_name}" - trap "mount_gpt_cleanup" EXIT + trap "unmount_image ; delete_prompt" EXIT - ${SCRIPTS_DIR}/mount_gpt_image.sh --from "${OUTPUT_DIR}" \ - --image "${image_name}" -r "${ROOT_FS_DIR}" \ - -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}" + mount_image "${OUTPUT_DIR}/${image_name}" "${ROOT_FS_DIR}" \ + "${STATEFUL_FS_DIR}" "${ESP_FS_DIR}" # Determine the root dir for developer packages. local root_dev_dir="${ROOT_FS_DIR}" @@ -445,9 +433,8 @@ install_dev_packages() { fi echo "Developer image built and stored at ${image_name}" + unmount_image trap - EXIT - ${SCRIPTS_DIR}/mount_gpt_image.sh -u -r "${ROOT_FS_DIR}" \ - -s "${STATEFUL_FS_DIR}" -e "${ESP_FS_DIR}" } zero_free_space() { @@ -462,7 +449,7 @@ zero_free_space() { create_base_image() { local image_name=$1 - trap "cleanup && delete_prompt" EXIT + trap "loopback_cleanup && delete_prompt" EXIT # Create and format the root file system. @@ -616,8 +603,7 @@ create_base_image() { # payloads become smaller zero_free_space "${ROOT_FS_DIR}" - cleanup - + loopback_cleanup trap delete_prompt EXIT # Now that the filler file has been sync'd to disk and has filled up all free @@ -647,11 +633,6 @@ generate_au_zip () { $lgenerateauzip $largs } -mod_image_for_test_cleanup() { - "${SCRIPTS_DIR}/mount_gpt_image.sh" -u -r "${ROOT_FS_DIR}" \ - -s "${STATEFUL_FS_DIR}" -} - # Emerges chromeos-test onto the image. emerge_chromeos_test() { # Determine the root dir for test packages. @@ -714,11 +695,10 @@ mod_image_for_test () { local image_dir=$(dirname ${test_pathname}) local image_name=$(basename ${test_pathname}) - trap mod_image_for_test_cleanup EXIT + trap unmount_image EXIT - # Mounts gpt image and sets up var, /usr/local and symlinks. - "${SCRIPTS_DIR}/mount_gpt_image.sh" -i "${image_name}" -f "${image_dir}" \ - -r "${ROOT_FS_DIR}" -s "${STATEFUL_FS_DIR}" + mount_image "${image_dir}/${image_name}" \ + "${ROOT_FS_DIR}" "${STATEFUL_FS_DIR}" emerge_chromeos_test @@ -748,15 +728,13 @@ mod_image_for_test () { # Re-run ldconfig to fix /etc/ldconfig.so.cache. sudo ldconfig -r "${ROOT_FS_DIR}" - mod_image_for_test_cleanup + unmount_image + trap - EXIT # Now make it bootable with the flags from build_image - "${SCRIPTS_DIR}/bin/cros_make_image_bootable" "${image_dir}" "${image_name}" \ - --force_developer_mode - - print_time_elapsed - - trap - EXIT + "${SCRIPTS_DIR}/bin/cros_make_image_bootable" "${image_dir}" \ + "${image_name}" \ + --force_developer_mode } # Create the output directory. diff --git a/build_library/mount_gpt_util.sh b/build_library/mount_gpt_util.sh new file mode 100755 index 0000000000..bf83e281c3 --- /dev/null +++ b/build_library/mount_gpt_util.sh @@ -0,0 +1,35 @@ +# 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. + +# This global array variable is used to remember options from +# mount_image so that unmount_image can do its job. +MOUNT_GPT_OPTIONS=( ) + +# mount_image - Mount the root, stateful, and optionally ESP partitions +# in a Chromium OS image. +# $1: path to image to be mounted +# $2: path to root fs mount point +# $3: path to stateful fs mount point +# $4: path to ESP fs mount point; if empty the ESP will not be mounted +mount_image() { + local image_dir="$(dirname $1)" + local image="$(basename $1)" + MOUNT_GPT_OPTIONS=( -r "$2" -s "$3" ) + + if [ $# -ge 4 ]; then + MOUNT_GPT_OPTIONS=( "${MOUNT_GPT_OPTIONS[@]}" -e "$4" ) + fi + + "${SCRIPTS_DIR}/mount_gpt_image.sh" --from="$image_dir" --image="$image" \ + "${MOUNT_GPT_OPTIONS[@]}" +} + +# unmount_image - Unmount the file systems mounted in the previous +# call to mount_image. +# No arguments +unmount_image() { + "${SCRIPTS_DIR}/mount_gpt_image.sh" -u "${MOUNT_GPT_OPTIONS[@]}" + + MOUNT_GPT_OPTIONS=( ) +} diff --git a/mod_image_for_test.sh b/mod_image_for_test.sh index f08377ebe4..348bd9bbe6 100755 --- a/mod_image_for_test.sh +++ b/mod_image_for_test.sh @@ -53,7 +53,8 @@ eval set -- "$FLAGS_ARGV" # so will die prematurely if 'set -e' is specified before now. set -e -. "${SCRIPT_ROOT}/build_library/board_options.sh" || exit 1 +. "${BUILD_LIBRARY_DIR}/board_options.sh" || exit 1 +. "${BUILD_LIBRARY_DIR}/mount_gpt_util.sh" || exit 1 EMERGE_BOARD_CMD="emerge-$BOARD" @@ -76,23 +77,6 @@ fi # Turn path into an absolute path. FLAGS_image=$(eval readlink -f "$FLAGS_image") -# Make sure anything mounted in the rootfs/stateful is cleaned up ok on exit. -cleanup_mounts() { - # Occasionally there are some daemons left hanging around that have our - # root/stateful image file system open. We do a best effort attempt to kill - # them. - PIDS=$(sudo lsof -t "$1" | sort | uniq) - for pid in $PIDS; do - local cmdline=$(cat /proc/$pid/cmdline) - echo "Killing process that has open file on the mounted directory: $cmdline" - sudo kill $pid || true - done -} - -cleanup() { - "$SCRIPTS_DIR/mount_gpt_image.sh" -u -r "$ROOT_FS_DIR" -s "$STATEFUL_DIR" -} - # Emerges chromeos-test onto the image. emerge_chromeos_test() { INSTALL_MASK="" @@ -190,11 +174,10 @@ IMAGE_NAME=$(basename "$FLAGS_image") ROOT_FS_DIR="$IMAGE_DIR/rootfs" STATEFUL_DIR="$IMAGE_DIR/stateful_partition" -trap cleanup EXIT +trap unmount_image EXIT # Mounts gpt image and sets up var, /usr/local and symlinks. -"$SCRIPTS_DIR/mount_gpt_image.sh" -i "$IMAGE_NAME" -f "$IMAGE_DIR" \ - -r "$ROOT_FS_DIR" -s "$STATEFUL_DIR" +mount_image "$FLAGS_image" "$ROOT_FS_DIR" "$STATEFUL_DIR" emerge_chromeos_test @@ -231,14 +214,11 @@ if [ "$VERIFY" = "true" ]; then popd fi -cleanup +unmount_image +trap - EXIT # Now make it bootable with the flags from build_image "$SCRIPTS_DIR/bin/cros_make_image_bootable" "$(dirname "$FLAGS_image")" \ "$(basename "$FLAGS_image")" \ --force_developer_mode - - print_time_elapsed - -trap - EXIT