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 | 5b4be9c | 2018-03-30 23:58:27 -0700 | [diff] [blame] | 45 | if [[ ! -z ${PREP+x} ]] && [[ "x$PACKAGES" == "x" ]]; then |
| 46 | echo "Need to specifify something to prepare"; usage; |
| 47 | fi |
Joel Fernandes | 77e5379 | 2018-03-31 20:03:42 -0700 | [diff] [blame] | 48 | PACKAGES+="\ |
| 49 | bash |
| 50 | ca-certifcates |
| 51 | " |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 52 | |
| 53 | ########################################################## |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 54 | # SHELL |
Joel Fernandes | 83676b1 | 2018-03-30 23:33:47 -0700 | [diff] [blame] | 55 | ########################################################## |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 56 | if [[ ! -z ${ASHELL+x} ]]; then |
Joel Fernandes | c964681 | 2018-03-31 19:45:46 -0700 | [diff] [blame] | 57 | set +e; adb shell ls /data/androdeb/debian/.bashrc > /dev/null 2>&1 |
| 58 | if [ $? -ne 0 ]; then |
| 59 | die 2 "Device doesn't have an androdeb environment, run \"./androdeb prepare\" first"; |
| 60 | fi; set -e |
| 61 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 62 | echo "For a better shell experience, run the following commands:" |
| 63 | echo " adb shell" |
| 64 | echo " /data/androdeb/run" |
| 65 | adb shell -x /data/androdeb/run |
| 66 | exit 0 |
| 67 | fi |
| 68 | |
| 69 | ########################################################## |
| 70 | # PREPARE |
| 71 | ########################################################## |
| 72 | if [[ $EUID -ne 0 ]]; then echo "For prepare, this tool must run as root"; |
| 73 | echo "Try ./sudo androdeb prepare <other-args>"; exit 3; fi |
| 74 | |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 75 | # Where do we want to store temporary files |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 76 | MKTEMP=0; if [[ -z ${TDIR+x} ]] || [[ ! -d "${TDIR}" ]]; then |
| 77 | TDIR=`mktemp -d`; MKTEMP=1; fi |
Joel Fernandes | 6c1ca31 | 2018-03-30 17:13:15 -0700 | [diff] [blame] | 78 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 79 | OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 80 | |
| 81 | echo "Full package list: $PACKAGES" |
| 82 | echo "Using temporary directory: $OUT_TMP" |
| 83 | |
| 84 | time qemu-debootstrap --arch arm64 --include=$(make_csv "$PACKAGES") \ |
Joel Fernandes | 0bd6886 | 2018-03-31 16:03:00 -0700 | [diff] [blame] | 85 | $DISTRO $OUT_TMP http://deb.debian.org/debian/ |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 86 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 87 | # Some reason debootstrap leaves these founded |
| 88 | umount $OUT_TMP/proc/sys/fs/binfmt_misc |
| 89 | umount $OUT_TMP/proc |
| 90 | |
| 91 | # Make bash the default shell |
| 92 | chroot $OUT_TMP rm /bin/sh || true |
| 93 | chroot $OUT_TMP ln -s /bin/bash /bin/sh || true |
Joel Fernandes | 4fb97ea | 2018-03-31 11:15:19 -0700 | [diff] [blame] | 94 | cp $spath/addons/bashrc $OUT_TMP/.bashrc |
| 95 | |
Joel Fernandes | 8957a14 | 2018-03-31 16:58:45 -0700 | [diff] [blame] | 96 | # For mounting android partitions |
| 97 | mkdir $OUT_TMP/system |
| 98 | mkdir $OUT_TMP/vendor |
| 99 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 100 | # Cleanup |
| 101 | rm -rf $OUT_TMP/lib/udev/* |
| 102 | rm -rf $OUT_TMP/var/lib/apt/lists/* |
| 103 | rm -rf $OUT_TMP/var/cache/apt/archives/*deb |
| 104 | rm -rf $OUT_TMP/usr/share/locale/* |
| 105 | rm -rf $OUT_TMP/usr/lib/share/locale/* |
| 106 | rm -rf $OUT_TMP/usr/share/doc/* |
| 107 | rm -rf $OUT_TMP/usr/lib/share/doc/* |
| 108 | rm -rf $OUT_TMP/usr/share/ieee-data/* |
| 109 | rm -rf $OUT_TMP/usr/lib/share/ieee-data/* |
| 110 | rm -rf $OUT_TMP/usr/share/man/* |
| 111 | rm -rf $OUT_TMP/usr/lib/share/man/* |
| 112 | |
Joel Fernandes | 77e5379 | 2018-03-31 20:03:42 -0700 | [diff] [blame] | 113 | # Clone BCC if needed |
| 114 | if [[ ! -z ${INSTALL_BCC+x} ]]; then |
| 115 | git clone https://github.com/iovisor/bcc.git $TDIR/debian/bcc-master |
| 116 | cp $spath/bcc/build-bcc.sh $TDIR/debian/bcc-master/ |
| 117 | fi |
| 118 | |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 119 | echo "Compressing new filesystem to prepare to push to Android /data/androdeb/" |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 120 | tar -zcf $TDIR/deb.tar.gz -C $TDIR debian |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 121 | |
| 122 | # Push tar to device and start unpack |
| 123 | adb shell mkdir -p /data/androdeb/ |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 124 | adb push $TDIR/deb.tar.gz /data/androdeb/ |
Joel Fernandes | 4dd6086 | 2018-03-31 13:27:16 -0700 | [diff] [blame] | 125 | adb push $spath/addons/device-* /data/androdeb/ |
| 126 | adb shell /data/androdeb/device-unpack |
Joel Fernandes | b61d27d | 2018-03-31 11:59:45 -0700 | [diff] [blame] | 127 | |
Joel Fernandes | e2a0466 | 2018-03-31 19:33:23 -0700 | [diff] [blame] | 128 | # 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] | 129 | |
| 130 | # Use --foreign and --variant=minbase to build minimal deb first stage |
| 131 | # Use fakeroot tar to build tar with root files |