blob: c00232a27b804a077340e92c03008b9321809083 [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() {
Benson Leung57bf27f2018-01-14 19:16:58 -080065 local scp_rem
66 scp_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070067 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $(ssh_connect_settings) \
Benson Leung57bf27f2018-01-14 19:16:58 -080068 "$1" "root@${scp_rem}:$2")
Chris Sosaef964302010-04-27 13:21:08 -070069 return ${PIPESTATUS[0]}
70}
71
Doug Anderson4e678382012-12-07 12:38:54 -080072# Raw rsync access to the remote
73# Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/
74remote_rsync_raw() {
Dmitry Torokhovec132152016-05-13 11:22:59 -070075 rsync -e "ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings)" "$@"
Doug Anderson4e678382012-12-07 12:38:54 -080076}
77
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070078# Copies a list of remote files specified in file $1 to local location
79# $2. Directory paths in $1 are collapsed into $2.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040080remote_rsync_from() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070081 local rsync_rem
82 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
83 remote_rsync_raw --no-R --files-from="$1" \
84 root@"${rsync_rem}:/" "$2"
Doug Anderson4e678382012-12-07 12:38:54 -080085}
86
87# Send a directory from $1 to $2 on remote host
88#
89# Tries to use rsync -a but will fall back to tar if the remote doesn't
90# have rsync.
91#
92# Use like: remote_send_to /build/board/lib/modules/ /lib/modules/
93remote_send_to() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070094 local rsync_rem
Doug Anderson4e678382012-12-07 12:38:54 -080095 if [ ! -d "$1" ]; then
96 die "$1 must be a directory"
97 fi
98
99 if remote_sh rsync --version >/dev/null 2>&1; then
Marc Herbert5d519fa2015-06-12 15:15:44 -0700100 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
101 remote_rsync_raw -a "$1/" root@"${rsync_rem}:$2/"
Doug Anderson4e678382012-12-07 12:38:54 -0800102 else
103 tar -C "$1" -cz . | remote_sh tar -C "$2" -xz
104 fi
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700105}
106
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400107_remote_sh() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700108 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700109 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -0800110 return ${PIPESTATUS[0]}
111}
112
Chris Sosafaeee5f2011-09-26 16:08:14 -0700113# Wrapper for ssh that runs the commmand given by the args on the remote host
Chris Sosa539b3412012-02-27 14:46:10 -0800114# If an ssh error occurs, re-runs the ssh command.
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800115# Output is stored in REMOTE_OUT.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400116remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -0700117 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -0800118 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -0700119 # 255 indicates an ssh error.
120 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -0800121 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -0700122 else
123 return ${ssh_status}
124 fi
125}
126
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400127remote_sh_raw() {
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700128 ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700129 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -0700130 return $?
131}
132
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400133remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800134 rm -f $TMP_KNOWN_HOSTS
135 remote_sh "$@"
136}
137
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400138set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800139 cp $FLAGS_private_key $TMP_PRIVATE_KEY
140 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -0800141
142 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700143 local output
144 local status=0
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400145 if output=$(remote_sh -n "true" 2>&1); then
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700146 :
147 else
148 status=$?
149 echo "Could not initiate first contact with remote host"
150 echo "$output"
151 fi
152 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -0800153}
154
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700155# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400156learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700157 [ -n "${FLAGS_board}" ] && return
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400158 remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700159 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
160 if [ -z "${FLAGS_board}" ]; then
161 error "Board required"
162 exit 1
163 fi
164 info "Target reports board is ${FLAGS_board}"
165}
166
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400167learn_arch() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800168 [ -n "${FLAGS_arch}" ] && return
169 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -0700170 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800171 if [ -z "${FLAGS_arch}" ]; then
172 error "Arch required"
173 exit 1
174 fi
175 info "Target reports arch is ${FLAGS_arch}"
176}
177
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800178# Discover partition numbers from the target.
179learn_partition_layout() {
180 source <(remote_sh_raw cat /usr/sbin/write_gpt.sh)
181 load_base_vars
182}
183
Chris Wolfed91df7a2012-02-29 16:55:48 -0500184# Checks whether a remote device has rebooted successfully.
185#
186# This uses a rapidly-retried SSH connection, which will wait for at most
187# about ten seconds. If the network returns an error (e.g. host unreachable)
188# the actual delay may be shorter.
189#
190# Return values:
191# 0: The device has rebooted successfully
192# 1: The device has not yet rebooted
193# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400194_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500195 (
196 # In my tests SSH seems to be waiting rather longer than would be expected
197 # from these parameters. These values produce a ~10 second wait.
198 # (in a subshell to avoid clobbering the global settings)
199 SSH_CONNECT_SETTINGS="$(sed \
200 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
201 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700202 <<<"$(ssh_connect_settings)")"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500203 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
204 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800205}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800206
Chris Wolfed91df7a2012-02-29 16:55:48 -0500207# Triggers a reboot on a remote device and waits for it to complete.
208#
209# This function will not return until the SSH server on the remote device
210# is available after the reboot.
211#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400212remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500213 info "Rebooting ${FLAGS_remote}..."
Andrey Ulanovc68f4882017-01-30 17:41:42 -0800214 # 'reboot' is ran in background to make sure the command completes before
215 # sshd is terminated.
216 remote_sh_raw "touch /tmp/awaiting_reboot; reboot &"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500217 local start_time=${SECONDS}
218
219 # Wait for five seconds before we start polling
220 sleep 5
221
222 # Add a hard timeout of 5 minutes before giving up.
223 local timeout=300
224 local timeout_expiry=$(( start_time + timeout ))
225 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
226 # Used to throttle the loop -- see step_remaining_time at the bottom.
227 local step_start_time=${SECONDS}
228
229 local status=0
230 _check_if_rebooted || status=$?
231
232 local elapsed=$(( SECONDS - start_time ))
233 case ${status} in
234 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
235 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
236 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
237 *) die " internal error" ;;
238 esac
239
240 # To keep the loop from spinning too fast, delay until it has taken at
241 # least five seconds. When we are actively trying SSH connections this
242 # should never happen.
243 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
244 if [ ${step_remaining_time} -gt 0 ]; then
245 sleep ${step_remaining_time}
246 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800247 done
Brian Norris3123fa12017-09-28 10:26:28 -0700248 die_notrace "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800249}
250
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800251# Called by clients before exiting.
252# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400253cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800254 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800255}
256
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400257remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800258 TMP_PRIVATE_KEY=$TMP/private_key
259 TMP_KNOWN_HOSTS=$TMP/known_hosts
Douglas Andersond8807652016-11-11 14:01:18 -0800260 TMP_CONTROL_FILE="${TMP}/ssh_control%r@%h:%p"
261
Ken Mixter689b9ee2010-01-07 18:23:52 -0800262 if [ -z "$FLAGS_remote" ]; then
263 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
264 exit 1
265 fi
Douglas Andersonaaab1a32016-11-11 13:48:55 -0800266
267 # Having SSH_AUTH_SOCK set makes our ssh connections super slow so unset
268 # if it's not really needed.
269 if [[ ${FLAGS_ssh_allow_agent} -eq ${FLAGS_FALSE} ]]; then
270 unset SSH_AUTH_SOCK
271 fi
272
Ken Mixter689b9ee2010-01-07 18:23:52 -0800273 set_up_remote_access
274}