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