Monday, July 13, 2009

VirtualBox下VM使用独立IP

为在VirtualBox下的VM使用独立IP, 在内核需要增加如下选项:

Linux Kernel Configuration: 802.1d Support
Networking --->
Networking Options --->
<*> 802.1d Ethernet Bridging

Linux Kernel Configuration: 802.1d Support
Networking --->
Networking Options --->
<*> 802.1d Ethernet Bridging

安装的软件:
app-emulation/virtualbox-bin-2.2.0
app-emulation/virtualbox-modules-2.2.0
net-misc/bridge-utils-1.4

在没有操作前, 有如下正在使用的网络设备:
jessinio@niolaptop /etc/runlevels $ ifconfig -s
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 5283 0 0 0 5444 0 0 0 BMRU
lo 16436 0 2 0 0 0 2 0 0 0 LRU

而共有的网络设备如下:
jessinio@niolaptop /etc/runlevels $ ifconfig -a -s
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0 1500 0 5299 0 0 0 5461 0 0 0 BMRU
lo 16436 0 2 0 0 0 2 0 0 0 LRU
sit0 1480 0 0 0 0 0 0 0 0 0 O
wlan0 1500 0 0 0 0 0 0 0 0 0 BM
wmast 0 0 0 0 0 0 0 0 0
0 [NO FLAGS]


不知道什么跑出了一个sit0的网卡, 用法未知, 只看到IPv6-in-IPv4

现在还没有任何的bridge:
jessinio@niolaptop /etc/runlevels $ brctl show
bridge name bridge id STP enabled interfaces

配置bridge如下:
jessinio@niolaptop /etc $ sudo /sbin/brctl addbr br0
jessinio@niolaptop /etc $ /sbin/brctl addif br0 eth0
jessinio@niolaptop /etc $ sudo /sbin/brctl addif br0 eth0
jessinio@niolaptop /etc $ sudo /sbin/brctl stp br0 on
jessinio@niolaptop /etc $ sudo brctl show
bridge name bridge id STP enabled interfaces
br0 8000.001d606fd392 yes eth0

为br0网络设备分配IP:
jessinio@niolaptop /etc $ sudo dhcpcd br0
br0: dhcpcd 4.0.13 starting
br0: broadcasting for a lease
br0: offered 192.168.0.101 from 192.168.0.1
br0: acknowledged 192.168.0.101 from 192.168.0.1
br0: checking 192.168.0.101 is available on attached networks
br0: lease of 4294967295 would overflow, treating as infinite
br0: leased 192.168.0.101 for infinity

这时在host中测试网络正常, 转到配置VM:
为virtualbox加入Modules:
# modprobe vboxdrv
# modprobe vboxnetflt

加入后, 如下:
jessinio@niolaptop /etc $ sudo lsmod |less
Module Size Used by
vboxnetflt 64888 1
vboxdrv 89000 2 vboxnetflt


这时VM的settings如下:
* NetWork -> Attached to: -> Bridge NetWork, adapter为eth0

这时重启VM, 使用dhclient分配到IP!

这时主机内的网络配置如下:

jessinio@niolaptop /etc $ ifconfig -a -s
Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
br0 1500 0 1649 0 0 0 1198 0 0 0 BMRU
eth0 1500 0 9214 0 0 0 8809 0 0 0 BMRU
lo 16436 0 69 0 0 0 69 0 0 0 LRU
sit0 1480 0 0 0 0 0 0 0 0 0 O
vboxn 1500 0 0 0 0 0 0 0 0 0 BM
wlan0 1500 0 0 0 0 0 0 0 0 0 BM
wmast 0 0 0 0 0 0 0 0 0
0 [NO FLAGS]

多出了一个vboxnet0(vboxn)


参考文档:
* http://en.wikipedia.org/wiki/Network_bridge
* http://www.gentoo-wiki.info/VirtualBox
* http://www.gentoo-wiki.info/Bridging

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.