OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 # |
| 5 # Common vm functions for use in crosutils. |
| 6 |
| 7 DEFINE_string kvm_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." |
| 10 DEFINE_boolean persist "${FLAGS_FALSE}" "Persist vm." |
| 11 DEFINE_boolean snapshot ${FLAGS_FALSE} "Don't commit changes to image." |
| 12 DEFINE_integer ssh_port 9222 "Port to tunnel ssh traffic over." |
| 13 |
| 14 |
| 15 KVM_PID_FILE=/tmp/kvm.$$.pid |
| 16 |
| 17 function get_pid() { |
| 18 sudo cat "${KVM_PID_FILE}" |
| 19 } |
| 20 |
| 21 # TODO(rtc): These flags assume that we'll be using KVM on Lucid and won't work |
| 22 # on Hardy. |
| 23 # $1: Path to the virtual image to start. |
| 24 function start_kvm() { |
| 25 # Override default pid file. |
| 26 [ -n "${FLAGS_kvm_pid}" ] && KVM_PID_FILE=${FLAGS_kvm_pid} |
| 27 if [ -e "${KVM_PID_FILE}" ]; then |
| 28 local pid=$(get_pid) |
| 29 # Check if the process exists. |
| 30 if ps -p ${pid} > /dev/null ; then |
| 31 echo "Using a pre-created KVM instance specified by ${FLAGS_kvm_pid}." |
| 32 else |
| 33 # Let's be safe in case they specified a file that isn't a pid file. |
| 34 echo "File ${KVM_PID_FILE} exists but specified pid doesn't." |
| 35 exit 1 |
| 36 fi |
| 37 else |
| 38 # No pid specified by PID file. Let's create a VM instance in this case. |
| 39 echo "Starting a KVM instance" |
| 40 local nographics="" |
| 41 local usesnapshot="" |
| 42 if [ ${FLAGS_no_graphics} -eq ${FLAGS_TRUE} ]; then |
| 43 nographics="-nographic" |
| 44 fi |
| 45 |
| 46 if [ ${FLAGS_snapshot} -eq ${FLAGS_TRUE} ]; then |
| 47 snapshot="-snapshot" |
| 48 fi |
| 49 |
| 50 sudo kvm -m 1024 \ |
| 51 -vga std \ |
| 52 -pidfile "${KVM_PID_FILE}" \ |
| 53 -daemonize \ |
| 54 -net nic \ |
| 55 ${nographics} \ |
| 56 ${snapshot} \ |
| 57 -net user,hostfwd=tcp::${FLAGS_ssh_port}-:22 \ |
| 58 -hda "${1}" |
| 59 fi |
| 60 } |
| 61 |
| 62 function stop_kvm() { |
| 63 if [ "${FLAGS_persist}" -eq "${FLAGS_TRUE}" ]; then |
| 64 echo "Persist requested. Use --ssh_port ${FLAGS_ssh_port} " \ |
| 65 "--kvm_pid ${KVM_PID_FILE} to re-connect to it." |
| 66 else |
| 67 echo "Stopping the KVM instance" |
| 68 local pid=$(get_pid) |
| 69 echo "Killing ${pid}" |
| 70 sudo kill ${pid} |
| 71 sudo rm "${KVM_PID_FILE}" |
| 72 fi |
| 73 } |
OLD | NEW |