User Tools

Site Tools


unix:scripts:libvirt

libvirt admin scripts

VNC Ports Used

This little one liner grabs the lines that define the vnc ports used by all defined on a node (hypervisor) and returns them as a tab delimited string.

Code

grep vnc /etc/libvirt/qemu/*.xml |\
 perl -lane 'BEGIN{$,="\t"} m/^.*\/([^\/]+)\.xml:.*port=.(\d{4})/; print $1,$2;'

Why I did it

We use vnc on our libvirt domains, basically because we're familiar with vnc. When a domain is misbehaving, we use ssh port forwarding to redirect from our workstation to the domain virtual screen. However, it takes a few minutes each time to figure out which VNC port the domain in question uses.

Instead, I wanted to record this in our copy of CAMP, and since it has a tab delimited text file importer, wanted the domain name and vnc port separated by a tab.

The same could be accomplished by just saving it as a spreadsheet.

How it works

libvirt (virsh) stores it's domain (virtual) definitions in /etc/libvirt/qemu/*.xml. The grep command only returns those lines (along with the file name) in the form

/etc/libvirt/qemu/andrei.dailydata.noc.xml:    <graphics type='vnc' port='5917' autoport='no' listen='127.0.0.1'>

We then pass those lines to a Perl one liner which grabs the filename (without the .xml) and the port, then prints them to STDOUT, tab separated. Using the -n flag to perl turns the one liner into a filter, which will run the code on all lines received via STDIN. For the above line, the following is sent to STDOUT

andrei.dailydata.noc	5917

Caveats

  • If there is only one domain defined, grep does not return the filename, so the system breaks.
  • This is totally dependent on how libvirt stores the data; path, key and whether the key is on the same line. Any changes to the libvirt storage will break the script.
  • We use havirt for our clusters. havirt stores the domain definitions in <installdir>/conf, so the grep command will need to be changed.
unix/scripts/libvirt.txt · Last modified: 2024/09/20 21:23 by rodolico