on xenial help sign in
Rework run_on_device.sh to support non-root adb shell
f98678dMichał Sawicz
run_on_device.sh+45−52
@@ -4,8 +4,8 @@ USER=phablet
4USER_ID=32011
4USER_ID=32011
5PACKAGE=unity8
5PACKAGE=unity8
6BINARY=unity8
6BINARY=unity8
7−TARGET_IP=${TARGET_IP-127.0.0.1}
7+TARGET_IP=127.0.0.1
8−TARGET_SSH_PORT=${TARGET_SSH_PORT-2222}
8+TARGET_SSH_PORT=2222
9TARGET_DEBUG_PORT=3768
9TARGET_DEBUG_PORT=3768
10RUN_OPTIONS=-qmljsdebugger=port:$TARGET_DEBUG_PORT
10RUN_OPTIONS=-qmljsdebugger=port:$TARGET_DEBUG_PORT
11SETUP=false
11SETUP=false
@@ -14,17 +14,22 @@ PINLOCK=false
14KEYLOCK=false
14KEYLOCK=false
15NUM_JOBS='$(( `grep -c ^processor /proc/cpuinfo` + 1 ))'
15NUM_JOBS='$(( `grep -c ^processor /proc/cpuinfo` + 1 ))'
16FLIPPED=false
16FLIPPED=false
17−CHROOT_PREFIX="/data/ubuntu"
18SSH_WAS_STARTED=0
17SSH_WAS_STARTED=0
19SSH_STARTED=0
18SSH_STARTED=0
19+PASSWORD=""
20+PASSFILE=""
20
21
21usage() {
22usage() {
22− echo "usage: run_on_device [OPTIONS]\n"
23+ echo "usage: run_on_device -a SUDO_PASSWORD [OPTIONS]"
24+ echo
23− echo "Script to setup a build environment for the shell and sync build and run it on the device\n"
25+ echo "Script to setup a build environment for the shell and sync build and run it on the device"
26+ echo
24 echo "OPTIONS:"
27 echo "OPTIONS:"
28+ echo " -a The sudo password to use"
25− echo " -s, --setup Setup the build environment"
29+ echo " -s Setup the build environment"
30+ echo " -g Run with gdb"
26− echo " -p, --pinlock Enable a PIN lock screen when running"
31+ echo " -p Enable a PIN lock screen when running"
27− echo " -k, --keylock Enable a Keyboard lock screen when running"
32+ echo " -k Enable a Keyboard lock screen when running"
28 echo ""
33 echo ""
29 echo "IMPORTANT:"
34 echo "IMPORTANT:"
30 echo " * Make sure to have networking setup on the device beforehand."
35 echo " * Make sure to have networking setup on the device beforehand."
@@ -34,7 +39,7 @@ usage() {
34
39
35start_ssh() {
40start_ssh() {
36 if [ $SSH_STARTED -eq 0 ]; then
41 if [ $SSH_STARTED -eq 0 ]; then
37− exec_with_adb initctl start ssh
42+ adb_sudo initctl start ssh
38 SSH_STARTED=1
43 SSH_STARTED=1
39 fi
44 fi
40}
45}
@@ -44,25 +49,13 @@ exec_with_ssh() {
44 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -t $USER@$TARGET_IP -p $TARGET_SSH_PORT sudo -u $USER -i bash -ic \"$@\"
49 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -t $USER@$TARGET_IP -p $TARGET_SSH_PORT sudo -u $USER -i bash -ic \"$@\"
45}
50}
46
51
47−exec_with_adb() {
52+adb_sudo() {
48− if $FLIPPED; then
53+ adb shell "SUDO_ASKPASS=${PASSFILE} sudo --askpass $@"
49− adb shell $@
50− else
51− adb shell chroot /data/ubuntu /usr/bin/env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin "$@"
52− fi
53−}
54−
55−adb_root() {
56− adb root
57− adb wait-for-device
58}
54}
59
55
60install_ssh_key() {
56install_ssh_key() {
61− ssh-keygen -R $TARGET_IP
62− HOME_DIR="${CHROOT_PREFIX}/home/phablet"
57+ HOME_DIR="/home/phablet"
63 adb push ~/.ssh/id_rsa.pub $HOME_DIR/.ssh/authorized_keys
58 adb push ~/.ssh/id_rsa.pub $HOME_DIR/.ssh/authorized_keys
64− adb shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh
65− adb shell chown $USER_ID:$USER_ID $HOME_DIR/.ssh/authorized_keys
66 adb shell chmod 700 $HOME_DIR/.ssh
59 adb shell chmod 700 $HOME_DIR/.ssh
67 adb shell chmod 600 $HOME_DIR/.ssh/authorized_keys
60 adb shell chmod 600 $HOME_DIR/.ssh/authorized_keys
68}
61}
@@ -73,8 +66,8 @@ setup_adb_forwarding() {
73}
66}
74
67
75install_dependencies() {
68install_dependencies() {
76− exec_with_adb apt-get update
69+ adb_sudo apt-get update
77− exec_with_adb apt-get -y --force-yes install build-essential rsync bzr ccache gdb ninja-build devscripts equivs unity-plugin-scopes
70+ adb_sudo apt-get -y --force-yes install build-essential rsync bzr ccache gdb ninja-build devscripts equivs unity-plugin-scopes
78}
71}
79
72
80sync_code() {
73sync_code() {
@@ -108,37 +101,35 @@ run() {
108 exec_with_ssh "start unity8"
101 exec_with_ssh "start unity8"
109}
102}
110
103
111−set -- `getopt -n$0 -u -a --longoptions="setup,gdb,pinlock,keylock,help" "sgpkh" "$@"`
104+while getopts "a:sgpkh" OPTION; do
112−
113−# FIXME: giving incorrect arguments does not call usage and exit
114−while [ $# -gt 0 ]
115−do
116− case "$1" in
105+ case "$OPTION" in
106+ a|password) PASSWORD=$OPTARG;;
117− -s|--setup) SETUP=true;;
107+ s|setup) SETUP=true;;
118− -g|--gdb) GDB=true;;
108+ g|gdb) GDB=true;;
119− -p|--pinlock) PINLOCK=true;;
109+ p|pinlock) PINLOCK=true;;
120− -k|--keylock) KEYLOCK=true;;
110+ k|keylock) KEYLOCK=true;;
121− -h|--help) usage;;
111+ h|help) usage;;
122− --) shift;break;;
112+ ?) echo;usage;;
123 esac
113 esac
124− shift
125done
114done
126
115
116+shift $((OPTIND - 1))
127
117
128−adb_root
118+if [ -z "${PASSWORD}" ]; then
129−[ "${TARGET_IP}" = "127.0.0.1" ] && setup_adb_forwarding
119+ echo "ERROR: You need to provide a sudo password..."
130−
120+ echo
131−if [ "`adb shell 'grep -q Ubuntu /etc/lsb-release 2> /dev/null && echo -n FLIPPED'`" = "FLIPPED" ]; then
121+ usage
132− FLIPPED=true
133− CHROOT_PREFIX=""
134− if [ "${TARGET_IP}" != "127.0.0.1" ]; then
135− echo "ERROR: Flipped image detected, adb over TCP/IP isn't supported, yet :/"
136− echo "Unset TARGET_IP and try again."
137− exit 2
138− fi
139fi
122fi
140
123
124+PASSFILE=$( adb shell "FILE=\`mktemp\`
125+ echo \"#!/bin/sh\" > \$FILE
126+ echo \"echo \\\"${PASSWORD}\\\"\" >> \$FILE
127+ chmod +x \$FILE
128+ echo \$FILE" | tr -d '\r' )
129+
130+setup_adb_forwarding
131+
141−status_output=$(exec_with_adb initctl status ssh)
132+status_output=$(adb_sudo initctl status ssh)
142if [[ $status_output == "ssh start/running, process "* ]]; then
133if [[ $status_output == "ssh start/running, process "* ]]; then
143 SSH_WAS_STARTED=1
134 SSH_WAS_STARTED=1
144fi
135fi
@@ -158,6 +149,8 @@ else
158 run
149 run
159fi
150fi
160
151
152+adb shell "rm ${PASSFILE}"
153+
161if [ $SSH_WAS_STARTED -eq 0 ]; then
154if [ $SSH_WAS_STARTED -eq 0 ]; then
162− exec_with_adb initctl stop ssh
155+ adb_sudo initctl stop ssh
163fi
156fi