[libvirt] How to get the IP address of a Domain?
Richard W.M. Jones
rjones at redhat.com
Wed Mar 30 20:18:10 UTC 2011
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/
More information about the libvir-list
mailing list