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