Is there a straightforward way to find the VNC screen (i.e. port number minus 5900) onto which a KVM guest is bound?

My guests are all configured to run with VNC enabled, but the order in which they occupy the ports is random.

Alternately, is there a way to assign them in the configuration (of guest or host), so that each respective guest will occupy a predefined port?!

link|improve this question

75% accept rate
feedback

4 Answers

up vote 1 down vote accepted

Since you're using libvirt, you can just ask it!

root@onhost1:~# virsh list
 Id Name                 State
----------------------------------
  1 one-34               running
  2 one-36               running
  3 one-38               running

root@onhost1:~# virsh vncdisplay one-34
:34

root@onhost1:~# virsh vncdisplay 1
:34

(my particular correlation of name to VNC display port is due to the use of Open Nebula)


Here's a one-liner to execute this for all running guests at once:

for i in $(virsh list|tail -n +3|awk '{print $2}'|sort); do
  echo -e "\033[01;31m$i\033[00m -> $(virsh vncdisplay $i)"
done

Also made it into a function that sorts output by port number:

function vnc-list
{
  for i in $(virsh list|tail -n +3|awk '{print $2}'|sort); do
    PORTNUM=$(virsh vncdisplay $i|cut -f 2 -d ':')
    printf "% 2d: \033[01;32m%.20s\033[00m\n" "$PORTNUM" "$i";
  done | sort -n
}
link|improve this answer
Wow, thanks a bunch for that. It's spot-on what I was looking for. I'm going to edit a Bash one-liner into your answer for completeness. – STATUS_ACCESS_DENIED Feb 9 at 14:44
Hmm, well the on-liner I edited in apparently got edited into a three-liner. Makes me look stupid as if I didn't know what the difference between 1 and 3 is, but I promise that the version I had was cramming this onto one single line ;) ... don't have edit-rights yet, so I'm at the mercy of others with this. – STATUS_ACCESS_DENIED Feb 9 at 16:36
I edited it into a three-liner for readability and so it doesn't require scrolling to view. – MikeyB Feb 9 at 16:41
feedback

I would run :

ps aux | grep "VM name/config"

Note the process ID and then

netstat -apn | grep "process ID"

This should show you are port open by that process.

link|improve this answer
Okay, sorry I should have defined straightforward much better. I figured I could so something like this, but it's neither convenient nor "automated". Will wait for other replies, but thanks :) – STATUS_ACCESS_DENIED Nov 23 '11 at 20:26
feedback

To set the display, simply explicitly provide the -vnc <ip:display> option to qemu-kvm. See the man page to qemu-kvm, especially the -vnc parameter section for details.

link|improve this answer
Thanks, I'll try that. – STATUS_ACCESS_DENIED Nov 23 '11 at 21:40
feedback

I dont have linux box right now but I remember that folder named ".vnc" is created at home directory which has subfolders according to all the displays created. This subfolders will have the information you are looking for

link|improve this answer
What am I missing? I completely don't get it. Home folder of what user? ... – STATUS_ACCESS_DENIED Nov 24 '11 at 16:31
Home directory of the user who is logged in!! you can go there by simply using "cd" command. See there if there is any directory with name ".vnc"? it is hidden directory so you can see it using "ls -lart" – swd Nov 25 '11 at 9:12
I have not used vnc and linux since a year or so but i remember this folder which has information of all the displays created – swd Nov 25 '11 at 9:14
Ehrm, I think you may want to re-read my question. No user is locally logged on. I am running several KVM guests and want to know up front on which port a certain guest is running or find out easily on which port it got started (in case I cannot control where it is running). – STATUS_ACCESS_DENIED Nov 25 '11 at 13:48
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.