Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 1 | #!/bin/bash -e |
Joel Fernandes | efe6c4c | 2018-04-01 21:24:02 -0700 | [diff] [blame] | 2 | # |
| 3 | # (c) Joel Fernandes <joel@linuxinternals.org> |
| 4 | |
Joel Fernandes | 3d06342 | 2018-04-05 23:33:44 -0700 | [diff] [blame^] | 5 | VERSION=v0.98 |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 6 | |
Joel Fernandes | 464193e | 2018-04-02 23:54:49 -0700 | [diff] [blame] | 7 | spath="$(dirname "$(readlink -f "$0")")" |
| 8 | # spath=$( cd "$(dirname "$0")" ; pwd -P ) |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 9 | curdir=$( pwd -P ) |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 10 | source $spath/utils/android |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 11 | source $spath/utils/banners |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 12 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 13 | # Set default vars |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 14 | DISTRO=buster; ARCH=arm64 |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 15 | ADB="adb" |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 16 | |
| 17 | # Default packages |
| 18 | DEFAULT_PACKAGES+="\ |
| 19 | bash |
| 20 | ca-certificates |
| 21 | " |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 22 | |
Joel Fernandes | ab5f928 | 2018-04-05 22:53:23 -0700 | [diff] [blame] | 23 | config_full_build() { |
| 24 | for f in $(ls $spath/packages); do source packages/$f; done; |
| 25 | } |
| 26 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 27 | # Parse command line parameters |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 28 | if [ $# -lt 1 ]; then usage; fi; POSITIONAL=() |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 29 | while [[ $# -gt 0 ]]; do key="$1"; |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 30 | case $key in |
Joel Fernandes | e828d03 | 2018-03-31 23:57:35 -0700 | [diff] [blame] | 31 | prepare) PREPARE=1; shift || true; ;; |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 32 | shell) ASHELL=1; shift || true; ;; |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 33 | remove) REMOVE=1; shift || true; ;; |
Joel Fernandes | 1637115 | 2018-04-05 22:50:36 -0700 | [diff] [blame] | 34 | pull) PULL=1; shift || true; ;; |
Joel Fernandes | ab5f928 | 2018-04-05 22:53:23 -0700 | [diff] [blame] | 35 | buildtar) PREPARE=1; TARDIR="./"; config_full_build; shift || true; ;; |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 36 | --archive) TARF=$2; shift || true; shift || true; ;; |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 37 | --tracers) source $spath/packages/tracers; shift || true; ;; |
| 38 | --compilers) source $spath/packages/compilers; shift || true; ;; |
| 39 | --editors) source $spath/packages/editors; shift || true; ;; |
| 40 | --scheduler) source $spath/packages/scheduler; shift || true; ;; |
Joel Fernandes | ab5f928 | 2018-04-05 22:53:23 -0700 | [diff] [blame] | 41 | --fullbuild) config_full_build; shift || true; ;; |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 42 | --download) DOWNLOAD=1; shift || true; ;; |
Joel Fernandes | 100c468 | 2018-04-01 20:09:57 -0700 | [diff] [blame] | 43 | --bcc) source $spath/packages/bcc; shift || true; ;; |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 44 | --kernelsrc) KERNELSRC="$2"; shift || true; shift || true; ;; |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 45 | --tempdir) TDIR="$2"; shift || true; shift || true; ;; |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 46 | --buildtar) TARDIR="$2"; shift || true; shift || true; ;; |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 47 | --device|-s) ADB="$ADB -s $2"; shift || true; shift || true; ;; |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 48 | --debug) set -x; shift || true; ;; |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 49 | *) echo "Unknown option ($1)"; usage; ;; |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 50 | esac |
| 51 | done |
| 52 | |
Joel Fernandes | 1637115 | 2018-04-05 22:50:36 -0700 | [diff] [blame] | 53 | if [ ! -z "$PULL" ]; then |
| 54 | echo "Updating androdeb by git pull" |
| 55 | cd $spath |
| 56 | git pull |
| 57 | echo "Done." |
| 58 | exit 0 |
| 59 | fi |
| 60 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 61 | if [ ! -z "$PREPARE" ] && [ -z "$DOWNLOAD" ] && [ -z "$TARF" ] && [ -z "$PACKAGES" ] && [ -z "$KERNELSRC" ]; then |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 62 | echo "Need to specifify something to prepare, or try: ./andrdeb prepare --download"; usage; fi |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 63 | |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 64 | if [[ ! -z ${TARDIR+x} ]] && [[ ! -d $TARDIR ]]; then die 7 "Tar dir specified doesn't exist"; fi |
Joel Fernandes | 9205776 | 2018-03-31 23:38:43 -0700 | [diff] [blame] | 65 | |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 66 | do_adb_root "$ADB" || die 3 "adb root failed, make sure: |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 67 | - If multiple devices connected, provide --device <serialno> (or -s <serialno>) |
| 68 | - Try to run \"adb root\" manually and see if it works. Typically this needs a userdebug build. |
Joel Fernandes | af07ec7 | 2018-04-02 22:01:19 -0700 | [diff] [blame] | 69 | |
Joel Fernandes | e561d82 | 2018-04-02 23:10:12 -0700 | [diff] [blame] | 70 | Note: adb can be typically obtained using the android-tools-adb or the adb |
Joel Fernandes | af07ec7 | 2018-04-02 22:01:19 -0700 | [diff] [blame] | 71 | packages on your distro, or by installing the Android SDK." |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 72 | |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 73 | if [ ! -z "$REMOVE" ]; then |
| 74 | die_if_no_androdeb "Nothing to remove." |
| 75 | $ADB shell /data/androdeb/device-umount-all; |
| 76 | $ADB shell rm -rf /data/androdeb; exit 0; fi |
| 77 | |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 78 | ########################################################## |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 79 | # SHELL |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 80 | ########################################################## |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 81 | if [ ! -z ${ASHELL+x} ]; then |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 82 | set +e; $ADB shell ls /data/androdeb/debian/.bashrc > /dev/null 2>&1 |
Joel Fernandes | c964681 | 2018-03-31 19:45:46 -0700 | [diff] [blame] | 83 | if [ $? -ne 0 ]; then |
| 84 | die 2 "Device doesn't have an androdeb environment, run \"./androdeb prepare\" first"; |
| 85 | fi; set -e |
| 86 | |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 87 | $ADB shell -t /data/androdeb/run |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 88 | exit 0 |
| 89 | fi |
| 90 | |
| 91 | ########################################################## |
| 92 | # PREPARE |
| 93 | ########################################################## |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 94 | |
| 95 | function do_cleanup() { |
| 96 | rm -rf $TDIR/*; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi |
| 97 | } |
| 98 | |
| 99 | function push_unpack_headers() { |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 100 | die_if_no_androdeb "Couldn't update headers." |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 101 | |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 102 | $ADB shell rm -rf /data/androdeb/debian/kernel-headers/ |
| 103 | $ADB shell mkdir /data/androdeb/debian/kernel-headers/ |
| 104 | $ADB push $TDIR_ABS/kh.tgz /data/androdeb/ |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 105 | echo "Storing kernel headers into androdeb /kernel-headers/" |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 106 | $ADB shell tar -xvf /data/androdeb/kh.tgz -C /data/androdeb/debian/kernel-headers/ > /dev/null |
| 107 | $ADB shell rm /data/androdeb/kh.tgz |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 110 | function all_done_banner() { |
| 111 | echo "All done! Run \"androdeb shell\" to enter environment" |
| 112 | } |
| 113 | |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 114 | # Prepare is the last command checked |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 115 | if [ -z "$PREPARE" ]; then usage; fi |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 116 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 117 | if [ ! -z "$TARF" ] && [ ! -f $TARF ] && [ -z "$DOWNLOAD" ]; then die 7 "archive provided doesn't exist"; fi |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 118 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 119 | if [ ! -z "$KERNELSRC" ] && [ ! -d $KERNELSRC ]; then die 5 "Kernel source directory provided doesn't exist"; fi |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 120 | |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 121 | print_prepare_banner |
| 122 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 123 | # Where do we want to store temporary files |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 124 | MKTEMP=0; if [[ -z ${TDIR+x} ]] || [[ ! -d "${TDIR}" ]]; then |
| 125 | TDIR=`mktemp -d`; MKTEMP=1; fi |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 126 | rm -rf $TDIR/* |
| 127 | TDIR_ABS=$( cd "$TDIR" ; pwd -P ) |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 128 | |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 129 | if [ ! -z "$DOWNLOAD" ]; then |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 130 | echo "Downloading Androdeb from the web..."; echo "" |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 131 | curl -L https://github.com/joelagnel/androdeb/releases/download/$VERSION/androdeb-fs.tgz --output $TDIR_ABS/androdeb-fs.tgz; |
| 132 | TARF=$TDIR_ABS/androdeb-fs.tgz; fi |
| 133 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 134 | OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 135 | |
| 136 | # Package kernel headers |
| 137 | if [ ! -z "$KERNELSRC" ]; then |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 138 | echo "Building and updating kernel headers from kernel source dir ($KERNELSRC)" |
| 139 | $spath/bcc/build-kheaders-targz.sh ${KERNELSRC} $TDIR_ABS/kh.tgz > /dev/null |
| 140 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 141 | # Is header update the only thing left to do? |
| 142 | if [[ -z "$PACKAGES" ]] && [[ -z "$TARF" ]]; then |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 143 | push_unpack_headers; do_cleanup; all_done_banner; exit 0; fi |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 144 | |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 145 | mkdir $OUT_TMP/kernel-headers |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 146 | tar -xvf $TDIR_ABS/kh.tgz -C $OUT_TMP/kernel-headers/ > /dev/null |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 147 | fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 148 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 149 | # Build FS from existing tar, very simple. |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 150 | if [ ! -z "$TARF" ]; then |
| 151 | echo "Using archive at $TARF for filesystem preparation" |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 152 | $ADB shell mkdir -p /data/androdeb/ |
| 153 | $ADB push $TARF /data/androdeb/deb.tar.gz |
| 154 | $ADB push $spath/addons/* /data/androdeb/ |
| 155 | $ADB shell /data/androdeb/device-unpack |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 156 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 157 | if [ ! -z "$KERNELSRC" ]; then push_unpack_headers; fi |
| 158 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 159 | do_cleanup; all_done_banner; exit 0 |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 160 | fi |
| 161 | |
| 162 | PACKAGES+="$DEFAULT_PACKAGES" |
Joel Fernandes | 25f7915 | 2018-03-31 21:18:40 -0700 | [diff] [blame] | 163 | echo "Using temporary directory: $TDIR" |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 164 | |
Joel Fernandes | 301e455 | 2018-04-05 22:46:19 -0700 | [diff] [blame] | 165 | if [[ $EUID -ne 0 ]]; then echo "The next stage runs as sudo, please enter password if asked."; fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 166 | |
Joel Fernandes | 9d767b4 | 2018-04-05 22:55:24 -0700 | [diff] [blame] | 167 | sudo $spath/buildstrap $ARCH $DISTRO $TDIR $OUT_TMP "$(make_csv "$PACKAGES")" $INSTALL_BCC |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 168 | |
| 169 | # Push tar to device and start unpack |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 170 | $ADB shell mkdir -p /data/androdeb/ |
| 171 | $ADB push $TDIR/deb.tar.gz /data/androdeb/ |
| 172 | $ADB push $spath/addons/* /data/androdeb/ |
| 173 | $ADB shell /data/androdeb/device-unpack |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 174 | |
Joel Fernandes | 6f1c70f | 2018-03-31 20:26:26 -0700 | [diff] [blame] | 175 | # Build BCC and install bcc on device if needed |
| 176 | if [[ ! -z ${INSTALL_BCC+x} ]]; then |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 177 | $ADB shell /data/androdeb/run-command /bcc-master/build-bcc.sh; fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 178 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 179 | do_cleanup |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 180 | |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 181 | # Extract a tar of the built, compiled and installed androdeb env |
| 182 | if [[ ! -z ${TARDIR+x} ]]; then |
| 183 | echo "Creating and pulling tarball of androdeb env from device" |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 184 | $ADB shell /data/androdeb/build-debian-tar |
| 185 | $ADB pull /data/androdeb/androdeb-fs.tgz $TARDIR/ |
| 186 | $ADB shell rm /data/androdeb/androdeb-fs.tgz; fi |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 187 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 188 | all_done_banner |