diff --git a/build_image b/build_image index 3eb32e4a8c..2f418bf865 100755 --- a/build_image +++ b/build_image @@ -39,8 +39,6 @@ DEFINE_boolean statefuldev $FLAGS_TRUE \ "Install development packages on stateful partition rather than the rootfs" DEFINE_string to "" \ "The target image file or device" -DEFINE_boolean withtest $FLAGS_FALSE \ - "Include packages required for testing and prepare image for testing" DEFINE_string arm_extra_bootargs "" \ "Additional command line options to pass to the ARM kernel." @@ -167,34 +165,6 @@ delete_prompt() { # translate to /usr/local DEV_IMAGE_ROOT= -# Sets up symlinks for the stateful partition based on the root specified by -# ${1} and var directory specified by ${2}. -setup_symlinks_on_root() { - echo "Setting up symlinks on the stateful partition rooted at ${1} with"\ - "var directory located at ${2}" - - for path in usr local; do - if [ -h "${DEV_IMAGE_ROOT}/${path}" ] ; then - sudo unlink "${DEV_IMAGE_ROOT}/${path}" - elif [ -e "${DEV_IMAGE_ROOT}/${path}" ] ; then - echo "*** ERROR: ${DEV_IMAGE_ROOT}/${path} should be a symlink if exists" - return 1 - fi - sudo ln -s ${1} "${DEV_IMAGE_ROOT}/${path}" - done - - # Setup var. Var is on the stateful partition at /var for both non-developer - # builds and developer builds. - if [ -h "${DEV_IMAGE_ROOT}/var" ] ; then - sudo unlink "${DEV_IMAGE_ROOT}/var" - elif [ -e "${DEV_IMAGE_ROOT}/var" ] ; then - echo "*** ERROR: ${DEV_IMAGE_ROOT}/var should be a symlink if it exists" - return 1 - fi - - sudo ln -s "${2}" "${DEV_IMAGE_ROOT}/var" -} - trap "cleanup && delete_prompt" EXIT mkdir -p "$ROOT_FS_DIR" @@ -299,7 +269,8 @@ sudo mkdir -p "${STATEFUL_DIR}/var" # Create symlinks so that /usr/local/usr based directories are symlinked to # /usr/local/ directories e.g. /usr/local/usr/bin -> /usr/local/bin, etc. -setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_DIR}/var" +setup_symlinks_on_root "${DEV_IMAGE_ROOT}" "${STATEFUL_DIR}/var" \ + "${STATEFUL_DIR}" # Perform binding rather than symlinking because directories must exist # on rootfs so that we can bind at run-time since rootfs is read-only @@ -335,13 +306,6 @@ if [[ $FLAGS_withdev -eq $FLAGS_TRUE ]] ; then sudo cp -a "$(which ldd)" "${ROOT_DEV_DIR}/usr/bin" fi -# Install packages required for testing. -if [[ $FLAGS_withtest -eq $FLAGS_TRUE ]] ; then - sudo INSTALL_MASK="$INSTALL_MASK" emerge-${BOARD} \ - --root="$ROOT_DEV_DIR" --root-deps=rdeps \ - --usepkgonly chromeos-test $EMERGE_JOBS -fi - # Extract the kernel from the root filesystem for use by the GPT image. Legacy # BIOS will use the kernel in the rootfs (via syslinux), Chrome OS BIOS will # use the kernel partition. @@ -414,7 +378,7 @@ fi # Clean up symlinks so they work on a running target rooted at "/". # Here development packages are rooted at /usr/local. However, do not # create /usr/local or /var on host (already exist on target). -setup_symlinks_on_root "/usr/local" "/var" +setup_symlinks_on_root "/usr/local" "/var" "${STATEFUL_DIR}" # Cleanup loop devices. cleanup diff --git a/common.sh b/common.sh index 201679eb4b..9121a24f72 100644 --- a/common.sh +++ b/common.sh @@ -362,3 +362,44 @@ function safe_umount { sudo umount -d -l "${path}" || die "Failed to lazily unmount ${path}" fi } + +# Sets up symlinks for the developer root. It is necessary to symlink +# usr and local since the developer root is mounted at /usr/local and +# applications expect to be installed under /usr/local/bin, etc. +# This avoids packages installing into /usr/local/usr/local/bin. +# ${1} specifies the symlink target for the developer root. +# ${2} specifies the symlink target for the var directory. +# ${3} specifies the location of the stateful partition. +setup_symlinks_on_root() { + # Give args better names. + local dev_image_target=${1} + local var_target=${2} + local dev_image_root="${3}/dev_image" + + # If our var target is actually the standard var, we are cleaning up the + # symlinks (could also check for /usr/local for the dev_image_target). + if [ ${var_target} = "/var" ]; then + echo "Cleaning up /usr/local symlinks for ${dev_image_root}" + else + echo "Setting up symlinks for /usr/local for ${dev_image_root}" + fi + + # Set up symlinks that should point to ${dev_image_target}. + for path in usr local; do + if [ -h "${dev_image_root}/${path}" ]; then + sudo unlink "${dev_image_root}/${path}" + elif [ -e "${dev_image_root}/${path}" ]; then + die "${dev_image_root}/${path} should be a symlink if exists" + fi + sudo ln -s ${dev_image_target} "${dev_image_root}/${path}" + done + + # Setup var symlink. + if [ -h "${dev_image_root}/var" ]; then + sudo unlink "${dev_image_root}/var" + elif [ -e "${dev_image_root}/var" ]; then + die "${dev_image_root}/var should be a symlink if it exists" + fi + + sudo ln -s "${var_target}" "${dev_image_root}/var" +} diff --git a/mod_image_for_test.sh b/mod_image_for_test.sh index 8bcdcbd642..ddf060c7af 100755 --- a/mod_image_for_test.sh +++ b/mod_image_for_test.sh @@ -13,16 +13,24 @@ # Load functions and constants for chromeos-install . "$(dirname "$0")/chromeos-common.sh" +# We need to be in the chroot to emerge test packages. +assert_inside_chroot + get_default_board -DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built" -DEFINE_string qualdb "/tmp/run_remote_tests.*" \ - "Location of qualified component file" -DEFINE_string image "" "Location of the rootfs raw image file" -DEFINE_boolean factory $FLAGS_FALSE "Modify the image for manufacturing testing" +DEFINE_string board "$DEFAULT_BOARD" "Board for which the image was built" b +DEFINE_boolean factory $FLAGS_FALSE \ + "Modify the image for manufacturing testing" f DEFINE_boolean factory_install $FLAGS_FALSE \ "Modify the image for factory install shim" -DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts" "y" +DEFINE_string image "" "Location of the rootfs raw image file" i +DEFINE_boolean installmask $FLAGS_TRUE \ + "Use INSTALL_MASK to shrink the resulting image." m +DEFINE_integer jobs -1 \ + "How many packages to build in parallel at maximum." j +DEFINE_string qualdb "/tmp/run_remote_tests.*" \ + "Location of qualified component file" d +DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts" y # Parse command line FLAGS "$@" || exit 1 @@ -31,8 +39,7 @@ eval set -- "${FLAGS_ARGV}" # No board, no default and no image set then we can't find the image if [ -z $FLAGS_image ] && [ -z $FLAGS_board ] ; then setup_board_warning - echo "*** mod_image_for_test failed. No board set and no image set" - exit 1 + die "mod_image_for_test failed. No board set and no image set" fi # We have a board name but no image set. Use image at default location @@ -42,6 +49,9 @@ if [ -z $FLAGS_image ] ; then FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}" fi +# Turn path into an absolute path. +FLAGS_image=`eval readlink -f ${FLAGS_image}` + # Abort early if we can't find the image if [ ! -f $FLAGS_image ] ; then echo "No image found at $FLAGS_image" @@ -62,33 +72,23 @@ cleanup_mounts() { done } -cleanup_loop() { - sudo umount "$1" - sleep 1 # in case the loop device is in use - sudo losetup -d "$1" +cleanup() { + "$SCRIPTS_DIR/mount_gpt_image.sh" -u -r "$ROOT_FS_DIR" -s "$STATEFUL_DIR" } -cleanup() { - # Disable die on error. - set +e - - cleanup_mounts "${ROOT_FS_DIR}" - if [ -n "${ROOT_LOOP_DEV}" ] - then - sudo umount "${ROOT_FS_DIR}/var" - cleanup_loop "${ROOT_LOOP_DEV}" +# Emerges chromeos-test onto the image. +emerge_chromeos_test() { + INSTALL_MASK="" + if [[ $FLAGS_installmask -eq ${FLAGS_TRUE} ]]; then + INSTALL_MASK="$DEFAULT_INSTALL_MASK" fi - rmdir "${ROOT_FS_DIR}" - cleanup_mounts "${STATEFUL_DIR}" - if [ -n "${STATEFUL_LOOP_DEV}" ] - then - cleanup_loop "${STATEFUL_LOOP_DEV}" - fi - rmdir "${STATEFUL_DIR}" + # Determine the root dir for test packages. + ROOT_DEV_DIR="$ROOT_FS_DIR/usr/local" - # Turn die on error back on. - set -e + INSTALL_MASK="$INSTALL_MASK" emerge-${FLAGS_board} \ + --root="$ROOT_DEV_DIR" --root-deps=rdeps \ + --usepkgonly chromeos-test $EMERGE_JOBS } # main process begins here. @@ -107,38 +107,19 @@ fi set -e -ROOT_FS_DIR=$(dirname "${FLAGS_image}")/rootfs -mkdir -p "${ROOT_FS_DIR}" - -STATEFUL_DIR=$(dirname "${FLAGS_image}")/stateful_partition -mkdir -p "${STATEFUL_DIR}" +IMAGE_DIR="$(dirname "$FLAGS_image")" +IMAGE_NAME="$(basename "$FLAGS_image")" +ROOT_FS_DIR="$IMAGE_DIR/rootfs" +STATEFUL_DIR="$IMAGE_DIR/stateful_partition" +SCRIPTS_DIR=$(dirname "$0") trap cleanup EXIT -# Figure out how to loop mount the rootfs partition. It should be partition 3 -# on the disk image. -offset=$(partoffset "${FLAGS_image}" 3) +# 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" -ROOT_LOOP_DEV=$(sudo losetup -f) -if [ -z "$ROOT_LOOP_DEV" ]; then - echo "No free loop device" - exit 1 -fi -sudo losetup -o $(( $offset * 512 )) "${ROOT_LOOP_DEV}" "${FLAGS_image}" -sudo mount "${ROOT_LOOP_DEV}" "${ROOT_FS_DIR}" - -# The stateful partition should be partition 1 on the disk image. -offset=$(partoffset "${FLAGS_image}" 1) - -STATEFUL_LOOP_DEV=$(sudo losetup -f) -if [ -z "$STATEFUL_LOOP_DEV" ]; then - echo "No free loop device" - exit 1 -fi -sudo losetup -o $(( $offset * 512 )) "${STATEFUL_LOOP_DEV}" "${FLAGS_image}" -sudo mount "${STATEFUL_LOOP_DEV}" "${STATEFUL_DIR}" -sudo mount --bind "${STATEFUL_DIR}/var" "${ROOT_FS_DIR}/var" -STATEFUL_DIR="${STATEFUL_DIR}" +emerge_chromeos_test MOD_TEST_ROOT="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts" # Run test setup script to modify the image @@ -159,7 +140,7 @@ if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ]; then --root-deps=rdeps chromeos-factoryinstall # Set factory server if necessary. - if [ "${FACTORY_SERVER}" != "" ]; then + if [ "${FACTORY_SERVER}" != "" ]; then sudo sed -i \ "s/CHROMEOS_AUSERVER=.*$/CHROMEOS_AUSERVER=\ http:\/\/${FACTORY_SERVER}:8080\/update/" \ diff --git a/mount_gpt_image.sh b/mount_gpt_image.sh index 7d59ef4496..a739675d49 100755 --- a/mount_gpt_image.sh +++ b/mount_gpt_image.sh @@ -9,6 +9,9 @@ . "$(dirname "$0")/common.sh" +# For functions related to gpt images. +. "$(dirname "$0")/chromeos-common.sh" + get_default_board # Flags. @@ -18,7 +21,8 @@ DEFINE_boolean unmount $FLAGS_FALSE \ "Unmount previously mounted dir." u DEFINE_string from "/dev/sdc" \ "Directory containing image or device with image on it" f -DEFINE_boolean test $FLAGS_FALSE "Use chromiumos_test_image.bin" t +DEFINE_string image "chromiumos_image.bin"\ + "Name of the bin file if a directory is specified in the from flag" i DEFINE_string "rootfs_mountpt" "/tmp/m" "Mount point for rootfs" "r" DEFINE_string "stateful_mountpt" "/tmp/s" \ "Mount point for stateful partition" "s" @@ -31,13 +35,16 @@ eval set -- "${FLAGS_ARGV}" # Die on error set -e -# Common umounts for either a device or directory -function unmount_common() { +# Common unmounts for either a device or directory +function unmount_image() { echo "Unmounting image from ${FLAGS_stateful_mountpt}" \ "and ${FLAGS_rootfs_mountpt}" # Don't die on error to force cleanup set +e if [ -e "${FLAGS_rootfs_mountpt}/root/.dev_mode" ] ; then + # Reset symlinks in /usr/local. + setup_symlinks_on_root "/usr/local" "/var" \ + "${FLAGS_stateful_mountpt}" sudo umount "${FLAGS_rootfs_mountpt}/usr/local" fi sudo umount "${FLAGS_rootfs_mountpt}/var" @@ -46,55 +53,63 @@ function unmount_common() { set -e } -# Sets up the rootfs and stateful partitions specified by -# ${FLAGS_from}${prefix}[31] to the given mount points and sets up var and -# usr/local -# ${1} - prefix name for the partition -# ${2} - extra mount options for the partitions -function mount_common() { +function get_usb_partitions() { + sudo mount "${FLAGS_from}3" "${FLAGS_rootfs_mountpt}" + sudo mount "${FLAGS_from}1" "${FLAGS_stateful_mountpt}" +} + +function get_gpt_partitions() { + local filename="${FLAGS_image}" + + # Mount the rootfs partition using a loopback device. + local offset=$(partoffset "${FLAGS_from}/${filename}" 3) + sudo mount -o loop,offset=$(( offset * 512 )) "${FLAGS_from}/${filename}" \ + "${FLAGS_rootfs_mountpt}" + + # Mount the stateful partition using a loopback device. + offset=$(partoffset "${FLAGS_from}/${filename}" 1) + sudo mount -o loop,offset=$(( offset * 512 )) "${FLAGS_from}/${filename}" \ + "${FLAGS_stateful_mountpt}" +} + +# Mount a gpt based image. +function mount_image() { mkdir -p "${FLAGS_rootfs_mountpt}" mkdir -p "${FLAGS_stateful_mountpt}" - sudo mount ${2} "${FLAGS_from}${1}3" "${FLAGS_rootfs_mountpt}" - sudo mount ${2} "${FLAGS_from}${1}1" "${FLAGS_stateful_mountpt}" + + # Get the partitions for the image / device. + if [ -b ${FLAGS_from} ] ; then + get_usb_partitions + else + get_gpt_partitions + fi + + # Mount var unconditionally and then setup /usr/local for user of dev mode. sudo mount --bind "${FLAGS_stateful_mountpt}/var" \ "${FLAGS_rootfs_mountpt}/var" if [ -e "${FLAGS_rootfs_mountpt}/root/.dev_mode" ] ; then sudo mount --bind "${FLAGS_stateful_mountpt}/dev_image" \ "${FLAGS_rootfs_mountpt}/usr/local" + # Setup symlinks in /usr/local so you can emerge packages into /usr/local. + setup_symlinks_on_root "${FLAGS_stateful_mountpt}/dev_image" \ + "${FLAGS_stateful_mountpt}/var" "${FLAGS_stateful_mountpt}" fi - echo "Root FS specified by "${FLAGS_from}${1}3" mounted at"\ + echo "Image specified by ${FLAGS_from} mounted at"\ "${FLAGS_rootfs_mountpt} successfully." } -# Find the last image built on the board +# Find the last image built on the board. if [ ${FLAGS_most_recent} -eq ${FLAGS_TRUE} ] ; then IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" FLAGS_from="${IMAGES_DIR}/$(ls -t ${IMAGES_DIR} 2>&-| head -1)" fi -# Turn into absolute path +# Turn path into an absolute path. FLAGS_from=`eval readlink -f ${FLAGS_from}` -# Set the file name of the image if ${FLAGS_from} is not a device and cd -if [ -d "${FLAGS_from}" ] ; then - cd "${FLAGS_from}" - IMAGE_NAME=chromiumos_image.bin - [ ${FLAGS_test} -eq ${FLAGS_TRUE} ] && IMAGE_NAME=chromiumos_test_image.bin -fi - +# Perform desired operation. if [ ${FLAGS_unmount} -eq ${FLAGS_TRUE} ] ; then - unmount_common - if [ -d "${FLAGS_from}" ] ; then - echo "Re-packing partitions onto ${FLAGS_from}/${IMAGE_NAME}" - ./pack_partitions.sh ${IMAGE_NAME} 2> /dev/null - sudo rm part_* - fi + unmount_image else - if [ -b ${FLAGS_from} ] ; then - mount_common "" "" - else - echo "Unpacking partitions from ${FLAGS_from}/${IMAGE_NAME}" - ./unpack_partitions.sh ${IMAGE_NAME} 2> /dev/null - mount_common "/part_" "-o loop" - fi + mount_image fi