diff options
author | Brian Dolbec <dolsen@gentoo.org> | 2012-02-22 22:08:42 -0800 |
---|---|---|
committer | Brian Dolbec <dolsen@gentoo.org> | 2012-02-22 22:08:42 -0800 |
commit | 48b39c90b574d4658e12029e75044d774c36ae33 (patch) | |
tree | 96b938dec249eb7c340fbe44762511ad20386365 | |
download | kvm-tools-48b39c90b574d4658e12029e75044d774c36ae33.tar.gz kvm-tools-48b39c90b574d4658e12029e75044d774c36ae33.tar.bz2 kvm-tools-48b39c90b574d4658e12029e75044d774c36ae33.zip |
initial commit of the init script, a sample config and Readme.
-rw-r--r-- | README | 36 | ||||
-rw-r--r-- | kvm-conf.example | 12 | ||||
-rw-r--r-- | kvm-init-script | 113 |
3 files changed, 161 insertions, 0 deletions
@@ -0,0 +1,36 @@ +This copy of kvm-init-script has a bugfix from the one at +http://www.pkgcore.org/~ferringb/kvm-example-conf-funtoo +It also adds printing the vnc port number to the "Starting..." + message. This makes it easier to know the port # to connect to with + vnc viewer , etc. Usefull info when you run several vm's at the same time. + + +the instructions are quite simple. + + + cp kvm-init-script /etc/init.d/kvm + cp kvm-conf.example /etc/conf.d/kvm.example + + +Edit the kvm.example changing mac address, vnc port, etc. as needed. +You can see I have several OTHER_ARGS= lines pre-defined, so that it is easy to + switch the boot up options by just commenting/uncommenting the desired line. + + Hmm, this could probably be extended to accept startup options that can select + which OTHER_ARGS=line to use for the startup. + +Save it to /etc/kvm.instance-name + +create the init script instance with a matching name + + ln -s /etc/init.d/kvm /etc/init.d/kvm.instance-name + +then start it; + + /etc/init.d/kvm.instance-name start + +It also responds to stop, reboot (which sends the vm the reboot signal, + so it can do a proper shutdown/reboot). + +It can also be added to different run levels using rc-update. + diff --git a/kvm-conf.example b/kvm-conf.example new file mode 100644 index 0000000..46f8274 --- /dev/null +++ b/kvm-conf.example @@ -0,0 +1,12 @@ +VMSOFTWARE=kvm +MACADDR=52:54:1C:10:34:35 +DISKIMAGE=/mnt/kvm/gentoo64.qcow2 +MEMORY=1G +SMP=4 +VNC=:24 +NIC_MODEL=virtio +DRIVE_MODEL=virtio +#OTHER_ARGS="-boot order=dc -cdrom /mnt/archive/gentoo/install-amd64-minimal-20100408.iso" +OTHER_ARGS=" -boot order=cd -cdrom /mnt/archive/gentoo/systemrescuecd-x86-1.5.6.iso" +#OTHER_ARGS="-vga std -boot order=dc -cdrom /dev/sdc" +#TIMEOUT=60 diff --git a/kvm-init-script b/kvm-init-script new file mode 100644 index 0000000..155e7c6 --- /dev/null +++ b/kvm-init-script @@ -0,0 +1,113 @@ +#!/sbin/runscript +# Copyright 2009 Gentoo Foundation +# Distributed under the terms of the GNU General Public License v2 + +VMNAME=${SVCNAME#*.} +PIDFILE=/var/run/vm/${VMNAME}.pid +MONITOR=/var/run/vm/${VMNAME}.monitor +QTAP_FILE=/var/run/vm/${VMNAME}.qtap + +# modify this +VMSOFTWARE=${VMSOFTWARE:-kvm} +DROP_USER=${DROP_USER:-nobody} +MEMORY=${MEMORY:-500M} +TIMEOUT=${TIMEOUT:-300} +SMP=${SMP:-1} + +opts="reboot" + +depend() { + need net.br0 +} + +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 +} + +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; + fi +} + +start() { + 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 + fi + 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} \ + --pidfile ${PIDFILE} \ + -- -daemonize -pidfile ${PIDFILE} -monitor unix:${MONITOR},server,nowait \ + -runas ${DROP_USER} -name ${VMNAME} \ + -drive file="$img",if=${DRIVE_MODEL:-virtio},boot=on \ + -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} +} + +reboot() { + 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 $? +} + +stop() { + sanity_check + + 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 "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 $? +} |