[libvirt] How to get the IP address of a Domain?

HI,all: I'm new to libvirt, and have some questions about How to get the IP address of a Domain? I searched the old mails int the mail-list ,found one way to do this,it is first get the mac address of the Domain,then used some sniffer or arp tools to fetch the IP address , but: Q1: Is there any APIs of libvirt to get the domain's IP address directly? If does not exist now , have any plan to add this api or some extra tools to do this ,just like the vm-tools on VMware ESX? Q2: How to get the domain's mac? I download the virt-manager src, searching it's source to find how to do this : in details.py , this line seems to get the mac address: in function update_hwlist(hwtype, info, name, icon_name): Line 2475 for net in self.vm.get_network_devices(): mac = net.macaddr update_hwlist(HW_LIST_TYPE_NIC, net, "NIC %s" % mac[-9:], "network-idle") then ,I looked into domains.py, in function get_network_devices(): def get_network_devices(self, refresh_if_necc=True): return self._build_device_list("interface", refresh_if_necc) next, go to function _build_device_list() def _build_device_list(self, device_type, refresh_if_necc=True, inactive=False): guest = self._get_guest(refresh_if_necc=refresh_if_necc, inactive=inactive) devs = guest.get_devices(device_type) count = 0 for dev in devs: dev.vmmindex = count count += 1 return devs finally ,in funciton _get_guest(self, inactive=False, refresh_if_necc=True) def _get_guest(self, inactive=False, refresh_if_necc=True): raise NotImplementedError() this puzzled me , only raise a exception NotImplentedError(),but not deal with it . How to get the mac address? Sorry ,I 'm new to Python either... Really appreciate for your help! wade

Hello, Am Mittwoch 30 März 2011 12:10:18 schrieb 徐滕:
I'm new to libvirt, and have some questions about How to get the IP address of a Domain?
A domain does not have an IP address. A domain is equivalent to a PC in hardware, which might have none, 1 2 or more network cards, each one with it's own MAC address. This is the only propertpy of the hardware and can be configured via the domain XML description. What IP address your hosts uses is in complete control of your guest operating system: It might configure no IP addresses at all, use a mix of IPv4 and ipv4 addresses, use static assignment, or use external services like DHCP, but is completely independent from the hardware. So from libvirts point of view, your domain does not have an IP address. Sincerely Philipp Hahn -- Philipp Hahn Open Source Software Engineer hahn@univention.de Univention GmbH Linux for Your Business fon: +49 421 22 232- 0 Mary-Somerville-Str.1 D-28359 Bremen fax: +49 421 22 232-99 http://www.univention.de/

On Wed, Mar 30, 2011 at 01:43:37PM +0200, Philipp Hahn wrote:
Hello,
Am Mittwoch 30 März 2011 12:10:18 schrieb 徐滕:
I'm new to libvirt, and have some questions about How to get the IP address of a Domain?
A domain does not have an IP address. A domain is equivalent to a PC in hardware, which might have none, 1 2 or more network cards, each one with it's own MAC address. This is the only propertpy of the hardware and can be configured via the domain XML description. What IP address your hosts uses is in complete control of your guest operating system: It might configure no IP addresses at all, use a mix of IPv4 and ipv4 addresses, use static assignment, or use external services like DHCP, but is completely independent from the hardware. So from libvirts point of view, your domain does not have an IP address.
All of the above is absolutely true. Nevertheless you can probably get the IP address that a guest has chosen by reading out config files or (for the Windows) the Registry. Example with a Linux guest: # virt-cat RHEL60x64 /var/log/messages | grep 'dhclient.*bound to' Mar 30 19:56:22 rhel60x64 dhclient: bound to 192.168.122.220 -- renewal in 1527 seconds. Mar 30 20:21:49 rhel60x64 dhclient: bound to 192.168.122.220 -- renewal in 1375 seconds. Mar 30 20:44:44 rhel60x64 dhclient: bound to 192.168.122.220 -- renewal in 1287 seconds. Mar 30 21:06:11 rhel60x64 dhclient: bound to 192.168.122.220 -- renewal in 1461 seconds. For Windows: # virt-win-reg Win7x32 \ 'HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters' | \ grep DhcpIPAddress "DhcpIPAddress"=hex(1):31,00,39,00,32,00,2e,00,31,00,36,00,38,00,2e,00,31,00,32,00,32,00,2e,00,31,00,37,00,38,00,00,00 Windows notes: (1) The output is hex encoded UTF16-LE. Converting it to a printable string is left as an interesting exercise for the reader, but it is a dotted-quad IP address (192.168.122.178). (2) "ControlSet001" isn't exactly right there .. see the virt-win-reg man page for the full details. We could probably make a simple libguestfs-based tool that automated this for all the different guest types out there. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://et.redhat.com/~rjones/virt-df/

On 03/30/2011 02:18 PM, Richard W.M. Jones wrote:
A domain does not have an IP address. A domain is equivalent to a PC in hardware, which might have none, 1 2 or more network cards, each one with it's own MAC address. This is the only propertpy of the hardware and can be configured via the domain XML description. What IP address your hosts uses is in complete control of your guest operating system: It might configure no IP addresses at all, use a mix of IPv4 and ipv4 addresses, use static assignment, or use external services like DHCP, but is completely independent from the hardware. So from libvirts point of view, your domain does not have an IP address.
All of the above is absolutely true. Nevertheless you can probably get the IP address that a guest has chosen by reading out config files or (for the Windows) the Registry.
Also, it is possible to sniff network traffic to determine the IP address of a guest, and the nwfilter implementation uses just that. The documentation covers some of the details and limitations of nwfilter guessing an IP address based on traffic sniffing: http://libvirt.org/formatnwfilter.html#nwflimitsIP -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org
participants (4)
-
Eric Blake
-
Philipp Hahn
-
Richard W.M. Jones
-
徐滕