bridge方式配置libvirt网络
1、network.xml
<network> <name>local</name> <bridge name="local"/> <forward mode="route" dev="eth0"/> <ip address="192.168.0.204" netmask="255.255.255.0"> <dhcp> <range start="192.168.0.1" end="192.168.0.253"/> </dhcp> </ip> </network>
其中192.168.0.204是host的ip,网关为192.168.0.254
root@204:/opt# virsh net-define network.xml root@204:/opt# virsh net-autostart local root@204:/opt# virsh net-start local
如果报错:
error: Failed to create network from network.xml
error: internal error Network is already in use by interface eth0
先 ifconfig eth0 down
2、修改 /etc/network/interface,只保留 lo
auto lo iface lo inet loopback dns-nameservers 192.168.0.254
3、开机自动启动脚本:
#!/bin/sh sleep 3 ifconfig eth0 up brctl add local eth0 route del default route add default gw 192.168.0.254 metric 0 dev local脚本加入/etc/rc.local中
4、重启机器 reboot,即可看到:
root@204:~# virsh net-list --all Name State Autostart ----------------------------------------- default active yes local active yes
eth0 Link encap:Ethernet HWaddr 00:1e:68:04:74:68 inet6 addr: fe80::21e:68ff:fe04:7468/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3085 errors:0 dropped:0 overruns:0 frame:0 TX packets:248 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:491876 (491.8 KB) TX bytes:43962 (43.9 KB) Interrupt:18 Memory:fd9e0000-fda00000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:167 errors:0 dropped:0 overruns:0 frame:0 TX packets:167 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:8376 (8.3 KB) TX bytes:8376 (8.3 KB) local Link encap:Ethernet HWaddr 00:1e:68:04:74:68 inet addr:192.168.0.204 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1308 errors:0 dropped:0 overruns:0 frame:0 TX packets:238 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:132498 (132.4 KB) TX bytes:41842 (41.8 KB) virbr0 Link encap:Ethernet HWaddr ea:44:0a:49:d4:50 inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
root@204:/opt# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.0.254 0.0.0.0 UG 0 0 0 local 192.168.0.0 * 255.255.255.0 U 0 0 0 local 192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0 root@204:/opt#
虚拟机就可以使用和主机在同一个网段的ip了,在domain的配置文件中interface的配置段:
<interface type='bridge'> <source bridge='local' /> <mac address="00:2c:be:1a:fa:80"/> <model type="virtio" /> </interface>