brvirt: when brctl meets virsh

featured

Hypervisors diversity is definitely one of the benefits of having Nuage managing your next-generation network. That means that we, as Nuage engineers, have to play with all kinds of hypervisors — like KVM, ESXi and Hyper-V to be more precise. As to me, I love to work with KVM most, simply because it gives you that feel that you are in control and can fine-tune or troubleshoot with granularity you want. Thanks to opensource tools like tcpdump, virsh, ss, top, brctl and many many others!

But these tools won’t fit perfectly for every situation every time. For example consider a simple case of a Linux host with a bunch of VMs connected via Linux bridges and answer a simple question: how to determine what VM names correspond to what virtual network interfaces connected to what bridges? I have to say that this question arises quite often when you troubleshoot network connectivity between VMs or gathering network stats.

And the answer to this question can not be provided with above mentioned tools without some scripting. Indeed, in this post I will share a script called brvirt which does the job by combining iproute2 and virsh outputs.

Link: Github repo.

Problem

iproute2 & bridges

I am far away from top-notch linux guys but it doesn’t take much to notice that iproute2 package (which is the successor of ifconfig) has hard-to-read outputs when it comes to Linux bridges. And since we are heavily relying on linux bridges when doing KVM virtualization this is of major shortcoming. See for yourself, what ways of displaying bridges are known to you (with only iproute2):

Not the most human-readable output. It is even harder to see what interfaces are connected to these bridges:

I bet one will install bridge-utils right after two-three attempts of analyzing above output.

brctl & virsh

Still brctl beats iproute2 when it comes to creation, operation and troubleshooting of Linux bridges:

Still we can’t answer the question of what VM names correspond to these virtual interfaces: vnet0, vnet1, vnet4? It’s not brctl we shoud ask about, lets talk to virsh:

Ok, we have two VMs running, let’s list their interfaces:

Now, looking at these outputs we can tell that br0 has two virtual interfaces attached vnet0 and vnet4 where the first interface belongs to VM with a name cobbler and the second one to nuage-dns.

But it took us typing in four different commands and analyzing outputs making comparisons in memory. That is something that a computer should do for us.

Can’t bash/awk/sed one-liner do the trick?

Bash and co. wouldn’t be that powerful if they couldn’t solve 95% of cli-related tasks with some oddly-looking one-liner. This case is not an exception, take a look at few examples I found on serverfault.com.

Bash approach has its pros: no dependancies. And cons: its hard to do some eye-candy formatting and you have to have bash-skills.

Solution

What I needed to do is to implement similar to these one-liners logic and in the end produce a table-formatted output which let an operator to get the knowledge about bridges, physical interfaces, VM names, VM interfaces and their MACs. All in one.

I decided to solve this task with Python for several reasons:

  • I am bad at bash
  • Python code is way more understandable and therefore supportable
  • Python has several modules for table-formatted cli output

This is how brvirt was born. It’s logic is quite simple:

  1. parse ip address output and get knowledge about all interfaces and their parameters
  2. parse virsh list and see what active VMs does host have
  3. parse virsh domiflist <domain_id> to see what virtual network interfaces each domains has.
  4. combine these outputs and nicely print with table-formatting.

Yep, not a oneliner yet it does the job quick and with minimum dependancies.

Installation, requirements and usage

To install brvirt simply clone the repo or download main brvirt.py file.

Speaking of requirements the list is short:

  • of course you need virsh. Most probably you already have that one, since you are dealing with KVM machines. If not, install libvirt package for OS of yours.
  • and you need tabulate python module for printing tables. It installs with a simple one-liner TABULATE_INSTALL=lib-only pip install tabulate
  • Both python2.7 and python3+ are supported.

Using brvirt is as simple as  python brvirt.py . This will produce a table like the one you saw in the beginning of this post.

PS1

Yeah, you might probably think that installing some python package and cloning a repo is not worth the problem. Well, in my day-to-day activities it is worth additional steps. Moreover I plan to add some more scripts and bundle them into cli utility leveraging beautiful click module. I will opensource most interesting/useful parts of it, so stay tuned!

 

Roman Dodin

Network engineer at Nokia
Eagerness to learn multiplied by passion to share.
You can reach me at LinkedIn

You Might Also Like