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." |
11 DEFINE_boolean snapshot ${FLAGS_FALSE} "Don't commit changes to image." | 11 DEFINE_boolean snapshot ${FLAGS_FALSE} "Don't commit changes to image." |
12 DEFINE_integer ssh_port 9222 "Port to tunnel ssh traffic over." | 12 DEFINE_integer ssh_port 9222 "Port to tunnel ssh traffic over." |
13 DEFINE_string vnc "" "VNC Server to display to instead of SDL." | 13 DEFINE_string vnc "" "VNC Server to display to instead of SDL." |
14 | 14 |
15 KVM_PID_FILE=/tmp/kvm.$$.pid | 15 KVM_PID_FILE=/tmp/kvm.$$.pid |
16 LIVE_VM_IMAGE= | 16 LIVE_VM_IMAGE= |
17 | 17 |
18 function get_pid() { | 18 function get_pid() { |
19 sudo cat "${KVM_PID_FILE}" | 19 sudo cat "${KVM_PID_FILE}" |
20 } | 20 } |
21 | 21 |
22 # General purpose wait for process to terminate. | |
23 # Incrementally backs off timeouts until it is waiting for 30 seconds and then | |
petkov
2010/12/09 23:01:28
by 30 you mean 31 -- 1+2+4+8+16? with the exponent
| |
24 # tries to kill -9 before returning. | |
25 # $1 the process id. | |
26 function wait_on_pid() { | |
27 local timeout=1 | |
28 while ps -p ${1} > /dev/null && [ ${timeout} -le 30 ]; do | |
29 warn "Process still running, sleeping for ${timeout}" | |
30 sleep ${timeout} | |
31 timeout=$((timeout*2)) | |
32 done | |
33 if ps -p ${1}; then | |
34 sudo kill -9 ${1} | |
petkov
2010/12/09 23:01:28
the bonus kill here seems to contradict with the f
| |
35 fi | |
36 } | |
37 | |
22 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work | 38 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work |
23 # on Hardy. | 39 # on Hardy. |
24 # $1: Path to the virtual image to start. | 40 # $1: Path to the virtual image to start. |
25 function start_kvm() { | 41 function start_kvm() { |
26 # Override default pid file. | 42 # Override default pid file. |
27 [ -n "${FLAGS_kvm_pid}" ] && KVM_PID_FILE=${FLAGS_kvm_pid} | 43 [ -n "${FLAGS_kvm_pid}" ] && KVM_PID_FILE=${FLAGS_kvm_pid} |
28 if [ -e "${KVM_PID_FILE}" ]; then | 44 if [ -e "${KVM_PID_FILE}" ]; then |
29 local pid=$(get_pid) | 45 local pid=$(get_pid) |
30 # Check if the process exists. | 46 # Check if the process exists. |
31 if ps -p ${pid} > /dev/null ; then | 47 if ps -p ${pid} > /dev/null ; then |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 function stop_kvm() { | 108 function stop_kvm() { |
93 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then | 109 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then |
94 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ | 110 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ |
95 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 | 111 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 |
96 else | 112 else |
97 echo "Stopping the KVM instance" >&2 | 113 echo "Stopping the KVM instance" >&2 |
98 local pid=$(get_pid) | 114 local pid=$(get_pid) |
99 if [ -n "${pid}" ]; then | 115 if [ -n "${pid}" ]; then |
100 echo "Killing ${pid}" >&2 | 116 echo "Killing ${pid}" >&2 |
101 sudo kill ${pid} | 117 sudo kill ${pid} |
118 wait_on_pid ${pid} | |
102 sudo rm "${KVM_PID_FILE}" | 119 sudo rm "${KVM_PID_FILE}" |
103 else | 120 else |
104 echo "No kvm pid found to stop." >&2 | 121 echo "No kvm pid found to stop." >&2 |
105 return 1 | 122 return 1 |
106 fi | 123 fi |
107 fi | 124 fi |
108 } | 125 } |
OLD | NEW |