qemu用tap方式启动vm的网络试验(ip route)

ip route add 192.168.8.0/24 via 192.168.137.223


用qemu启动虚拟机:
/usr/libexec/qemu-kvm -kernel bzImage -drive file=hda.img,if=ide,cache=none -append "console=ttyS0 root=/dev/sda rw rdinit=/sbin/init notsc=1"  -nographic -boot order=dc,menu=on -net nic,vlan=0,macaddr=52:54:00:12:34:22,model=e1000,addr=08 -net tap,name=haha,ifname=tap1,script=no,downscript=no  

bzImage和hda.img自己去想办法吧,有个操作系统就行
这里关键地方是 -net nic -net tap,ifname=tap1 指定了tap1,和vm的网卡相连
建立桥并给桥设置IP
brctl addbr br0
brctl stp br0 on
ip link set br0 up
ifconfig br0 192.168.123.1 netmask 255.255.255.0 broadcast 192.168.123.255

启动dhcp的服务
dnsmasq --strict-order --except-interface=lo --interface=br0 --listen-address=192.168.123.1 --bind-interfaces  --dhcp-range=192.168.123.2,192.168.123.254 --conf-file=""  --pid-file=/var/run/qemu-dhcp-br0.pid  --dhcp-leasefile=/var/run/qemu-dhcp-br0.leases --dhcp-no-override

把tap绑定到桥上
brctl addif br0 tap1
ip link set tap1 up

这里最重要:设置ip的转发
iptables -t nat -A POSTROUTING -s "192.168.123.0/255.255.255.0" ! -d "192.168.123.0/255.255.255.0" -j MASQUERADE 
sysctl -w net.ipv4.ip_forward=1


进入刚起的vm,获取ip
dhclient
ping 114.114.114.114
可观察
root@(none):/# ip route
default via 192.168.123.1 dev eth0 
default dev sit0  scope link 
default dev lo  scope link 
192.168.123.0/24 dev eth0  scope link 
192.168.123.0/24 dev eth0  proto kernel  scope link  src 192.168.123.37 


qemu的操作
ctl+a x 退出
ctl+a c 切换monitor


################################################
不用dhcp,用ifconfig和route手动建立route的方式

主机
brctl addbr br0
ip link set br0 up
ip link set tap1 up
brctl addif br0 tap1
ifconfig br0 192.168.124.1 netmask 255.255.255.0 broadcast 192.168.124.255
iptables -t nat -L
iptables -t nat -A POSTROUTING -s "192.168.124.0/255.255.255.0" ! -d "192.168.124.0/255.255.255.0" -j MASQUERADE 


客户端手动建ip
ip link set eth0 up
ifconfig eth0 192.168.124.2 netmask 255.255.255.0 broadcast 192.168.124.255
route add default gw 192.168.124.1 dev eth0



############################################
不用dhcp,用ip addr和 ip route手动建立route的方式

brctl addbr br0
ip link set br0 up
ip link set tap1 up
brctl addif br0 tap1
ip addr add 10.3.0.1/24 dev br0

brctl show
ip addr show br0

iptables -t nat -A POSTROUTING -s "10.3.0.0/24" ! -d "10.3.0.0/24" -j MASQUERADE 

进入vm
ip link set eth0 up
ip addr add 10.3.0.2/24 dev eth0

ip route add default via 10.3.0.1 
ping 114.114.114.114


############################
备注:
可能会用到
ifconfig br0 promisc up 
ifconfig eth0 promisc up
##########
ip route参考:
http://www.mamicode.com/info-detail-1412618.html
http://www.cnblogs.com/sammyliu/p/4713562.html

0#表: 系统保留表
253#表: default table 没特别指定的默认路由都放在改表
254#表: main table 没指明路由表的所有路由放在该表
255#表: locale table 保存本地接口地址,广播地址、NAT地址 由系统维护,用户不得更改

ip route list table 253
ip route list table main


cat /etc/iproute2/rt_tables

via是下一跳

ip route add default via 10.3.0.1 table 1 在一号表中添加默认路由为192.168.1.1
ip route add 192.168.0.0/24 via 192.168.1.2 table 1 在一号表中添加一条到192.168.0.0网段的路由为192.168.1.2


参考
http://haoningabc.iteye.com/blog/2306952
tap的方式使用qemu建立虚拟机NAT网络

猜你喜欢

转载自haoningabc.iteye.com/blog/2324350
今日推荐