blob: 7e68940a5afb9eb31790b99289ab4e5a7001a454 [file] [log] [blame]
Ken Mixter689b9ee2010-01-07 18:23:52 -08001# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5# Library for setting up remote access and running remote commands.
6
Sean O'Connora6db82e2010-01-27 12:11:08 -08007DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\
8ssh_keys/testing_rsa"
Ken Mixter689b9ee2010-01-07 18:23:52 -08009
10DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance"
11DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \
12 "Private key of root account on remote host"
Zelidrag Hornung61d97682010-06-15 11:55:21 -070013DEFINE_integer ssh_port 22 \
14 "SSH port of the remote machine running Chromium OS instance"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070015DEFINE_integer ssh_connect_timeout 30 \
16 "SSH connect timeout in seconds"
17DEFINE_integer ssh_connection_attempts 4 \
18 "SSH connection attempts"
Douglas Andersonaaab1a32016-11-11 13:48:55 -080019DEFINE_boolean ssh_allow_agent ${FLAGS_FALSE} "Don't block out SSH_AUTH_SOCK"
Ken Mixter689b9ee2010-01-07 18:23:52 -080020
Marc Herbert5d519fa2015-06-12 15:15:44 -070021# Returns true if $1 has at least two colons.
22has_two_colons_or_more() {
23 # IPv6 addresses have at least two colons while IPv4 addresses and
24 # hostnames have none.
25 [[ "$1" == *:*:* ]]
26}
27
28# Prints $1 enclosed with brackets if it looks like an IPv6 address
29# and unchanged otherwise.
30brackets_enclosed_if_ipv6() {
31 local rem="$1"
32 if has_two_colons_or_more "${rem}"; then
33 rem="[${rem}]"
34 fi
35 echo "${rem}"
36}
37
Gilad Arnold2ff2f112012-08-28 10:13:05 -070038ssh_connect_settings() {
39 if [[ -n "$SSH_CONNECT_SETTINGS" ]]; then
40 # If connection settings were fixed in an environment variable, just return
41 # those values.
42 echo -n "$SSH_CONNECT_SETTINGS"
43 else
44 # Otherwise, return the default (or user overridden) settings.
45 local settings=(
46 "Protocol=2"
47 "ConnectTimeout=${FLAGS_ssh_connect_timeout}"
48 "ConnectionAttempts=${FLAGS_ssh_connection_attempts}"
49 "ServerAliveInterval=10"
50 "ServerAliveCountMax=3"
51 "StrictHostKeyChecking=no"
Dmitry Torokhovec132152016-05-13 11:22:59 -070052 "IdentitiesOnly=yes"
53 "IdentityFile=${TMP_PRIVATE_KEY}"
54 "UserKnownHostsFile=${TMP_KNOWN_HOSTS}"
Douglas Andersond8807652016-11-11 14:01:18 -080055 "ControlPath=${TMP_CONTROL_FILE}"
56 "ControlMaster=auto"
57 "ControlPersist=45"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070058 )
59 printf -- '-o %s ' "${settings[@]}"
60 fi
61}
David Jamesf5850902011-09-30 10:51:48 -070062
Chris Sosaef964302010-04-27 13:21:08 -070063# Copies $1 to $2 on remote host
Mike Frysinger6b1abb22012-05-11 13:44:06 -040064remote_cp_to() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -070065 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $(ssh_connect_settings) \
Dmitry Torokhovec132152016-05-13 11:22:59 -070066 $1 root@$FLAGS_remote:$2)
Chris Sosaef964302010-04-27 13:21:08 -070067 return ${PIPESTATUS[0]}
68}
69
Doug Anderson4e678382012-12-07 12:38:54 -080070# Raw rsync access to the remote
71# Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/
72remote_rsync_raw() {
Dmitry Torokhovec132152016-05-13 11:22:59 -070073 rsync -e "ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings)" "$@"
Doug Anderson4e678382012-12-07 12:38:54 -080074}
75
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070076# Copies a list of remote files specified in file $1 to local location
77# $2. Directory paths in $1 are collapsed into $2.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040078remote_rsync_from() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070079 local rsync_rem
80 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
81 remote_rsync_raw --no-R --files-from="$1" \
82 root@"${rsync_rem}:/" "$2"
Doug Anderson4e678382012-12-07 12:38:54 -080083}
84
85# Send a directory from $1 to $2 on remote host
86#
87# Tries to use rsync -a but will fall back to tar if the remote doesn't
88# have rsync.
89#
90# Use like: remote_send_to /build/board/lib/modules/ /lib/modules/
91remote_send_to() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070092 local rsync_rem
Doug Anderson4e678382012-12-07 12:38:54 -080093 if [ ! -d "$1" ]; then
94 die "$1 must be a directory"
95 fi
96
97 if remote_sh rsync --version >/dev/null 2>&1; then
Marc Herbert5d519fa2015-06-12 15:15:44 -070098 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
99 remote_rsync_raw -a "$1/" root@"${rsync_rem}:$2/"
Doug Anderson4e678382012-12-07 12:38:54 -0800100 else
101 tar -C "$1" -cz . | remote_sh tar -C "$2" -xz
102 fi
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700103}
104
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400105_remote_sh() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700106 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700107 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -0800108 return ${PIPESTATUS[0]}
109}
110
Chris Sosafaeee5f2011-09-26 16:08:14 -0700111# Wrapper for ssh that runs the commmand given by the args on the remote host
Chris Sosa539b3412012-02-27 14:46:10 -0800112# If an ssh error occurs, re-runs the ssh command.
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800113# Output is stored in REMOTE_OUT.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400114remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -0700115 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -0800116 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -0700117 # 255 indicates an ssh error.
118 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -0800119 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -0700120 else
121 return ${ssh_status}
122 fi
123}
124
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400125remote_sh_raw() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700126 ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700127 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -0700128 return $?
129}
130
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400131remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800132 rm -f $TMP_KNOWN_HOSTS
133 remote_sh "$@"
134}
135
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400136set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800137 cp $FLAGS_private_key $TMP_PRIVATE_KEY
138 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -0800139
140 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700141 local output
142 local status=0
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400143 if output=$(remote_sh -n "true" 2>&1); then
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700144 :
145 else
146 status=$?
147 echo "Could not initiate first contact with remote host"
148 echo "$output"
149 fi
150 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -0800151}
152
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700153# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400154learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700155 [ -n "${FLAGS_board}" ] && return
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400156 remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700157 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
158 if [ -z "${FLAGS_board}" ]; then
159 error "Board required"
160 exit 1
161 fi
162 info "Target reports board is ${FLAGS_board}"
163}
164
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400165learn_arch() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800166 [ -n "${FLAGS_arch}" ] && return
167 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -0700168 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800169 if [ -z "${FLAGS_arch}" ]; then
170 error "Arch required"
171 exit 1
172 fi
173 info "Target reports arch is ${FLAGS_arch}"
174}
175
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800176# Discover partition numbers from the target.
177learn_partition_layout() {
178 source <(remote_sh_raw cat /usr/sbin/write_gpt.sh)
179 load_base_vars
180}
181
Chris Wolfed91df7a2012-02-29 16:55:48 -0500182# Checks whether a remote device has rebooted successfully.
183#
184# This uses a rapidly-retried SSH connection, which will wait for at most
185# about ten seconds. If the network returns an error (e.g. host unreachable)
186# the actual delay may be shorter.
187#
188# Return values:
189# 0: The device has rebooted successfully
190# 1: The device has not yet rebooted
191# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400192_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500193 (
194 # In my tests SSH seems to be waiting rather longer than would be expected
195 # from these parameters. These values produce a ~10 second wait.
196 # (in a subshell to avoid clobbering the global settings)
197 SSH_CONNECT_SETTINGS="$(sed \
198 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
199 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700200 <<<"$(ssh_connect_settings)")"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500201 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
202 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800203}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800204
Chris Wolfed91df7a2012-02-29 16:55:48 -0500205# Triggers a reboot on a remote device and waits for it to complete.
206#
207# This function will not return until the SSH server on the remote device
208# is available after the reboot.
209#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400210remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500211 info "Rebooting ${FLAGS_remote}..."
Andrey Ulanovc68f4882017-01-30 17:41:42 -0800212 # 'reboot' is ran in background to make sure the command completes before
213 # sshd is terminated.
214 remote_sh_raw "touch /tmp/awaiting_reboot; reboot &"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500215 local start_time=${SECONDS}
216
217 # Wait for five seconds before we start polling
218 sleep 5
219
220 # Add a hard timeout of 5 minutes before giving up.
221 local timeout=300
222 local timeout_expiry=$(( start_time + timeout ))
223 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
224 # Used to throttle the loop -- see step_remaining_time at the bottom.
225 local step_start_time=${SECONDS}
226
227 local status=0
228 _check_if_rebooted || status=$?
229
230 local elapsed=$(( SECONDS - start_time ))
231 case ${status} in
232 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
233 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
234 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
235 *) die " internal error" ;;
236 esac
237
238 # To keep the loop from spinning too fast, delay until it has taken at
239 # least five seconds. When we are actively trying SSH connections this
240 # should never happen.
241 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
242 if [ ${step_remaining_time} -gt 0 ]; then
243 sleep ${step_remaining_time}
244 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800245 done
Brian Norris3123fa12017-09-28 10:26:28 -0700246 die_notrace "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800247}
248
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800249# Called by clients before exiting.
250# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400251cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800252 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800253}
254
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400255remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800256 TMP_PRIVATE_KEY=$TMP/private_key
257 TMP_KNOWN_HOSTS=$TMP/known_hosts
Douglas Andersond8807652016-11-11 14:01:18 -0800258 TMP_CONTROL_FILE="${TMP}/ssh_control%r@%h:%p"
259
Ken Mixter689b9ee2010-01-07 18:23:52 -0800260 if [ -z "$FLAGS_remote" ]; then
261 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
262 exit 1
263 fi
Douglas Andersonaaab1a32016-11-11 13:48:55 -0800264
265 # Having SSH_AUTH_SOCK set makes our ssh connections super slow so unset
266 # if it's not really needed.
267 if [[ ${FLAGS_ssh_allow_agent} -eq ${FLAGS_FALSE} ]]; then
268 unset SSH_AUTH_SOCK
269 fi
270
Ken Mixter689b9ee2010-01-07 18:23:52 -0800271 set_up_remote_access
272}