blob: b63a9dc96d71d70319ec378faa87965bb7cae4a0 [file] [log] [blame]
Chris Sosa687f0a62010-04-02 14:06:29 -07001#!/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 Sosa96bd5f62010-04-09 08:54:26 -07007# Script that emerges packages and their dependencies from a host development
Chris Sosa687f0a62010-04-02 14:06:29 -07008# 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
18assert_inside_chroot
19
20get_default_board
21
22# Flags
23DEFINE_string board "$DEFAULT_BOARD" \
24 "The board to build packages for."
25DEFINE_boolean usepkg $FLAGS_FALSE \
26 "Use binary packages to bootstrap build when possible."
27DEFINE_string build_root "/build" \
28 "The root location for board sysroots."
29DEFINE_integer jobs -1 \
30 "How many packages to build in parallel at maximum."
31DEFINE_boolean unmerge $FLAGS_FALSE \
32 "Whether to unmerge the package versus emerge it."
33DEFINE_integer retries -1 \
34 "On build failure, the number of times to retry"
35DEFINE_boolean onstatefuldev $FLAGS_TRUE \
36 "Build packages and install them into stateful partition on device."
37DEFINE_boolean installmask $FLAGS_TRUE \
38 "Use INSTALL_MASK to shrink the resulting image."
39DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance"
40
41# Parse command line
42FLAGS_HELP="usage: $0 [flags]"
43FLAGS "$@" || exit 1
44eval set -- "${FLAGS_ARGV}"
45
46# Die on any errors.
47set -e
48
49# Checks $1 and prints out $2 is required before exiting if $1 does not exist
50check_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
58cleanup_sshfs_mount() {
59 echo "Cleaning up mount point"
Chris Sosa96bd5f62010-04-09 08:54:26 -070060 sudo umount "${REMOTE_ROOT_DIR}"
Chris Sosa687f0a62010-04-02 14:06:29 -070061}
62
63check_args "${FLAGS_board}"
64check_args "${FLAGS_ARGV}" "Emerge package name"
65
66# Just in case we haven't done this before.
67eval /sbin/modprobe fuse
68
69EMERGE_FLAGS=""
Chris Sosa96bd5f62010-04-09 08:54:26 -070070[[ $FLAGS_usepkg -eq $FLAGS_TRUE ]] &&
Chris Sosa687f0a62010-04-02 14:06:29 -070071 EMERGE_FLAGS="${EMERGE_FLAGS} --getbinpkg --usepkg --with-bdeps y"
72
73INSTALL_MASK=""
74[[ ${FLAGS_installmask} -eq ${FLAGS_TRUE} ]] && \
75 INSTALL_MASK="${DEFAULT_INSTALL_MASK}"
76
77EMERGE_JOBS=
78[[ ${FLAGS_jobs} -ne -1 ]] && EMERGE_JOBS="--jobs=${FLAGS_jobs}"
79
80# Duplicate from build_packages to get host packages on system.
81
82# Skip building on host if we are unmerging.
83if [ ${FLAGS_unmerge} -eq ${FLAGS_FALSE} ] ; then
84 eretry sudo emerge -uDNv $EMERGE_FLAGS world
85
86 # Build request and install them onto the sysroot
87 for emerge_request in $FLAGS_ARGV; do
88 eretry sudo emerge-${FLAGS_board} -uDNv \
89 $(remove_quotes "${emerge_request}")
90 done
91fi
92
93# Name is unique per client. This allows one to run gmergefs in parallel
94# with multiple clients.
95REMOTE_ROOT_DIR="${FLAGS_build_root}/${FLAGS_board}/gmergefs/${FLAGS_remote}"
96
97# Default is to install on stateful dev, otherwise install on root.
98REMOTE_RELATIVE_PATH=""
99[ ${FLAGS_onstatefuldev} -eq $FLAGS_TRUE ] && \
100 REMOTE_RELATIVE_PATH="mnt/stateful_partition/dev_image"
101REMOTE_PATH="${REMOTE_ROOT_DIR}/${REMOTE_RELATIVE_PATH}"
102
103# Create directory for remote image
104sudo mkdir -p "${REMOTE_ROOT_DIR}"
105
106# Mount remote fs into chroot
107echo "Enter password for remote machine"
108sudo sshfs -o idmap=user -o allow_other -o nonempty -o transform_symlinks \
109 "root@${FLAGS_remote}:/" "${REMOTE_ROOT_DIR}"
Chris Sosa96bd5f62010-04-09 08:54:26 -0700110
Chris Sosa687f0a62010-04-02 14:06:29 -0700111trap cleanup_sshfs_mount EXIT
112
113# Ensure root path is created for emerge
114sudo mkdir -p "${REMOTE_PATH}"
115
116# Same arguments used in build_image to emerge these packages onto the target
117for emerge_request in $FLAGS_ARGV; do
118 if [ ${FLAGS_unmerge} -eq ${FLAGS_FALSE} ] ; then
119 sudo INSTALL_MASK="$INSTALL_MASK" emerge-${FLAGS_board} \
120 --root="${REMOTE_PATH}" --root-deps=rdeps \
121 --usepkgonly $(remove_quotes "${emerge_request}") $EMERGE_JOBS
122 else
123 sudo emerge-${FLAGS_board} --unmerge \
124 --root="${REMOTE_PATH}" $(remove_quotes "${emerge_request}")
125 fi
126done
127
Chris Sosa96bd5f62010-04-09 08:54:26 -0700128# Update ld.so.cache of root partition if installing to /usr/local
129if [ ${FLAGS_onstatefuldev} -eq $FLAGS_TRUE ] ; then
130 # Re-run ldconfig to fix /etc/ldconfig.so.cache
131 sudo /sbin/ldconfig -r "${REMOTE_ROOT_DIR}"
132fi
133
Chris Sosa687f0a62010-04-02 14:06:29 -0700134sync
Chris Sosa96bd5f62010-04-09 08:54:26 -0700135sudo umount "${REMOTE_ROOT_DIR}"
Chris Sosa687f0a62010-04-02 14:06:29 -0700136
Chris Sosa96bd5f62010-04-09 08:54:26 -0700137trap - EXIT