# HG changeset patch # User Heiko Schlittermann # Date 1501853539 -7200 # Node ID 586609aedc5cc031d5d377be646b32b84ca07eb3 # Parent f10fa2b7fc440de123762a8617c7d7015b79bfdd move to ssh://git@git.schlittermann.de/ius/kvmtool.git diff -r f10fa2b7fc44 -r 586609aedc5c Makefile --- a/Makefile Tue Sep 06 16:07:27 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -DESTDIR = / - -prefix = /usr/local -sbindir = ${prefix}/sbin -etcdir = /etc - -.PHONY: all clean install - -all: kvmtool - -kvmtool: kvmtool.sh - @sed -e 's|\[% VERSION %\]|'"`hg id -bit`"'|;' \ - -e 's|\[% BUILDINFO %\]|'"`hostname`:`pwd`"'|;' \ - <$^ >$@ - @chmod +x $@ - -clean: ; rm -f kvmtool - -install: all - install -m 0755 -d ${DESTDIR}${sbindir}/ - install -m 0555 kvmtool ${DESTDIR}${sbindir}/kvmtool - install -m 0755 -d ${DESTDIR}${etcdir}/ - test -f ${DESTDIR}${etcdir}/kvmtab \ - || install -m 0644 kvmtab.ex ${DESTDIR}${etcdir}/kvmtab - diff -r f10fa2b7fc44 -r 586609aedc5c README --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README Fri Aug 04 15:32:19 2017 +0200 @@ -0,0 +1,1 @@ +=> ssh://git@git.schlittermann.de/ius/kvmtool.git diff -r f10fa2b7fc44 -r 586609aedc5c kvmtab.ex --- a/kvmtab.ex Tue Sep 06 16:07:27 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -# this table is read by kvmtool (or vm-*) -# do not forget to sync this table to the other hosts (*-{a,b}) -# -# name host id mac config file (builtin if empty) - diff -r f10fa2b7fc44 -r 586609aedc5c kvmtool.sh --- a/kvmtool.sh Tue Sep 06 16:07:27 2016 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,165 +0,0 @@ -#! /bin/bash -e - -# global stuff - - -ME=${0##*/} -NODE=$(uname -n) -KVMTAB=/etc/kvmtab -KVM_OPTS_BUILTIN='-drive file=/dev/drbd$ID,cache=none,aio=native,if=virtio,index=0 - -boot order=c - -m 512 - -net nic,macaddr=$MAC,model=virtio - -net tap,ifname=tap$ID,script=no - -k de' - -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 - # 'device is held open by someone' :( - sleep 1 - echo "switch drbd$ID into secondary role" - drbdadm secondary drbd$ID - ;; - restart) - $0 stop $NAME - sleep 3 - $0 start $NAME - ;; - *) 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 - - -# "kvmtool start" or "kvmtool start all" will try to start -# all the vm's we find in /etc/kvmtab; if called "kvmtool start " it -# will try to start the the vm called 'vm' -if [[ "$ME" == *$ME ]]; then - cmd="$1"; shift - - if test $# = 0 || ( test $# = 1 && test $1 = 'all' ); 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 or 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: