blob: 4e9f54858ee817b5b98bc574b5fe584789a35e52 [file] [log] [blame]
Jack Rosenthal9541b8c2019-07-26 10:45:55 -06001#!/bin/bash
Ken Mixter689b9ee2010-01-07 18:23:52 -08002# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# Library for setting up remote access and running remote commands.
7
Sean O'Connora6db82e2010-01-27 12:11:08 -08008DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\
9ssh_keys/testing_rsa"
Ken Mixter689b9ee2010-01-07 18:23:52 -080010
11DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance"
12DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \
13 "Private key of root account on remote host"
Qiang(Joe) Xu678435c2018-01-26 02:46:40 +000014DEFINE_integer ssh_port 22 \
Zelidrag Hornung61d97682010-06-15 11:55:21 -070015 "SSH port of the remote machine running Chromium OS instance"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070016DEFINE_integer ssh_connect_timeout 30 \
17 "SSH connect timeout in seconds"
18DEFINE_integer ssh_connection_attempts 4 \
19 "SSH connection attempts"
Douglas Andersonaaab1a32016-11-11 13:48:55 -080020DEFINE_boolean ssh_allow_agent ${FLAGS_FALSE} "Don't block out SSH_AUTH_SOCK"
Ken Mixter689b9ee2010-01-07 18:23:52 -080021
Marc Herbert5d519fa2015-06-12 15:15:44 -070022# Returns true if $1 has at least two colons.
23has_two_colons_or_more() {
24 # IPv6 addresses have at least two colons while IPv4 addresses and
25 # hostnames have none.
26 [[ "$1" == *:*:* ]]
27}
28
29# Prints $1 enclosed with brackets if it looks like an IPv6 address
30# and unchanged otherwise.
31brackets_enclosed_if_ipv6() {
32 local rem="$1"
33 if has_two_colons_or_more "${rem}"; then
34 rem="[${rem}]"
35 fi
36 echo "${rem}"
37}
38
Gilad Arnold2ff2f112012-08-28 10:13:05 -070039ssh_connect_settings() {
40 if [[ -n "$SSH_CONNECT_SETTINGS" ]]; then
41 # If connection settings were fixed in an environment variable, just return
42 # those values.
43 echo -n "$SSH_CONNECT_SETTINGS"
44 else
45 # Otherwise, return the default (or user overridden) settings.
46 local settings=(
47 "Protocol=2"
48 "ConnectTimeout=${FLAGS_ssh_connect_timeout}"
49 "ConnectionAttempts=${FLAGS_ssh_connection_attempts}"
50 "ServerAliveInterval=10"
51 "ServerAliveCountMax=3"
52 "StrictHostKeyChecking=no"
Dmitry Torokhovec132152016-05-13 11:22:59 -070053 "IdentitiesOnly=yes"
54 "IdentityFile=${TMP_PRIVATE_KEY}"
55 "UserKnownHostsFile=${TMP_KNOWN_HOSTS}"
Douglas Andersond8807652016-11-11 14:01:18 -080056 "ControlPath=${TMP_CONTROL_FILE}"
57 "ControlMaster=auto"
58 "ControlPersist=45"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070059 )
60 printf -- '-o %s ' "${settings[@]}"
61 fi
62}
David Jamesf5850902011-09-30 10:51:48 -070063
Chris Sosaef964302010-04-27 13:21:08 -070064# Copies $1 to $2 on remote host
Mike Frysinger6b1abb22012-05-11 13:44:06 -040065remote_cp_to() {
Benson Leung57bf27f2018-01-14 19:16:58 -080066 local scp_rem
67 scp_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
Qiang(Joe) Xu678435c2018-01-26 02:46:40 +000068 REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $(ssh_connect_settings) \
Benson Leung57bf27f2018-01-14 19:16:58 -080069 "$1" "root@${scp_rem}:$2")
Chris Sosaef964302010-04-27 13:21:08 -070070 return ${PIPESTATUS[0]}
71}
72
Doug Anderson4e678382012-12-07 12:38:54 -080073# Raw rsync access to the remote
74# Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/
75remote_rsync_raw() {
Jack Rosenthal9541b8c2019-07-26 10:45:55 -060076 local reason=0
77 rsync -e "ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings)" "$@" || reason=$?
78 case ${reason} in
79 11 )
80 # no space left on device, call handle_no_space if implemented
81 if command -v handle_no_space >/dev/null; then
82 handle_no_space
83 fi
84 ;;
85 * )
86 ;;
87 esac
88 return ${reason}
Doug Anderson4e678382012-12-07 12:38:54 -080089}
90
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070091# Copies a list of remote files specified in file $1 to local location
92# $2. Directory paths in $1 are collapsed into $2.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040093remote_rsync_from() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070094 local rsync_rem
95 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
96 remote_rsync_raw --no-R --files-from="$1" \
97 root@"${rsync_rem}:/" "$2"
Doug Anderson4e678382012-12-07 12:38:54 -080098}
99
100# Send a directory from $1 to $2 on remote host
101#
102# Tries to use rsync -a but will fall back to tar if the remote doesn't
103# have rsync.
104#
105# Use like: remote_send_to /build/board/lib/modules/ /lib/modules/
106remote_send_to() {
Marc Herbert5d519fa2015-06-12 15:15:44 -0700107 local rsync_rem
Doug Anderson4e678382012-12-07 12:38:54 -0800108 if [ ! -d "$1" ]; then
109 die "$1 must be a directory"
110 fi
111
112 if remote_sh rsync --version >/dev/null 2>&1; then
Marc Herbert5d519fa2015-06-12 15:15:44 -0700113 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
114 remote_rsync_raw -a "$1/" root@"${rsync_rem}:$2/"
Doug Anderson4e678382012-12-07 12:38:54 -0800115 else
116 tar -C "$1" -cz . | remote_sh tar -C "$2" -xz
117 fi
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700118}
119
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400120_remote_sh() {
Qiang(Joe) Xu678435c2018-01-26 02:46:40 +0000121 REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700122 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -0800123 return ${PIPESTATUS[0]}
124}
125
Chris Sosafaeee5f2011-09-26 16:08:14 -0700126# Wrapper for ssh that runs the commmand given by the args on the remote host
Chris Sosa539b3412012-02-27 14:46:10 -0800127# If an ssh error occurs, re-runs the ssh command.
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800128# Output is stored in REMOTE_OUT.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400129remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -0700130 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -0800131 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -0700132 # 255 indicates an ssh error.
133 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -0800134 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -0700135 else
136 return ${ssh_status}
137 fi
138}
139
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400140remote_sh_raw() {
Qiang(Joe) Xu678435c2018-01-26 02:46:40 +0000141 ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \
David Jamesf5850902011-09-30 10:51:48 -0700142 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -0700143 return $?
144}
145
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400146remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800147 rm -f $TMP_KNOWN_HOSTS
148 remote_sh "$@"
149}
150
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400151set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800152 cp $FLAGS_private_key $TMP_PRIVATE_KEY
153 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -0800154
155 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700156 local output
157 local status=0
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400158 if output=$(remote_sh -n "true" 2>&1); then
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700159 :
160 else
161 status=$?
162 echo "Could not initiate first contact with remote host"
163 echo "$output"
164 fi
165 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -0800166}
167
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700168# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400169learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700170 [ -n "${FLAGS_board}" ] && return
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400171 remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700172 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
173 if [ -z "${FLAGS_board}" ]; then
174 error "Board required"
175 exit 1
176 fi
177 info "Target reports board is ${FLAGS_board}"
178}
179
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800180# Discover partition numbers from the target.
181learn_partition_layout() {
182 source <(remote_sh_raw cat /usr/sbin/write_gpt.sh)
183 load_base_vars
184}
185
Chris Wolfed91df7a2012-02-29 16:55:48 -0500186# Checks whether a remote device has rebooted successfully.
187#
188# This uses a rapidly-retried SSH connection, which will wait for at most
189# about ten seconds. If the network returns an error (e.g. host unreachable)
190# the actual delay may be shorter.
191#
192# Return values:
193# 0: The device has rebooted successfully
194# 1: The device has not yet rebooted
195# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400196_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500197 (
198 # In my tests SSH seems to be waiting rather longer than would be expected
199 # from these parameters. These values produce a ~10 second wait.
200 # (in a subshell to avoid clobbering the global settings)
201 SSH_CONNECT_SETTINGS="$(sed \
202 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
203 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700204 <<<"$(ssh_connect_settings)")"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500205 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
206 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800207}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800208
Chris Wolfed91df7a2012-02-29 16:55:48 -0500209# Triggers a reboot on a remote device and waits for it to complete.
210#
211# This function will not return until the SSH server on the remote device
212# is available after the reboot.
213#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400214remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500215 info "Rebooting ${FLAGS_remote}..."
Andrey Ulanovc68f4882017-01-30 17:41:42 -0800216 # 'reboot' is ran in background to make sure the command completes before
217 # sshd is terminated.
218 remote_sh_raw "touch /tmp/awaiting_reboot; reboot &"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500219 local start_time=${SECONDS}
220
221 # Wait for five seconds before we start polling
222 sleep 5
223
224 # Add a hard timeout of 5 minutes before giving up.
225 local timeout=300
226 local timeout_expiry=$(( start_time + timeout ))
227 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
228 # Used to throttle the loop -- see step_remaining_time at the bottom.
229 local step_start_time=${SECONDS}
230
231 local status=0
232 _check_if_rebooted || status=$?
233
234 local elapsed=$(( SECONDS - start_time ))
235 case ${status} in
236 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
237 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
238 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
239 *) die " internal error" ;;
240 esac
241
242 # To keep the loop from spinning too fast, delay until it has taken at
243 # least five seconds. When we are actively trying SSH connections this
244 # should never happen.
245 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
246 if [ ${step_remaining_time} -gt 0 ]; then
247 sleep ${step_remaining_time}
248 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800249 done
Brian Norris3123fa12017-09-28 10:26:28 -0700250 die_notrace "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800251}
252
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800253# Called by clients before exiting.
254# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400255cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800256 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800257}
258
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400259remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800260 TMP_PRIVATE_KEY=$TMP/private_key
261 TMP_KNOWN_HOSTS=$TMP/known_hosts
Douglas Andersond8807652016-11-11 14:01:18 -0800262 TMP_CONTROL_FILE="${TMP}/ssh_control%r@%h:%p"
263
Ken Mixter689b9ee2010-01-07 18:23:52 -0800264 if [ -z "$FLAGS_remote" ]; then
265 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
266 exit 1
267 fi
Douglas Andersonaaab1a32016-11-11 13:48:55 -0800268
269 # Having SSH_AUTH_SOCK set makes our ssh connections super slow so unset
270 # if it's not really needed.
271 if [[ ${FLAGS_ssh_allow_agent} -eq ${FLAGS_FALSE} ]]; then
272 unset SSH_AUTH_SOCK
273 fi
274
Ken Mixter689b9ee2010-01-07 18:23:52 -0800275 set_up_remote_access
276}