Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Chris Sosa | 96bd5f6 | 2010-04-09 08:54:26 -0700 | [diff] [blame] | 7 | # Script that emerges packages and their dependencies from a host development |
Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 8 | # machine onto a target Chromium OS device |
| 9 | # |
| 10 | # NOTE: This script must be run from the chromiumos build chroot environment. |
| 11 | # |
| 12 | |
| 13 | # Load common constants. This should be the first executable line. |
| 14 | # The path to common.sh should be relative to your script's location. |
| 15 | . "$(dirname "$0")/../../scripts/common.sh" |
| 16 | |
| 17 | # Script must be run inside the chroot |
| 18 | assert_inside_chroot |
| 19 | |
| 20 | get_default_board |
| 21 | |
| 22 | # Flags |
| 23 | DEFINE_string board "$DEFAULT_BOARD" \ |
| 24 | "The board to build packages for." |
| 25 | DEFINE_boolean usepkg $FLAGS_FALSE \ |
| 26 | "Use binary packages to bootstrap build when possible." |
| 27 | DEFINE_string build_root "/build" \ |
| 28 | "The root location for board sysroots." |
| 29 | DEFINE_integer jobs -1 \ |
| 30 | "How many packages to build in parallel at maximum." |
| 31 | DEFINE_boolean unmerge $FLAGS_FALSE \ |
| 32 | "Whether to unmerge the package versus emerge it." |
| 33 | DEFINE_integer retries -1 \ |
| 34 | "On build failure, the number of times to retry" |
| 35 | DEFINE_boolean onstatefuldev $FLAGS_TRUE \ |
| 36 | "Build packages and install them into stateful partition on device." |
| 37 | DEFINE_boolean installmask $FLAGS_TRUE \ |
| 38 | "Use INSTALL_MASK to shrink the resulting image." |
| 39 | DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" |
| 40 | |
| 41 | # Parse command line |
| 42 | FLAGS_HELP="usage: $0 [flags]" |
| 43 | FLAGS "$@" || exit 1 |
| 44 | eval set -- "${FLAGS_ARGV}" |
| 45 | |
| 46 | # Die on any errors. |
| 47 | set -e |
| 48 | |
| 49 | # Checks $1 and prints out $2 is required before exiting if $1 does not exist |
| 50 | check_args() { |
| 51 | if [ -z "${1}" ] ; then |
| 52 | echo "Error: ${2} is required." |
| 53 | exit 1 |
| 54 | fi |
| 55 | } |
| 56 | |
| 57 | # Clean up function for mount point |
| 58 | cleanup_sshfs_mount() { |
| 59 | echo "Cleaning up mount point" |
Chris Sosa | 96bd5f6 | 2010-04-09 08:54:26 -0700 | [diff] [blame] | 60 | sudo umount "${REMOTE_ROOT_DIR}" |
Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | check_args "${FLAGS_board}" |
| 64 | check_args "${FLAGS_ARGV}" "Emerge package name" |
| 65 | |
Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 66 | EMERGE_FLAGS="" |
Chris Sosa | 96bd5f6 | 2010-04-09 08:54:26 -0700 | [diff] [blame] | 67 | [[ $FLAGS_usepkg -eq $FLAGS_TRUE ]] && |
Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 68 | EMERGE_FLAGS="${EMERGE_FLAGS} --getbinpkg --usepkg --with-bdeps y" |
| 69 | |
| 70 | INSTALL_MASK="" |
| 71 | [[ ${FLAGS_installmask} -eq ${FLAGS_TRUE} ]] && \ |
| 72 | INSTALL_MASK="${DEFAULT_INSTALL_MASK}" |
| 73 | |
| 74 | EMERGE_JOBS= |
| 75 | [[ ${FLAGS_jobs} -ne -1 ]] && EMERGE_JOBS="--jobs=${FLAGS_jobs}" |
| 76 | |
| 77 | # Duplicate from build_packages to get host packages on system. |
| 78 | |
| 79 | # Skip building on host if we are unmerging. |
| 80 | if [ ${FLAGS_unmerge} -eq ${FLAGS_FALSE} ] ; then |
| 81 | eretry sudo emerge -uDNv $EMERGE_FLAGS world |
| 82 | |
| 83 | # Build request and install them onto the sysroot |
| 84 | for emerge_request in $FLAGS_ARGV; do |
| 85 | eretry sudo emerge-${FLAGS_board} -uDNv \ |
| 86 | $(remove_quotes "${emerge_request}") |
| 87 | done |
| 88 | fi |
| 89 | |
| 90 | # Name is unique per client. This allows one to run gmergefs in parallel |
| 91 | # with multiple clients. |
| 92 | REMOTE_ROOT_DIR="${FLAGS_build_root}/${FLAGS_board}/gmergefs/${FLAGS_remote}" |
| 93 | |
| 94 | # Default is to install on stateful dev, otherwise install on root. |
| 95 | REMOTE_RELATIVE_PATH="" |
| 96 | [ ${FLAGS_onstatefuldev} -eq $FLAGS_TRUE ] && \ |
| 97 | REMOTE_RELATIVE_PATH="mnt/stateful_partition/dev_image" |
| 98 | REMOTE_PATH="${REMOTE_ROOT_DIR}/${REMOTE_RELATIVE_PATH}" |
| 99 | |
| 100 | # Create directory for remote image |
| 101 | sudo mkdir -p "${REMOTE_ROOT_DIR}" |
| 102 | |
| 103 | # Mount remote fs into chroot |
| 104 | echo "Enter password for remote machine" |
| 105 | sudo sshfs -o idmap=user -o allow_other -o nonempty -o transform_symlinks \ |
| 106 | "root@${FLAGS_remote}:/" "${REMOTE_ROOT_DIR}" |
Chris Sosa | 96bd5f6 | 2010-04-09 08:54:26 -0700 | [diff] [blame] | 107 | |
Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 108 | trap cleanup_sshfs_mount EXIT |
| 109 | |
| 110 | # Ensure root path is created for emerge |
| 111 | sudo mkdir -p "${REMOTE_PATH}" |
| 112 | |
| 113 | # Same arguments used in build_image to emerge these packages onto the target |
| 114 | for emerge_request in $FLAGS_ARGV; do |
| 115 | if [ ${FLAGS_unmerge} -eq ${FLAGS_FALSE} ] ; then |
| 116 | sudo INSTALL_MASK="$INSTALL_MASK" emerge-${FLAGS_board} \ |
| 117 | --root="${REMOTE_PATH}" --root-deps=rdeps \ |
| 118 | --usepkgonly $(remove_quotes "${emerge_request}") $EMERGE_JOBS |
| 119 | else |
| 120 | sudo emerge-${FLAGS_board} --unmerge \ |
| 121 | --root="${REMOTE_PATH}" $(remove_quotes "${emerge_request}") |
| 122 | fi |
| 123 | done |
| 124 | |
Chris Sosa | 96bd5f6 | 2010-04-09 08:54:26 -0700 | [diff] [blame] | 125 | # Update ld.so.cache of root partition if installing to /usr/local |
| 126 | if [ ${FLAGS_onstatefuldev} -eq $FLAGS_TRUE ] ; then |
| 127 | # Re-run ldconfig to fix /etc/ldconfig.so.cache |
| 128 | sudo /sbin/ldconfig -r "${REMOTE_ROOT_DIR}" |
| 129 | fi |
| 130 | |
Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 131 | sync |
Chris Sosa | 96bd5f6 | 2010-04-09 08:54:26 -0700 | [diff] [blame] | 132 | sudo umount "${REMOTE_ROOT_DIR}" |
Chris Sosa | 687f0a6 | 2010-04-02 14:06:29 -0700 | [diff] [blame] | 133 | |
Chris Sosa | 96bd5f6 | 2010-04-09 08:54:26 -0700 | [diff] [blame] | 134 | trap - EXIT |