blob: ce8d02a8302491afc80936c804cb0b077ea2d4c9 [file] [log] [blame]
#!/bin/bash -e
VERSION=v0.9
spath=$( cd "$(dirname "$0")" ; pwd -P )
curdir=$( pwd -P )
source $spath/utils/android
source $spath/utils/banners
# Set default vars
DISTRO=buster; PACKAGES=""; ARCH=arm64
# Parse command line parameters
if [ $# -lt 1 ]; then usage; fi; POSITIONAL=()
while [[ $# -gt 0 ]]; do key="$1";
case $key in
prepare) PREPARE=1; shift || true; ;;
shell) ASHELL=1; shift || true; ;;
--archive) TARF=$2; shift || true; shift || true; ;;
--tracers) source $spath/packages/tracers; shift || true; ;;
--compilers) source $spath/packages/compilers; shift || true; ;;
--editors) source $spath/packages/editors; shift || true; ;;
--scheduler) source $spath/packages/scheduler; shift || true; ;;
--fullbuild) for f in $(ls $spath/packages|grep -v bcc); do source packages/$f; done; shift || true; ;;
--download) DOWNLOAD=1; shift || true; ;;
--bcc) source $spath/packages/bcc; INSTALL_BCC=1; shift || true; ;;
--kernelsrc) KERNELSRC="$2"; shift || true; shift || true; ;;
--tempdir) TDIR="$2"; shift || true; shift || true; ;;
--buildtar) TARDIR="$2"; shift || true; shift || true; ;;
--debug) set -x; shift || true; ;;
*) echo "Unknown option ($1)"; usage; ;;
esac
done
if [ ! -z "$PREPARE" ] && [ -z "$DOWNLOAD" ] && [ -z "$TARF" ] && [ "x$PACKAGES" == "x" ]; then
echo "Need to specifify something to prepare, or try: ./andrdeb prepare --download"; usage; fi
if [[ ! -z ${TARDIR+x} ]] && [[ ! -d $TARDIR ]]; then die 7 "Tar dir specified doesn't exist"; fi
# Default packages
PACKAGES+="\
bash
ca-certificates
"
do_adb_root || die 3 "adb root failed, make sure:
- device is connected, and there's only one device (TODO: add multi device support)
- device is userdebug."
##########################################################
# SHELL
##########################################################
if [ ! -z ${ASHELL+x} ]; then
set +e; adb shell ls /data/androdeb/debian/.bashrc > /dev/null 2>&1
if [ $? -ne 0 ]; then
die 2 "Device doesn't have an androdeb environment, run \"./androdeb prepare\" first";
fi; set -e
adb shell -t /data/androdeb/run
exit 0
fi
##########################################################
# PREPARE
##########################################################
# Prepare is the last command checked
if [ -z "$PREPARE" ]; then usage; fi
if [[ $EUID -ne 0 ]]; then die 6 "For prepare, this tool must run as root. Try: ./sudo androdeb prepare <args>"; fi
if [ ! -z "$TARF" ] && [ -z "$DOWNLOAD" ]; then
if [ ! -f ${TARF} ]; then die 7 "archive provided doesn't exist"; fi
fi
if [[ ! -z ${INSTALL_BCC+x} ]] && [[ -z ${KERNELSRC+x} ]]; then die 4 "--kernelsrc must be provided with --bcc"; fi
if [[ ! -z ${KERNELSRC+x} ]] && [[ ! -d ${KERNELSRC} ]]; then die 5 "Kernel source directory provided doesn't exist"; fi
print_prepare_banner
# Where do we want to store temporary files
MKTEMP=0; if [[ -z ${TDIR+x} ]] || [[ ! -d "${TDIR}" ]]; then
TDIR=`mktemp -d`; MKTEMP=1; fi
rm -rf $TDIR/*
TDIR_ABS=$( cd "$TDIR" ; pwd -P )
if [ ! -z "$DOWNLOAD" ]; then
echo "Downloading Androdeb from the web..."; echo ""
curl -L https://github.com/joelagnel/androdeb/releases/download/$VERSION/androdeb-fs.tgz --output $TDIR_ABS/androdeb-fs.tgz;
TARF=$TDIR_ABS/androdeb-fs.tgz; fi
# Package kernel headers
if [[ ! -z ${INSTALL_BCC} ]] && [[ ! -z ${KERNELSRC} ]]; then
$spath/bcc/build-kheaders-targz.sh ${KERNELSRC} $TDIR_ABS/kh.tgz || die 6 "Failed to package kernel headers"; fi
OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP
if [ -f $TDIR_ABS/kh.tgz ]; then
mkdir $OUT_TMP/kernel-headers
tar -xvf $TDIR_ABS/kh.tgz -C $OUT_TMP/kernel-headers/
fi
if [ ! -z "$TARF" ]; then
echo "Using archive at $TARF for filesystem preparation"
adb shell mkdir -p /data/androdeb/
adb push $TARF /data/androdeb/deb.tar.gz
adb push $spath/addons/* /data/androdeb/
adb shell /data/androdeb/device-unpack
else
echo "Full package list: $PACKAGES"
echo "Using temporary directory: $TDIR"
time qemu-debootstrap --arch arm64 --include=$(make_csv "$PACKAGES") \
$DISTRO $OUT_TMP http://deb.debian.org/debian/
# Some reason debootstrap leaves these mounted
umount $OUT_TMP/proc/sys/fs/binfmt_misc || true
umount $OUT_TMP/proc || true
# Make bash the default shell
chroot $OUT_TMP rm /bin/sh || true
chroot $OUT_TMP ln -s /bin/bash /bin/sh || true
cp $spath/addons/bashrc $OUT_TMP/.bashrc
# For mounting android partitions
mkdir $OUT_TMP/system
mkdir $OUT_TMP/vendor
# Cleanup
rm -rf $OUT_TMP/lib/udev/*
rm -rf $OUT_TMP/var/lib/apt/lists/*
rm -rf $OUT_TMP/var/cache/apt/archives/*deb
rm -rf $OUT_TMP/usr/share/locale/*
rm -rf $OUT_TMP/usr/lib/share/locale/*
rm -rf $OUT_TMP/usr/share/doc/*
rm -rf $OUT_TMP/usr/lib/share/doc/*
rm -rf $OUT_TMP/usr/share/ieee-data/*
rm -rf $OUT_TMP/usr/lib/share/ieee-data/*
rm -rf $OUT_TMP/usr/share/man/*
rm -rf $OUT_TMP/usr/lib/share/man/*
# Clone BCC if needed
if [[ ! -z ${INSTALL_BCC+x} ]]; then
git clone https://github.com/iovisor/bcc.git $TDIR/debian/bcc-master
cp $spath/bcc/build-bcc.sh $TDIR/debian/bcc-master/; fi
echo "Compressing new filesystem to prepare to push to Android /data/androdeb/"
tar -zcf $TDIR/deb.tar.gz -C $TDIR debian
# Push tar to device and start unpack
adb shell mkdir -p /data/androdeb/
adb push $TDIR/deb.tar.gz /data/androdeb/
adb push $spath/addons/* /data/androdeb/
adb shell /data/androdeb/device-unpack
# Build BCC and install bcc on device if needed
if [[ ! -z ${INSTALL_BCC+x} ]]; then
adb shell /data/androdeb/run-command /bcc-master/build-bcc.sh; fi
fi # !TARF
rm -rf $TDIR/*; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi
# Extract a tar of the built, compiled and installed androdeb env
if [[ ! -z ${TARDIR+x} ]]; then
echo "Creating and pulling tarball of androdeb env from device"
adb shell /data/androdeb/build-debian-tar
adb pull /data/androdeb/androdeb-fs.tgz $TARDIR/
adb shell rm /data/androdeb/androdeb-fs.tgz; fi
# Use --foreign and --variant=minbase to build minimal deb first stage
# Use fakeroot tar to build tar with root files