blob: 82d3f5107b2fa8d892258d967aad0bb4dbd50593 [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"
Kristian H. Kristensen8caf9882018-01-17 22:37:12 -080013DEFINE_integer ssh_port -1 \
Zelidrag Hornung61d97682010-06-15 11:55:21 -070014 "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() {
Kristian H. Kristensen8caf9882018-01-17 22:37:12 -080039 local port_option="$1"
40 if [[ ${FLAGS_ssh_port} != -1 ]]; then
41 printf -- '%s %s ' "${port_option}" "${FLAGS_ssh_port}"
42 fi
Gilad Arnold2ff2f112012-08-28 10:13:05 -070043 if [[ -n "$SSH_CONNECT_SETTINGS" ]]; then
44 # If connection settings were fixed in an environment variable, just return
45 # those values.
46 echo -n "$SSH_CONNECT_SETTINGS"
47 else
48 # Otherwise, return the default (or user overridden) settings.
49 local settings=(
50 "Protocol=2"
51 "ConnectTimeout=${FLAGS_ssh_connect_timeout}"
52 "ConnectionAttempts=${FLAGS_ssh_connection_attempts}"
53 "ServerAliveInterval=10"
54 "ServerAliveCountMax=3"
55 "StrictHostKeyChecking=no"
Dmitry Torokhovec132152016-05-13 11:22:59 -070056 "IdentitiesOnly=yes"
57 "IdentityFile=${TMP_PRIVATE_KEY}"
58 "UserKnownHostsFile=${TMP_KNOWN_HOSTS}"
Douglas Andersond8807652016-11-11 14:01:18 -080059 "ControlPath=${TMP_CONTROL_FILE}"
60 "ControlMaster=auto"
61 "ControlPersist=45"
Gilad Arnold2ff2f112012-08-28 10:13:05 -070062 )
63 printf -- '-o %s ' "${settings[@]}"
64 fi
65}
David Jamesf5850902011-09-30 10:51:48 -070066
Chris Sosaef964302010-04-27 13:21:08 -070067# Copies $1 to $2 on remote host
Mike Frysinger6b1abb22012-05-11 13:44:06 -040068remote_cp_to() {
Benson Leung57bf27f2018-01-14 19:16:58 -080069 local scp_rem
70 scp_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
Kristian H. Kristensen8caf9882018-01-17 22:37:12 -080071 REMOTE_OUT=$(scp $(ssh_connect_settings -P) \
Benson Leung57bf27f2018-01-14 19:16:58 -080072 "$1" "root@${scp_rem}:$2")
Chris Sosaef964302010-04-27 13:21:08 -070073 return ${PIPESTATUS[0]}
74}
75
Doug Anderson4e678382012-12-07 12:38:54 -080076# Raw rsync access to the remote
77# Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/
78remote_rsync_raw() {
Kristian H. Kristensen8caf9882018-01-17 22:37:12 -080079 rsync -e "ssh $(ssh_connect_settings -p)" "$@"
Doug Anderson4e678382012-12-07 12:38:54 -080080}
81
Ken Mixtercc4f1dd2010-08-31 12:07:11 -070082# Copies a list of remote files specified in file $1 to local location
83# $2. Directory paths in $1 are collapsed into $2.
Mike Frysinger6b1abb22012-05-11 13:44:06 -040084remote_rsync_from() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070085 local rsync_rem
86 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
87 remote_rsync_raw --no-R --files-from="$1" \
88 root@"${rsync_rem}:/" "$2"
Doug Anderson4e678382012-12-07 12:38:54 -080089}
90
91# Send a directory from $1 to $2 on remote host
92#
93# Tries to use rsync -a but will fall back to tar if the remote doesn't
94# have rsync.
95#
96# Use like: remote_send_to /build/board/lib/modules/ /lib/modules/
97remote_send_to() {
Marc Herbert5d519fa2015-06-12 15:15:44 -070098 local rsync_rem
Doug Anderson4e678382012-12-07 12:38:54 -080099 if [ ! -d "$1" ]; then
100 die "$1 must be a directory"
101 fi
102
103 if remote_sh rsync --version >/dev/null 2>&1; then
Marc Herbert5d519fa2015-06-12 15:15:44 -0700104 rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")"
105 remote_rsync_raw -a "$1/" root@"${rsync_rem}:$2/"
Doug Anderson4e678382012-12-07 12:38:54 -0800106 else
107 tar -C "$1" -cz . | remote_sh tar -C "$2" -xz
108 fi
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700109}
110
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400111_remote_sh() {
Kristian H. Kristensen8caf9882018-01-17 22:37:12 -0800112 REMOTE_OUT=$(ssh $(ssh_connect_settings -p) \
David Jamesf5850902011-09-30 10:51:48 -0700113 root@$FLAGS_remote "$@")
Ken Mixter689b9ee2010-01-07 18:23:52 -0800114 return ${PIPESTATUS[0]}
115}
116
Chris Sosafaeee5f2011-09-26 16:08:14 -0700117# Wrapper for ssh that runs the commmand given by the args on the remote host
Chris Sosa539b3412012-02-27 14:46:10 -0800118# If an ssh error occurs, re-runs the ssh command.
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800119# Output is stored in REMOTE_OUT.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400120remote_sh() {
Chris Sosafaeee5f2011-09-26 16:08:14 -0700121 local ssh_status=0
Chris Sosa539b3412012-02-27 14:46:10 -0800122 _remote_sh "$@" || ssh_status=$?
Chris Sosafaeee5f2011-09-26 16:08:14 -0700123 # 255 indicates an ssh error.
124 if [ ${ssh_status} -eq 255 ]; then
Chris Sosa539b3412012-02-27 14:46:10 -0800125 _remote_sh "$@"
Chris Sosafaeee5f2011-09-26 16:08:14 -0700126 else
127 return ${ssh_status}
128 fi
129}
130
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400131remote_sh_raw() {
Kristian H. Kristensen8caf9882018-01-17 22:37:12 -0800132 ssh $(ssh_connect_settings -p) \
David Jamesf5850902011-09-30 10:51:48 -0700133 $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@"
Andrew de los Reyese08639b2011-09-21 15:44:05 -0700134 return $?
135}
136
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400137remote_sh_allow_changed_host_key() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800138 rm -f $TMP_KNOWN_HOSTS
139 remote_sh "$@"
140}
141
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400142set_up_remote_access() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800143 cp $FLAGS_private_key $TMP_PRIVATE_KEY
144 chmod 0400 $TMP_PRIVATE_KEY
Ken Mixter689b9ee2010-01-07 18:23:52 -0800145
146 # Verify the client is reachable before continuing
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700147 local output
148 local status=0
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400149 if output=$(remote_sh -n "true" 2>&1); then
Gaurav Shahaf7d5d12011-09-21 16:42:16 -0700150 :
151 else
152 status=$?
153 echo "Could not initiate first contact with remote host"
154 echo "$output"
155 fi
156 return $status
Ken Mixter689b9ee2010-01-07 18:23:52 -0800157}
158
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700159# Ask the target what board it is
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400160learn_board() {
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700161 [ -n "${FLAGS_board}" ] && return
Frank Henigmand6b6cf62012-11-02 13:47:16 -0400162 remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release
Ken Mixtercc4f1dd2010-08-31 12:07:11 -0700163 FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2)
164 if [ -z "${FLAGS_board}" ]; then
165 error "Board required"
166 exit 1
167 fi
168 info "Target reports board is ${FLAGS_board}"
169}
170
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400171learn_arch() {
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800172 [ -n "${FLAGS_arch}" ] && return
173 remote_sh uname -m
Mandeep Singh Baines175422f2011-05-31 10:51:02 -0700174 FLAGS_arch=$(echo "${REMOTE_OUT}" | sed -e s/armv7l/arm/ -e s/i686/x86/ )
Olof Johanssonf53fa0d2011-01-26 13:06:46 -0800175 if [ -z "${FLAGS_arch}" ]; then
176 error "Arch required"
177 exit 1
178 fi
179 info "Target reports arch is ${FLAGS_arch}"
180}
181
Ian Coolidgec3d5d912017-03-07 14:21:28 -0800182# Discover partition numbers from the target.
183learn_partition_layout() {
184 source <(remote_sh_raw cat /usr/sbin/write_gpt.sh)
185 load_base_vars
186}
187
Chris Wolfed91df7a2012-02-29 16:55:48 -0500188# Checks whether a remote device has rebooted successfully.
189#
190# This uses a rapidly-retried SSH connection, which will wait for at most
191# about ten seconds. If the network returns an error (e.g. host unreachable)
192# the actual delay may be shorter.
193#
194# Return values:
195# 0: The device has rebooted successfully
196# 1: The device has not yet rebooted
197# 255: Unable to communicate with the device
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400198_check_if_rebooted() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500199 (
200 # In my tests SSH seems to be waiting rather longer than would be expected
201 # from these parameters. These values produce a ~10 second wait.
202 # (in a subshell to avoid clobbering the global settings)
203 SSH_CONNECT_SETTINGS="$(sed \
204 -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \
205 -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \
Gilad Arnold2ff2f112012-08-28 10:13:05 -0700206 <<<"$(ssh_connect_settings)")"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500207 remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]'
208 )
Chris Sosa24da49e2011-02-01 17:06:12 -0800209}
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800210
Chris Wolfed91df7a2012-02-29 16:55:48 -0500211# Triggers a reboot on a remote device and waits for it to complete.
212#
213# This function will not return until the SSH server on the remote device
214# is available after the reboot.
215#
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400216remote_reboot() {
Chris Wolfed91df7a2012-02-29 16:55:48 -0500217 info "Rebooting ${FLAGS_remote}..."
Andrey Ulanovc68f4882017-01-30 17:41:42 -0800218 # 'reboot' is ran in background to make sure the command completes before
219 # sshd is terminated.
220 remote_sh_raw "touch /tmp/awaiting_reboot; reboot &"
Chris Wolfed91df7a2012-02-29 16:55:48 -0500221 local start_time=${SECONDS}
222
223 # Wait for five seconds before we start polling
224 sleep 5
225
226 # Add a hard timeout of 5 minutes before giving up.
227 local timeout=300
228 local timeout_expiry=$(( start_time + timeout ))
229 while [ ${SECONDS} -lt ${timeout_expiry} ]; do
230 # Used to throttle the loop -- see step_remaining_time at the bottom.
231 local step_start_time=${SECONDS}
232
233 local status=0
234 _check_if_rebooted || status=$?
235
236 local elapsed=$(( SECONDS - start_time ))
237 case ${status} in
238 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;;
239 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;;
240 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;;
241 *) die " internal error" ;;
242 esac
243
244 # To keep the loop from spinning too fast, delay until it has taken at
245 # least five seconds. When we are actively trying SSH connections this
246 # should never happen.
247 local step_remaining_time=$(( step_start_time + 5 - SECONDS ))
248 if [ ${step_remaining_time} -gt 0 ]; then
249 sleep ${step_remaining_time}
250 fi
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800251 done
Brian Norris3123fa12017-09-28 10:26:28 -0700252 die_notrace "Reboot has not completed after ${timeout} seconds; giving up."
Mandeep Singh Bainesa63cd2d2010-12-02 11:58:26 -0800253}
254
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800255# Called by clients before exiting.
256# Part of the remote_access.sh interface but now empty.
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400257cleanup_remote_access() {
Mandeep Singh Bainesaef91ad2011-01-14 14:17:25 -0800258 true
Sean O'Connor9969ce92010-02-01 17:10:03 -0800259}
260
Mike Frysinger6b1abb22012-05-11 13:44:06 -0400261remote_access_init() {
Ken Mixter689b9ee2010-01-07 18:23:52 -0800262 TMP_PRIVATE_KEY=$TMP/private_key
263 TMP_KNOWN_HOSTS=$TMP/known_hosts
Douglas Andersond8807652016-11-11 14:01:18 -0800264 TMP_CONTROL_FILE="${TMP}/ssh_control%r@%h:%p"
265
Ken Mixter689b9ee2010-01-07 18:23:52 -0800266 if [ -z "$FLAGS_remote" ]; then
267 echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance"
268 exit 1
269 fi
Douglas Andersonaaab1a32016-11-11 13:48:55 -0800270
271 # Having SSH_AUTH_SOCK set makes our ssh connections super slow so unset
272 # if it's not really needed.
273 if [[ ${FLAGS_ssh_allow_agent} -eq ${FLAGS_FALSE} ]]; then
274 unset SSH_AUTH_SOCK
275 fi
276
Ken Mixter689b9ee2010-01-07 18:23:52 -0800277 set_up_remote_access
278}