diff options
author | Richard Yao <ryao@cs.stonybrook.edu> | 2012-03-17 16:40:57 -0400 |
---|---|---|
committer | Richard Yao <ryao@cs.stonybrook.edu> | 2012-03-17 16:44:44 -0400 |
commit | eb70edb705d8456c43ebf52a5eae2d91b6b61ecd (patch) | |
tree | 2947a6a47c9faae6dbe65ae41d9fb8f6b5deac56 | |
parent | initial commit of the init script, a sample config and Readme. (diff) | |
download | kvm-tools-eb70edb705d8456c43ebf52a5eae2d91b6b61ecd.tar.gz kvm-tools-eb70edb705d8456c43ebf52a5eae2d91b6b61ecd.tar.bz2 kvm-tools-eb70edb705d8456c43ebf52a5eae2d91b6b61ecd.zip |
Many small changes
Eliminate bashisms
Include missing qtap-manipulate script
Make indentation use tabs to match other scripts in Gentoo
Eliminate boot=on flag being removed from upstream QEMU
Switch from default binary from kvm to qemu-kvm
-rw-r--r-- | kvm-init-script | 120 | ||||
-rwxr-xr-x | qtap-manipulate | 55 |
2 files changed, 115 insertions, 60 deletions
diff --git a/kvm-init-script b/kvm-init-script index 155e7c6..87d5a48 100644 --- a/kvm-init-script +++ b/kvm-init-script @@ -8,9 +8,9 @@ MONITOR=/var/run/vm/${VMNAME}.monitor QTAP_FILE=/var/run/vm/${VMNAME}.qtap # modify this -VMSOFTWARE=${VMSOFTWARE:-kvm} +VMSOFTWARE=${VMSOFTWARE:-qemu-kvm} DROP_USER=${DROP_USER:-nobody} -MEMORY=${MEMORY:-500M} +MEMORY=${MEMORY:-512M} TIMEOUT=${TIMEOUT:-300} SMP=${SMP:-1} @@ -21,93 +21,93 @@ depend() { } send_command() { - local command="socat -u - UNIX-CONNECT:${MONITOR}" - which nc6 &> /dev/null && command="nc6 -U ${MONITOR} --send-only" - echo "$@" | ${command} >/dev/null 2>&1 + local command="socat -u - UNIX-CONNECT:${MONITOR}" + which nc6 2>&1 > /dev/null && command="nc6 -U ${MONITOR} --send-only" + echo "$@" | ${command} >/dev/null 2>&1 } sanity_check() { - if [ ${VMNAME} = ${SVCNAME} ]; then - eerror "You have to create an init script for each vm:" - eerror " ln -s vm /etc/init.d/vm.vmname" - return 1 - elif [ ! -f "${DISKIMAGE}" ]; then - eerror "couldn't find \$DISKIMAGE '$DISKIMAGE'" - return 1; + if [ "${VMNAME}" = "${SVCNAME}" ]; then + eerror "You have to create an init script for each vm:" + eerror " ln -s vm /etc/init.d/vm.vmname" + return 1 + elif [ ! -f "${DISKIMAGE}" ]; then + eerror "couldn't find \$DISKIMAGE '$DISKIMAGE'" + return 1; fi } start() { - sanity_check + sanity_check - img=$(readlink -f "${DISKIMAGE}") - [ -z "$img" ] && { - eerror "couldn't find ${DISKIMAGE}" - return 1; - } - - mkdir -p $(dirname ${PIDFILE}) $(dirname ${MONITOR}) - ebegin "creating qtap ${QTAP:-(auto allocating one)}" - QTAP_RET=$(qtap-manipulate create ${QTAP}); - if [ 0 != $? ]; then - eerror "failed to create qtap interface" - return 1 + img=$(readlink -f "${DISKIMAGE}") + [ -z "$img" ] && { + eerror "couldn't find ${DISKIMAGE}" + return 1; + } + + mkdir -p $(dirname ${PIDFILE}) $(dirname ${MONITOR}) + ebegin "creating qtap ${QTAP:-(auto allocating one)}" + QTAP_RET=$(qtap-manipulate create ${QTAP}); + if [ 0 != $? ]; then + eerror "failed to create qtap interface" + return 1 fi - QTAP=${QTAP:-${QTAP_RET}} - echo "${QTAP}" > ${QTAP_FILE} - eend $? + QTAP=${QTAP:-${QTAP_RET}} + echo "${QTAP}" > ${QTAP_FILE} + eend $? - ebegin "Starting ${VMSOFTWARE-qemu} for ${VMNAME} at VNC port${VNC}" - start-stop-daemon --start /usr/bin/${VMSOFTWARE-qemu} \ + ebegin "Starting ${VMSOFTWARE-qemu} for ${VMNAME} at VNC port${VNC}" + start-stop-daemon --start /usr/bin/${VMSOFTWARE-qemu} \ --pidfile ${PIDFILE} \ -- -daemonize -pidfile ${PIDFILE} -monitor unix:${MONITOR},server,nowait \ -runas ${DROP_USER} -name ${VMNAME} \ - -drive file="$img",if=${DRIVE_MODEL:-virtio},boot=on \ + -drive file="$img",if=${DRIVE_MODEL:-virtio},cache=${DRIVE_CACHE:-none} \ -net nic,model=${NIC_MODEL:-virtio},macaddr=${MACADDR} -net tap,ifname=${QTAP},script=no \ ${DISABLE_KVM:---enable-kvm} \ ${MEMORY:+-m ${MEMORY}} ${SMP:+-smp ${SMP}} ${VNC:+-vnc ${VNC}} ${OTHER_ARGS} - ret=$? - if [ "0" != "${ret}" ]; then - qtap-manipulate destroy ${QTAP} - fi - eend ${ret} + ret=$? + if [ "0" != "${ret}" ]; then + qtap-manipulate destroy ${QTAP} + fi + eend ${ret} } reboot() { - if [ ${VMNAME} = ${SVCNAME} ]; then + if [ ${VMNAME} = ${SVCNAME} ]; then eerror "You have to create an init script for each vm:" eerror " ln -s vm /etc/init.d/vm.vmname" return 1 - fi - - ebegin "Rebooting ${VMNAME}" - send_command system_reset - eend $? + fi + + ebegin "Rebooting ${VMNAME}" + send_command system_reset + eend $? } stop() { - sanity_check + sanity_check - ebegin "Powering off ${VMNAME}" - send_command system_powerdown - eend $? + ebegin "Powering off ${VMNAME}" + send_command system_powerdown + eend $? - ebegin "waiting up to ${TIMEOUT:-60} seconds for it to die" - ret=1 - for x in $(seq 0 ${TIMEOUT:-60}); do - kill -0 $(<$PIDFILE) &> /dev/null || { ret=0; break; } - sleep 1s - done - eend ${ret} + ebegin "waiting up to ${TIMEOUT:-60} seconds for it to die" + ret=1 + for x in $(seq 0 ${TIMEOUT:-60}); do + kill -0 $(cat $PIDFILE) 2>&1 > /dev/null || { ret=0; break; } + sleep 1s + done + eend ${ret} - ebegin "Stopping ${VMSOFTWARE-qemu} for ${VMNAME}" - start-stop-daemon --stop /usr/bin/${VMSOFTWARE-qemu} \ + ebegin "Stopping ${VMSOFTWARE-qemu} for ${VMNAME}" + start-stop-daemon --stop /usr/bin/${VMSOFTWARE-qemu} \ --user ${DROP_USER} \ --pidfile ${PIDFILE} \ --quiet - eend $? - QTAP=$(<${QTAP_FILE}) - ebegin "destroying qtap ${QTAP}" - qtap-manipulate destroy ${QTAP} - eend $? + eend $? + QTAP=$(cat ${QTAP_FILE}) + ebegin "destroying qtap ${QTAP}" + qtap-manipulate destroy ${QTAP} + eend $? } diff --git a/qtap-manipulate b/qtap-manipulate new file mode 100755 index 0000000..b75c5ef --- /dev/null +++ b/qtap-manipulate @@ -0,0 +1,55 @@ +#!/bin/sh +[ -z "$1" ] || ( [ "$1" != "create" ] && [ -z "$2" ] ) && { echo "invalid usage, require create [device] || |destroy device"; exit 1; } + +#KVM_USER="kvm-envs" + +has() { + local desired=$1 x + shift + for x in "$@"; do + [ "$desired" = "$x" ] && return 0; + done + return 1 +} + +find_node() { + local val=$(ifconfig -a | grep -i ^qtap | cut -d ' ' -f1) + local pos=0 + while has qtap${pos} $val; do + pos=$(( $pos + 1 )) + done + echo qtap${pos} +} + +create_node() { + tunctl -b -u "${KVM_USER}" -t ${1} > /dev/null || { echo "tunctl failed"; exit 2; } + brctl addif br0 ${1} || { echo "brctl failed"; exit 2; } + ifconfig ${1} up 0.0.0.0 promisc || { echo "ifconfig failed"; exit 2; } +} + +destroy_node() { + issue= + ifconfig ${1} down || { echo "ifconfig failed";issue=1; } + brctl delif br0 ${1} || { echo "brctl failed";issue=2; } + tunctl -d ${1} > /dev/null || { echo "tunctl failed";issue=3;} + [ -n "${issue}" ] && exit -$(( $issue )) +} + +command=$1 +qtap=$2 + +if [ -z "$qtap" ]; then + qtap=$(find_node) +fi + +if [ "$command" = "create" ]; then + create_node "$qtap" + [ "$2" != "${qtap}" ] && echo "${qtap}" +elif [ "$command" = "destroy" ]; then + destroy_node "$qtap" +else + echo "$command isn't a valid command; must be create or destroy"; + exit 1 +fi + +exit 0 |