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 KVM_MONITOR_SOCKET= |
17 | 18 |
18 function get_pid() { | 19 function get_pid() { |
19 sudo cat "${KVM_PID_FILE}" | 20 sudo cat "${KVM_PID_FILE}" |
20 } | 21 } |
21 | 22 |
22 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work | 23 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work |
23 # on Hardy. | 24 # on Hardy. |
24 # $1: Path to the virtual image to start. | 25 # $1: Path to the virtual image to start. |
25 function start_kvm() { | 26 function start_kvm() { |
26 # Override default pid file. | 27 # Override default pid file. |
27 [ -n "${FLAGS_kvm_pid}" ] && KVM_PID_FILE=${FLAGS_kvm_pid} | 28 [ -n "${FLAGS_kvm_pid}" ] && KVM_PID_FILE=${FLAGS_kvm_pid} |
28 if [ -e "${KVM_PID_FILE}" ]; then | 29 if [ -e "${KVM_PID_FILE}" ]; then |
29 local pid=$(get_pid) | 30 local pid=$(get_pid) |
30 # Check if the process exists. | 31 # Check if the process exists. |
31 if ps -p ${pid} > /dev/null ; then | 32 if ps -p ${pid} > /dev/null ; then |
32 echo "Using a pre-created KVM instance specified by ${FLAGS_kvm_pid}." >&2 | 33 echo "Using a pre-created KVM instance specified by ${FLAGS_kvm_pid}." >&2 |
33 else | 34 else |
34 # Let's be safe in case they specified a file that isn't a pid file. | 35 # Let's be safe in case they specified a file that isn't a pid file. |
35 echo "File ${KVM_PID_FILE} exists but specified pid doesn't." >&2 | 36 echo "File ${KVM_PID_FILE} exists but specified pid doesn't." >&2 |
36 exit 1 | 37 exit 1 |
37 fi | 38 fi |
38 else | 39 else |
39 # No pid specified by PID file. Let's create a VM instance in this case. | 40 # No pid specified by PID file. Let's create a VM instance in this case. |
40 echo "Starting a KVM instance" >&2 | 41 echo "Starting a KVM instance" >&2 |
41 local nographics="" | 42 local nographics="" |
42 local usesnapshot="" | 43 local usesnapshot="" |
| 44 local save_vm_state="" |
43 if [ ${FLAGS_no_graphics} -eq ${FLAGS_TRUE} ]; then | 45 if [ ${FLAGS_no_graphics} -eq ${FLAGS_TRUE} ]; then |
44 nographics="-nographic -serial none" | 46 nographics="-nographic -serial none" |
45 fi | 47 fi |
46 if [ -n "${FLAGS_vnc}" ]; then | 48 if [ -n "${FLAGS_vnc}" ]; then |
47 nographics="-vnc ${FLAGS_vnc}" | 49 nographics="-vnc ${FLAGS_vnc}" |
48 fi | 50 fi |
49 | 51 |
50 if [ ${FLAGS_snapshot} -eq ${FLAGS_TRUE} ]; then | 52 if [ ${FLAGS_snapshot} -eq ${FLAGS_TRUE} ]; then |
51 snapshot="-snapshot" | 53 snapshot="-snapshot" |
52 fi | 54 fi |
53 | 55 |
| 56 if [ ${FLAGS_save_vm_state} -eq ${FLAGS_TRUE} ]; then |
| 57 # Redirect the QEMU monitor to the guest usb serial port so tests |
| 58 # can trigger a "savevm" command from inside the guest. |
| 59 KVM_MONITOR_SOCKET=/tmp/kvm.mon.socket.$$ |
| 60 save_vm_state="-monitor unix:${KVM_MONITOR_SOCKET},server,nowait |
| 61 -usbdevice serial::unix:${KVM_MONITOR_SOCKET}" |
| 62 fi |
| 63 |
54 sudo kvm -m 1024 \ | 64 sudo kvm -m 1024 \ |
55 -vga std \ | 65 -vga std \ |
56 -pidfile "${KVM_PID_FILE}" \ | 66 -pidfile "${KVM_PID_FILE}" \ |
57 -daemonize \ | 67 -daemonize \ |
58 -net nic,model=e1000 \ | 68 -net nic,model=e1000 \ |
59 ${nographics} \ | 69 ${nographics} \ |
60 ${snapshot} \ | 70 ${snapshot} \ |
| 71 ${save_vm_state} \ |
61 -net user,hostfwd=tcp::${FLAGS_ssh_port}-:22 \ | 72 -net user,hostfwd=tcp::${FLAGS_ssh_port}-:22 \ |
62 -hda "${1}" | 73 -hda "${1}" |
63 | 74 |
64 LIVE_VM_IMAGE="${1}" | 75 LIVE_VM_IMAGE="${1}" |
65 fi | 76 fi |
66 } | 77 } |
67 | 78 |
68 # Checks to see if we can access the target virtual machine with ssh. | 79 # Checks to see if we can access the target virtual machine with ssh. |
69 function ssh_ping() { | 80 function ssh_ping() { |
70 "$(dirname $0)"/../ssh_test.sh \ | 81 "$(dirname $0)"/../ssh_test.sh \ |
(...skipping 22 matching lines...) Expand all Loading... |
93 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then | 104 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then |
94 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ | 105 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ |
95 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 | 106 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." >&2 |
96 else | 107 else |
97 echo "Stopping the KVM instance" >&2 | 108 echo "Stopping the KVM instance" >&2 |
98 local pid=$(get_pid) | 109 local pid=$(get_pid) |
99 if [ -n "${pid}" ]; then | 110 if [ -n "${pid}" ]; then |
100 echo "Killing ${pid}" >&2 | 111 echo "Killing ${pid}" >&2 |
101 sudo kill ${pid} | 112 sudo kill ${pid} |
102 sudo rm "${KVM_PID_FILE}" | 113 sudo rm "${KVM_PID_FILE}" |
| 114 if [ -n "${KVM_MONITOR_SOCKET}" ]; then |
| 115 sudo rm "${KVM_MONITOR_SOCKET}" |
| 116 fi |
103 else | 117 else |
104 echo "No kvm pid found to stop." >&2 | 118 echo "No kvm pid found to stop." >&2 |
105 return 1 | 119 return 1 |
106 fi | 120 fi |
107 fi | 121 fi |
108 } | 122 } |
OLD | NEW |