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 blocking kill on a pid. | |
23 # This function sends a specified kill signal [0-9] to a pid and waits for it | |
24 # die up to a given timeout. It exponentially backs off it's timeout starting | |
25 # at 1 second. | |
26 # $1 the process id. | |
27 # $2 signal to send (-#). | |
28 # $3 max timeout in seconds. | |
29 # Returns 0 on success. | |
30 function blocking_kill() { | |
31 local timeout=1 | |
32 sudo kill -$2 $1 | |
33 while ps -p $1 > /dev/null && [ ${timeout} -le $3 ]; do | |
34 warn "Process still running, sleeping for ${timeout}" | |
35 sleep ${timeout} | |
36 timeout=$((timeout*2)) | |
37 done | |
38 ! ps -p ${1} > /dev/null | |
petkov
2010/12/09 23:37:17
don't know bash enough -- are you sure ! switched
| |
39 } | |
40 | |
22 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work | 41 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work |
23 # on Hardy. | 42 # on Hardy. |
24 # $1: Path to the virtual image to start. | 43 # $1: Path to the virtual image to start. |
25 function start_kvm() { | 44 function start_kvm() { |
26 # Override default pid file. | 45 # Override default pid file. |
27 [ -n "${FLAGS_kvm_pid}" ] && KVM_PID_FILE=${FLAGS_kvm_pid} | 46 [ -n "${FLAGS_kvm_pid}" ] && KVM_PID_FILE=${FLAGS_kvm_pid} |
28 if [ -e "${KVM_PID_FILE}" ]; then | 47 if [ -e "${KVM_PID_FILE}" ]; then |
29 local pid=$(get_pid) | 48 local pid=$(get_pid) |
30 # Check if the process exists. | 49 # Check if the process exists. |
31 if ps -p ${pid} > /dev/null ; then | 50 if ps -p ${pid} > /dev/null ; then |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 | 110 |
92 function stop_kvm() { | 111 function stop_kvm() { |
93 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then | 112 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then |
94 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ | 113 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ |
95 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 | 114 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 |
96 else | 115 else |
97 echo "Stopping the KVM instance" >&2 | 116 echo "Stopping the KVM instance" >&2 |
98 local pid=$(get_pid) | 117 local pid=$(get_pid) |
99 if [ -n "${pid}" ]; then | 118 if [ -n "${pid}" ]; then |
100 echo "Killing ${pid}" >&2 | 119 echo "Killing ${pid}" >&2 |
101 sudo kill ${pid} | 120 blocking_kill ${pid} 1 16 || blocking_kill 9 1 |
102 sudo rm "${KVM_PID_FILE}" | 121 sudo rm "${KVM_PID_FILE}" |
103 else | 122 else |
104 echo "No kvm pid found to stop." >&2 | 123 echo "No kvm pid found to stop." >&2 |
105 return 1 | 124 return 1 |
106 fi | 125 fi |
107 fi | 126 fi |
108 } | 127 } |
OLD | NEW |