aboutsummaryrefslogtreecommitdiff
path: root/kvmx
blob: b12eb7782be72f72fe8454854242093f3836b0a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
#
# KVM and SPICE client wrapper
#

# Parameters
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"

# 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