From b3353cfd760b9f6116937172459fe413d49f881d Mon Sep 17 00:00:00 2001 From: Svenne Krap Date: Thu, 19 Sep 2013 21:21:42 +0200 Subject: [PATCH] first version --- .gitignore | 1 + README.txt | 4 ++++ _all_stop.sh | 3 +++ _pids/readme.txt | 1 + _run/common.sh | 35 +++++++++++++++++++++++++++++++++++ _run/ifup_br0 | 1 + _run/ifup_common | 4 ++++ _run/plain.sh | 24 ++++++++++++++++++++++++ _run/virtio.sh | 24 ++++++++++++++++++++++++ autostart.sh | 18 ++++++++++++++++++ gluster.vm | 9 +++++++++ kill.sh | 17 +++++++++++++++++ list-running.sh | 3 +++ newmac | 2 ++ plain.vm | 9 +++++++++ run.sh | 12 ++++++++++++ stop.sh | 16 ++++++++++++++++ used-ram.sh | 2 ++ virtio.vm | 9 +++++++++ 19 files changed, 194 insertions(+) create mode 100644 .gitignore create mode 100644 README.txt create mode 100755 _all_stop.sh create mode 100644 _pids/readme.txt create mode 100644 _run/common.sh create mode 120000 _run/ifup_br0 create mode 100755 _run/ifup_common create mode 100644 _run/plain.sh create mode 100644 _run/virtio.sh create mode 100755 autostart.sh create mode 100644 gluster.vm create mode 100755 kill.sh create mode 100755 list-running.sh create mode 100755 newmac create mode 100644 plain.vm create mode 100755 run.sh create mode 100755 stop.sh create mode 100755 used-ram.sh create mode 100644 virtio.vm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d040ffe --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.un~ diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..07e5c3b --- /dev/null +++ b/README.txt @@ -0,0 +1,4 @@ +Svenne's KVM control scripts +(c) 2011- Svenne Krap + +Released under the 2-clause BSD license diff --git a/_all_stop.sh b/_all_stop.sh new file mode 100755 index 0000000..7d1c35b --- /dev/null +++ b/_all_stop.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cd /virtual-machines +for x in *.vm ; do ./stop.sh $x ; done diff --git a/_pids/readme.txt b/_pids/readme.txt new file mode 100644 index 0000000..8501eeb --- /dev/null +++ b/_pids/readme.txt @@ -0,0 +1 @@ +Dir for pid-files diff --git a/_run/common.sh b/_run/common.sh new file mode 100644 index 0000000..95f7317 --- /dev/null +++ b/_run/common.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +IP=0.0.0.0 +MONPORT=$(( 8500+${VMID} )) +VNCPORT=$(( ${VMID} )) +SERPORT=$(( 9900+${VMID} )) +TAP_NAME="kvm${VMID}" +PWD=$( pwd ) + +if [ -n "$DRIVE2" ] ; then + DRIVE2="-drive file=${DRIVE2},if=virtio,cache=none" +fi + +if [ -n "$CORES" ] ; then + CORES="-smp cores=$CORES" + +fi +if [ -n "$TABLET" ] ; then + TABLET="-usbdevice tablet" +fi + +if [ -z "$VLAN" ] ; then + VLAN=0 +fi +if [ ! -L _run/ifup_br${VLAN} ] ; then + pushd _run + ln -s ifup_common ifup_br${VLAN} + popd +fi + +if [ -z "MACADDR" ] ; then + VLANDIGIT=$( echo 'ibase=10;obase=16;'$VLAN | bc) + VMDIGIT=$( echo 'ibase=10;obase=16;'$VMID | bc ) + MACADDR="C0:FF:EE:19:$VLANDIGIT:$VMDIGIT" +fi diff --git a/_run/ifup_br0 b/_run/ifup_br0 new file mode 120000 index 0000000..f7890b9 --- /dev/null +++ b/_run/ifup_br0 @@ -0,0 +1 @@ +ifup_common \ No newline at end of file diff --git a/_run/ifup_common b/_run/ifup_common new file mode 100755 index 0000000..ca1e7ba --- /dev/null +++ b/_run/ifup_common @@ -0,0 +1,4 @@ +#!/bin/sh +switch=${0:10} +/bin/ifconfig $1 0.0.0.0 up +/sbin/brctl addif ${switch} $1 diff --git a/_run/plain.sh b/_run/plain.sh new file mode 100644 index 0000000..3a59746 --- /dev/null +++ b/_run/plain.sh @@ -0,0 +1,24 @@ +#!/bin/bash +. _run/common.sh + +echo "$0 $NAME started, vnc at $VNCPORT, monitor at $MONPORT" +qemu-kvm \ + -name $NAME \ + -monitor telnet:${IP}:${MONPORT},server,nowait \ + -drive file=${BOOTDRIVE},if=ide,cache=none \ + -boot order=c \ + $CORES \ + $DRIVE2 \ + $TABLET \ + -m ${MEMORY} \ + -k da \ + -vga std \ + -vnc :${VNCPORT} \ + -enable-kvm \ + -rtc base=utc \ + -daemonize \ + -cpu kvm64 \ + -net nic,macaddr=${MACADDR} \ + -net tap,ifname=${TAP_NAME},script=_run/ifup_br${VLAN} \ + -pidfile _pids/$NAME \ + $* diff --git a/_run/virtio.sh b/_run/virtio.sh new file mode 100644 index 0000000..3995a93 --- /dev/null +++ b/_run/virtio.sh @@ -0,0 +1,24 @@ +#!/bin/bash +. _run/common.sh + +echo "$0 $NAME started, vnc at $VNCPORT, monitor at $MONPORT" +qemu-kvm \ + -name $NAME \ + -monitor telnet:${IP}:${MONPORT},server,nowait \ + -drive file=${BOOTDRIVE},if=virtio,cache=none \ + -boot order=c \ + $CORES \ + $DRIVE2 \ + $TABLET \ + -m ${MEMORY} \ + -k da \ + -vga std \ + -vnc :${VNCPORT} \ + -enable-kvm \ + -rtc base=utc \ + -daemonize \ + -cpu kvm64 \ + -net nic,model=virtio,macaddr=${MACADDR} \ + -net tap,ifname=${TAP_NAME},script=_run/ifup_br${VLAN} \ + -pidfile _pids/$NAME \ + $* diff --git a/autostart.sh b/autostart.sh new file mode 100755 index 0000000..ca9f030 --- /dev/null +++ b/autostart.sh @@ -0,0 +1,18 @@ +#!/bin/bash +if [ $# -lt 1 ] ; then + echo "$0 " + exit +fi +if [ ! -f $1 ] ; then + echo " does not exists at $1"; + exit +fi +. $1 +if [ ${AUTOSTART} -ne 1 ] ; then + echo "$1 not autostarting" + exit +else + echo "$1 autostart" +fi +shift +. _run/${TYPE}.sh diff --git a/gluster.vm b/gluster.vm new file mode 100644 index 0000000..6dbacba --- /dev/null +++ b/gluster.vm @@ -0,0 +1,9 @@ +#!/bin/bash +TYPE=virtio +VMID=3 +NAME="gluster" +BOOTDRIVE="gluster://otherhost/gv0/qcows/docker.qcow2" +MEMORY=2048 +CORES=2 +VLAN=3 +AUTOSTART=1 diff --git a/kill.sh b/kill.sh new file mode 100755 index 0000000..070d96e --- /dev/null +++ b/kill.sh @@ -0,0 +1,17 @@ +#!/bin/bash +if [ $# -lt 1 ] ; then + echo "$0 " + exit +fi +if [ ! -f $1 ] ; then + echo " does not exists at $1"; + exit +fi +. $1 +if [ ! -f _pids/$NAME ] ; then + echo "machine $NAME is not running"; + exit +fi +. _run/common.sh +kill $( cat _pids/$NAME ) +rm _pids/$NAME diff --git a/list-running.sh b/list-running.sh new file mode 100755 index 0000000..12d383a --- /dev/null +++ b/list-running.sh @@ -0,0 +1,3 @@ +#!/bin/bash +#ps aux | grep [q]emu | grep -P -o "\-name ([^ ]*) .* -vnc ([^ ]*) " | cut -b 7- +ps aux | gawk '/qemu/ {match($0,"-name ([^ ]*).*-vnc ([^ ]*)", a) ; print(a[1], "pid " $2, a[2])} ' | grep -v "(\[^" diff --git a/newmac b/newmac new file mode 100755 index 0000000..911f624 --- /dev/null +++ b/newmac @@ -0,0 +1,2 @@ +#!/bin/bash +echo -n C0:FF:EE ; for i in `seq 1 3` ; do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done; echo diff --git a/plain.vm b/plain.vm new file mode 100644 index 0000000..a94ccdb --- /dev/null +++ b/plain.vm @@ -0,0 +1,9 @@ +#!/bin/bash +TYPE=plain +VMID=2 +NAME="plain" +BOOTDRIVE="qcows/plain.qcow2" +MEMORY=2048 +CORES=2 +VLAN=3 +AUTOSTART=1 diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..75d9db3 --- /dev/null +++ b/run.sh @@ -0,0 +1,12 @@ +#!/bin/bash +if [ $# -lt 1 ] ; then + echo "$0 " + exit +fi +if [ ! -f $1 ] ; then + echo " does not exists at $1"; + exit +fi +. $1 +shift +. _run/${TYPE}.sh diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..aa9a344 --- /dev/null +++ b/stop.sh @@ -0,0 +1,16 @@ +#!/bin/bash +if [ $# -lt 1 ] ; then + echo "$0 " + exit +fi +if [ ! -f $1 ] ; then + echo " does not exists at $1"; + exit +fi +. $1 +if [ ! -f _pids/$NAME ] ; then + echo "machine $NAME is not running"; + exit +fi +echo "system_powerdown" | socat - TCP:localhost:$MONPORT +rm _pids/$NAME diff --git a/used-ram.sh b/used-ram.sh new file mode 100755 index 0000000..126dbde --- /dev/null +++ b/used-ram.sh @@ -0,0 +1,2 @@ +#!/bin/bash +grep MEM *.vm | awk 'FS="=" { sum += $2 } END { print sum } ' diff --git a/virtio.vm b/virtio.vm new file mode 100644 index 0000000..f0ffed0 --- /dev/null +++ b/virtio.vm @@ -0,0 +1,9 @@ +#!/bin/bash +TYPE=virtio +VMID=1 +NAME="virtio" +BOOTDRIVE="/dev/vg0/kvm__virtio" +MEMORY=2048 +CORES=2 +VLAN=3 +AUTOSTART=1 -- 2.36.1