qemu tap - fun - fun

qemu tap

abelard posted @ 2011年11月28日 22:27 in linux系统管理 , 5153 阅读

说明:[主机操作系统为 Fedora 16 x86_64]使用qemu的网桥模式,这样由qemu创建的guest os和 主机操作系统在同一个局域网内,即局域网内的所有主机都可以访问 该guest os。当然由于网桥可以在直接网桥方式和NAT/Masquerading 方式下工作,因此guest 和 host可以在同一个子网内(直接桥接方 式)也可以将guest隐藏起来,不与host在同一个子网,如下图 (http://en.gentoo-wiki.com/wiki/KVM#Networking_2)。

直接网桥模式:

                     host   
          +-----------------------+
          |                       |        KVM GUEST1
          |  +-----------+        |     +--------------+ 
LAN ------+--+---  eth0  |   +----------+---  nic0     |      
          |  |     tap0 -----+    |     | 192.168.0.83 |      KVM GUEST2         
          |  |     tap1 -----+    |     +--------------+  +--------------+
          |  +-----------+   |    |                       |              |
          |          br0     +----+-----------------------+---- nic0     |
          |    192.168.0.88       |                       | 192.168.0.84 |
          +-----------------------+                       +--------------+

NAT/Masquerading (网络地址转换/隐藏)网桥模式:

                     host   
          +-----------------------+
          |                       |        KVM GUEST1
          |   192.168.1.88        |     +--------------+ 
LAN ------+-----  eth0            |     |              |
          |  +-----------+   +----+-----+-----nic0     |   
          |  |     tap0 -----+    |     | 192.168.0.83 |      KVM GUEST2         
          |  |     tap1 -----+    |     +--------------+  +--------------+
          |  +-----------+   |    |                       |              |
          |          br0     +----+-----------------------+---- nic0     |
          |    192.168.0.88       |                       | 192.168.0.84 |
          +-----------------------+                       +--------------+

第一步:安装和设置网桥(参考Virtualization With KVM On A Fedora 14 Server)

  1. 安装网桥和准备工作
yum install bridge-utils

为了让网桥顺利工作,需要关闭Network Manager,因为Network Manager好像不 支持网桥启动,可以先查看Network Manager是否启动,由于从Fedora14以后, 采用了Systemd 作为linux的系统和服务管理器,因此查看和关闭服务使用:

systemctl status networkmanager.service
systemctl stop networkmanager.service

永久关闭该服务使用:

chkconfig NetworkManager off
chkconfig --levels 35 network on

这时候重启网络,看看什么情况

systemctl restart network.service
  1. 开始配置网卡和网桥

/etc/sysconfig/network-scripts/ifcfg-p4p1的内容如下:重点需要关注的是 这里的NMCONTROLLED="no",而不是"yes",就是说要关闭network manager,另 外就是加上了BRIDGE=br0。 (至于为什么是p4p1 不是eth0等,那是fedora新版本采用的名称)

DEVICE=p4p1
#BOOTPROTO=static
ONBOOT=yes
NM_CONTROLLED="no"
TYPE=Ethernet
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System p4p1"
UUID=5dd47203-fffb-671a-4fd0-4cff98347a3b
HWADDR=00:25:64:8E:58:8C
PREFIX0=24
BRIDGE=br0

还需要配置网桥,使用/etc/sysconfig/network-scripts/ifcfg-br0文件来完成, 可以看到该文件中配置了ip,gateway,dns等

DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.0.88
NETMASK=255.255.255.0
GATEWAY=192.168.0.1
DNS1=192.168.0.1
DELAY=0
STP=off

这时候重启网络

systemctl restart network.service

正常的情况下应该看到如下信息:

[abelard@localhost ~]$ ifconfig
br0       Link encap:Ethernet  HWaddr 00:25:64:8E:58:8C  
          inet addr:192.168.0.88  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::225:64ff:fe8e:588c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:11611 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10348 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:8935226 (8.5 MiB)  TX bytes:1445532 (1.3 MiB)

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:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:480 (480.0 b)  TX bytes:480 (480.0 b)

p4p1      Link encap:Ethernet  HWaddr 00:25:64:8E:58:8C  
          inet6 addr: fe80::225:64ff:fe8e:588c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12061 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10384 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:9225251 (8.7 MiB)  TX bytes:1497788 (1.4 MiB)
          Interrupt:16 

可以使用nslookup www.google.com命令看看是否有问题!

第二步,创建tap设备 有了网桥以后,先使用

[abelard@localhost ~]$ sudo lsmod | grep tun

如果没有任何信息,使用

[abelard@localhost ~]$ sudo modprobe tun
[abelard@localhost ~]$ sudo lsmod | grep tun
tun                    14111  0 

如果还没有信息,就需要google一下,查找怎么yum一个tun模块。有信息的话, 就继续。 使用tunctl命令创建tap设备,并将tap设备添加到网桥br0中,

[abelard@localhost ~]$ sudo tunctl -b  -t tap0
[abelard@localhost ~]$ brctl addif br0 tap0
[abelard@localhost ~]$ sudo ifconfig tap0 up
[abelard@localhost ~]$ ifconfig
br0       Link encap:Ethernet  HWaddr 00:25:64:8E:58:8C  
          inet addr:192.168.0.88  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::225:64ff:fe8e:588c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12165 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10867 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:9116034 (8.6 MiB)  TX bytes:1529806 (1.4 MiB)

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:8 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:480 (480.0 b)  TX bytes:480 (480.0 b)

p4p1      Link encap:Ethernet  HWaddr 00:25:64:8E:58:8C  
          inet6 addr: fe80::225:64ff:fe8e:588c/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:12677 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10903 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:9431240 (8.9 MiB)  TX bytes:1584348 (1.5 MiB)
          Interrupt:16 

tap0      Link encap:Ethernet  HWaddr 26:A1:36:5F:18:64  
          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:500 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

这时候,网桥和tap设备都可以工作了,意味着qemu的网桥模式可以开始。

第三步 在qemu中使用网桥模式

使用过两种方式,

(1) 不设置启动脚本

sudo qemu-kvm -hda CentOs5.7.img -net nic,macaddr=00:1d:92:ab:3f:78 -net tap,ifname=tap0,script=no,downscript=no -boot c

这种方式,因为没有用到启动脚本(script=no,downscript=no),因此需要手动来使用上面的命令:

[abelard@localhost ~]$ brctl addif br0 tap0
[abelard@localhost ~]$ sudo ifconfig tap0 up

(2) 设置qemu网络启动脚本/etc/qemu-ifup

#!/bin/sh
brctl addif br0 $1
ifconfig $1 up 0.0.0.0 promisc

使用以下命令就可以启动guest os,并且可以在同一个子网中访问这个guest(192.168.0.81) 了,

sudo qemu-kvm -hda CentOs5.7.img -net nic,macaddr=00:1d:92:ab:3f:78 -net tap,ifname=tap0  -boot c

sudo qemu-kvm -hda CentOs5.7.img -net nic,macaddr=00:00:00:00:00:00 -net tap,ifname=tap0 -boot c

sudo qemu-kvm -hda CentOs5.7.img -net nic -net tap,ifname=tap0 -boot c

 

Avatar_small
jiamo 说:
2012年2月16日 00:03

如果非要使用Network manager 呢?
还有一点。 你的p4p1 和 br0 的mac地址是一样的。
这个怎么解释。

Avatar_small
MTNL Duplicate Bill 说:
2023年2月03日 19:03

Mahanagar Telecom Nigam Limited does give a variety of options to its customers which can be availed online for MTNL Mumbai bill payment and for a plan change through the dedicated portal which customers of the zone can easily track their bills and service records. MTNL Duplicate Bill It makes it easy for customers to track their dues and as well select a better plan in future days, and the switching of plans is far east through the Online MTNL Mumbai web portal.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee