OLD | NEW |
1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 # | 4 # |
5 # Common vm functions for use in crosutils. | 5 # Common vm functions for use in crosutils. |
6 | 6 |
7 DEFINE_string kvm_pid "" \ | 7 DEFINE_string kvm_pid "" \ |
8 "Use this pid file. If it exists and is set, use the vm specified by pid." | 8 "Use this pid file. If it exists and is set, use the vm specified by pid." |
9 DEFINE_boolean no_graphics ${FLAGS_FALSE} "Runs the KVM instance silently." | 9 DEFINE_boolean no_graphics ${FLAGS_FALSE} "Runs the KVM instance silently." |
10 DEFINE_boolean persist "${FLAGS_FALSE}" "Persist vm." | 10 DEFINE_boolean persist "${FLAGS_FALSE}" "Persist vm." |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 function ssh_ping() { | 93 function ssh_ping() { |
94 "${SCRIPT_ROOT}/ssh_test.sh" \ | 94 "${SCRIPT_ROOT}/ssh_test.sh" \ |
95 --ssh_port=${FLAGS_ssh_port} \ | 95 --ssh_port=${FLAGS_ssh_port} \ |
96 --remote=127.0.0.1 >&2 | 96 --remote=127.0.0.1 >&2 |
97 } | 97 } |
98 | 98 |
99 # Tries to ssh into live image $1 times. After first failure, a try involves | 99 # Tries to ssh into live image $1 times. After first failure, a try involves |
100 # shutting down and restarting kvm. | 100 # shutting down and restarting kvm. |
101 function retry_until_ssh() { | 101 function retry_until_ssh() { |
102 local can_ssh_into=1 | 102 local can_ssh_into=1 |
| 103 local max_retries=3 |
103 local retries=0 | 104 local retries=0 |
104 ssh_ping && can_ssh_into=0 | 105 ssh_ping && can_ssh_into=0 |
105 | 106 |
106 while [ ${can_ssh_into} -eq 1 ] && [ ${retries} -lt ${1} ]; do | 107 while [ ${can_ssh_into} -eq 1 ] && [ ${retries} -lt ${max_retries} ]; do |
107 echo "Failed to connect to virtual machine, retrying ... " >&2 | 108 echo "Failed to connect to virtual machine, retrying ... " >&2 |
108 stop_kvm || echo "Could not stop kvm. Retrying anyway." >&2 | 109 stop_kvm || echo "Could not stop kvm. Retrying anyway." >&2 |
109 start_kvm "${LIVE_VM_IMAGE}" | 110 start_kvm "${LIVE_VM_IMAGE}" |
110 ssh_ping && can_ssh_into=0 | 111 ssh_ping && can_ssh_into=0 |
111 retries=$((retries + 1)) | 112 retries=$((retries + 1)) |
112 done | 113 done |
113 return ${can_ssh_into} | 114 return ${can_ssh_into} |
114 } | 115 } |
115 | 116 |
116 function stop_kvm() { | 117 function stop_kvm() { |
117 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then | 118 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then |
118 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ | 119 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ |
119 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 | 120 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 |
120 else | 121 else |
121 echo "Stopping the KVM instance" >&2 | 122 echo "Stopping the KVM instance" >&2 |
122 local pid=$(get_pid) | 123 local pid=$(get_pid) |
123 if [ -n "${pid}" ]; then | 124 if [ -n "${pid}" ]; then |
124 echo "Killing ${pid}" >&2 | 125 echo "Killing ${pid}" >&2 |
125 blocking_kill ${pid} 1 16 || blocking_kill 9 1 | 126 blocking_kill ${pid} 1 16 || blocking_kill 9 1 |
126 sudo rm "${KVM_PID_FILE}" | 127 sudo rm "${KVM_PID_FILE}" |
127 else | 128 else |
128 echo "No kvm pid found to stop." >&2 | 129 echo "No kvm pid found to stop." >&2 |
129 return 1 | 130 return 1 |
130 fi | 131 fi |
131 fi | 132 fi |
132 } | 133 } |
OLD | NEW |