Is there a way to determine which virtual interface belongs to a virtual machine in a kvm host?

Solution 1:

How about this (example for vnet13):

$ VNET=vnet13; for vm in $(virsh list | grep running | awk '{print $2}'); do virsh dumpxml $vm|grep -q "$VNET" && echo $vm; done

Here we use virsh dumpxml to show dynamic properties about the VM, which are not available in the static XML definition of the VM in /etc/libvirt/qemu/foo.xml. Which vnetX interface is attached to which VM is such a dynamic property. Same goes for the VM's MAC addresses.

Solution 2:

Try virsh dumpxml $domain, you'll see something like:

  <interface type='network'>
  <mac address='52:54:00:9d:9d:10'/>
  <source network='default'/>
  <target dev='vnet1'/>
  <model type='e1000'/>
  <alias name='net1'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x0c' function='0x0'/>

the alias name is what is used in the qemu-kvm command line, so if you run ps -ef |grep qemu|grep net1 from my example, you will see the actual command syntax used for this interface.

Solution 3:

Every one of the solutions given above assumes that the VMs are being managed by libvirt. It is quite possible to run QEMU VMs without that, in which case you cannot use virsh or look at XML to find the answer.

In the case of running QEMU VMs from a "raw" command line:

  1. tcpdump -i tap0 -f 'icmp' (substitute whichever tap interface you're interested in)

  2. Ping each candidate VM until you see packets in the trace. The interface you are tracing when ICMP packets appear is the one you're looking for!

Conversely you can start a ping to a particular VM and then tcpdump each tap interface in turn until one "lights up". Depends whether you're interested in finding the VM that matches the tap interface, or the tap interface that matches the VM.