Jack Rosenthal | 9541b8c | 2019-07-26 10:45:55 -0600 | [diff] [blame] | 1 | #!/bin/bash |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 2 | # 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'Connor | a6db82e | 2010-01-27 12:11:08 -0800 | [diff] [blame] | 8 | DEFAULT_PRIVATE_KEY="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts/\ |
| 9 | ssh_keys/testing_rsa" |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 10 | |
| 11 | DEFINE_string remote "" "remote hostname/IP of running Chromium OS instance" |
| 12 | DEFINE_string private_key "$DEFAULT_PRIVATE_KEY" \ |
| 13 | "Private key of root account on remote host" |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 14 | DEFINE_integer ssh_port 0 \ |
Zelidrag Hornung | 61d9768 | 2010-06-15 11:55:21 -0700 | [diff] [blame] | 15 | "SSH port of the remote machine running Chromium OS instance" |
Gilad Arnold | 2ff2f11 | 2012-08-28 10:13:05 -0700 | [diff] [blame] | 16 | DEFINE_integer ssh_connect_timeout 30 \ |
| 17 | "SSH connect timeout in seconds" |
| 18 | DEFINE_integer ssh_connection_attempts 4 \ |
| 19 | "SSH connection attempts" |
Douglas Anderson | aaab1a3 | 2016-11-11 13:48:55 -0800 | [diff] [blame] | 20 | DEFINE_boolean ssh_allow_agent ${FLAGS_FALSE} "Don't block out SSH_AUTH_SOCK" |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 21 | |
Marc Herbert | 5d519fa | 2015-06-12 15:15:44 -0700 | [diff] [blame] | 22 | # Returns true if $1 has at least two colons. |
| 23 | has_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. |
| 31 | brackets_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 Arnold | 2ff2f11 | 2012-08-28 10:13:05 -0700 | [diff] [blame] | 39 | ssh_connect_settings() { |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 40 | local for_tool="$1" |
| 41 | |
Gilad Arnold | 2ff2f11 | 2012-08-28 10:13:05 -0700 | [diff] [blame] | 42 | if [[ -n "$SSH_CONNECT_SETTINGS" ]]; then |
| 43 | # If connection settings were fixed in an environment variable, just return |
| 44 | # those values. |
| 45 | echo -n "$SSH_CONNECT_SETTINGS" |
| 46 | else |
| 47 | # Otherwise, return the default (or user overridden) settings. |
| 48 | local settings=( |
| 49 | "Protocol=2" |
| 50 | "ConnectTimeout=${FLAGS_ssh_connect_timeout}" |
| 51 | "ConnectionAttempts=${FLAGS_ssh_connection_attempts}" |
| 52 | "ServerAliveInterval=10" |
| 53 | "ServerAliveCountMax=3" |
| 54 | "StrictHostKeyChecking=no" |
Dmitry Torokhov | ec13215 | 2016-05-13 11:22:59 -0700 | [diff] [blame] | 55 | "IdentitiesOnly=yes" |
| 56 | "IdentityFile=${TMP_PRIVATE_KEY}" |
| 57 | "UserKnownHostsFile=${TMP_KNOWN_HOSTS}" |
Douglas Anderson | d880765 | 2016-11-11 14:01:18 -0800 | [diff] [blame] | 58 | "ControlPath=${TMP_CONTROL_FILE}" |
| 59 | "ControlMaster=auto" |
| 60 | "ControlPersist=45" |
Gilad Arnold | 2ff2f11 | 2012-08-28 10:13:05 -0700 | [diff] [blame] | 61 | ) |
| 62 | printf -- '-o %s ' "${settings[@]}" |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 63 | |
| 64 | if [[ "${FLAGS_ssh_port}" -ne 0 ]]; then |
| 65 | if [[ "${for_tool}" == "scp" ]]; then |
| 66 | printf -- ' -P %d ' "${FLAGS_ssh_port}" |
| 67 | else |
| 68 | printf -- ' -p %d ' "${FLAGS_ssh_port}" |
| 69 | fi |
| 70 | fi |
Gilad Arnold | 2ff2f11 | 2012-08-28 10:13:05 -0700 | [diff] [blame] | 71 | fi |
| 72 | } |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 73 | |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 74 | # Copies $1 to $2 on remote host |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 75 | remote_cp_to() { |
Benson Leung | 57bf27f | 2018-01-14 19:16:58 -0800 | [diff] [blame] | 76 | local scp_rem |
| 77 | scp_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")" |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 78 | REMOTE_OUT=$(scp $(ssh_connect_settings scp) \ |
Benson Leung | 57bf27f | 2018-01-14 19:16:58 -0800 | [diff] [blame] | 79 | "$1" "root@${scp_rem}:$2") |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 80 | return ${PIPESTATUS[0]} |
| 81 | } |
| 82 | |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 83 | # Raw rsync access to the remote |
| 84 | # Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/ |
| 85 | remote_rsync_raw() { |
Jack Rosenthal | 9541b8c | 2019-07-26 10:45:55 -0600 | [diff] [blame] | 86 | local reason=0 |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 87 | rsync -e "ssh $(ssh_connect_settings ssh)" "$@" || reason=$? |
Jack Rosenthal | 9541b8c | 2019-07-26 10:45:55 -0600 | [diff] [blame] | 88 | case ${reason} in |
| 89 | 11 ) |
| 90 | # no space left on device, call handle_no_space if implemented |
| 91 | if command -v handle_no_space >/dev/null; then |
| 92 | handle_no_space |
| 93 | fi |
| 94 | ;; |
| 95 | * ) |
| 96 | ;; |
| 97 | esac |
| 98 | return ${reason} |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 101 | # Copies a list of remote files specified in file $1 to local location |
| 102 | # $2. Directory paths in $1 are collapsed into $2. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 103 | remote_rsync_from() { |
Marc Herbert | 5d519fa | 2015-06-12 15:15:44 -0700 | [diff] [blame] | 104 | local rsync_rem |
| 105 | rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")" |
| 106 | remote_rsync_raw --no-R --files-from="$1" \ |
| 107 | root@"${rsync_rem}:/" "$2" |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | # Send a directory from $1 to $2 on remote host |
| 111 | # |
| 112 | # Tries to use rsync -a but will fall back to tar if the remote doesn't |
| 113 | # have rsync. |
| 114 | # |
| 115 | # Use like: remote_send_to /build/board/lib/modules/ /lib/modules/ |
| 116 | remote_send_to() { |
Marc Herbert | 5d519fa | 2015-06-12 15:15:44 -0700 | [diff] [blame] | 117 | local rsync_rem |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 118 | if [ ! -d "$1" ]; then |
| 119 | die "$1 must be a directory" |
| 120 | fi |
| 121 | |
| 122 | if remote_sh rsync --version >/dev/null 2>&1; then |
Marc Herbert | 5d519fa | 2015-06-12 15:15:44 -0700 | [diff] [blame] | 123 | rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")" |
| 124 | remote_rsync_raw -a "$1/" root@"${rsync_rem}:$2/" |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 125 | else |
| 126 | tar -C "$1" -cz . | remote_sh tar -C "$2" -xz |
| 127 | fi |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 130 | _remote_sh() { |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 131 | REMOTE_OUT=$(ssh $(ssh_connect_settings ssh) \ |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 132 | root@$FLAGS_remote "$@") |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 133 | return ${PIPESTATUS[0]} |
| 134 | } |
| 135 | |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 136 | # Wrapper for ssh that runs the commmand given by the args on the remote host |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 137 | # If an ssh error occurs, re-runs the ssh command. |
Ian Coolidge | c3d5d91 | 2017-03-07 14:21:28 -0800 | [diff] [blame] | 138 | # Output is stored in REMOTE_OUT. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 139 | remote_sh() { |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 140 | local ssh_status=0 |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 141 | _remote_sh "$@" || ssh_status=$? |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 142 | # 255 indicates an ssh error. |
| 143 | if [ ${ssh_status} -eq 255 ]; then |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 144 | _remote_sh "$@" |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 145 | else |
| 146 | return ${ssh_status} |
| 147 | fi |
| 148 | } |
| 149 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 150 | remote_sh_raw() { |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 151 | ssh $(ssh_connect_settings ssh) \ |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 152 | $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@" |
Andrew de los Reyes | e08639b | 2011-09-21 15:44:05 -0700 | [diff] [blame] | 153 | return $? |
| 154 | } |
| 155 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 156 | remote_sh_allow_changed_host_key() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 157 | rm -f $TMP_KNOWN_HOSTS |
| 158 | remote_sh "$@" |
| 159 | } |
| 160 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 161 | set_up_remote_access() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 162 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 163 | chmod 0400 $TMP_PRIVATE_KEY |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 164 | |
| 165 | # Verify the client is reachable before continuing |
Gaurav Shah | af7d5d1 | 2011-09-21 16:42:16 -0700 | [diff] [blame] | 166 | local output |
| 167 | local status=0 |
Frank Henigman | d6b6cf6 | 2012-11-02 13:47:16 -0400 | [diff] [blame] | 168 | if output=$(remote_sh -n "true" 2>&1); then |
Gaurav Shah | af7d5d1 | 2011-09-21 16:42:16 -0700 | [diff] [blame] | 169 | : |
| 170 | else |
| 171 | status=$? |
| 172 | echo "Could not initiate first contact with remote host" |
| 173 | echo "$output" |
| 174 | fi |
| 175 | return $status |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 178 | # Ask the target what board it is |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 179 | learn_board() { |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 180 | [ -n "${FLAGS_board}" ] && return |
Frank Henigman | d6b6cf6 | 2012-11-02 13:47:16 -0400 | [diff] [blame] | 181 | remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 182 | FLAGS_board=$(echo "${REMOTE_OUT}" | cut -d '=' -f 2) |
| 183 | if [ -z "${FLAGS_board}" ]; then |
| 184 | error "Board required" |
| 185 | exit 1 |
| 186 | fi |
| 187 | info "Target reports board is ${FLAGS_board}" |
| 188 | } |
| 189 | |
Ian Coolidge | c3d5d91 | 2017-03-07 14:21:28 -0800 | [diff] [blame] | 190 | # Discover partition numbers from the target. |
| 191 | learn_partition_layout() { |
| 192 | source <(remote_sh_raw cat /usr/sbin/write_gpt.sh) |
| 193 | load_base_vars |
| 194 | } |
| 195 | |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 196 | # Checks whether a remote device has rebooted successfully. |
| 197 | # |
| 198 | # This uses a rapidly-retried SSH connection, which will wait for at most |
| 199 | # about ten seconds. If the network returns an error (e.g. host unreachable) |
| 200 | # the actual delay may be shorter. |
| 201 | # |
| 202 | # Return values: |
| 203 | # 0: The device has rebooted successfully |
| 204 | # 1: The device has not yet rebooted |
| 205 | # 255: Unable to communicate with the device |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 206 | _check_if_rebooted() { |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 207 | ( |
| 208 | # In my tests SSH seems to be waiting rather longer than would be expected |
| 209 | # from these parameters. These values produce a ~10 second wait. |
| 210 | # (in a subshell to avoid clobbering the global settings) |
| 211 | SSH_CONNECT_SETTINGS="$(sed \ |
| 212 | -e 's/\(ConnectTimeout\)=[0-9]*/\1=2/' \ |
| 213 | -e 's/\(ConnectionAttempts\)=[0-9]*/\1=2/' \ |
Douglas Anderson | edc2e00 | 2020-05-14 10:48:26 -0700 | [diff] [blame] | 214 | <<<"$(ssh_connect_settings ssh)")" |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 215 | remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]' |
| 216 | ) |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 217 | } |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 218 | |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 219 | # Triggers a reboot on a remote device and waits for it to complete. |
| 220 | # |
| 221 | # This function will not return until the SSH server on the remote device |
| 222 | # is available after the reboot. |
| 223 | # |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 224 | remote_reboot() { |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 225 | info "Rebooting ${FLAGS_remote}..." |
Andrey Ulanov | c68f488 | 2017-01-30 17:41:42 -0800 | [diff] [blame] | 226 | # 'reboot' is ran in background to make sure the command completes before |
| 227 | # sshd is terminated. |
| 228 | remote_sh_raw "touch /tmp/awaiting_reboot; reboot &" |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 229 | local start_time=${SECONDS} |
| 230 | |
| 231 | # Wait for five seconds before we start polling |
| 232 | sleep 5 |
| 233 | |
| 234 | # Add a hard timeout of 5 minutes before giving up. |
| 235 | local timeout=300 |
| 236 | local timeout_expiry=$(( start_time + timeout )) |
| 237 | while [ ${SECONDS} -lt ${timeout_expiry} ]; do |
| 238 | # Used to throttle the loop -- see step_remaining_time at the bottom. |
| 239 | local step_start_time=${SECONDS} |
| 240 | |
| 241 | local status=0 |
| 242 | _check_if_rebooted || status=$? |
| 243 | |
| 244 | local elapsed=$(( SECONDS - start_time )) |
| 245 | case ${status} in |
| 246 | 0) printf ' %4ds: reboot complete\n' ${elapsed} >&2 ; return 0 ;; |
| 247 | 1) printf ' %4ds: device has not yet shut down\n' ${elapsed} >&2 ;; |
| 248 | 255) printf ' %4ds: can not connect to device\n' ${elapsed} >&2 ;; |
| 249 | *) die " internal error" ;; |
| 250 | esac |
| 251 | |
| 252 | # To keep the loop from spinning too fast, delay until it has taken at |
| 253 | # least five seconds. When we are actively trying SSH connections this |
| 254 | # should never happen. |
| 255 | local step_remaining_time=$(( step_start_time + 5 - SECONDS )) |
| 256 | if [ ${step_remaining_time} -gt 0 ]; then |
| 257 | sleep ${step_remaining_time} |
| 258 | fi |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 259 | done |
Brian Norris | 3123fa1 | 2017-09-28 10:26:28 -0700 | [diff] [blame] | 260 | die_notrace "Reboot has not completed after ${timeout} seconds; giving up." |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 263 | # Called by clients before exiting. |
| 264 | # Part of the remote_access.sh interface but now empty. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 265 | cleanup_remote_access() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 266 | true |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 269 | remote_access_init() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 270 | TMP_PRIVATE_KEY=$TMP/private_key |
| 271 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
Brian Norris | b9bd9ab | 2020-07-21 11:34:47 -0700 | [diff] [blame] | 272 | TMP_CONTROL_FILE="${TMP}/ssh_control-%C" |
Douglas Anderson | d880765 | 2016-11-11 14:01:18 -0800 | [diff] [blame] | 273 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 274 | if [ -z "$FLAGS_remote" ]; then |
| 275 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 276 | exit 1 |
| 277 | fi |
Douglas Anderson | aaab1a3 | 2016-11-11 13:48:55 -0800 | [diff] [blame] | 278 | |
| 279 | # Having SSH_AUTH_SOCK set makes our ssh connections super slow so unset |
| 280 | # if it's not really needed. |
| 281 | if [[ ${FLAGS_ssh_allow_agent} -eq ${FLAGS_FALSE} ]]; then |
| 282 | unset SSH_AUTH_SOCK |
| 283 | fi |
| 284 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 285 | set_up_remote_access |
| 286 | } |