diff -r 9504de208106 -r aa417316b86f kvmtool.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/kvmtool.sh Thu Jul 15 10:00:19 2010 +0200 @@ -0,0 +1,161 @@ +#! /bin/bash -e + +# global stuff + + +ME=${0##*/} +NODE=$(uname -n) +KVMTAB=/etc/kvmtab +KVM_OPTS_BUILTIN='-drive file=/dev/drbd$ID,if=virtio,index=0,boot=on + -boot c + -m 512 + -net nic,macaddr=$MAC,model=virtio + -net tap,ifname=tap$ID,script=no' + +function die() { echo $0: "$@" >&2; exit 1; } + +function machine() { + + local cmd="$1"; shift + local NAME="$1"; shift + + # we need the username + id -u "kvm-$NAME" >/dev/null + + local HOST ID MAC CONFIG MONITOR KVM_OPTS + + # could use grep... + while read name HOST ID MAC CONFIG; do + [ "$name" = "$NAME" ] && break + done <$KVMTAB + + test -n "$ID" || die "no ID found in $KVMTAB" + test -n "$MAC" || die "no MAC found in $KVMTAB" + test "$NODE" = "$HOST" || die "$NAME should run on $HOST, not on $NODE" + + CONFIG=${CONFIG:-builtin} + MONITOR=$((4000 + ID)) + + case "$cmd" in + start) + echo "starting (k)vm-$NAME (id $ID)" + echo "switch drbd$ID device into primary state" + if test "$CONFIG" != builtin; then + echo "using config from $CONFIG" + KVM_OPTS= + source "$CONFIG" + echo "using config $CONFIG" + test "$KVM_OPTS" || die "$CONFIG should set KVM_OPTS" + else + echo "using builtin config" + KVM_OPTS=$KVM_OPTS_BUILTIN + fi + eval KVM_OPTS=\"-name $NAME -vnc :$ID $KVM_OPTS -monitor tcp:127.0.0.1:$MONITOR,server,nowait\" + drbdadm primary drbd$ID + tunctl -u kvm-$NAME -t tap$ID + brctl delif br0 tap$ID &> /dev/null || true + brctl addif br0 tap$ID &> /dev/null || true + ip link set tap$ID up + chown kvm-$NAME /dev/drbd$ID + chgrp kvm /dev/drbd$ID + chmod u=rw,go= /dev/drbd$ID + su -s /bin/sh - kvm-$NAME -c "kvm $KVM_OPTS -daemonize $*" + ;; + stop) + echo "stopping (k)vm-$NAME" + echo system_powerdown | nc 0 $MONITOR + ip link set tap$ID down + brctl delif br0 tap$ID + echo "switch drbd$ID into secondary role" + drbdadm secondary drbd$ID + ;; + restart) + $0 stop + sleep 3 + $0 start + ;; + *) die "bad usage" + ;; + esac +} + +## MAIN + +# some option handling - currently just options for help and manpage +tmp=$(getopt --name $0 -o hmv -l help,man,version -- "$@") || exec pod2usage $0 +eval set -- $tmp +while true; do + opt="$1"; shift; + case "$opt" in + -h|--help) exec pod2usage -verbose 1 -exit 0 $0 + ;; + -m|--man) if perldoc -V >/dev/null; then + pod2usage -verbose 2 -exit 0 $0 + else + pod2man --section 8 $0 | man -l - + exit + fi + ;; + -v|--version) + echo "$ME: [% VERSION %], built: [% BUILDINFO %]" + exit 0 + ;; + --) break + ;; + esac +done + + +# When called as "kvmtool", just try to start +# all the vm's we find in /etc/kvmtab, except, there +# are more parameters on the command line +if [[ "$ME" == *$ME ]]; then + cmd="$1"; shift + + if test $# = 0; then + vms=$(egrep "^[[:alnum:]_-]+[[:space:]]+$NODE\b" $KVMTAB | tr -s '\t ' ' ' | cut -f1 -d' ') + else + vms="$@" + fi +fi + + +# - below we only go for a single machine + +for vm in $vms; do + machine $cmd $vm +done + + +exit + +=pod + +=head1 NAME + + kvmtool - helper to start virtual machines + +=head1 SYNOPSIS + + kvmtool {start|stop|restart} [machine...] + kvmtool-MACHINE {start|stop|restart} + +=head1 DESCRIPTION + +This small tool helps to start the virtual machines known in F. +If just called as C it starts all machines that should run on the +current host. If called as C, it tries to start machine C, but +this may fail, if the F lists this machine for another host. + +=head1 OPTIONS + +Currently there are no options at all. The only exceptions are B<-h>|B<--help> +and B<-m>|B<--man>. + +=head1 FILES + + /etc/kvmtab - list of machines, hosts to run on, MAC address, optional config file + +=cut + +# vim:sts=4 sw=4 aw ai sm: