XEN環境中的VLAN實現

上次和朋友一塊兒探討xen中的一個問題。基本狀況是這樣的,公司適用xen虛擬機作VPS,因爲單臺機器中的VPS數量比較多,因此存在幾個公司同時使用一臺物理機中的VPS的狀況。因爲默認狀況下,VPS所處的網絡環境是同樣的,就是同屬於一個網段,這樣安全方面存在問題,好比會發生arp***之類,因此須要能隔絕開不一樣單位的VPS間的通信。這樣想到了使用VLAN的方式,即不一樣的公司用的VPS都橋接到不一樣的網橋上,同時,在該vif上啓用8021q,這樣即可以完美解決該問題了。參考了網上一些文檔,確實有一些解決方法,可是都是修改配置文件的方式,這樣確實能解決該問題,可是每每須要重啓xend,或是重啓物理機,這對於生產環境來講是不可能的,因此,使用如下方法完美解決該問題。
cd /etc/sysconfig/network-scripts
touch ifcfg-xenbr2 ifcfg-eth0.2
ifcfg-xenbr1 ifcfg-eth0.1的內容以下:
[root@XenServer network-scripts]# cat ifcfg-xenbr2
DEVICE=xenbr2
BOOTPROTO=static
ONBOOT=yes
TYPE=Bridge
[root@XenServer network-scripts]# cat ifcfg-eth0.2
DEVICE=eth0.2
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
VLAN=yes
BRIDGE=xenbr2
建立了這兩個文件後,依次使用命令:
ifup ifcfg-xenbr2
ifup ifcfg-eth0.2
這樣就能夠首先建立一個叫xenbr2的網橋,而後,把eth0.2橋接到該往橋上,因此,啓動的順序不可反過來。
而後,在domu的配置中,使用網橋的配置改成:
vif = [ "bridge=xenbr1,script=vif-bridge" ]

不過這時候別忘記在物理端口直連的交換機端口上開啓trunk,使用8021q的標籤。
Enjoy it!php

附上其餘的解決方法

xen 3.0.3 - create 3 alias with the only one NIC and 3 vlan with each alias interface

Preparation

Install Vlan Config tool

yum install vconfig


modprobe 8021q

vi /etc/sysconfig/modules/8021q.modules
modprobe 8021q

chmod a+x /etc/sysconfig/modules/8021q.modules


create 3 alias with the only one NIC

copy configuration file

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.2
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.3
cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.4


edit example

vi /etc/sysconfig/network-scripts/ifcfg-eth0.4
DEVICE=eth0.4
BOOTPROTO=static
DHCPCLASS=
IPADDR=10.4.100.73
NETMASK=255.255.255.0
VLAN=yes
ONBOOT=yes


Announcements:each alias interface does not need to config ip address,the purpose for that just want to test whether it works.eth0 should not have any ip address(i.e. the same network segment as the default gateway ),because the other network segment will match the rule (10.1.100.0 to eth0),and by default the bridges created by xen

cannot talk with each other.
安全

create custom script for vlan

main function script

cp /etc/xen/scripts/network-bridge /etc/xen/scripts/network-bridge-vlan
vi /etc/xen/scripts/network-bridge-vlan
#DL# if is_bonding ${netdev} || ! ifdown ${netdev}; then
# Remember the IP details if necessary.
get_ip_info ${netdev}
ip link set ${netdev} down
ip addr flush ${netdev}
#DL# fi
#DL# if ! ifdown ${netdev}; then
get_ip_info ${netdev}
#DL# fi
chmod a+x /etc/xen/scripts/network-bridge-vlan


create main script for load

vi /etc/xen/scripts/network-bridge-withvlan
#!/bin/sh
function call_network_bridge
{
dir=/etc/xen/scripts
"$dir/network-bridge-vlan" "$@" netdev=eth0 vifnum=0 bridge=xenbr0
"$dir/network-bridge-vlan" "$@" netdev="eth0.2" vifnum=1 bridge=xenbr0V2
"$dir/network-bridge-vlan" "$@" netdev="eth0.3" vifnum=2 bridge=xenbr0V3
"$dir/network-bridge-vlan" "$@" netdev="eth0.4" vifnum=3 bridge=xenbr0V4
}

case "$1" in

start)
echo "start"
vconfig set_name_type DEV_PLUS_VID_NO_PAD
vconfig add "eth0" 2
vconfig add "eth0" 3
vconfig add "eth0" 4
call_network_bridge start
;;
stop)
echo "stop"
call_network_bridge stop
vconfig rem "eth0.2"
vconfig rem "eth0.3"
vconfig rem "eth0.4"
;;
esac
chmod a+x /etc/xen/scripts/network-bridge-withvlan


Change xend config

vi /etc/xen/xend-config.sxp
(network-script network-bridge-withvlan)


to validate mentioned in inhttp://wiki.xensource.com/xenwiki/XenNetworking網絡

[root:/etc/xen]# cat /etc/init.d/xen-vlan #!/bin/sh ethtool -K eth0 tx off
相關文章
相關標籤/搜索