第一步:宿主机配置双网桥
实验环境:ubuntu16的操作系统
1、编辑配置文件,配置双网桥
liu@liu:~$ sudo vim /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
#auto ens33
#iface ens33 inet manual
auto br0
iface br0 inet dhcp
# address 192.168.254.128
# netmask 255.255.255.0
# broadcast 192.168.254.255
# gateway 192.168.254.2
# dns-nameservers 8.8.8.8
bridge_ports ens33
bridge_stp off
bridge_fd 0
bridge_maxwait 0
#auto ens38
#iface ens38 inet manual
auto br1
iface br1 inet dhcp
# address 192.168.72.129
# netmask 255.255.255.0
# broadcast 192.168.72.255
# gateway 192.168.72.2
# dns-nameservers 8.8.8.8
bridge_ports ens38
bridge_stp off
bridge_fd 0
bridge_maxwait 0
具体配置,根据网络环境的不同,进行内外网桥的搭建,此处就是简单的两个动态网桥,静态也是可行的。宿主机所在网络环境须有dhcp服务
br0 Link encap:Ethernet HWaddr 00:0c:29:b9:00:a2
inet addr:192.168.254.129 Bcast:192.168.254.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb9:a2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:214 errors:0 dropped:0 overruns:0 frame:0
TX packets:83 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:19040 (19.0 KB) TX bytes:8819 (8.8 KB)
br1 Link encap:Ethernet HWaddr 00:0c:29:b9:00:ac
inet addr:192.168.254.128 Bcast:192.168.254.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb9:ac/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:143 errors:0 dropped:0 overruns:0 frame:0
TX packets:57 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12212 (12.2 KB) TX bytes:6915 (6.9 KB)
第二步:虚拟机xml文件中配置双网桥
此处的xml文件为模板文件中的一部分
<interface type='bridge'>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'>
<source bridge='br1'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
</interface>
模板镜像文配置了qemu-agent-guest的Ubuntu16的镜像
具体配置参考:https://blog.csdn.net/qq_42388880/article/details/106359741
第三步:python代码调用libvirt获取双ip
def getVMIp(domain):
# 获取虚拟机ip domain为虚拟机实例
count = 1
ip_list = []
vmip = None
# while vmip == None:
while count <= 2:
try:
time.sleep(5)
ifaces = domain.interfaceAddresses(libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT, 0)
if ifaces:
print(ifaces)
for (name, val) in ifaces.items():
if name == 'lo':
continue
if val['addrs']:
for ipaddr in val['addrs']:
if ipaddr['type'] == libvirt.VIR_IP_ADDR_TYPE_IPV4:
vmip = ipaddr['addr']
print(" VIR_IP_ADDR_TYPE_IPV4 = " + vmip)
if len(ip_list) == 0:
ip_list.append(vmip)
count = count + 1
elif ip_list[0] != vmip:
ip_list.append(vmip)
count = count + 1
# return vmip
if count == 3:
return ip_list[0], ip_list[1]
if count ==2 and ip_list[0] == vmip:
return ip_list[0], vmip
except:
print('WAIT:VM IP is not obtained...')
ifaces的一个实例:
{‘lo’: {‘addrs’: [{‘addr’: ‘127.0.0.1’, ‘prefix’: 8, ‘type’: 0}, {‘addr’: ‘::1’, ‘prefix’: 128, ‘type’: 1}], ‘hwaddr’: ‘00:00:00:00:00:00’}, ‘ens3’: {‘addrs’: [{‘addr’: ‘192.168.254.138’, ‘prefix’: 24, ‘type’: 0}, {‘addr’: ‘fe80::ef8b:b8fd:5d1d:6842’, ‘prefix’: 64, ‘type’: 1}], ‘hwaddr’: ‘52:54:00:49:fb:d3’}, ‘ens7’: {‘addrs’: [{‘addr’: ‘192.168.254.137’, ‘prefix’: 24, ‘type’: 0}, {‘addr’: ‘fe80::fb6:2a69:5e28:f15’, ‘prefix’: 64, ‘type’: 1}], ‘hwaddr’: ‘52:54:00:3a:77:98’}}
输出结果可以看到:
VIR_IP_ADDR_TYPE_IPV4 = 192.168.254.138
VIR_IP_ADDR_TYPE_IPV4 = 192.168.254.137
已成功获取到两个ip
第四步:虚拟机内部验证结果
如图:虚拟机生成了两张网卡,绑定在不同网桥
可以看出ens3获取到对应网桥br0的ip,ens7获取到对应网桥br1的ip
代码先获取到的也是br0的IP,其次获取br1的IP,和虚拟机模板配置文件顺序一致。
到此,可以实现虚拟机配置内外网络