LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Virtualization and Cloud
User Name
Password
Linux - Virtualization and Cloud This forum is for the discussion of all topics relating to Linux Virtualization and Linux Cloud platforms. Xen, KVM, OpenVZ, VirtualBox, VMware, Linux-VServer and all other Linux Virtualization platforms are welcome. OpenStack, CloudStack, ownCloud, Cloud Foundry, Eucalyptus, Nimbus, OpenNebula and all other Linux Cloud platforms are welcome. Note that questions relating solely to non-Linux OS's should be asked in the General forum.

Notices

Reply
 
LinkBack Search this Thread
Old 10-28-2013, 08:22 AM   #1
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Rep: Reputation: 24
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!
 
Old 10-28-2013, 08:59 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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).

Last edited by druuna; 10-28-2013 at 09:01 AM. Reason: fixed 2 typo's
 
Old 10-28-2013, 09:15 AM   #3
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 24
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!
 
Old 10-28-2013, 09:46 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.

Last edited by druuna; 10-28-2013 at 09:59 AM. Reason: arp -b -> arp -n
 
1 members found this post helpful.
Old 10-28-2013, 10:04 AM   #5
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 24
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.
 
Old 10-28-2013, 10:12 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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).
 
Old 10-29-2013, 06:49 AM   #7
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 24
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
 
Old 10-29-2013, 09:06 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by rgdacosta View Post
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 )
 
1 members found this post helpful.
Old 10-29-2013, 09:27 AM   #9
dyasny
Member
 
Registered: Dec 2007
Location: Canada
Distribution: RHEL,Fedora
Posts: 808

Rep: Reputation: 87
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)?
 
Old 10-29-2013, 10:08 AM   #10
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 24
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
 
Old 10-29-2013, 10:23 AM   #11
dyasny
Member
 
Registered: Dec 2007
Location: Canada
Distribution: RHEL,Fedora
Posts: 808

Rep: Reputation: 87
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.
 
Old 10-29-2013, 10:51 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by rgdacosta View Post
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

Last edited by druuna; 10-29-2013 at 10:53 AM.
 
1 members found this post helpful.
Old 10-29-2013, 11:00 AM   #13
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Original Poster
Rep: Reputation: 24
Thanks druuna!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dynamic memory allocation in assembly hda7 Linux - Software 9 10-28-2009 08:25 AM
Dynamic Memory Allocation jvff Programming 1 09-05-2005 05:01 AM
dynamic memory allocation failure guam Programming 4 04-13-2005 09:16 AM
pointers and dynamic memory allocation deveraux83 Programming 2 01-24-2004 10:35 AM
Dynamic Memory Allocation query dhanakom Programming 2 07-21-2003 02:19 PM


All times are GMT -5. The time now is 11:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration