From acac2d527bb995932a199d438abdab55935185b2 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Sat, 4 Mar 2017 12:47:31 -0300 Subject: Split kvmx code into functions and adds new options --- kvmx | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 83 insertions(+), 24 deletions(-) diff --git a/kvmx b/kvmx index 8c918bb..f1b3c24 100755 --- a/kvmx +++ b/kvmx @@ -8,32 +8,18 @@ BASENAME="`basename $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" +LOGIN="user" -# Check -if [ -z "$VM" ] && [ "$ACTION" != "clip" ]; then - echo "usage: $BASENAME " - exit 1 -elif [ ! -e "$STORAGE/$VM.img" ] && [ "$ACTION" != "clip" ]; then - echo "file not found: $STORAGE/$VM.img" - exit 1 -fi - -# Dispatch -if [ "$ACTION" == "up" ]; then - # Run virtual machine - kvm -m 2048 -name $VM -drive file=$STORAGE/$VM.img,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:5555-:22 \ - - # Run spice client +# 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 @@ -44,7 +30,10 @@ if [ "$ACTION" == "up" ]; then # Fix window titles xdotool search --name "SPICEc:0" set_window --name $VM -elif [ "$ACTION" == "clip" ]; then +} + +# 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 @@ -56,4 +45,74 @@ elif [ "$ACTION" == "clip" ]; then 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 " + 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 + 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 -- cgit v1.2.3