Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 1 | #!/bin/bash -e |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 2 | spath=$( cd "$(dirname "$0")" ; pwd -P ) |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 3 | curdir=$( pwd -P ) |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 4 | source $spath/utils/android |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 5 | |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 6 | usage() { |
| 7 | echo "androdeb" |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 8 | echo " shell Enter the androdeb shell environment and get to work!" |
| 9 | echo "" |
Joel Fernandes | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 10 | echo " prepare Prepare the device (when running for the first time)" |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 11 | echo " --tracers Enable tracing packages (perf and trace-cmd)" |
| 12 | echo " --compilers Enable compilers on the FS (gcc and clang)" |
| 13 | echo " --editors Enable vim, emacs and git packages" |
| 14 | echo " --scheduler scheduler testing tools (only rt-app for now)" |
| 15 | echo " --bcc Build and install BCC from source" |
| 16 | echo " --fullbuild Enable all of the above tools" |
| 17 | echo "" |
| 18 | echo " --kernelsrc path-to-kernel-sources (can be used for bcc)" |
| 19 | echo " --tempdir use a specific temporary directory" |
| 20 | echo " --distro Debian distro to base on (default is buster)" |
| 21 | exit 1 |
| 22 | } |
| 23 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 24 | # Set default vars |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 25 | DISTRO=buster; PACKAGES=""; ARCH=arm64 |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 26 | |
| 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 | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 31 | prepare) PREP=1; shift || true; ;; |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 32 | shell) ASHELL=1; shift || true; ;; |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 33 | --tracers) source $spath/packages/tracers; shift || true; ;; |
| 34 | --compilers) source $spath/packages/compilers; shift || true; ;; |
| 35 | --editors) source $spath/packages/editors; shift || true; ;; |
| 36 | --scheduler) source $spath/packages/scheduler; shift || true; ;; |
| 37 | --fullbuild) for f in $(ls $spath/packages); do source packages/$f; done; shift || true; ;; |
Joel Fernandes | 77e5379 | 2018-03-31 20:03:42 -0700 | [diff] [blame] | 38 | --bcc) source $spath/packages/bcc; INSTALL_BCC=1; 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 | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 41 | *) echo "Unknown option ($1)"; usage; ;; |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 42 | esac |
| 43 | done |
| 44 | |
Joel Fernandes | 6f1c70f | 2018-03-31 20:26:26 -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 | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 49 | if [[ ! -z ${PREP+x} ]] && [[ "x$PACKAGES" == "x" ]]; then |
| 50 | echo "Need to specifify something to prepare"; usage; |
| 51 | fi |
Joel Fernandes | 77e5379 | 2018-03-31 20:03:42 -0700 | [diff] [blame] | 52 | PACKAGES+="\ |
| 53 | bash |
| 54 | ca-certifcates |
| 55 | " |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 56 | |
| 57 | ########################################################## |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 58 | # SHELL |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 59 | ########################################################## |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 60 | if [[ ! -z ${ASHELL+x} ]]; then |
Joel Fernandes | c964681 | 2018-03-31 19:45:46 -0700 | [diff] [blame] | 61 | set +e; adb shell ls /data/androdeb/debian/.bashrc > /dev/null 2>&1 |
| 62 | if [ $? -ne 0 ]; then |
| 63 | die 2 "Device doesn't have an androdeb environment, run \"./androdeb prepare\" first"; |
| 64 | fi; set -e |
| 65 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 66 | echo "For a better shell experience, run the following commands:" |
| 67 | echo " adb shell" |
| 68 | echo " /data/androdeb/run" |
| 69 | adb shell -x /data/androdeb/run |
| 70 | exit 0 |
| 71 | fi |
| 72 | |
| 73 | ########################################################## |
| 74 | # PREPARE |
| 75 | ########################################################## |
| 76 | if [[ $EUID -ne 0 ]]; then echo "For prepare, this tool must run as root"; |
| 77 | echo "Try ./sudo androdeb prepare <other-args>"; exit 3; fi |
| 78 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 79 | # Where do we want to store temporary files |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 80 | MKTEMP=0; if [[ -z ${TDIR+x} ]] || [[ ! -d "${TDIR}" ]]; then |
| 81 | TDIR=`mktemp -d`; MKTEMP=1; fi |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 82 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 83 | OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 84 | |
| 85 | echo "Full package list: $PACKAGES" |
| 86 | echo "Using temporary directory: $OUT_TMP" |
| 87 | |
| 88 | time qemu-debootstrap --arch arm64 --include=$(make_csv "$PACKAGES") \ |
Joel Fernandes | 0bd6886 | 2018-03-31 16:03:00 -0700 | [diff] [blame] | 89 | $DISTRO $OUT_TMP http://deb.debian.org/debian/ |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 90 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 91 | # Some reason debootstrap leaves these founded |
| 92 | umount $OUT_TMP/proc/sys/fs/binfmt_misc |
| 93 | umount $OUT_TMP/proc |
| 94 | |
| 95 | # Make bash the default shell |
| 96 | chroot $OUT_TMP rm /bin/sh || true |
| 97 | chroot $OUT_TMP ln -s /bin/bash /bin/sh || true |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 98 | cp $spath/addons/bashrc $OUT_TMP/.bashrc |
| 99 | |
Joel Fernandes | 8957a14 | 2018-03-31 16:58:45 -0700 | [diff] [blame] | 100 | # For mounting android partitions |
| 101 | mkdir $OUT_TMP/system |
| 102 | mkdir $OUT_TMP/vendor |
| 103 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 104 | # Cleanup |
| 105 | rm -rf $OUT_TMP/lib/udev/* |
| 106 | rm -rf $OUT_TMP/var/lib/apt/lists/* |
| 107 | rm -rf $OUT_TMP/var/cache/apt/archives/*deb |
| 108 | rm -rf $OUT_TMP/usr/share/locale/* |
| 109 | rm -rf $OUT_TMP/usr/lib/share/locale/* |
| 110 | rm -rf $OUT_TMP/usr/share/doc/* |
| 111 | rm -rf $OUT_TMP/usr/lib/share/doc/* |
| 112 | rm -rf $OUT_TMP/usr/share/ieee-data/* |
| 113 | rm -rf $OUT_TMP/usr/lib/share/ieee-data/* |
| 114 | rm -rf $OUT_TMP/usr/share/man/* |
| 115 | rm -rf $OUT_TMP/usr/lib/share/man/* |
| 116 | |
Joel Fernandes | 77e5379 | 2018-03-31 20:03:42 -0700 | [diff] [blame] | 117 | # Clone BCC if needed |
| 118 | if [[ ! -z ${INSTALL_BCC+x} ]]; then |
| 119 | git clone https://github.com/iovisor/bcc.git $TDIR/debian/bcc-master |
| 120 | cp $spath/bcc/build-bcc.sh $TDIR/debian/bcc-master/ |
| 121 | fi |
| 122 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 123 | echo "Compressing new filesystem to prepare to push to Android /data/androdeb/" |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 124 | tar -zcf $TDIR/deb.tar.gz -C $TDIR debian |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 125 | |
| 126 | # Push tar to device and start unpack |
| 127 | adb shell mkdir -p /data/androdeb/ |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 128 | adb push $TDIR/deb.tar.gz /data/androdeb/ |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 129 | adb push $spath/addons/device-* /data/androdeb/ |
| 130 | adb shell /data/androdeb/device-unpack |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 131 | |
Joel Fernandes | 6f1c70f | 2018-03-31 20:26:26 -0700 | [diff] [blame^] | 132 | # Build BCC and install bcc on device if needed |
| 133 | if [[ ! -z ${INSTALL_BCC+x} ]]; then |
| 134 | adb shell /data/androdeb/run-command /bcc-master/build-bcc.sh |
| 135 | fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 136 | |
Joel Fernandes | 6f1c70f | 2018-03-31 20:26:26 -0700 | [diff] [blame^] | 137 | # rm -rf $OUT_TMP; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 138 | # Use --foreign and --variant=minbase to build minimal deb first stage |
| 139 | # Use fakeroot tar to build tar with root files |