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" |
Qiang(Joe) Xu | 678435c | 2018-01-26 02:46:40 +0000 | [diff] [blame] | 14 | DEFINE_integer ssh_port 22 \ |
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() { |
| 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 Torokhov | ec13215 | 2016-05-13 11:22:59 -0700 | [diff] [blame] | 53 | "IdentitiesOnly=yes" |
| 54 | "IdentityFile=${TMP_PRIVATE_KEY}" |
| 55 | "UserKnownHostsFile=${TMP_KNOWN_HOSTS}" |
Douglas Anderson | d880765 | 2016-11-11 14:01:18 -0800 | [diff] [blame] | 56 | "ControlPath=${TMP_CONTROL_FILE}" |
| 57 | "ControlMaster=auto" |
| 58 | "ControlPersist=45" |
Gilad Arnold | 2ff2f11 | 2012-08-28 10:13:05 -0700 | [diff] [blame] | 59 | ) |
| 60 | printf -- '-o %s ' "${settings[@]}" |
| 61 | fi |
| 62 | } |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 63 | |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 64 | # Copies $1 to $2 on remote host |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 65 | remote_cp_to() { |
Benson Leung | 57bf27f | 2018-01-14 19:16:58 -0800 | [diff] [blame] | 66 | local scp_rem |
| 67 | scp_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")" |
Qiang(Joe) Xu | 678435c | 2018-01-26 02:46:40 +0000 | [diff] [blame] | 68 | REMOTE_OUT=$(scp -P ${FLAGS_ssh_port} $(ssh_connect_settings) \ |
Benson Leung | 57bf27f | 2018-01-14 19:16:58 -0800 | [diff] [blame] | 69 | "$1" "root@${scp_rem}:$2") |
Chris Sosa | ef96430 | 2010-04-27 13:21:08 -0700 | [diff] [blame] | 70 | return ${PIPESTATUS[0]} |
| 71 | } |
| 72 | |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 73 | # Raw rsync access to the remote |
| 74 | # Use like: remote_rsync_raw -a /path/from/ root@${FLAGS_remote}:/path/to/ |
| 75 | remote_rsync_raw() { |
Jack Rosenthal | 9541b8c | 2019-07-26 10:45:55 -0600 | [diff] [blame] | 76 | 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 Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 91 | # Copies a list of remote files specified in file $1 to local location |
| 92 | # $2. Directory paths in $1 are collapsed into $2. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 93 | remote_rsync_from() { |
Marc Herbert | 5d519fa | 2015-06-12 15:15:44 -0700 | [diff] [blame] | 94 | 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 Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 98 | } |
| 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/ |
| 106 | remote_send_to() { |
Marc Herbert | 5d519fa | 2015-06-12 15:15:44 -0700 | [diff] [blame] | 107 | local rsync_rem |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 108 | 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 Herbert | 5d519fa | 2015-06-12 15:15:44 -0700 | [diff] [blame] | 113 | rsync_rem="$(brackets_enclosed_if_ipv6 "${FLAGS_remote}")" |
| 114 | remote_rsync_raw -a "$1/" root@"${rsync_rem}:$2/" |
Doug Anderson | 4e67838 | 2012-12-07 12:38:54 -0800 | [diff] [blame] | 115 | else |
| 116 | tar -C "$1" -cz . | remote_sh tar -C "$2" -xz |
| 117 | fi |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 120 | _remote_sh() { |
Qiang(Joe) Xu | 678435c | 2018-01-26 02:46:40 +0000 | [diff] [blame] | 121 | REMOTE_OUT=$(ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \ |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 122 | root@$FLAGS_remote "$@") |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 123 | return ${PIPESTATUS[0]} |
| 124 | } |
| 125 | |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 126 | # 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] | 127 | # If an ssh error occurs, re-runs the ssh command. |
Ian Coolidge | c3d5d91 | 2017-03-07 14:21:28 -0800 | [diff] [blame] | 128 | # Output is stored in REMOTE_OUT. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 129 | remote_sh() { |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 130 | local ssh_status=0 |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 131 | _remote_sh "$@" || ssh_status=$? |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 132 | # 255 indicates an ssh error. |
| 133 | if [ ${ssh_status} -eq 255 ]; then |
Chris Sosa | 539b341 | 2012-02-27 14:46:10 -0800 | [diff] [blame] | 134 | _remote_sh "$@" |
Chris Sosa | faeee5f | 2011-09-26 16:08:14 -0700 | [diff] [blame] | 135 | else |
| 136 | return ${ssh_status} |
| 137 | fi |
| 138 | } |
| 139 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 140 | remote_sh_raw() { |
Qiang(Joe) Xu | 678435c | 2018-01-26 02:46:40 +0000 | [diff] [blame] | 141 | ssh -p ${FLAGS_ssh_port} $(ssh_connect_settings) \ |
David James | f585090 | 2011-09-30 10:51:48 -0700 | [diff] [blame] | 142 | $EXTRA_REMOTE_SH_ARGS root@$FLAGS_remote "$@" |
Andrew de los Reyes | e08639b | 2011-09-21 15:44:05 -0700 | [diff] [blame] | 143 | return $? |
| 144 | } |
| 145 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 146 | remote_sh_allow_changed_host_key() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 147 | rm -f $TMP_KNOWN_HOSTS |
| 148 | remote_sh "$@" |
| 149 | } |
| 150 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 151 | set_up_remote_access() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 152 | cp $FLAGS_private_key $TMP_PRIVATE_KEY |
| 153 | chmod 0400 $TMP_PRIVATE_KEY |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 154 | |
| 155 | # Verify the client is reachable before continuing |
Gaurav Shah | af7d5d1 | 2011-09-21 16:42:16 -0700 | [diff] [blame] | 156 | local output |
| 157 | local status=0 |
Frank Henigman | d6b6cf6 | 2012-11-02 13:47:16 -0400 | [diff] [blame] | 158 | if output=$(remote_sh -n "true" 2>&1); then |
Gaurav Shah | af7d5d1 | 2011-09-21 16:42:16 -0700 | [diff] [blame] | 159 | : |
| 160 | else |
| 161 | status=$? |
| 162 | echo "Could not initiate first contact with remote host" |
| 163 | echo "$output" |
| 164 | fi |
| 165 | return $status |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 168 | # Ask the target what board it is |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 169 | learn_board() { |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 170 | [ -n "${FLAGS_board}" ] && return |
Frank Henigman | d6b6cf6 | 2012-11-02 13:47:16 -0400 | [diff] [blame] | 171 | remote_sh -n grep CHROMEOS_RELEASE_BOARD /etc/lsb-release |
Ken Mixter | cc4f1dd | 2010-08-31 12:07:11 -0700 | [diff] [blame] | 172 | 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 Coolidge | c3d5d91 | 2017-03-07 14:21:28 -0800 | [diff] [blame] | 180 | # Discover partition numbers from the target. |
| 181 | learn_partition_layout() { |
| 182 | source <(remote_sh_raw cat /usr/sbin/write_gpt.sh) |
| 183 | load_base_vars |
| 184 | } |
| 185 | |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 186 | # 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 Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 196 | _check_if_rebooted() { |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 197 | ( |
| 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 Arnold | 2ff2f11 | 2012-08-28 10:13:05 -0700 | [diff] [blame] | 204 | <<<"$(ssh_connect_settings)")" |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 205 | remote_sh_allow_changed_host_key -q -- '[ ! -e /tmp/awaiting_reboot ]' |
| 206 | ) |
Chris Sosa | 24da49e | 2011-02-01 17:06:12 -0800 | [diff] [blame] | 207 | } |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 208 | |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 209 | # 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 Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 214 | remote_reboot() { |
Chris Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 215 | info "Rebooting ${FLAGS_remote}..." |
Andrey Ulanov | c68f488 | 2017-01-30 17:41:42 -0800 | [diff] [blame] | 216 | # '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 Wolfe | d91df7a | 2012-02-29 16:55:48 -0500 | [diff] [blame] | 219 | 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 Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 249 | done |
Brian Norris | 3123fa1 | 2017-09-28 10:26:28 -0700 | [diff] [blame] | 250 | die_notrace "Reboot has not completed after ${timeout} seconds; giving up." |
Mandeep Singh Baines | a63cd2d | 2010-12-02 11:58:26 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 253 | # Called by clients before exiting. |
| 254 | # Part of the remote_access.sh interface but now empty. |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 255 | cleanup_remote_access() { |
Mandeep Singh Baines | aef91ad | 2011-01-14 14:17:25 -0800 | [diff] [blame] | 256 | true |
Sean O'Connor | 9969ce9 | 2010-02-01 17:10:03 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Mike Frysinger | 6b1abb2 | 2012-05-11 13:44:06 -0400 | [diff] [blame] | 259 | remote_access_init() { |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 260 | TMP_PRIVATE_KEY=$TMP/private_key |
| 261 | TMP_KNOWN_HOSTS=$TMP/known_hosts |
Douglas Anderson | d880765 | 2016-11-11 14:01:18 -0800 | [diff] [blame] | 262 | TMP_CONTROL_FILE="${TMP}/ssh_control%r@%h:%p" |
| 263 | |
Ken Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 264 | if [ -z "$FLAGS_remote" ]; then |
| 265 | echo "Please specify --remote=<IP-or-hostname> of the Chromium OS instance" |
| 266 | exit 1 |
| 267 | fi |
Douglas Anderson | aaab1a3 | 2016-11-11 13:48:55 -0800 | [diff] [blame] | 268 | |
| 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 Mixter | 689b9ee | 2010-01-07 18:23:52 -0800 | [diff] [blame] | 275 | set_up_remote_access |
| 276 | } |