LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Virtualization and Cloud (https://www.linuxquestions.org/questions/linux-virtualization-and-cloud-90/)
-   -   View dynamic IP address allocation with virsh (https://www.linuxquestions.org/questions/linux-virtualization-and-cloud-90/view-dynamic-ip-address-allocation-with-virsh-4175482470/)

rgdacosta 10-28-2013 08:22 AM

View dynamic IP address allocation with virsh
 
Hello

Is there any way to view the dynamic IP addresses allocated to guests using virsh or any other command?

Thanks in advance!

druuna 10-28-2013 08:59 AM

Not sure if you are looking for this, but you can view the dynamic IP range for a network:
Code:

virsh # net-list --all
Name                State      Autostart
-----------------------------------------
def_10              active    yes     
def_172              active    yes     
default              active    yes     

virsh # net-dumpxml def_10
<network>
  <name>def_10</name>
  <uuid>a00d7041-0cac-129f-e383-d4415f8d4cf1</uuid>
  <forward mode='nat'/>
  <bridge name='virbr2' stp='on' delay='0' />
  <ip address='10.0.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='10.0.122.201' end='10.0.122.254' />
    </dhcp>
  </ip>
</network>

The bold/italic part shows the DHCP range (201 -> 254 in this case).

rgdacosta 10-28-2013 09:15 AM

Thanks for the reply druuna but that isn't what I'm looking for.

Let me elaborate what I'm dealing with.

I have a number of virtual machines which I use for testing and the default setup is to get an IP address from a DHCP server. When I fire up a virtual machine, I want to make an SSH connection to it but need to know what IP address it was allocated.

Code:

virsh list --all
 Id    Name                          State
----------------------------------------------------
 4    ULTS                          running
 -    ELSA                          shut off
 -    RHEL                          shut off
 -    RHWS300                        shut off
 -    SLES                          shut off
 -    Ubuntu                        shut off
 -    Windows                        shut off

So when I fire up the VM called SLES for example, I then want to know what IP address it was allocated to make an SSH connection.

Yes I know I can configure a static IP address but when I need another instance of SLES I will use a template and it will get its IP dynamically allocated again.

Hope this helps making my question better understood!

druuna 10-28-2013 09:46 AM

I'm not aware of a virsh command that can do this, maybe arp can help you:
Code:

$ arp
Address                  HWtype  HWaddress          Flags Mask            Iface
router.wildfire.nl      ether  00:00:00:00:00:00  C                    eth0
192.168.122.131          ether  11:11:11:11:11:11  C                    virbr0

The virbr0 interface info:
Code:

virsh # net-dumpxml default
<network>
  <name>default</name>
  <uuid>d49df7c2-b17d-4c08-a262-ae30c34ddd17</uuid>
  <forward mode='nat'/>
  <bridge name='virbr0' stp='on' delay='0' />
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254' />
    </dhcp>
  </ip>
</network>

The arp command might take a while if there are multiple interfaces. arp -n might speed things up a bit.

rgdacosta 10-28-2013 10:04 AM

Excellent suggestion!


What worked for me was:

Code:

arp -i virbr0
I'm using Ubuntu and it didn't know about the -b option!

If you don't mind, I'm keeping this open to see what other suggestions come in but I'm pleased with yours, thank you.

druuna 10-28-2013 10:12 AM

The arp -b was a typo, it should be arp -n. Already fixed it in my previous post.

I don't mind if you keep this open, I'm also interested in other methods (if any).

rgdacosta 10-29-2013 06:49 AM

I wrote a little shell script to get what I wanted.

It assumes the network virbr0 but you can naturally modify it to pass the network as the first argument. It produces the output in the form IP_Address Name_of_VM

Code:

#!/bin/bash
MAC=$(grep '^[[:digit:]]' /proc/net/arp | grep virbr0 | awk '{print $4}')
for VM in $(virsh list | grep '^[ ]' | grep -v Name | awk '{print $2}')
        do
                if virsh dumpxml $VM | grep $MAC &> /dev/null
                        then
                                grep '^[[:digit:]]' /proc/net/arp | grep virbr0 | sed 's/'$MAC'/'$VM'/' | awk '{print $1,$4}'
                fi
             
        done


druuna 10-29-2013 09:06 AM

Quote:

Originally Posted by rgdacosta (Post 5054424)
I wrote a little shell script to get what I wanted.

Always nice to see people take an effort to write scripts :)

Although I'm not able to test your script at the moment I do have some (positive) criticism: You seem to be using grep a lot to get what you want/need. Most of the time this can be replaced by a simpler solution.

Here's an examples
Code:

MAC=$(grep '^[[:digit:]]' /proc/net/arp | grep virbr0 | awk '{print $4}')
The above can be done more efficient and elegant:
Code:

MAC=$( awk '/virbr0/ { print $4 }' /proc/net/arp )

dyasny 10-29-2013 09:27 AM

libvirt does not hand IPs out, so you cannot query it for IPs it has given out. The proper solution for finding out about a VM configuration is to have an agent in the VM, that will report whatever you want it to, back to the host, through the hyperchannel. ovirt-guest-agent or qemu-ga are two existing options I am aware of, or you can write your own.

using arp doesn't scale - what if the VM migrated away recently (for example)?

rgdacosta 10-29-2013 10:08 AM

Quite right, dyasny. But fortunately this isn't a production system. I just need to save a few minutes a day figuring out which VM (which will ultimately be trashed once I've broken it) has which IP address.

