blob: 4287926261fd4e17d767074825bce1af876a8b4d [file] [log] [blame]
Joel Fernandes6c1ca312018-03-30 17:13:15 -07001#!/bin/bash -e
Joel Fernandesefe6c4c2018-04-01 21:24:02 -07002#
Joel Fernandes1ed30d72018-05-18 14:08:20 -07003# (c) Joel Fernandes <joel@joelfernandes.org>
Joel Fernandesefe6c4c2018-04-01 21:24:02 -07004
Joel Fernandes5ac823f2018-07-08 14:04:31 -07005VERSION=v0.99e
Joel Fernandesa67be0b2018-04-01 13:40:34 -07006
Joel Fernandes464193e2018-04-02 23:54:49 -07007spath="$(dirname "$(readlink -f "$0")")"
8# spath=$( cd "$(dirname "$0")" ; pwd -P )
Joel Fernandes4fb97ea2018-03-31 11:15:19 -07009curdir=$( pwd -P )
Joel Fernandes5b4be9c2018-03-30 23:58:27 -070010source $spath/utils/android
Joel Fernandesca8fb552018-04-01 13:48:16 -070011source $spath/utils/banners
Joel Fernandes83676b12018-03-30 23:33:47 -070012
Joel Fernandes6c1ca312018-03-30 17:13:15 -070013# Set default vars
Joel Fernandesf0c54092018-04-01 19:05:10 -070014DISTRO=buster; ARCH=arm64
Joel Fernandes02939a32018-04-02 22:18:10 -070015ADB="adb"
Joel Fernandesf0c54092018-04-01 19:05:10 -070016
17# Default packages
Joel Fernandes (Google)4f711082018-05-29 16:23:15 -070018DEFAULT_PACKAGES+="bash
19ca-certificates"
20
Joel Fernandes (Google)4b216fd2018-05-29 12:33:27 -070021EXTRA_FILES="none"
Joel Fernandes6c1ca312018-03-30 17:13:15 -070022
Joel Fernandesab5f9282018-04-05 22:53:23 -070023config_full_build() {
Joel Fernandes (Google)d7f21f12018-05-27 20:19:51 -070024 for f in $(ls $spath/packages); do source $spath/packages/$f; done;
Joel Fernandesab5f9282018-04-05 22:53:23 -070025}
26
Joel Fernandes6c1ca312018-03-30 17:13:15 -070027# Parse command line parameters
Joel Fernandes5b4be9c2018-03-30 23:58:27 -070028if [ $# -lt 1 ]; then usage; fi; POSITIONAL=()
Joel Fernandes4fb97ea2018-03-31 11:15:19 -070029while [[ $# -gt 0 ]]; do key="$1";
Joel Fernandesccb993f2018-07-02 19:59:04 -070030
31# If its shell mode, any future args become shell's args
32if [ "x$ASHELL" == "x1" ]; then
33 if [ -z "$SHELL_ARGS" ]; then
34 SHELL_ARGS=$key
35 else
36 SHELL_ARGS="$SHELL_ARGS $key"
37 fi
38 shift || true; continue
39fi
40
Joel Fernandes6c1ca312018-03-30 17:13:15 -070041case $key in
Joel Fernandes (Google)448a1742018-05-29 12:23:18 -070042 prepare) PREPARE=1; shift || true; ;;
Joel Fernandese2a04662018-03-31 19:33:23 -070043 shell) ASHELL=1; shift || true; ;;
Joel Fernandesb4c80a62018-04-02 22:37:22 -070044 remove) REMOVE=1; shift || true; ;;
Erick Reyese25caea2018-06-19 15:42:59 -070045 git-pull) GIT_PULL=1; shift || true; ;;
46 pull) PULL=1; shift || true; break ;;
Erick Reyes5104a6c2018-06-27 15:31:58 -070047 push) PUSH=1; shift || true; break ;;
Joel Fernandes (Google)08d1a072018-05-28 13:29:03 -070048 --arch) ARCH=$2; shift || true; shift || true; ;;
Joel Fernandes9f00bf42018-04-01 10:36:33 -070049 --archive) TARF=$2; shift || true; shift || true; ;;
Joel Fernandes4fb97ea2018-03-31 11:15:19 -070050 --tracers) source $spath/packages/tracers; shift || true; ;;
51 --compilers) source $spath/packages/compilers; shift || true; ;;
52 --editors) source $spath/packages/editors; shift || true; ;;
53 --scheduler) source $spath/packages/scheduler; shift || true; ;;
Joel Fernandesab5f9282018-04-05 22:53:23 -070054 --fullbuild) config_full_build; shift || true; ;;
Joel Fernandesa67be0b2018-04-01 13:40:34 -070055 --download) DOWNLOAD=1; shift || true; ;;
Joel Fernandes100c4682018-04-01 20:09:57 -070056 --bcc) source $spath/packages/bcc; shift || true; ;;
Joel Fernandes5b4be9c2018-03-30 23:58:27 -070057 --kernelsrc) KERNELSRC="$2"; shift || true; shift || true; ;;
Joel Fernandes (Google)1171d2d2018-05-30 17:22:09 -070058 --skip-install) SKIP_INSTALL=1; shift || true; ;;
Joel Fernandes1ed30d72018-05-18 14:08:20 -070059 --kernel-headers-targz) KERNELHDRS=$2; shift || true; shift || true; ;;
Joel Fernandese2a04662018-03-31 19:33:23 -070060 --tempdir) TDIR="$2"; shift || true; shift || true; ;;
Joel Fernandes97958e12018-03-31 23:51:44 -070061 --buildtar) TARDIR="$2"; shift || true; shift || true; ;;
Joel Fernandes02939a32018-04-02 22:18:10 -070062 --device|-s) ADB="$ADB -s $2"; shift || true; shift || true; ;;
Joel Fernandes (Google)9c3ae932018-05-28 14:34:48 -070063 --build-image) BUILD_IMAGE=$2; shift || true; shift || true; ;;
Joel Fernandesca8fb552018-04-01 13:48:16 -070064 --debug) set -x; shift || true; ;;
Joel Fernandes5b4be9c2018-03-30 23:58:27 -070065 *) echo "Unknown option ($1)"; usage; ;;
Joel Fernandes6c1ca312018-03-30 17:13:15 -070066esac
67done
68
Erick Reyese25caea2018-06-19 15:42:59 -070069if [ ! -z "$GIT_PULL" ]; then
Joel Fernandes16371152018-04-05 22:50:36 -070070 echo "Updating androdeb by git pull"
71 cd $spath
72 git pull
73 echo "Done."
74 exit 0
75fi
76
Erick Reyese25caea2018-06-19 15:42:59 -070077if [ ! -z "$PULL" ]; then
78 if [ $1 == "-a" ]; then
79 PRESERVE="-a"
80 echo "Preserving filestamps and mode"
81 shift || true
82 fi
Erick Reyes5104a6c2018-06-27 15:31:58 -070083 file_count=`count_sources $@`
Erick Reyese25caea2018-06-19 15:42:59 -070084 i=0
85 while [ $i -lt $file_count ]; do
86 files["$i"]=/data/androdeb/debian/$1
87 shift || true
88 i=$((i + 1))
89 done
90 $ADB pull $PRESERVE "${files[@]}" "$@"
91 exit 0
92fi
93
Erick Reyes5104a6c2018-06-27 15:31:58 -070094if [ ! -z "$PUSH" ]; then
Erick Reyes5104a6c2018-06-27 15:31:58 -070095 file_count=`count_sources $@`
96 i=0
97 while [ $i -lt $file_count ]; do
98 files["$i"]=$1
99 shift || true
100 i=$((i + 1))
101 done
102 dest=/data/androdeb/debian/$1
103 $ADB push $sync "${files[@]}" $dest
104 exit 0
105fi
106
Joel Fernandes9649a262018-07-02 20:49:41 -0700107if [ -z "$ASHELL" ] && [ -z "$REMOVE" ] && [ -z "$PACKAGES" ]; then
Joel Fernandes (Google)448a1742018-05-29 12:23:18 -0700108 echo "No packages specified, so I'm going to build/install all packages (--fullbuild)"
Joel Fernandes (Google)9c3ae932018-05-28 14:34:48 -0700109 config_full_build
110fi
111
Joel Fernandes97958e12018-03-31 23:51:44 -0700112if [[ ! -z ${TARDIR+x} ]] && [[ ! -d $TARDIR ]]; then die 7 "Tar dir specified doesn't exist"; fi
Joel Fernandes92057762018-03-31 23:38:43 -0700113
Joel Fernandes (Google)9c3ae932018-05-28 14:34:48 -0700114if [ -z "$BUILD_IMAGE" ]; then
Joel Fernandesb4c80a62018-04-02 22:37:22 -0700115do_adb_root "$ADB" || die 3 "adb root failed, make sure:
Joel Fernandes02939a32018-04-02 22:18:10 -0700116- If multiple devices connected, provide --device <serialno> (or -s <serialno>)
117- Try to run \"adb root\" manually and see if it works. Typically this needs a userdebug build.
Joel Fernandesaf07ec72018-04-02 22:01:19 -0700118
Joel Fernandese561d822018-04-02 23:10:12 -0700119Note: adb can be typically obtained using the android-tools-adb or the adb
Joel Fernandesaf07ec72018-04-02 22:01:19 -0700120packages on your distro, or by installing the Android SDK."
Joel Fernandes (Google)9c3ae932018-05-28 14:34:48 -0700121fi
Joel Fernandes72656c52018-03-31 20:35:33 -0700122
Joel Fernandesb4c80a62018-04-02 22:37:22 -0700123if [ ! -z "$REMOVE" ]; then
124 die_if_no_androdeb "Nothing to remove."
Joel Fernandes (Google)7c8b1082018-06-25 12:29:10 -0700125 $ADB shell /data/androdeb/device-umount-all || true;
Joel Fernandesb4c80a62018-04-02 22:37:22 -0700126 $ADB shell rm -rf /data/androdeb; exit 0; fi
127
Joel Fernandes83676b12018-03-30 23:33:47 -0700128##########################################################
Joel Fernandese2a04662018-03-31 19:33:23 -0700129# SHELL
Joel Fernandes83676b12018-03-30 23:33:47 -0700130##########################################################
Joel Fernandese381ac72018-03-31 21:46:36 -0700131if [ ! -z ${ASHELL+x} ]; then
Joel Fernandes02939a32018-04-02 22:18:10 -0700132 set +e; $ADB shell ls /data/androdeb/debian/.bashrc > /dev/null 2>&1
Joel Fernandesc9646812018-03-31 19:45:46 -0700133 if [ $? -ne 0 ]; then
134 die 2 "Device doesn't have an androdeb environment, run \"./androdeb prepare\" first";
135 fi; set -e
136
Joel Fernandesccb993f2018-07-02 19:59:04 -0700137 if [ ! -z ${SHELL_ARGS+x} ]; then
138 # Explanation of quotes:
139 # Outer quote is so that androdeb's bash passes the SHELL_ARGS as a single
140 # argument to $ADB shell. Inner quotes is so that run-command can receive all
141 # the args even though they may be separated by spaces. \m/
142 $ADB shell -t /data/androdeb/run-command "\"$SHELL_ARGS\""
143 else
144 $ADB shell -t /data/androdeb/run
145 fi
146
Joel Fernandese2a04662018-03-31 19:33:23 -0700147 exit 0
148fi
149
150##########################################################
151# PREPARE
152##########################################################
Joel Fernandesf0c54092018-04-01 19:05:10 -0700153
154function do_cleanup() {
155 rm -rf $TDIR/*; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi
156}
157
158function push_unpack_headers() {
Joel Fernandesb4c80a62018-04-02 22:37:22 -0700159 die_if_no_androdeb "Couldn't update headers."
Joel Fernandesf0c54092018-04-01 19:05:10 -0700160
Joel Fernandes02939a32018-04-02 22:18:10 -0700161 $ADB shell rm -rf /data/androdeb/debian/kernel-headers/
162 $ADB shell mkdir /data/androdeb/debian/kernel-headers/
163 $ADB push $TDIR_ABS/kh.tgz /data/androdeb/
Joel Fernandesf0c54092018-04-01 19:05:10 -0700164 echo "Storing kernel headers into androdeb /kernel-headers/"
Joel Fernandes02939a32018-04-02 22:18:10 -0700165 $ADB shell tar -xvf /data/androdeb/kh.tgz -C /data/androdeb/debian/kernel-headers/ > /dev/null
166 $ADB shell rm /data/androdeb/kh.tgz
Joel Fernandesf0c54092018-04-01 19:05:10 -0700167}
168
Joel Fernandes1ed30d72018-05-18 14:08:20 -0700169function push_unpack_tarred_headers() {
170 die_if_no_androdeb "Couldn't update headers."
171
172 $ADB shell rm -rf /data/androdeb/debian/kernel-headers/
173 $ADB shell mkdir /data/androdeb/debian/kernel-headers/
174 $ADB push $1 /data/androdeb/
175 echo "Storing kernel headers into androdeb root directory"
176 $ADB shell tar -xvf /data/androdeb/$(basename $1) -C /data/androdeb/debian/ > /dev/null
177
178 $ADB shell rm /data/androdeb/$(basename $1)
179}
180
Joel Fernandes72065112018-04-01 20:02:53 -0700181function all_done_banner() {
182 echo "All done! Run \"androdeb shell\" to enter environment"
183}
184
Joel Fernandes72656c52018-03-31 20:35:33 -0700185# Prepare is the last command checked
Joel Fernandes9f00bf42018-04-01 10:36:33 -0700186if [ -z "$PREPARE" ]; then usage; fi
Joel Fernandes72656c52018-03-31 20:35:33 -0700187
Joel Fernandes1ed30d72018-05-18 14:08:20 -0700188if [ ! -z "$TARF" ] && [ ! -f $TARF ] && [ -z "$DOWNLOAD" ]; then die 5 "archive provided doesn't exist"; fi
Joel Fernandes9f00bf42018-04-01 10:36:33 -0700189
Joel Fernandes1ed30d72018-05-18 14:08:20 -0700190if [ ! -z "$KERNELSRC" ] && [ ! -d $KERNELSRC ]; then die 6 "Kernel source directory provided doesn't exist"; fi
191
192if [ ! -z "$KERNELHDRS" ] && [ ! -f $KERNELHDRS ]; then die 7 "Kernel headers tar.gz doesn't exist"; fi
Joel Fernandese2a04662018-03-31 19:33:23 -0700193
Joel Fernandesca8fb552018-04-01 13:48:16 -0700194print_prepare_banner
195
Joel Fernandes6c1ca312018-03-30 17:13:15 -0700196# Where do we want to store temporary files
Joel Fernandese2a04662018-03-31 19:33:23 -0700197MKTEMP=0; if [[ -z ${TDIR+x} ]] || [[ ! -d "${TDIR}" ]]; then
198 TDIR=`mktemp -d`; MKTEMP=1; fi
Joel Fernandese381ac72018-03-31 21:46:36 -0700199rm -rf $TDIR/*
200TDIR_ABS=$( cd "$TDIR" ; pwd -P )
Joel Fernandes6c1ca312018-03-30 17:13:15 -0700201
Joel Fernandesa67be0b2018-04-01 13:40:34 -0700202if [ ! -z "$DOWNLOAD" ]; then
Joel Fernandesca8fb552018-04-01 13:48:16 -0700203 echo "Downloading Androdeb from the web..."; echo ""
Joel Fernandes (Google)9e670aa2018-06-25 13:39:34 -0700204 # Github dropped tar gz support! ##?#??#! Now we've to zip everything.
205 curl -L https://github.com/joelagnel/androdeb/releases/download/$VERSION/androdeb-fs.tgz.zip --output $TDIR_ABS/androdeb-fs.tgz.zip;
206 unzip -e $TDIR_ABS/androdeb-fs.tgz.zip -d $TDIR_ABS/
Joel Fernandesa67be0b2018-04-01 13:40:34 -0700207 TARF=$TDIR_ABS/androdeb-fs.tgz; fi
208
Joel Fernandese2a04662018-03-31 19:33:23 -0700209OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP
Joel Fernandesf0c54092018-04-01 19:05:10 -0700210
Joel Fernandes1ed30d72018-05-18 14:08:20 -0700211# Unpack the supplied kernel headers tar.gz directly into androdeb root
212if [ ! -z "$KERNELHDRS" ]; then
213 echo "Building updating kernel headers from supplied tar.gz ($KERNELHDRS)"
214
215 # Is header tar gz update the only thing left to do?
Joel Fernandes (Google)1171d2d2018-05-30 17:22:09 -0700216 if [[ ! -z "$SKIP_INSTALL" ]]; then
217 echo "Skipping install"
Joel Fernandes1ed30d72018-05-18 14:08:20 -0700218 push_unpack_tarred_headers $KERNELHDRS; do_cleanup; all_done_banner; exit 0; fi
219
220 tar -xvf $KERNELHDRS -C $OUT_TMP/ > /dev/null
221fi
222
Joel Fernandesf0c54092018-04-01 19:05:10 -0700223# Package kernel headers
224if [ ! -z "$KERNELSRC" ]; then
Joel Fernandes72065112018-04-01 20:02:53 -0700225 echo "Building and updating kernel headers from kernel source dir ($KERNELSRC)"
226 $spath/bcc/build-kheaders-targz.sh ${KERNELSRC} $TDIR_ABS/kh.tgz > /dev/null
227
Joel Fernandesf0c54092018-04-01 19:05:10 -0700228 # Is header update the only thing left to do?
Joel Fernandes (Google)1171d2d2018-05-30 17:22:09 -0700229 if [[ ! -z "$SKIP_INSTALL" ]]; then
230 echo "Skipping install"
Joel Fernandes72065112018-04-01 20:02:53 -0700231 push_unpack_headers; do_cleanup; all_done_banner; exit 0; fi
Joel Fernandesf0c54092018-04-01 19:05:10 -0700232
Joel Fernandese381ac72018-03-31 21:46:36 -0700233 mkdir $OUT_TMP/kernel-headers
Joel Fernandes72065112018-04-01 20:02:53 -0700234 tar -xvf $TDIR_ABS/kh.tgz -C $OUT_TMP/kernel-headers/ > /dev/null
Joel Fernandese381ac72018-03-31 21:46:36 -0700235fi
Joel Fernandes4fb97ea2018-03-31 11:15:19 -0700236
Joel Fernandesf0c54092018-04-01 19:05:10 -0700237# Build FS from existing tar, very simple.
Joel Fernandes9f00bf42018-04-01 10:36:33 -0700238if [ ! -z "$TARF" ]; then
239 echo "Using archive at $TARF for filesystem preparation"
Joel Fernandes02939a32018-04-02 22:18:10 -0700240 $ADB shell mkdir -p /data/androdeb/
241 $ADB push $TARF /data/androdeb/deb.tar.gz
242 $ADB push $spath/addons/* /data/androdeb/
243 $ADB shell /data/androdeb/device-unpack
Joel Fernandes9f00bf42018-04-01 10:36:33 -0700244
Joel Fernandes1ed30d72018-05-18 14:08:20 -0700245 if [ ! -z "$KERNELHDRS" ]; then push_unpack_tarred_headers $KERNELHDRS; fi
Joel Fernandesf0c54092018-04-01 19:05:10 -0700246 if [ ! -z "$KERNELSRC" ]; then push_unpack_headers; fi
247
Joel Fernandes72065112018-04-01 20:02:53 -0700248 do_cleanup; all_done_banner; exit 0
Joel Fernandesf0c54092018-04-01 19:05:10 -0700249fi
250
251PACKAGES+="$DEFAULT_PACKAGES"
Joel Fernandes25f79152018-03-31 21:18:40 -0700252echo "Using temporary directory: $TDIR"
Joel Fernandes4fb97ea2018-03-31 11:15:19 -0700253
Joel Fernandes301e4552018-04-05 22:46:19 -0700254if [[ $EUID -ne 0 ]]; then echo "The next stage runs as sudo, please enter password if asked."; fi
Joel Fernandes4fb97ea2018-03-31 11:15:19 -0700255
Joel Fernandes (Google)9c3ae932018-05-28 14:34:48 -0700256SKIP_COMPRESS=0; if [ ! -z "$BUILD_IMAGE" ]; then SKIP_COMPRESS=1; fi
257
Joel Fernandes (Google)57b3fd22018-05-29 16:27:03 -0700258ex_files=$(mktemp); echo $EXTRA_FILES > $ex_files
259
Joel Fernandes (Google)4f711082018-05-29 16:23:15 -0700260sudo $spath/buildstrap $ARCH $DISTRO $TDIR $OUT_TMP \
261 "$(make_csv "$PACKAGES")"\
Joel Fernandes (Google)57b3fd22018-05-29 16:27:03 -0700262 $ex_files $INSTALL_BCC $SKIP_COMPRESS
263rm $ex_files
Joel Fernandes (Google)9c3ae932018-05-28 14:34:48 -0700264
265# If we only wanted to prepare a rootfs and don't have
266# a device connected, then just echo that and skip cleanup
267if [ ! -z "$BUILD_IMAGE" ]; then
268 echo "For --build-image, only the image is built"
269 echo "Note that BCC will not be built on the image, you've to do it yourself"
270 sudo $spath/buildimage $OUT_TMP $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
271 sudo chmod a+rw $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
272 do_cleanup; echo "Your .img has been built! Enjoy!"; exit 0
273fi
Joel Fernandes4dd60862018-03-31 13:27:16 -0700274
275# Push tar to device and start unpack
Joel Fernandes02939a32018-04-02 22:18:10 -0700276$ADB shell mkdir -p /data/androdeb/
277$ADB push $TDIR/deb.tar.gz /data/androdeb/
278$ADB push $spath/addons/* /data/androdeb/
279$ADB shell /data/androdeb/device-unpack
Joel Fernandesb61d27d2018-03-31 11:59:45 -0700280
Joel Fernandes6f1c70f2018-03-31 20:26:26 -0700281# Build BCC and install bcc on device if needed
282if [[ ! -z ${INSTALL_BCC+x} ]]; then
Joel Fernandes02939a32018-04-02 22:18:10 -0700283$ADB shell /data/androdeb/run-command /bcc-master/build-bcc.sh; fi
Joel Fernandes4fb97ea2018-03-31 11:15:19 -0700284
Joel Fernandesf0c54092018-04-01 19:05:10 -0700285do_cleanup
Joel Fernandese381ac72018-03-31 21:46:36 -0700286
Joel Fernandes97958e12018-03-31 23:51:44 -0700287# Extract a tar of the built, compiled and installed androdeb env
288if [[ ! -z ${TARDIR+x} ]]; then
289 echo "Creating and pulling tarball of androdeb env from device"
Joel Fernandes02939a32018-04-02 22:18:10 -0700290 $ADB shell /data/androdeb/build-debian-tar
291 $ADB pull /data/androdeb/androdeb-fs.tgz $TARDIR/
292 $ADB shell rm /data/androdeb/androdeb-fs.tgz; fi
Joel Fernandes97958e12018-03-31 23:51:44 -0700293
Joel Fernandes72065112018-04-01 20:02:53 -0700294all_done_banner