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