druuna, I like that you mentioned my unnecessary use of grep so would you mind criticising this?

Code:

#!/bin/bash -x
MAC=$(awk '/virbr0/ { print $4 }' /proc/net/arp )
for VM in $(virsh list | awk '!/Name/ {print $2}')
        do
                if virsh dumpxml $VM | grep $MAC &> /dev/null
                        then
                                awk '/virbr0/ { gsub(/'$MAC'/,"'$VM'"); print $1,$4}' /proc/net/arp
                fi
        done


dyasny 10-29-2013 10:23 AM

Quote:

Quite right, dyasny. But fortunately this isn't a production system. I just need to save a few minutes a day figuring out which VM (which will ultimately be trashed once I've broken it) has which IP address.
Just use dhcpd then, and check with MAC got which IP. Since you control the MAC addresses of the VMs, you can set up reservations in DHCP and always get the same IPs on your VMs.

druuna 10-29-2013 10:51 AM

Quote:

Originally Posted by rgdacosta (Post 5054544)
druuna, I like that you mentioned my unnecessary use of grep so would you mind criticising this?

Code:

#!/bin/bash -x
MAC=$(awk '/virbr0/ { print $4 }' /proc/net/arp )
for VM in $(virsh list | awk '!/Name/ {print $2}')
        do
                if virsh dumpxml $VM | grep $MAC &> /dev/null
                        then
                                awk '/virbr0/ { gsub(/'$MAC'/,"'$VM'"); print $1,$4}' /proc/net/arp
                fi
        done


As stated before; I'm not able to run any virsh commands at the moment and have to do this from the top of my head. With that said...

I do believe that the virsh list command has a header that is 2 lines followed by info about the VM:
Code:

No + Name + State
- - - - - - - - -
  1  some_vm  running
  2  other_vm  running

This
Code:

virsh list | awk '!/Name/ {print $2}'
will only get rid of the first line, not the second. Have a look at this:
Code:

virsh list | awk '/running/ { print $2 }'

This seems to elaborate: awk '/virbr0/ { gsub(/'$MAC'/,"'$VM'"); print $1,$4}' /proc/net/arp (it is clever though):
Code:

awk '/virbr0/ { printf $1 }' /proc/net/arp
echo " $VM"

Putting it all together:
Code:

#!/bin/bash
# set -x
MAC=$(awk '/virbr0/ { print $4 }' /proc/net/arp )
for VM in $( virsh list | awk '/running/ { print $2 }' )
do
  if virsh dumpxml $VM | grep $MAC &> /dev/null
  then
    awk '/virbr0/ { printf $1 }' /proc/net/arp
    echo " $VM"
  fi
done


rgdacosta 10-29-2013 11:00 AM

Thanks druuna!


All times are GMT -5. The time now is 01:57 PM.