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 | # |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 3 | # (c) Joel Fernandes <joel@joelfernandes.org> |
Joel Fernandes | efe6c4c | 2018-04-01 21:24:02 -0700 | [diff] [blame] | 4 | |
Erick Reyes | dd195c0 | 2018-08-16 14:50:47 -0700 | [diff] [blame] | 5 | VERSION=v0.99g |
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 | 4e19ccd | 2018-07-08 17:23:07 -0700 | [diff] [blame] | 10 | source $spath/utils/support |
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 | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 16 | FULL=0 # Default to a minimal install |
| 17 | DOWNLOAD=1 # Default to downloading from web |
Joel Fernandes | fdc5200 | 2018-07-08 18:49:20 -0700 | [diff] [blame] | 18 | SKIP_DEVICE=0 # Skip device preparation |
Joel Fernandes | ffcbfe4 | 2018-07-08 19:08:02 -0700 | [diff] [blame] | 19 | INSTALL_BCC=0 # Decide if BCC is to be installed |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 20 | |
| 21 | # Default packages |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 22 | PACKAGES="" |
| 23 | DEFAULT_PACKAGES="bash ca-certificates apt net-tools iputils-ping procps vim" |
Joel Fernandes (Google) | 4f71108 | 2018-05-29 16:23:15 -0700 | [diff] [blame] | 24 | |
Joel Fernandes (Google) | 4b216fd | 2018-05-29 12:33:27 -0700 | [diff] [blame] | 25 | EXTRA_FILES="none" |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 26 | |
Joel Fernandes | ab5f928 | 2018-04-05 22:53:23 -0700 | [diff] [blame] | 27 | config_full_build() { |
Joel Fernandes (Google) | d7f21f1 | 2018-05-27 20:19:51 -0700 | [diff] [blame] | 28 | for f in $(ls $spath/packages); do source $spath/packages/$f; done; |
Joel Fernandes | ab5f928 | 2018-04-05 22:53:23 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 31 | # Parse command line parameters |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 32 | if [ $# -lt 1 ]; then usage; fi; POSITIONAL=() |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 33 | while [[ $# -gt 0 ]]; do key="$1"; |
Joel Fernandes | ccb993f | 2018-07-02 19:59:04 -0700 | [diff] [blame] | 34 | |
| 35 | # If its shell mode, any future args become shell's args |
| 36 | if [ "x$ASHELL" == "x1" ]; then |
| 37 | if [ -z "$SHELL_ARGS" ]; then |
| 38 | SHELL_ARGS=$key |
| 39 | else |
| 40 | SHELL_ARGS="$SHELL_ARGS $key" |
| 41 | fi |
| 42 | shift || true; continue |
| 43 | fi |
| 44 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 45 | case $key in |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 46 | shell) ASHELL=1; shift || true; ;; |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 47 | remove) REMOVE=1; shift || true; ;; |
Erick Reyes | e25caea | 2018-06-19 15:42:59 -0700 | [diff] [blame] | 48 | git-pull) GIT_PULL=1; shift || true; ;; |
| 49 | pull) PULL=1; shift || true; break ;; |
Erick Reyes | 5104a6c | 2018-06-27 15:31:58 -0700 | [diff] [blame] | 50 | push) PUSH=1; shift || true; break ;; |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 51 | prepare) PREPARE=1; shift || true; ;; |
| 52 | --full) FULL=1; config_full_build; shift || true; ;; |
Joel Fernandes (Google) | 08d1a07 | 2018-05-28 13:29:03 -0700 | [diff] [blame] | 53 | --arch) ARCH=$2; shift || true; shift || true; ;; |
Joel Fernandes (Google) | 387c895 | 2018-07-25 16:27:49 -0700 | [diff] [blame] | 54 | --archive) DOWNLOAD=0; TARF=$2; shift || true; shift || true; ;; |
Joel Fernandes (Google) | 60ab928 | 2018-08-17 12:11:52 -0700 | [diff] [blame] | 55 | --bcc) FULL=1; source $spath/packages/bcc; shift || true; ;; |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 56 | --kernelsrc) KERNELSRC="$2"; shift || true; shift || true; ;; |
Joel Fernandes (Google) | 1171d2d | 2018-05-30 17:22:09 -0700 | [diff] [blame] | 57 | --skip-install) SKIP_INSTALL=1; shift || true; ;; |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 58 | --kernel-headers-targz) KERNELHDRS=$2; shift || true; shift || true; ;; |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 59 | --tempdir) TDIR="$2"; shift || true; shift || true; ;; |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 60 | --build) DOWNLOAD=0; shift || true; ;; |
| 61 | --buildtar) BTAR=1; DOWNLOAD=0; TARDIR="$2"; shift || true; shift || true; ;; |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 62 | --device|-s) ADB="$ADB -s $2"; shift || true; shift || true; ;; |
Joel Fernandes (Google) | 5e40e89 | 2018-10-14 13:14:34 -0700 | [diff] [blame] | 63 | --build-image) BI=1; BUILD_IMAGEF=$2; SKIP_DEVICE=1; DOWNLOAD=0; shift || true; shift || true; ;; |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 64 | --debug) set -x; shift || true; ;; |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 65 | *) c_error "Unknown option ($1)"; usage; ;; |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 66 | esac |
| 67 | done |
| 68 | |
Joel Fernandes | c755196 | 2018-07-10 14:12:19 -0700 | [diff] [blame] | 69 | [ -z $ASHELL ] && box_out "adeb: $VERSION" |
Joel Fernandes | dd102af | 2018-07-08 19:38:42 -0700 | [diff] [blame] | 70 | |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 71 | if [ $FULL -eq 1 ]; then |
| 72 | FNAME=androdeb-fs.tgz.zip |
| 73 | FNAME_UZ=androdeb-fs.tgz |
| 74 | else |
| 75 | FNAME=androdeb-fs-minimal.tgz.zip |
| 76 | FNAME_UZ=androdeb-fs-minimal.tgz |
| 77 | fi |
| 78 | |
Joel Fernandes | ddd2803 | 2018-07-08 23:13:17 -0700 | [diff] [blame] | 79 | if [ ! -z $BTAR ] && [ -z $TARDIR ]; then |
| 80 | TARDIR=$spath |
| 81 | fi |
| 82 | |
Erick Reyes | e25caea | 2018-06-19 15:42:59 -0700 | [diff] [blame] | 83 | if [ ! -z "$GIT_PULL" ]; then |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 84 | c_info "Updating androdeb by git pull" |
Joel Fernandes | 1637115 | 2018-04-05 22:50:36 -0700 | [diff] [blame] | 85 | cd $spath |
| 86 | git pull |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 87 | c_info "Done." |
Joel Fernandes | 1637115 | 2018-04-05 22:50:36 -0700 | [diff] [blame] | 88 | exit 0 |
| 89 | fi |
| 90 | |
Erick Reyes | e25caea | 2018-06-19 15:42:59 -0700 | [diff] [blame] | 91 | if [ ! -z "$PULL" ]; then |
| 92 | if [ $1 == "-a" ]; then |
| 93 | PRESERVE="-a" |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 94 | c_info "Preserving filestamps and mode" |
Erick Reyes | e25caea | 2018-06-19 15:42:59 -0700 | [diff] [blame] | 95 | shift || true |
| 96 | fi |
Erick Reyes | 5104a6c | 2018-06-27 15:31:58 -0700 | [diff] [blame] | 97 | file_count=`count_sources $@` |
Erick Reyes | e25caea | 2018-06-19 15:42:59 -0700 | [diff] [blame] | 98 | i=0 |
| 99 | while [ $i -lt $file_count ]; do |
| 100 | files["$i"]=/data/androdeb/debian/$1 |
| 101 | shift || true |
| 102 | i=$((i + 1)) |
| 103 | done |
| 104 | $ADB pull $PRESERVE "${files[@]}" "$@" |
| 105 | exit 0 |
| 106 | fi |
| 107 | |
Erick Reyes | 5104a6c | 2018-06-27 15:31:58 -0700 | [diff] [blame] | 108 | if [ ! -z "$PUSH" ]; then |
Erick Reyes | 5104a6c | 2018-06-27 15:31:58 -0700 | [diff] [blame] | 109 | file_count=`count_sources $@` |
| 110 | i=0 |
| 111 | while [ $i -lt $file_count ]; do |
| 112 | files["$i"]=$1 |
| 113 | shift || true |
| 114 | i=$((i + 1)) |
| 115 | done |
| 116 | dest=/data/androdeb/debian/$1 |
| 117 | $ADB push $sync "${files[@]}" $dest |
| 118 | exit 0 |
| 119 | fi |
| 120 | |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 121 | 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] | 122 | |
Joel Fernandes | fdc5200 | 2018-07-08 18:49:20 -0700 | [diff] [blame] | 123 | if [ -z $BI ]; then |
Joel Fernandes | dd102af | 2018-07-08 19:38:42 -0700 | [diff] [blame] | 124 | [ -z $ASHELL ] && c_info "Looking for device.." |
Joel Fernandes (Google) | 9ed573a | 2018-07-25 16:07:55 -0700 | [diff] [blame] | 125 | set +e |
Joel Fernandes | c26696d | 2018-07-08 18:28:12 -0700 | [diff] [blame] | 126 | do_adb_root "$ADB" |
Joel Fernandes | af07ec7 | 2018-04-02 22:01:19 -0700 | [diff] [blame] | 127 | |
Joel Fernandes | c26696d | 2018-07-08 18:28:12 -0700 | [diff] [blame] | 128 | if [ $? -ne 0 ]; then |
| 129 | c_error "adb root failed, make sure:" |
| 130 | c_error " * If multiple devices connected, provide --device <serialno> (or -s <serialno>)" |
| 131 | c_error " * Try to run \"adb root\" manually and see if it works. Typically this needs a userdebug build." |
| 132 | c_error "" |
| 133 | c_error "Note: adb can be typically obtained using the android-tools-adb or the adb" |
| 134 | c_error "packages on your distro, or by installing the Android SDK." |
| 135 | die 3 "Exiting." |
| 136 | fi |
Joel Fernandes (Google) | 9ed573a | 2018-07-25 16:07:55 -0700 | [diff] [blame] | 137 | set -e |
Joel Fernandes | fdc5200 | 2018-07-08 18:49:20 -0700 | [diff] [blame] | 138 | else |
| 139 | [ ! -z $BUILD_IMAGEF ] || die 8 "--build-image passed but no image file provided" |
Joel Fernandes (Google) | 9c3ae93 | 2018-05-28 14:34:48 -0700 | [diff] [blame] | 140 | fi |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 141 | |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 142 | if [ ! -z "$REMOVE" ]; then |
| 143 | die_if_no_androdeb "Nothing to remove." |
Joel Fernandes (Google) | 7c8b108 | 2018-06-25 12:29:10 -0700 | [diff] [blame] | 144 | $ADB shell /data/androdeb/device-umount-all || true; |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 145 | $ADB shell rm -rf /data/androdeb; exit 0; fi |
| 146 | |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 147 | ########################################################## |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 148 | # SHELL |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 149 | ########################################################## |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 150 | if [ ! -z ${ASHELL+x} ]; then |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 151 | 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] | 152 | if [ $? -ne 0 ]; then |
| 153 | die 2 "Device doesn't have an androdeb environment, run \"./androdeb prepare\" first"; |
| 154 | fi; set -e |
| 155 | |
Joel Fernandes | ccb993f | 2018-07-02 19:59:04 -0700 | [diff] [blame] | 156 | if [ ! -z ${SHELL_ARGS+x} ]; then |
| 157 | # Explanation of quotes: |
| 158 | # Outer quote is so that androdeb's bash passes the SHELL_ARGS as a single |
| 159 | # argument to $ADB shell. Inner quotes is so that run-command can receive all |
| 160 | # the args even though they may be separated by spaces. \m/ |
| 161 | $ADB shell -t /data/androdeb/run-command "\"$SHELL_ARGS\"" |
| 162 | else |
| 163 | $ADB shell -t /data/androdeb/run |
| 164 | fi |
| 165 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 166 | exit 0 |
| 167 | fi |
| 168 | |
| 169 | ########################################################## |
| 170 | # PREPARE |
| 171 | ########################################################## |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 172 | |
| 173 | function do_cleanup() { |
| 174 | rm -rf $TDIR/*; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi |
| 175 | } |
| 176 | |
| 177 | function push_unpack_headers() { |
Joel Fernandes | b4c80a6 | 2018-04-02 22:37:22 -0700 | [diff] [blame] | 178 | die_if_no_androdeb "Couldn't update headers." |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 179 | |
Joel Fernandes | f95f2b2 | 2018-07-08 17:54:47 -0700 | [diff] [blame] | 180 | c_info "Storing kernel headers into androdeb /kernel-headers/" |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 181 | $ADB shell rm -rf /data/androdeb/debian/kernel-headers/ |
| 182 | $ADB shell mkdir /data/androdeb/debian/kernel-headers/ |
Joel Fernandes | f95f2b2 | 2018-07-08 17:54:47 -0700 | [diff] [blame] | 183 | run_quiet $ADB push $TDIR_ABS/kh.tgz /data/androdeb/ |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 184 | $ADB shell tar -xvf /data/androdeb/kh.tgz -C /data/androdeb/debian/kernel-headers/ > /dev/null |
| 185 | $ADB shell rm /data/androdeb/kh.tgz |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 188 | function push_unpack_tarred_headers() { |
| 189 | die_if_no_androdeb "Couldn't update headers." |
| 190 | |
| 191 | $ADB shell rm -rf /data/androdeb/debian/kernel-headers/ |
| 192 | $ADB shell mkdir /data/androdeb/debian/kernel-headers/ |
Joel Fernandes | f95f2b2 | 2018-07-08 17:54:47 -0700 | [diff] [blame] | 193 | |
| 194 | c_info "Pushing headers tar onto device" |
| 195 | run_quiet $ADB push $1 /data/androdeb/ |
| 196 | |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 197 | c_info "Storing kernel headers into androdeb root directory" |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 198 | $ADB shell tar -xvf /data/androdeb/$(basename $1) -C /data/androdeb/debian/ > /dev/null |
| 199 | |
| 200 | $ADB shell rm /data/androdeb/$(basename $1) |
| 201 | } |
| 202 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 203 | function all_done_banner() { |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 204 | c_info "All done! Run \"adeb shell\" to enter environment" |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Erick Reyes | eb6bc73 | 2018-08-16 15:39:48 -0700 | [diff] [blame] | 207 | function detect_repo_url() { |
Erick Reyes | ab56927 | 2018-08-17 16:02:00 -0700 | [diff] [blame] | 208 | ADEB_REPO_URL=`cd $spath && git config -l | grep -m1 remote | grep url | sed -e "s/.*url=//" \ |
Erick Reyes | eb6bc73 | 2018-08-16 15:39:48 -0700 | [diff] [blame] | 209 | -e "s/.*@//" \ |
| 210 | -e "s/https:\/\///" \ |
| 211 | -e "s/:/\//" \ |
| 212 | -e "s/\.git$//"`"/" |
| 213 | c_info "Detected URL: $ADEB_REPO_URL" |
| 214 | } |
| 215 | |
Erick Reyes | de9be05 | 2018-08-02 20:08:28 -0700 | [diff] [blame] | 216 | function check_repo_url () { |
Erick Reyes | eb6bc73 | 2018-08-16 15:39:48 -0700 | [diff] [blame] | 217 | if [ -z $ADEB_REPO_URL ]; then |
| 218 | c_info "No repository URL provided in enviromnent. Attempting to auto-detect it" |
| 219 | detect_repo_url |
| 220 | fi |
| 221 | |
| 222 | if [ -z $ADEB_REPO_URL ]; then |
| 223 | c_warning "Automatic download is disabled. To enable it, please set the \$ADEB_REPO_URL" |
| 224 | c_warning "environment variable as recommended in the setup instructions in the README.md" |
| 225 | do_cleanup |
| 226 | exit 0 |
| 227 | fi |
Erick Reyes | de9be05 | 2018-08-02 20:08:28 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | function download_headers() { |
| 231 | KERNEL_MAJOR=`$ADB shell uname -r | cut -d - -f 1 | cut -d . -f 1` |
| 232 | KERNEL_MINOR=`$ADB shell uname -r | cut -d - -f 1 | cut -d . -f 2` |
| 233 | KERNEL_VERSION="$KERNEL_MAJOR.$KERNEL_MINOR" |
Erick Reyes | dd195c0 | 2018-08-16 14:50:47 -0700 | [diff] [blame] | 234 | PREBUILT_HEADERS_FILE=headers-$ARCH-$KERNEL_VERSION.tar.gz.zip |
Erick Reyes | de9be05 | 2018-08-02 20:08:28 -0700 | [diff] [blame] | 235 | |
| 236 | check_repo_url |
| 237 | |
Erick Reyes | dd195c0 | 2018-08-16 14:50:47 -0700 | [diff] [blame] | 238 | curl -L https://$ADEB_REPO_URL/releases/download/$VERSION/$PREBUILT_HEADERS_FILE --output $TDIR_ABS/$PREBUILT_HEADERS_FILE || |
| 239 | die 9 "Failed to download kernel headers. Please check your internet connection and repository URL" |
Erick Reyes | de9be05 | 2018-08-02 20:08:28 -0700 | [diff] [blame] | 240 | |
| 241 | unzip -e $TDIR_ABS/$PREBUILT_HEADERS_FILE -d $TDIR_ABS/ || |
Erick Reyes | dd195c0 | 2018-08-16 14:50:47 -0700 | [diff] [blame] | 242 | die 10 "Failed to download kernel headers. Kernel $KERNEL_VERSION for $ARCH may not be supported or ADEB_REPO_URL is incorrect." |
Erick Reyes | de9be05 | 2018-08-02 20:08:28 -0700 | [diff] [blame] | 243 | KERNELHDRS=$TDIR_ABS/`echo "$PREBUILT_HEADERS_FILE" | sed "s/.zip//"` |
| 244 | } |
| 245 | |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 246 | # Prepare is the last command checked |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 247 | if [ -z "$PREPARE" ]; then usage; fi |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 248 | |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 249 | if [ ! -z "$TARF" ] && [ ! -f $TARF ] && [ -z "$DOWNLOAD" ]; then die 5 "archive provided doesn't exist"; fi |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 250 | |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 251 | if [ ! -z "$KERNELSRC" ] && [ ! -d $KERNELSRC ]; then die 6 "Kernel source directory provided doesn't exist"; fi |
| 252 | |
| 253 | if [ ! -z "$KERNELHDRS" ] && [ ! -f $KERNELHDRS ]; then die 7 "Kernel headers tar.gz doesn't exist"; fi |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 254 | |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 255 | print_prepare_banner |
| 256 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 257 | # Where do we want to store temporary files |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 258 | MKTEMP=0; if [[ -z ${TDIR+x} ]] || [[ ! -d "${TDIR}" ]]; then |
| 259 | TDIR=`mktemp -d`; MKTEMP=1; fi |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 260 | rm -rf $TDIR/* |
| 261 | TDIR_ABS=$( cd "$TDIR" ; pwd -P ) |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 262 | |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 263 | if [ $DOWNLOAD -eq 1 ]; then |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 264 | c_info "Downloading Androdeb from the web..."; c_info "" |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 265 | |
Joel Fernandes (Google) | 9e670aa | 2018-06-25 13:39:34 -0700 | [diff] [blame] | 266 | # Github dropped tar gz support! ##?#??#! Now we've to zip everything. |
Erick Reyes | de9be05 | 2018-08-02 20:08:28 -0700 | [diff] [blame] | 267 | check_repo_url |
Joel Fernandes (Google) | db9005f | 2018-07-25 16:44:12 -0700 | [diff] [blame] | 268 | |
Erick Reyes | dd195c0 | 2018-08-16 14:50:47 -0700 | [diff] [blame] | 269 | curl -L https://$ADEB_REPO_URL/releases/download/$VERSION/$FNAME --output $TDIR_ABS/$FNAME || |
Joel Fernandes (Google) | db9005f | 2018-07-25 16:44:12 -0700 | [diff] [blame] | 270 | die 9 "Failed to download adeb release." |
| 271 | |
| 272 | unzip -e $TDIR_ABS/$FNAME -d $TDIR_ABS/ || |
| 273 | die 10 "Failed to download adeb release. Double check the ADEB_REPO_URL value." |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 274 | TARF=$TDIR_ABS/$FNAME_UZ |
| 275 | fi |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 276 | |
Joel Fernandes (Google) | 5e40e89 | 2018-10-14 13:14:34 -0700 | [diff] [blame] | 277 | if [ ! -z "$FULL" ] && [ -z "$KERNELSRC" ] && [ -z "$KERNELHDRS" ] && [ -z "$BI" ]; then |
Erick Reyes | de9be05 | 2018-08-02 20:08:28 -0700 | [diff] [blame] | 278 | c_info "Kernel headers are needed but none were provided. Downloading pre-built headers" |
| 279 | download_headers |
| 280 | fi |
| 281 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 282 | OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 283 | |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 284 | # Unpack the supplied kernel headers tar.gz directly into androdeb root |
| 285 | if [ ! -z "$KERNELHDRS" ]; then |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 286 | c_info "Building updating kernel headers from supplied tar.gz ($KERNELHDRS)" |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 287 | |
| 288 | # Is header tar gz update the only thing left to do? |
Joel Fernandes (Google) | 1171d2d | 2018-05-30 17:22:09 -0700 | [diff] [blame] | 289 | if [[ ! -z "$SKIP_INSTALL" ]]; then |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 290 | c_info "Skipping install" |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 291 | push_unpack_tarred_headers $KERNELHDRS; do_cleanup; all_done_banner; exit 0; fi |
| 292 | |
| 293 | tar -xvf $KERNELHDRS -C $OUT_TMP/ > /dev/null |
| 294 | fi |
| 295 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 296 | # Package kernel headers |
| 297 | if [ ! -z "$KERNELSRC" ]; then |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 298 | c_info "Building and updating kernel headers from kernel source dir ($KERNELSRC)" |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 299 | $spath/bcc/build-kheaders-targz.sh ${KERNELSRC} $TDIR_ABS/kh.tgz > /dev/null |
| 300 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 301 | # Is header update the only thing left to do? |
Joel Fernandes (Google) | 1171d2d | 2018-05-30 17:22:09 -0700 | [diff] [blame] | 302 | if [[ ! -z "$SKIP_INSTALL" ]]; then |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 303 | c_info "Skipping install" |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 304 | push_unpack_headers; do_cleanup; all_done_banner; exit 0; fi |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 305 | |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 306 | mkdir $OUT_TMP/kernel-headers |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 307 | 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] | 308 | fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 309 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 310 | # Build FS from existing tar, very simple. |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 311 | if [ ! -z "$TARF" ]; then |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 312 | c_info "Using archive at $TARF for filesystem preparation" |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 313 | $ADB shell mkdir -p /data/androdeb/ |
Joel Fernandes | f95f2b2 | 2018-07-08 17:54:47 -0700 | [diff] [blame] | 314 | |
| 315 | c_info "Pushing filesystem to device.." |
| 316 | run_quiet $ADB push $TARF /data/androdeb/deb.tar.gz |
| 317 | |
| 318 | c_info "Pushing addons to device.." |
| 319 | run_quiet $ADB push $spath/addons/* /data/androdeb/ |
| 320 | |
| 321 | c_info "Unpacking filesystem in device.." |
| 322 | run_quiet $ADB shell /data/androdeb/device-unpack |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 323 | |
Joel Fernandes | 1ed30d7 | 2018-05-18 14:08:20 -0700 | [diff] [blame] | 324 | if [ ! -z "$KERNELHDRS" ]; then push_unpack_tarred_headers $KERNELHDRS; fi |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 325 | if [ ! -z "$KERNELSRC" ]; then push_unpack_headers; fi |
| 326 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 327 | do_cleanup; all_done_banner; exit 0 |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 328 | fi |
| 329 | |
| 330 | PACKAGES+="$DEFAULT_PACKAGES" |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 331 | c_info "Using temporary directory: $TDIR" |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 332 | |
Joel Fernandes | 112243b | 2018-07-08 17:42:48 -0700 | [diff] [blame] | 333 | if [[ $EUID -ne 0 ]]; then c_info "The next stage runs as sudo, please enter password if asked."; fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 334 | |
Joel Fernandes (Google) | 57b3fd2 | 2018-05-29 16:27:03 -0700 | [diff] [blame] | 335 | ex_files=$(mktemp); echo $EXTRA_FILES > $ex_files |
| 336 | |
Joel Fernandes (Google) | 4f71108 | 2018-05-29 16:23:15 -0700 | [diff] [blame] | 337 | sudo $spath/buildstrap $ARCH $DISTRO $TDIR $OUT_TMP \ |
| 338 | "$(make_csv "$PACKAGES")"\ |
Joel Fernandes | fdc5200 | 2018-07-08 18:49:20 -0700 | [diff] [blame] | 339 | $ex_files $INSTALL_BCC $SKIP_DEVICE |
Joel Fernandes (Google) | 57b3fd2 | 2018-05-29 16:27:03 -0700 | [diff] [blame] | 340 | rm $ex_files |
Joel Fernandes (Google) | 9c3ae93 | 2018-05-28 14:34:48 -0700 | [diff] [blame] | 341 | |
| 342 | # If we only wanted to prepare a rootfs and don't have |
| 343 | # a device connected, then just echo that and skip cleanup |
Joel Fernandes | fdc5200 | 2018-07-08 18:49:20 -0700 | [diff] [blame] | 344 | if [ $SKIP_DEVICE -eq 1 ]; then |
| 345 | c_info "Device preparation is being skipped for the selected options" |
Joel Fernandes | ba7214e | 2018-07-08 18:15:49 -0700 | [diff] [blame] | 346 | c_info "any builds that need to happen on device may be cloned but not built." |
Joel Fernandes | 47ef215 | 2018-07-08 19:13:51 -0700 | [diff] [blame] | 347 | |
| 348 | if [ ! -z $BI ]; then |
| 349 | sudo $spath/buildimage $OUT_TMP $(dirname $BUILD_IMAGEF)/$(basename $BUILD_IMAGEF) |
| 350 | sudo chmod a+rw $(dirname $BUILD_IMAGEF)/$(basename $BUILD_IMAGEF) |
Joel Fernandes | 9a38e60 | 2018-07-08 19:26:32 -0700 | [diff] [blame] | 351 | c_info "Your .img has been built! Enjoy!" |
Joel Fernandes | 47ef215 | 2018-07-08 19:13:51 -0700 | [diff] [blame] | 352 | fi |
Joel Fernandes | ba7214e | 2018-07-08 18:15:49 -0700 | [diff] [blame] | 353 | |
| 354 | do_cleanup |
Joel Fernandes | 9a38e60 | 2018-07-08 19:26:32 -0700 | [diff] [blame] | 355 | exit 0 |
Joel Fernandes (Google) | 9c3ae93 | 2018-05-28 14:34:48 -0700 | [diff] [blame] | 356 | fi |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 357 | |
| 358 | # Push tar to device and start unpack |
Joel Fernandes | 02939a3 | 2018-04-02 22:18:10 -0700 | [diff] [blame] | 359 | $ADB shell mkdir -p /data/androdeb/ |
| 360 | $ADB push $TDIR/deb.tar.gz /data/androdeb/ |
| 361 | $ADB push $spath/addons/* /data/androdeb/ |
| 362 | $ADB shell /data/androdeb/device-unpack |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 363 | |
Joel Fernandes | 6f1c70f | 2018-03-31 20:26:26 -0700 | [diff] [blame] | 364 | # Build BCC and install bcc on device if needed |
Joel Fernandes | 241160e | 2018-07-08 23:36:11 -0700 | [diff] [blame] | 365 | if [ $INSTALL_BCC -eq 1 ]; then |
Joel Fernandes | ffcbfe4 | 2018-07-08 19:08:02 -0700 | [diff] [blame] | 366 | $ADB shell /data/androdeb/run-command /bcc-master/build-bcc.sh; |
| 367 | fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 368 | |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 369 | # Extract a tar of the built, compiled and installed androdeb env |
| 370 | if [[ ! -z ${TARDIR+x} ]]; then |
Joel Fernandes | 6975db2 | 2018-07-11 20:53:16 -0700 | [diff] [blame] | 371 | c_info "Creating tarball" |
| 372 | pushd $TARDIR |
| 373 | if [ $INSTALL_BCC -eq 0 ]; then |
| 374 | mv $TDIR/deb.tar.gz $FNAME_UZ |
| 375 | else |
| 376 | $ADB shell /data/androdeb/build-debian-tar |
| 377 | $ADB pull /data/androdeb/androdeb-fs.tgz $FNAME_UZ |
| 378 | $ADB shell rm /data/androdeb/androdeb-fs.tgz; |
| 379 | fi |
| 380 | zip -r $FNAME $FNAME_UZ |
| 381 | popd |
| 382 | fi |
| 383 | |
| 384 | do_cleanup |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 385 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 386 | all_done_banner |