blob: f6c7fa60e69d4145bb46ffe378eaa92021739bb6 [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.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400113remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -0700114 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -0800115 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -0700116 # 255 indicates an ssh error.
117 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -0800118 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -0700119 else
120 return ${ssh_status}
121 fi
122}
123
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400124remote_sh_raw() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700125 ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700126 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -0700127 return $?
128}
129
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400130remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800131 rm -f $TMP_KNOWN_HOSTS
132 remote_sh "$@"
133}
134
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400135set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800136 cp $FLAGS_private_key $TMP_PRIVATE_KEY
137 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -0800138
139 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700140 local output
141 local status=0
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400142 if output=$(remote_sh -n "true" 2>&1); then
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700143 :
144 else
145 status=$?
146 echo "Could not initiate first contact with remote host"
147 echo "$output"
148 fi
149 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -0800150}
151
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700152# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400153learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700154 [ -n "${FLAGS_board}" ] && return
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400155 remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700156 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
157 if [ -z "${FLAGS_board}" ]; then
158 error "Board required"
159 exit 1
160 fi
161 info "Target reports board is ${FLAGS_board}"
162}
163
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400164learn_arch() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800165 [ -n "${FLAGS_arch}" ] && return
166 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -0700167 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800168 if [ -z "${FLAGS_arch}" ]; then
169 error "Arch required"
170 exit 1
171 fi
172 info "Target reports arch is ${FLAGS_arch}"
173}
174
Chris Wolfed91df7a2012-02-29 16:55:48 -0500175# Checks whether a remote device has rebooted successfully.
176#
177# This uses a rapidly-retried SSH connection, which will wait for at most
178# about ten seconds. If the network returns an error (e.g. host unreachable)
179# the actual delay may be shorter.
180#
181# Return values:
182# 0: The device has rebooted successfully
183# 1: The device has not yet rebooted
184# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400185_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500186 (
187 # In my tests SSH seems to be waiting rather longer than would be expected
188 # from these parameters. These values produce a ~10 second wait.
189 # (in a subshell to avoid clobbering the global settings)
190 SSH_CONNECT_SETTINGS="$(sed \
191 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
192 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700193 <<<"$(ssh_connect_settings)")"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500194 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
195 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800196}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800197
Chris Wolfed91df7a2012-02-29 16:55:48 -0500198# Triggers a reboot on a remote device and waits for it to complete.
199#
200# This function will not return until the SSH server on the remote device
201# is available after the reboot.
202#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400203remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500204 info "Rebooting ${FLAGS_remote}..."
Andrey Ulanovc68f4882017-01-30 17:41:42 -0800205 # 'reboot' is ran in background to make sure the command completes before
206 # sshd is terminated.
207 remote_sh_raw "touch /tmp/awaiting_reboot; reboot &"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500208 local start_time=${SECONDS}
209
210 # Wait for five seconds before we start polling
211 sleep 5
212
213 # Add a hard timeout of 5 minutes before giving up.
214 local timeout=300
215 local timeout_expiry=$(( start_time + timeout ))
216 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
217 # Used to throttle the loop -- see step_remaining_time at the bottom.
218 local step_start_time=${SECONDS}
219
220 local status=0
221 _check_if_rebooted || status=$?
222
223 local elapsed=$(( SECONDS - start_time ))
224 case ${status} in
225 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
226 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
227 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
228 *) die " internal error" ;;
229 esac
230
231 # To keep the loop from spinning too fast, delay until it has taken at
232 # least five seconds. When we are actively trying SSH connections this
233 # should never happen.
234 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
235 if [ ${step_remaining_time} -gt 0 ]; then
236 sleep ${step_remaining_time}
237 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800238 done
Chris Wolfed91df7a2012-02-29 16:55:48 -0500239 die "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800240}
241
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800242# Called by clients before exiting.
243# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400244cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800245 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800246}
247
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400248remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800249 TMP_PRIVATE_KEY=$TMP/private_key
250 TMP_KNOWN_HOSTS=$TMP/known_hosts
Douglas Andersond8807652016-11-11 14:01:18 -0800251 TMP_CONTROL_FILE="${TMP}/ssh_control%r@%h:%p"
252
Ken Mixter689b9ee2010-01-07 18:23:52 -0800253 if [ -z "$FLAGS_remote" ]; then
254 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
255 exit 1
256 fi
Douglas Andersonaaab1a32016-11-11 13:48:55 -0800257
258 # Having SSH_AUTH_SOCK set makes our ssh connections super slow so unset
259 # if it's not really needed.
260 if [[ ${FLAGS_ssh_allow_agent} -eq ${FLAGS_FALSE} ]]; then
261 unset SSH_AUTH_SOCK
262 fi
263
Ken Mixter689b9ee2010-01-07 18:23:52 -0800264 set_up_remote_access
265}