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 | 13 |
14 KVM_PID_FILE=/tmp/kvm.$$.pid | 14 KVM_PID_FILE=/tmp/kvm.$$.pid |
| 15 LIVE_VM_IMAGE= |
15 | 16 |
16 function get_pid() { | 17 function get_pid() { |
17 sudo cat "${KVM_PID_FILE}" | 18 sudo cat "${KVM_PID_FILE}" |
18 } | 19 } |
19 | 20 |
20 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work | 21 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work |
21 # on Hardy. | 22 # on Hardy. |
22 # $1: Path to the virtual image to start. | 23 # $1: Path to the virtual image to start. |
23 function start_kvm() { | 24 function start_kvm() { |
24 # Override default pid file. | 25 # Override default pid file. |
(...skipping 23 matching lines...) Expand all Loading... |
48 | 49 |
49 sudo kvm -m 1024 \ | 50 sudo kvm -m 1024 \ |
50 -vga std \ | 51 -vga std \ |
51 -pidfile "${KVM_PID_FILE}" \ | 52 -pidfile "${KVM_PID_FILE}" \ |
52 -daemonize \ | 53 -daemonize \ |
53 -net nic \ | 54 -net nic \ |
54 ${nographics} \ | 55 ${nographics} \ |
55 ${snapshot} \ | 56 ${snapshot} \ |
56 -net user,hostfwd=tcp::${FLAGS_ssh_port}-:22 \ | 57 -net user,hostfwd=tcp::${FLAGS_ssh_port}-:22 \ |
57 -hda "${1}" | 58 -hda "${1}" |
| 59 |
| 60 LIVE_VM_IMAGE="${1}" |
58 fi | 61 fi |
59 } | 62 } |
60 | 63 |
| 64 # Checks to see if we can access the target virtual machine with ssh. |
| 65 function ssh_ping() { |
| 66 "$(dirname $0)"/../ssh_test.sh \ |
| 67 --ssh_port=${FLAGS_ssh_port} \ |
| 68 --remote=127.0.0.1 |
| 69 } |
| 70 |
| 71 # Tries to ssh into live image $1 times. After first failure, a try involves |
| 72 # shutting down and restarting kvm. |
| 73 function retry_until_ssh() { |
| 74 local can_ssh_into=1 |
| 75 local retries=0 |
| 76 ssh_ping && can_ssh_into=0 |
| 77 |
| 78 while [ ${can_ssh_into} -eq 1 ] && [ ${retries} -lt ${1} ]; do |
| 79 echo "Failed to connect to virtual machine, retrying ... " >&2 |
| 80 stop_kvm || echo "Could not stop kvm. Retrying anyway." >&2 |
| 81 start_kvm "${LIVE_VM_IMAGE}" |
| 82 ssh_ping && can_ssh_into=0 |
| 83 retries=$((retries + 1)) |
| 84 done |
| 85 return ${can_ssh_into} |
| 86 } |
| 87 |
61 function stop_kvm() { | 88 function stop_kvm() { |
62 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then | 89 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then |
63 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ | 90 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ |
64 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." | 91 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." |
65 else | 92 else |
66 echo "Stopping the KVM instance" | 93 echo "Stopping the KVM instance" >&2 |
67 local pid=$(get_pid) | 94 local pid=$(get_pid) |
68 echo "Killing ${pid}" | 95 if [ -n "${pid}" ]; then |
69 sudo kill ${pid} | 96 echo "Killing ${pid}" |
70 sudo rm "${KVM_PID_FILE}" | 97 sudo kill ${pid} |
| 98 sudo rm "${KVM_PID_FILE}" |
| 99 else |
| 100 echo "No kvm pid found to stop." >&2 |
| 101 return 1 |
| 102 fi |
71 fi | 103 fi |
72 } | 104 } |
OLD | NEW |