如何设置xen桥?

我一直在阅读有关如何为xen设置网络的各种页面 。 不幸的是,它们中没有一个实际上有完整的示例配置。 它们清楚地显示了xenbr0部分应该是什么样子,但是在提到之后你不应该如何改变eth0:

注意! 桥接设备的IP配置应替换底层接口的IP配置,即从eth0中删除IP设置并将其移至桥接接口。 eth0将完全作为桥接器的物理上行链路运行,因此不能对其进行任何IP(L3)设置!

我已经尝试了许多失败的配置(运行/etc/init.d/networking重启后,没有正常的netowork访问权限,无法进入或退出)。

这是我当前的配置:

auto lo iface lo inet loopback auto xenbr0 iface xenbr0 inet static bridge_ports eth0 address 10.0.0.3 netmask 255.0.0.0 broadcast 10.255.255.255 gateway 10.0.0.1 auto eth0 iface eth0 inet manual 

也许这是正确的,我只需要设置一些iptables转发规则? 我尝试运行命令sudo iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT但我收到了一条错误消息–physdev-is-bridged不是公认的选项。

重新启动网络的调试输出提供以下输出:

 Reconfiguring network interfaces... Waiting for xenbr0 to get ready (MAXWAIT is 32 seconds). RTNETLINK answers: No such process Failed to bring up xenbr0 ssh stop/waiting ssh start/running, process 3775 

我已经检查过xenbr0已经存在,因为当我尝试创建一个具有该名称的桥时,brctl告诉我它不能创建一个已存在的桥。

最后,我最终创建了一个接口,并通过一些iptables规则在其上转发数据包,这似乎对我有用。 这不是使用所有教程似乎建议的’bridge’选项,所以我不知道是否存在致命缺陷?

 auto lo iface lo inet loopback auto xenbr0 iface xenbr0 inet static bridge_ports none address 192.168.2.1 netmask 255.255.255.0 network 192.168.2.0 broadcast 192.168.2.255 gateway 10.0.0.3 # The primary network interface auto eth0 iface eth0 inet static address 10.0.0.3 netmask 255.0.0.0 network 10.0.0.0 broadcast 10.255.255.255 gateway 10.0.0.1 # dns-* options are implemented by the resolvconf package, if installed dns-nameservers 8.8.8.8 

您需要编辑/etc/sysctl.conf并取消注释以下行:

 net.ipv4.ip_forward=1 

然后你需要创建一个脚本来编辑iptables来转发数据包:

 sudo /sbin/iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE sudo /sbin/iptables --append FORWARD --in-interface xenbr0 -j ACCEPT return 0 

然后,您需要确保rc.local文件调用该脚本:

 sudo vi /etc/rc.local 

添加以下行:

 /bin/sh  

然后重新启动以使所有设置生效。

正如您可能注意到的那样,我将其设置为使得虚拟机使用192.168.2.x地址子网,而外部局域网使用10.xxx,这可能与大多数人想要的不同,因此您必须将这些编辑为你自己的个人需求。


更新
后来我意识到缺乏桥接意味着我无法从网络外部访问我的虚拟机(即我不能直接从家里ssh到它们,或者从它们运行网站等)

使用如此工作的网络配置:

 # The loopback network interface auto lo iface lo inet loopback auto eth0 iface eth0 inet manual auto xenbr0 iface xenbr0 inet static address 23.29.115.142 netmask 255.255.255.248 network 23.29.115.136 broadcast 23.29.115.143 gateway 23.29.115.137 bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0 

(从这里复制)

我猜这些额外的桥接选项使它工作,或者可能是接口在文件中列出的顺序(这次是桥接之前的eth0)

首先定义eth0, 而不设置网关和IP。 (否则当系统尝试为接口创建路由时,您将有“RTNETLINK答案:文件存在”错误,因为网桥将尝试创建具有相同优先级和网关的路由,并且它不够聪明,无法实现它们’反正完全相同。)

 auto lo iface lo inet loopback auto eth0 iface eth0 inet manual auto xenbr0 iface xenbr0 inet static address 192.168.1.10 netmask 255.255.255.0 gateway 192.168.1.1 bridge_ports eth0 #allow-hotplug xenbr0 #Uncomment if using vSwitches 

或者,您的网桥可以使用DHCP:

 auto lo iface lo inet loopback auto eth0 iface eth0 inet manual auto xenbr0 iface xenbr0 inet dhcp bridge_ports eth0 #allow-hotplug xenbr0 #Uncomment if using vSwitches 

完成后,重启。 否则,由于您一直在更改eth0但未设置新IP,因此即使您使用ifup命令/重新启动网络,您的网桥也可能无法正常运行。 这是因为eth0可能会意外地保留其IP地址。

最后,将客户操作系统的网络接口配置为网络上的任何其他物理主机。 (使用示例1,您可以使用192.168.1.11。)此时,网络上的其他设备应该能够访问来宾。

 ping 192.168.1.11 

不需要iptables或IP转发(sysctl.conf)。 当您的网络支持STP并且您需要避免第2层网络环路并且您不希望手动处理STP 时才需要STP。 (即小型网络不需要bridge_stp,bridge_fd或bridge_maxwait。)