Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 283 lines (239 sloc) 7.51 KB
#!/bin/sh
#
# Copyright (c) 2014 Canonical
#
# Author: Oliver Grawert <ogra@canonical.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
#
set -e
TARPATH=$1
SYSIMG=$2
check_prereq()
{
if [ ! $SYSTEM_ADB ]; then
# POSIX shell-only replacement for 'dirname'
a="/$0"; a=${a%/*}; a=${a:-.}; a=${a#/}/; BINDIR=$(cd $a; pwd)
unset a
PATH="$BINDIR:$PATH"
fi
if [ ! $(which make_ext4fs) ] || [ ! -x $(which simg2img) ] || \
[ ! -x $(which adb) ]; then
echo "please install the android-tools-fsutils and adb packages" && exit 1
fi
}
do_shell()
{
adb shell "$@"
}
convert_android_img()
{
if file $SYSIMG | grep -v ": Linux rev 1.0 ext4" >/dev/null; then
echo "Converting sparse image to contiguious image"
simg2img $SYSIMG $WORKDIR/system.img.raw
mkdir $TMPMOUNT
mount -t ext4 -o loop $WORKDIR/system.img.raw $TMPMOUNT
IMAGE_SIZE=`wc -c < $SYSIMG`
make_ext4fs -l $IMAGE_SIZE $WORKDIR/system.img $TMPMOUNT
SYSIMAGE=$WORKDIR/system.img
else
SYSIMAGE=$SYSIMG
fi
}
send_busybox() {
adb push "$BUSYBOX_PUSH" "/data/"
BUSYBOX="/data/`basename $BUSYBOX_PUSH`"
}
check_mounts(){
MOUNTS=$(do_shell "cat /proc/mounts")
if ! echo $MOUNTS | grep -qs '/data'; then
do_shell "mount /data"
fi
}
prepare_ubuntu_system()
{
do_shell "rm -f /data/ubuntu.img"
for data in system android; do
do_shell "rm -rf /data/$data-data"
done
if [ -z "$KEEP" ]; then
do_shell "rm -rf /data/user-data"
else
echo -n "keep option set, keeping user data ... "
fi
do_shell "dd if=/dev/zero of=/data/ubuntu.img seek=511K bs=4096 count=0 2>&1"
do_shell "mkfs.ext2 -F /data/ubuntu.img 2>&1"
do_shell "mkdir -p /data/recovery/system"
do_shell "mount -t ext4 -o loop /data/ubuntu.img /data/recovery/system/"
# system.img is the deprecated name, but we make link to ubuntu.img
do_shell "ln /data/ubuntu.img /data/system.img"
}
cleanup()
{
echo "Cleaning up... "
mount | grep -q $TMPMOUNT 2>/dev/null && umount $TMPMOUNT
cleanup_device
rm -rf $WORKDIR
echo
}
cleanup_device()
{
[ -e $WORKDIR/device-clean ] && return
do_shell "umount /data/recovery/system/ && rm -rf /data/recovery/system 2>/dev/null"
do_shell "rm -f /data/$TARBALL"
if [ ! -z "$BUSYBOX_PUSH" ]; then
do_shell "rm $BUSYBOX 2>/dev/null"
fi
do_shell "sync"
[ -e $WORKDIR ] && touch $WORKDIR/device-clean 2>/dev/null || true
}
trap cleanup 0 1 2 3 9 15
usage()
{
echo "usage: $(basename $0) <path to rootfs tarball> <path to android system.img> [options]
options:
-h|--help this message
-c|--custom path to customization tarball (needs to be tar.xz)
-k|--keep-userdata do not wipe user data on device
-w|--wipe-file absolute path of a file inside the image to wipe (empty)
-b|--busybox path to custom busybox (or toybox) to push/use on device
-a|--system-adb use the installed adb binary instead of the one included"
exit 1
}
SUDOARGS="$@"
# Holds the path to the busybox binary on the phone we will run commands with. The default one is in the recovery user's PATH, so we'll just leave it as is.
BUSYBOX="busybox"
# The busybox that we're going to push to the device
BUSYBOX_PUSH=""
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
usage
;;
-c|--custom)
[ -n "$2" ] && CUST_TARPATH=$2 shift || usage
;;
-k|--keep-userdata)
KEEP=1
;;
-w|--wipe-file)
[ -n "$2" ] && WIPE_PATH=$2 shift || usage
;;
-b|--busybox)
[ -n "$2" ] && BUSYBOX_PUSH=$2 shift || usage
;;
-a|--system-adb)
SYSTEM_ADB=true
esac
shift
done
if [ -z "$TARPATH" ]; then
echo "need valid rootfs tarball path"
usage
fi
TARBALL=$(basename $TARPATH)
if [ -z "$TARBALL" ]; then
echo "need valid rootfs tarball path"
usage
fi
TARTYPE=$(file --mime-type $TARPATH|sed 's/^.* //')
case ${TARTYPE#application\/} in
gzip|x-gzip)
;;
*)
echo "Need valid rootfs tarball gzip type"
usage
;;
esac
if [ -z "$SYSIMG" ] || \
[ "$(file --mime-type $SYSIMG|sed 's/^.* //')" != "application/octet-stream" ]; then
echo "need valid system.img path and type application/octet-stream"
usage
fi
if [ ! -z "$CUST_TARPATH" ] && \
[ "$(file --mime-type $CUST_TARPATH|sed 's/^.* //')" != "application/x-xz" ]; then
echo "Custom tarball needs to be valid path and type .tar.xz"
usage
fi
[ $(id -u) -ne 0 ] && exec sudo $0 $SUDOARGS
check_prereq
if ! adb devices | grep -q recovery; then
echo "please make sure the device is attched via USB in recovery mode"
exit 1
fi
check_mounts
WORKDIR=$(mktemp -d /tmp/rootstock-touch-install.XXXXX)
TMPMOUNT="$WORKDIR/tmpmount"
if [ ! -z "$BUSYBOX_PUSH" ]; then
echo -n "Pushing Busybox to device ... "
send_busybox
echo "[done]"
fi
echo -n "transfering rootfs tarball ... "
adb push $TARPATH /data/ 2>&1
echo "[done]"
if [ ! -z "$CUST_TARPATH" ]; then
CUST_TARBALL=$(basename $CUST_TARPATH)
echo -n "transferring custom tarball"
adb push $CUST_TARPATH /data/ >/dev/null 2>&1
echo "[done]"
fi
echo -n "preparing system-image on device ... "
prepare_ubuntu_system
echo "[done]"
echo -n "unpacking rootfs tarball to system-image ... "
do_shell "cd /data/recovery/system && $BUSYBOX tar xf /data/$TARBALL"
do_shell "mkdir -p /data/recovery/system/android/firmware"
do_shell "mkdir -p /data/recovery/system/android/persist"
do_shell "mkdir -p /data/recovery/system/userdata"
do_shell "[ -e /data/recovery/system/SWAP.swap ] && mv /data/recovery/system/SWAP.swap /data/SWAP.img || exit 0"
for link in cache data factory firmware persist system; do
# We really don't care if this fails, so || true it is!
do_shell "cd /data/recovery/system && ln -s /android/$link $link" || true
done || true
do_shell "cd /data/recovery/system/lib && ln -s /system/lib/modules modules"
do_shell "cd /data/recovery/system && ln -s /android/system/vendor vendor"
do_shell "[ -e /data/recovery/system/etc/mtab ] && rm /data/recovery/system/etc/mtab || true"
do_shell "cd /data/recovery/system/etc && ln -s /proc/mounts mtab"
if [ ! -z "$WIPE_PATH" ]; then
do_shell "echo ' ' >/data/recovery/system/$WIPE_PATH || true"
fi
echo "[done]"
if [ ! -z "$CUST_TARPATH" ]; then
echo -n "unpacking custom tarball"
do_shell "mkdir -p /data/recovery/system/custom"
do_shell "cd /data/recovery && $BUSYBOX xzcat /data/$CUST_TARBALL | $BUSYBOX tar xf -"
echo "[done]"
fi
echo -n "adding android system image to installation ... "
convert_android_img
ANDROID_DIR="/data/recovery/system/var/lib/lxc/android/"
adb push $SYSIMAGE $ANDROID_DIR 2>&1
echo "[done]"
echo -n "enabling Mir ... "
do_shell "touch /data/recovery/system/home/phablet/.display-mir"
echo "[done]"
echo -n "enabling SSH ... "
do_shell "sed -i 's/PasswordAuthentication=no/PasswordAuthentication=yes/g' /data/recovery/system/etc/init/ssh.override"
do_shell "sed -i 's/manual/start on startup/g' /data/recovery/system/etc/init/ssh.override"
do_shell "$BUSYBOX chroot /data/recovery/system/ /bin/bash -c '/usr/bin/passwd phablet'"
echo "[done]"
echo -n "cleaning up on device ... "
cleanup_device
echo "[done]"
echo "rebooting device"
adb reboot
echo "Installation is complete."