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