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 | 536edfc | 2018-04-01 20:31:07 -0700 | [diff] [blame] | 5 | VERSION=v0.95 |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 6 | |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 7 | spath=$( cd "$(dirname "$0")" ; pwd -P ) |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 8 | curdir=$( pwd -P ) |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 9 | source $spath/utils/android |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 10 | source $spath/utils/banners |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 11 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 12 | # Set default vars |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 13 | DISTRO=buster; ARCH=arm64 |
| 14 | |
| 15 | # Default packages |
| 16 | DEFAULT_PACKAGES+="\ |
| 17 | bash |
| 18 | ca-certificates |
| 19 | " |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 20 | |
| 21 | # Parse command line parameters |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 22 | if [ $# -lt 1 ]; then usage; fi; POSITIONAL=() |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 23 | while [[ $# -gt 0 ]]; do key="$1"; |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 24 | case $key in |
Joel Fernandes | e828d03 | 2018-03-31 23:57:35 -0700 | [diff] [blame] | 25 | prepare) PREPARE=1; shift || true; ;; |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 26 | shell) ASHELL=1; shift || true; ;; |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 27 | --archive) TARF=$2; shift || true; shift || true; ;; |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 28 | --tracers) source $spath/packages/tracers; shift || true; ;; |
| 29 | --compilers) source $spath/packages/compilers; shift || true; ;; |
| 30 | --editors) source $spath/packages/editors; shift || true; ;; |
| 31 | --scheduler) source $spath/packages/scheduler; shift || true; ;; |
Joel Fernandes | 100c468 | 2018-04-01 20:09:57 -0700 | [diff] [blame] | 32 | --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] | 33 | --download) DOWNLOAD=1; shift || true; ;; |
Joel Fernandes | 100c468 | 2018-04-01 20:09:57 -0700 | [diff] [blame] | 34 | --bcc) source $spath/packages/bcc; shift || true; ;; |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 35 | --kernelsrc) KERNELSRC="$2"; shift || true; shift || true; ;; |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 36 | --tempdir) TDIR="$2"; shift || true; shift || true; ;; |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 37 | --buildtar) TARDIR="$2"; shift || true; shift || true; ;; |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 38 | --debug) set -x; shift || true; ;; |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 39 | *) echo "Unknown option ($1)"; usage; ;; |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 40 | esac |
| 41 | done |
| 42 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 43 | if [ ! -z "$PREPARE" ] && [ -z "$DOWNLOAD" ] && [ -z "$TARF" ] && [ -z "$PACKAGES" ] && [ -z "$KERNELSRC" ]; then |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 44 | 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] | 45 | |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 46 | 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] | 47 | |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 48 | do_adb_root || die 3 "adb root failed, make sure: |
| 49 | - device is connected, and there's only one device (TODO: add multi device support) |
| 50 | - device is userdebug." |
| 51 | |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 52 | ########################################################## |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 53 | # SHELL |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 54 | ########################################################## |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 55 | if [ ! -z ${ASHELL+x} ]; then |
Joel Fernandes | c964681 | 2018-03-31 19:45:46 -0700 | [diff] [blame] | 56 | set +e; adb shell ls /data/androdeb/debian/.bashrc > /dev/null 2>&1 |
| 57 | if [ $? -ne 0 ]; then |
| 58 | die 2 "Device doesn't have an androdeb environment, run \"./androdeb prepare\" first"; |
| 59 | fi; set -e |
| 60 | |
Joel Fernandes | cf1ceb3 | 2018-04-01 10:10:15 -0700 | [diff] [blame] | 61 | adb shell -t /data/androdeb/run |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 62 | exit 0 |
| 63 | fi |
| 64 | |
| 65 | ########################################################## |
| 66 | # PREPARE |
| 67 | ########################################################## |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 68 | |
| 69 | function do_cleanup() { |
| 70 | rm -rf $TDIR/*; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi |
| 71 | } |
| 72 | |
| 73 | function push_unpack_headers() { |
| 74 | adb shell ls /data/androdeb/debian > /dev/null 2>&1 |
| 75 | if [ $? -ne 0 ]; then die 8 "Existing androdev env not found to update kernel headers into."; fi |
| 76 | |
| 77 | adb shell rm -rf /data/androdeb/debian/kernel-headers/ |
| 78 | adb shell mkdir /data/androdeb/debian/kernel-headers/ |
| 79 | adb push $TDIR_ABS/kh.tgz /data/androdeb/ |
| 80 | echo "Storing kernel headers into androdeb /kernel-headers/" |
| 81 | adb shell tar -xvf /data/androdeb/kh.tgz -C /data/androdeb/debian/kernel-headers/ > /dev/null |
| 82 | adb shell rm /data/androdeb/kh.tgz |
| 83 | } |
| 84 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 85 | function all_done_banner() { |
| 86 | echo "All done! Run \"androdeb shell\" to enter environment" |
| 87 | } |
| 88 | |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 89 | # Prepare is the last command checked |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 90 | if [ -z "$PREPARE" ]; then usage; fi |
Joel Fernandes | 72656c5 | 2018-03-31 20:35:33 -0700 | [diff] [blame] | 91 | |
| 92 | if [[ $EUID -ne 0 ]]; then die 6 "For prepare, this tool must run as root. Try: ./sudo androdeb prepare <args>"; fi |
| 93 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 94 | 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] | 95 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 96 | 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] | 97 | |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 98 | print_prepare_banner |
| 99 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 100 | # Where do we want to store temporary files |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 101 | MKTEMP=0; if [[ -z ${TDIR+x} ]] || [[ ! -d "${TDIR}" ]]; then |
| 102 | TDIR=`mktemp -d`; MKTEMP=1; fi |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 103 | rm -rf $TDIR/* |
| 104 | TDIR_ABS=$( cd "$TDIR" ; pwd -P ) |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 105 | |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 106 | if [ ! -z "$DOWNLOAD" ]; then |
Joel Fernandes | ca8fb55 | 2018-04-01 13:48:16 -0700 | [diff] [blame] | 107 | echo "Downloading Androdeb from the web..."; echo "" |
Joel Fernandes | a67be0b | 2018-04-01 13:40:34 -0700 | [diff] [blame] | 108 | curl -L https://github.com/joelagnel/androdeb/releases/download/$VERSION/androdeb-fs.tgz --output $TDIR_ABS/androdeb-fs.tgz; |
| 109 | TARF=$TDIR_ABS/androdeb-fs.tgz; fi |
| 110 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 111 | OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 112 | |
| 113 | # Package kernel headers |
| 114 | if [ ! -z "$KERNELSRC" ]; then |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 115 | echo "Building and updating kernel headers from kernel source dir ($KERNELSRC)" |
| 116 | $spath/bcc/build-kheaders-targz.sh ${KERNELSRC} $TDIR_ABS/kh.tgz > /dev/null |
| 117 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 118 | # Is header update the only thing left to do? |
| 119 | if [[ -z "$PACKAGES" ]] && [[ -z "$TARF" ]]; then |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 120 | push_unpack_headers; do_cleanup; all_done_banner; exit 0; fi |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 121 | |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 122 | mkdir $OUT_TMP/kernel-headers |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 123 | 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] | 124 | fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 125 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 126 | # Build FS from existing tar, very simple. |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 127 | if [ ! -z "$TARF" ]; then |
| 128 | echo "Using archive at $TARF for filesystem preparation" |
| 129 | adb shell mkdir -p /data/androdeb/ |
Joel Fernandes | bb6337d | 2018-04-01 11:03:16 -0700 | [diff] [blame] | 130 | adb push $TARF /data/androdeb/deb.tar.gz |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 131 | adb push $spath/addons/* /data/androdeb/ |
| 132 | adb shell /data/androdeb/device-unpack |
Joel Fernandes | 9f00bf4 | 2018-04-01 10:36:33 -0700 | [diff] [blame] | 133 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 134 | if [ ! -z "$KERNELSRC" ]; then push_unpack_headers; fi |
| 135 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 136 | do_cleanup; all_done_banner; exit 0 |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 137 | fi |
| 138 | |
| 139 | PACKAGES+="$DEFAULT_PACKAGES" |
Joel Fernandes | 25f7915 | 2018-03-31 21:18:40 -0700 | [diff] [blame] | 140 | echo "Using temporary directory: $TDIR" |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 141 | |
| 142 | time qemu-debootstrap --arch arm64 --include=$(make_csv "$PACKAGES") \ |
Joel Fernandes | 0bd6886 | 2018-03-31 16:03:00 -0700 | [diff] [blame] | 143 | $DISTRO $OUT_TMP http://deb.debian.org/debian/ |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 144 | |
Joel Fernandes | 0b735b5 | 2018-04-01 15:33:57 -0700 | [diff] [blame] | 145 | # Some reason debootstrap leaves these mounted |
| 146 | umount $OUT_TMP/proc/sys/fs/binfmt_misc || true |
| 147 | umount $OUT_TMP/proc || true |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 148 | |
| 149 | # Make bash the default shell |
| 150 | chroot $OUT_TMP rm /bin/sh || true |
| 151 | chroot $OUT_TMP ln -s /bin/bash /bin/sh || true |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 152 | cp $spath/addons/bashrc $OUT_TMP/.bashrc |
| 153 | |
Joel Fernandes | 8957a14 | 2018-03-31 16:58:45 -0700 | [diff] [blame] | 154 | # For mounting android partitions |
| 155 | mkdir $OUT_TMP/system |
| 156 | mkdir $OUT_TMP/vendor |
| 157 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 158 | # Cleanup |
| 159 | rm -rf $OUT_TMP/lib/udev/* |
| 160 | rm -rf $OUT_TMP/var/lib/apt/lists/* |
| 161 | rm -rf $OUT_TMP/var/cache/apt/archives/*deb |
| 162 | rm -rf $OUT_TMP/usr/share/locale/* |
| 163 | rm -rf $OUT_TMP/usr/lib/share/locale/* |
| 164 | rm -rf $OUT_TMP/usr/share/doc/* |
| 165 | rm -rf $OUT_TMP/usr/lib/share/doc/* |
| 166 | rm -rf $OUT_TMP/usr/share/ieee-data/* |
| 167 | rm -rf $OUT_TMP/usr/lib/share/ieee-data/* |
| 168 | rm -rf $OUT_TMP/usr/share/man/* |
| 169 | rm -rf $OUT_TMP/usr/lib/share/man/* |
| 170 | |
Joel Fernandes | 77e5379 | 2018-03-31 20:03:42 -0700 | [diff] [blame] | 171 | # Clone BCC if needed |
| 172 | if [[ ! -z ${INSTALL_BCC+x} ]]; then |
| 173 | git clone https://github.com/iovisor/bcc.git $TDIR/debian/bcc-master |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 174 | cp $spath/bcc/build-bcc.sh $TDIR/debian/bcc-master/; fi |
Joel Fernandes | 77e5379 | 2018-03-31 20:03:42 -0700 | [diff] [blame] | 175 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 176 | echo "Compressing new filesystem to prepare to push to Android /data/androdeb/" |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 177 | tar -zcf $TDIR/deb.tar.gz -C $TDIR debian |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 178 | |
| 179 | # Push tar to device and start unpack |
| 180 | adb shell mkdir -p /data/androdeb/ |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 181 | adb push $TDIR/deb.tar.gz /data/androdeb/ |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 182 | adb push $spath/addons/* /data/androdeb/ |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 183 | adb shell /data/androdeb/device-unpack |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 184 | |
Joel Fernandes | 6f1c70f | 2018-03-31 20:26:26 -0700 | [diff] [blame] | 185 | # Build BCC and install bcc on device if needed |
| 186 | if [[ ! -z ${INSTALL_BCC+x} ]]; then |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 187 | adb shell /data/androdeb/run-command /bcc-master/build-bcc.sh; fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 188 | |
Joel Fernandes | f0c5409 | 2018-04-01 19:05:10 -0700 | [diff] [blame] | 189 | do_cleanup |
Joel Fernandes | e381ac7 | 2018-03-31 21:46:36 -0700 | [diff] [blame] | 190 | |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 191 | # Extract a tar of the built, compiled and installed androdeb env |
| 192 | if [[ ! -z ${TARDIR+x} ]]; then |
| 193 | echo "Creating and pulling tarball of androdeb env from device" |
| 194 | adb shell /data/androdeb/build-debian-tar |
Joel Fernandes | a34c884 | 2018-04-01 13:48:47 -0700 | [diff] [blame] | 195 | adb pull /data/androdeb/androdeb-fs.tgz $TARDIR/ |
Joel Fernandes | 46c642d | 2018-04-01 15:08:52 -0700 | [diff] [blame] | 196 | adb shell rm /data/androdeb/androdeb-fs.tgz; fi |
Joel Fernandes | 97958e1 | 2018-03-31 23:51:44 -0700 | [diff] [blame] | 197 | |
Joel Fernandes | 7206511 | 2018-04-01 20:02:53 -0700 | [diff] [blame] | 198 | all_done_banner |