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