blob: d2190d9d0e4c2db9e96409f11f2433ae03cd8a8d [file] [log] [blame]
#!/bin/bash -e
#
# (c) Joel Fernandes <joel@joelfernandes.org>
VERSION=v0.99
spath="$(dirname "$(readlink -f "$0")")"
# spath=$( cd "$(dirname "$0")" ; pwd -P )
curdir=$( pwd -P )
source $spath/utils/android
source $spath/utils/banners
# Set default vars
DISTRO=buster; ARCH=arm64
ADB="adb"
# Default packages
DEFAULT_PACKAGES+="bash
ca-certificates"
EXTRA_FILES="none"
config_full_build() {
for f in $(ls $spath/packages); do source $spath/packages/$f; done;
}
# 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; ;;
remove) REMOVE=1; shift || true; ;;
pull) PULL=1; shift || true; ;;
--arch) ARCH=$2; shift || true; 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) config_full_build; shift || true; ;;
--download) DOWNLOAD=1; shift || true; ;;
--bcc) source $spath/packages/bcc; shift || true; ;;
--kernelsrc) KERNELSRC="$2"; shift || true; shift || true; ;;
--kernel-headers-targz) KERNELHDRS=$2; shift || true; shift || true; ;;
--tempdir) TDIR="$2"; shift || true; shift || true; ;;
--buildtar) TARDIR="$2"; shift || true; shift || true; ;;
--device|-s) ADB="$ADB -s $2"; shift || true; shift || true; ;;
--build-image) BUILD_IMAGE=$2; shift || true; shift || true; ;;
--debug) set -x; shift || true; ;;
*) echo "Unknown option ($1)"; usage; ;;
esac
done
if [ ! -z "$PULL" ]; then
echo "Updating androdeb by git pull"
cd $spath
git pull
echo "Done."
exit 0
fi
if [ -z "$ASHELL" ] && [ -z "$PACKAGES" ]; then
echo "No packages specified, so I'm going to build/install all packages (--fullbuild)"
config_full_build
fi
if [[ ! -z ${TARDIR+x} ]] && [[ ! -d $TARDIR ]]; then die 7 "Tar dir specified doesn't exist"; fi
if [ -z "$BUILD_IMAGE" ]; then
do_adb_root "$ADB" || die 3 "adb root failed, make sure:
- If multiple devices connected, provide --device <serialno> (or -s <serialno>)
- Try to run \"adb root\" manually and see if it works. Typically this needs a userdebug build.
Note: adb can be typically obtained using the android-tools-adb or the adb
packages on your distro, or by installing the Android SDK."
fi
if [ ! -z "$REMOVE" ]; then
die_if_no_androdeb "Nothing to remove."
$ADB shell /data/androdeb/device-umount-all;
$ADB shell rm -rf /data/androdeb; exit 0; fi
##########################################################
# 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
##########################################################
function do_cleanup() {
rm -rf $TDIR/*; if [ $MKTEMP -eq 1 ]; then rm -rf $TDIR; fi
}
function push_unpack_headers() {
die_if_no_androdeb "Couldn't update headers."
$ADB shell rm -rf /data/androdeb/debian/kernel-headers/
$ADB shell mkdir /data/androdeb/debian/kernel-headers/
$ADB push $TDIR_ABS/kh.tgz /data/androdeb/
echo "Storing kernel headers into androdeb /kernel-headers/"
$ADB shell tar -xvf /data/androdeb/kh.tgz -C /data/androdeb/debian/kernel-headers/ > /dev/null
$ADB shell rm /data/androdeb/kh.tgz
}
function push_unpack_tarred_headers() {
die_if_no_androdeb "Couldn't update headers."
$ADB shell rm -rf /data/androdeb/debian/kernel-headers/
$ADB shell mkdir /data/androdeb/debian/kernel-headers/
$ADB push $1 /data/androdeb/
echo "Storing kernel headers into androdeb root directory"
$ADB shell tar -xvf /data/androdeb/$(basename $1) -C /data/androdeb/debian/ > /dev/null
$ADB shell rm /data/androdeb/$(basename $1)
}
function all_done_banner() {
echo "All done! Run \"androdeb shell\" to enter environment"
}
# Prepare is the last command checked
if [ -z "$PREPARE" ]; then usage; fi
if [ ! -z "$TARF" ] && [ ! -f $TARF ] && [ -z "$DOWNLOAD" ]; then die 5 "archive provided doesn't exist"; fi
if [ ! -z "$KERNELSRC" ] && [ ! -d $KERNELSRC ]; then die 6 "Kernel source directory provided doesn't exist"; fi
if [ ! -z "$KERNELHDRS" ] && [ ! -f $KERNELHDRS ]; then die 7 "Kernel headers tar.gz 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
OUT_TMP=$TDIR/debian; rm -rf $OUT_TMP; mkdir -p $OUT_TMP
# Unpack the supplied kernel headers tar.gz directly into androdeb root
if [ ! -z "$KERNELHDRS" ]; then
echo "Building updating kernel headers from supplied tar.gz ($KERNELHDRS)"
# Is header tar gz update the only thing left to do?
if [[ -z "$PACKAGES" ]] && [[ -z "$TARF" ]] && [[ -z "$BUILD_IMAGE" ]] && [[ -z "$KERNELSRC" ]] ; then
push_unpack_tarred_headers $KERNELHDRS; do_cleanup; all_done_banner; exit 0; fi
tar -xvf $KERNELHDRS -C $OUT_TMP/ > /dev/null
fi
# Package kernel headers
if [ ! -z "$KERNELSRC" ]; then
echo "Building and updating kernel headers from kernel source dir ($KERNELSRC)"
$spath/bcc/build-kheaders-targz.sh ${KERNELSRC} $TDIR_ABS/kh.tgz > /dev/null
# Is header update the only thing left to do?
if [[ -z "$PACKAGES" ]] && [[ -z "$BUILD_IMAGE" ]] && [[ -z "$TARF" ]]; then
push_unpack_headers; do_cleanup; all_done_banner; exit 0; fi
mkdir $OUT_TMP/kernel-headers
tar -xvf $TDIR_ABS/kh.tgz -C $OUT_TMP/kernel-headers/ > /dev/null
fi
# Build FS from existing tar, very simple.
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
if [ ! -z "$KERNELHDRS" ]; then push_unpack_tarred_headers $KERNELHDRS; fi
if [ ! -z "$KERNELSRC" ]; then push_unpack_headers; fi
do_cleanup; all_done_banner; exit 0
fi
PACKAGES+="$DEFAULT_PACKAGES"
echo "Using temporary directory: $TDIR"
if [[ $EUID -ne 0 ]]; then echo "The next stage runs as sudo, please enter password if asked."; fi
SKIP_COMPRESS=0; if [ ! -z "$BUILD_IMAGE" ]; then SKIP_COMPRESS=1; fi
ex_files=$(mktemp); echo $EXTRA_FILES > $ex_files
sudo $spath/buildstrap $ARCH $DISTRO $TDIR $OUT_TMP \
"$(make_csv "$PACKAGES")"\
$ex_files $INSTALL_BCC $SKIP_COMPRESS
rm $ex_files
# If we only wanted to prepare a rootfs and don't have
# a device connected, then just echo that and skip cleanup
if [ ! -z "$BUILD_IMAGE" ]; then
echo "For --build-image, only the image is built"
echo "Note that BCC will not be built on the image, you've to do it yourself"
sudo $spath/buildimage $OUT_TMP $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
sudo chmod a+rw $(dirname $BUILD_IMAGE)/$(basename $BUILD_IMAGE)
do_cleanup; echo "Your .img has been built! Enjoy!"; exit 0
fi
# 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
do_cleanup
# 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
all_done_banner