Q1,Tip: Find the IP address of a virtual machine
libvirt: fetch ipv4 address from guest

I’m installing avahi on each VM, so they will advertise their own addresses. However that’s not the only option available (especiall if you VM contains something different from Linux). So enter magical world of virsh options!

*) First you need to get MAC addresses of your VM’s NICs:

[root@5844 ~]# virsh domiflist b2bua
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      network    default    virtio      52:54:00:aa:bb:cc
vnet1      bridge     br1        virtio      52:54:00:dd:ee:ff

[root@5844 ~]#

*) Now let’s take a look at the ARP table

[root@5844 ~]# arp -e
 Address                  HWtype  HWaddress           Flags Mask            Iface
 xx.xx.xx.xx              ether   xx:xx:xx:xx:xx:xx   C                     br0
 192.168.122.14           ether   52:54:00:xx:xx:xx   C                     virbr0
 192.168.122.51           ether   52:54:00:aa:bb:cc   C                     virbr0
 [root@5844 ~]#

*) Now let’s glue everything together (and adding a bit of shell/regex magic):

[root@5844 ~]# for mac in `virsh domiflist b2bua |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do arp -e |grep $mac  |grep -o -P "^d{1,3}.d{1,3}.d{1,3}.d{1,3}" ; done
192.168.122.51
[root@5844 ~]#

 

http://www.naturalborncoder.com/virtualization/2014/10/27/installing-and-running-kvm-on-ubuntu-14-04-part-2/

auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
auto br0
iface br0 inet dhcp
 pre-up ip link add br0 type bridge
 pre-up ip link set dev br0 address 02:0e:89:06:d9:0f
 pre-up ip link set eth0 master br0
 post-down ip link set eth0 nomaster
 post-down ip link delete br0
 bridge_stp off
 bridge_fd 0
 bridge_maxwait 0

 

https://www.centos.org/docs/5/html/5.2/Virtualization/chap-Virtualization-Managing_guests_with_virsh.html

Managing virtual networks

This section covers managing virtual networks with the virsh command. To list virtual networks:

virsh net-list

This command generates output similar to:

[root@domain ~]# virsh net-list
Name                 State      Autostart
-----------------------------------------
default              active     yes      
vnet1	             active     yes      
vnet2	             active     yes

To view network information for a specific virtual network:

virsh net-dumpxml [vnet name]

This displays information about a specified virtual network in XML format:

	# virsh net-dumpxml vnet1
<network>
  <name>vnet1</name>
  <uuid>98361b46-1581-acb7-1643-85a412626e70</uuid>
  <forward dev='eth0'/>
  <bridge name='vnet0' stp='on' forwardDelay='0' />
  <ip address='192.168.100.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.100.128' end='192.168.100.254' />
    </dhcp>
  </ip>
</network>

Other virsh commands used in managing virtual networks are:

  • virsh net-autostart [network name] — Autostart a network specified as [network name]

  • virsh net-create [XML file] — Generates and starts a new network using a preexisting XML file

  • virsh net-define [XML file] — Generates a new network from a preexisting XML file without starting it

  • virsh net-destroy [network name] — Destroy a network specified as [network name]

  • virsh net-name [network UUID] — Convert a specified [network UUID] to a network name

  • virsh net-uuid [network name — Convert a specified [network name] to a network UUID

  • virsh net-start [name of an inactive network] — Starts a previously undefined inactive network

  • virsh net-undefine [name of an inactive network] — Undefine an inactive network

 

补充:

bridge-utils
The bridge-utils package contains a utility needed to create and manage bridge devices. This is useful in setting up networks for a hosted virtual machine (VM).
http://www.linuxfromscratch.org/blfs/view/svn/basicnet/bridge-utils.html

Open vSwitch to gain network virtualization
http://www.networkworld.com/article/2870964/cisco-subnet/open-vswitch-to-gain-network-virtualization.html
http://www.slideshare.net/teyenliu/the-basic-introduction-of-open-vswitch