summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominique Martinet <asmadeus@codewreck.org>2015-07-02 18:25:08 +0200
committerDominique Martinet <asmadeus@codewreck.org>2015-07-02 18:26:37 +0200
commitbbf7d605d28850470ab807ef30a14102b2340ef7 (patch)
tree11f7f4b90a7b418f1ba72b01c2953fcf0ab2f76f
parent7a7e1e92c322113c3dca9816dd62bbcfb6db783e (diff)
add (ugly) vncviewer command
-rw-r--r--host_config/bash_completion.d-kvm-wrapper4
-rwxr-xr-xkvm-wrapper.sh38
2 files changed, 40 insertions, 2 deletions
diff --git a/host_config/bash_completion.d-kvm-wrapper b/host_config/bash_completion.d-kvm-wrapper
index f9ae450..ba59e17 100644
--- a/host_config/bash_completion.d-kvm-wrapper
+++ b/host_config/bash_completion.d-kvm-wrapper
@@ -13,7 +13,7 @@ _kvm_wrapper()
command=${COMP_WORDS[1]}
if [[ "$COMP_CWORD" == "1" ]]; then
- COMPREPLY=( $( compgen -W 'help attach create-desc start stop list screen create bootstrap remove edit create-disk status mount-disk umount-disk migrate receive-migrate save-state load-state balloon monitor serial conf top' -- "$cur" ) )
+ COMPREPLY=( $( compgen -W 'help attach create-desc start stop list screen create bootstrap remove edit create-disk status mount-disk umount-disk migrate receive-migrate save-state load-state balloon monitor serial conf top vncviewer' -- "$cur" ) )
return 0
fi
@@ -42,7 +42,7 @@ _kvm_wrapper()
COMPREPLY=( $( compgen -W "$vms" -- "$cur" ) )
return 0
;;
- stop|status|attach|migrate|save-state|balloon|monitor|serial)
+ stop|status|attach|migrate|save-state|balloon|monitor|serial|vncviewer)
vms=`\ls -1 $PID_DIR/*-vm.pid 2>/dev/null |sed -e "s@$PID_DIR/.*:@@g" -e 's/-vm.pid//g'`
COMPREPLY=( $( compgen -W "$vms" -- "$cur" ) )
return 0
diff --git a/kvm-wrapper.sh b/kvm-wrapper.sh
index d303e6d..999c331 100755
--- a/kvm-wrapper.sh
+++ b/kvm-wrapper.sh
@@ -1061,6 +1061,38 @@ function kvm_remove_vm ()
test_exist "$VM_DESCRIPTOR" && fail_exit "Failed to remove descriptor $VM_DSCRIPTOR."
}
+function kvm_vncviewer_vm ()
+{
+ local PORT LOCALPORT SSHPID
+ kvm_init_env "$1"
+
+ ! test_exist "$PID_FILE" && fail_exit "Error: $VM_NAME doesn't seem to be running."
+
+ PORT=$(echo "info vnc" | kvm-wrapper monitor $VM_NAME 2>&1 | sed -ne 's/.*address: .*:\([0-9]*\).*/\1/p')
+
+ [[ -z "$PORT" ]] && fail_exit "Error: couldn't find vnc port used"
+
+ if test_nodename "$KVM_CLUSTER_NODE"; then
+ while
+ LOCALPORT=$((RANDOM%1000+4000))
+ ss -nplt \( sport = :$LOCALPORT \) | grep -q $LOCALPORT
+ do :; done
+
+ SSH_OPTS="-L $LOCALPORT:localhost:$PORT -N" run_remote "$KVM_CLUSTER_NODE" &
+ SSHPID=$!
+
+ trap "kill $SSHPID" EXIT
+ while ! ss -nplt \( sport = :$LOCALPORT \) | grep -q $LOCALPORT; do
+ sleep 0.1
+ done
+ vncviewer localhost:$LOCALPORT || fail_exit "Error: couldn't start vncviewer"
+
+ kill $SSHPID
+ else
+ vncviewer localhost:$PORT
+ fi
+}
+
function kvm_edit_conf ()
{
EDITOR=${EDITOR-vim}
@@ -1171,6 +1203,12 @@ case "$1" in
kvm_edit_conf
exit 0
;;
+ vncviewer)
+ if [[ $# -ge 2 ]]; then
+ kvm_vncviewer_vm "$2" "$3"
+ else print_help; fi
+ exit 0
+ ;;
help)
shift
print_help $@