aboutsummaryrefslogtreecommitdiff
path: root/kvmx
diff options
context:
space:
mode:
Diffstat (limited to 'kvmx')
-rwxr-xr-xkvmx120
1 files changed, 120 insertions, 0 deletions
diff --git a/kvmx b/kvmx
new file mode 100755
index 0000000..00a41be
--- /dev/null
+++ b/kvmx
@@ -0,0 +1,120 @@
+#!/bin/bash
+#
+# KVM and SPICE client wrapper
+#
+
+# Parameters
+BASENAME="`basename $0`"
+DIRNAME="`dirname $0`"
+STORAGE="/var/cache/qemu"
+SHARED="/var/data/load"
+PORT="$(($RANDOM + 1024))"
+SSH="$(($PORT + 22))"
+ACTION="$1"
+VM="$2"
+BOX="$STORAGE/$VM/box.img"
+PIDFILE="$STORAGE/$VM/pid"
+PORTFILE="$STORAGE/$VM/port"
+SSHFILE="$STORAGE/$VM/ssh"
+SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=FATAL -i $DIRNAME/ssh/insecure_private_key"
+LOGIN="user"
+
+# Run spice client
+function kvmx_spice_client {
+ # https://lists.freedesktop.org/archives/spice-devel/2013-September/014643.html
+ SPICE_NOGRAB=1 spicec --host localhost --port $PORT &
+ #spicy -h localhost -p $PORT
+ #remote-viewer spice://localhost:$PORT
+
+ # Give time to boot
+ sleep 5
+
+ # Fix window titles
+ xdotool search --name "SPICEc:0" set_window --name $VM
+}
+
+# Restart vdagent inside the guest
+function kvmx_clip {
+ instances="`ps -o pid,command -e | grep "spice-vdagent$" | cut -d ' ' -f 2 | xargs`"
+
+ # Kill old instances
+ for pid in $instances; do
+ kill -9 $pid &> /dev/null
+ done
+
+ # Just to make sure we're inside a virtual machine
+ if which spice-vdagent &> /dev/null ; then
+ spice-vdagent
+ fi
+}
+
+# Bring virtual machine up
+function kvmx_up {
+ # FIXME
+ # Check if machine is up
+
+ # Run virtual machine
+ kvm -m 2048 -name $VM -drive file=$BOX,if=virtio -vga qxl \
+ -spice port=$PORT,addr=127.0.0.1,disable-ticketing,streaming-video=off,jpeg-wan-compression=never,playback-compression=off,zlib-glz-wan-compression=never,image-compression=off \
+ -device virtio-serial-pci \
+ -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
+ -chardev spicevmc,id=spicechannel0,name=vdagent \
+ -smp 2 -soundhw ac97 -cpu host -balloon virtio \
+ -fsdev local,id=shared,path=$SHARED,security_model=none -device virtio-9p-pci,fsdev=shared,mount_tag=shared \
+ -net nic,model=virtio \
+ -net user,hostfwd=tcp:127.0.0.1:$SSH-:22 &
+
+ PID="$!"
+
+ # Save state
+ echo $PID > $PIDFILE
+ echo $PORT > $PORTFILE
+ echo $SSH > $SSHFILE
+
+ kvmx_spice_client
+}
+
+# Check
+if [ -z "$VM" ] && [ "$ACTION" != "clip" ]; then
+ echo "usage: $BASENAME <action> <vm>"
+ exit 1
+elif [ ! -e "$BOX" ] && [ "$ACTION" != "clip" ]; then
+ echo "file not found: $BOX"
+ exit 1
+fi
+
+# TODO: check for a ~/.kvmx config
+# TODO: check for a kvmxfile
+
+# Dispatch
+if [ "$ACTION" == "up" ]; then
+ kvmx_up
+elif [ "$ACTION" == "clip" ]; then
+ kvmx_clip
+elif [ "$ACTION" == "suspend" ]; then
+ PID="`cat $PIDFILE`"
+ kill -STOP $PID
+elif [ "$ACTION" == "resume" ]; then
+ PID="`cat $PIDFILE`"
+ kill -CONT $PID
+elif [ "$ACTION" == "poweroff" ]; then
+ echo TODO
+elif [ "$ACTION" == "ssh" ]; then
+ shift 2
+ SSH="`cat $SSHFILE`"
+ $SSH_COMMAND -p $SSH $LOGIN@127.0.0.1 $*
+elif [ "$ACTION" == "rsync" ]; then
+ ORIG="$3"
+ DEST="$4"
+ SSH="`cat $SSHFILE`"
+ rsync -av "$SSH_COMMAND -p $SSH" $ORIG/ $LOGIN@127.0.0.1:$DEST/
+elif [ "$ACTION" == "provision" ]; then
+ echo TODO
+elif [ "$ACTION" == "create" ]; then
+ echo TODO
+elif [ "$ACTION" == "init" ]; then
+ # TODO: copy from template
+ touch .kvmxfile
+elif [ "$ACTION" == "upgrade" ]; then
+ echo TODO
+fi