socket的方式使用qemu建立虚拟机

qemu的-net参数
-net nic 必须有,基本配置model=网卡类型,
-net tap 使用tap设备
参考

NAT的方式使用qemu建立虚拟机: http://haoningabc.iteye.com/blog/2306952
桥接的方式使用qemu建立虚拟机: http://haoningabc.iteye.com/blog/2306736

-net user 使用qemu自身实现

-net socket 两种 tcp方式和udp的方式
tcp使用listen=192.168.139.85:1234
其他vm使用 connect=192.168.139.85:1234
udp的方式使用多播地址 mcast=224.0.0.5:1234
在IPv4中,它历史上被叫做D类地址,一种类型的IP地址,它的范围从224.0.0.0到239.255.255.255,或,等同的,在224.0.0.0/4。在IPv6,多播地址都有前缀ff00::/8。但是注意,224.0.0.0被保留,不能赋给任何多播组。
########################
例子:
使用tcp的方式:
一个vm使用-net socket,listen=
qemu-system-i386 -kernel /root/jslinux/obj/linux-x86-basic/arch/i386/boot/bzImage -drive file=hda_hasbacking_file.qcow2,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 socket,name=mysock1,listen=192.168.139.85:1234

其他vm使用-net socket,connect=
qemu-system-i386 -kernel /root/jslinux/obj/linux-x86-basic/arch/i386/boot/bzImage -drive file=hda_hasbacking_file1.qcow2,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:23,model=e1000,addr=08 -net socket,name=mysock2,connect=192.168.139.85:1234 

在mysock1的vm中
ip link set eth0 up
ifconfig eth0 10.0.2.2 netmask 255.255.255.0

在mysock2的vm中
ip link set eth0 up
ifconfig eth0 10.0.2.3 netmask 255.255.255.0

测试可以互相ping通
切换到monitor模式
ctrl+a c
(qemu) info network
hub 0
 \ hub0port1: mysock1: index=0,type=socket,socket: connection from 192.168.139.85:42941
 \ hub0port0: e1000.0: index=0,type=nic,model=e1000,macaddr=52:54:00:12:34:22
(qemu) 


使用udp的方式:

多台机器可以使用不同的"-net nic,macaddr="和"-net socket,name="
qemu-system-i386 -kernel /root/jslinux/obj/linux-x86-basic/arch/i386/boot/bzImage -drive file=hda_hasbacking_file.qcow2,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 socket,name=mysock1,mcast=192.168.139.85:1234

ip设置同上,也可以ping通
切换monitor模式
(qemu) info network
hub 0
 \ hub0port1: mysock1: index=0,type=socket,socket: mcast=224.0.0.5:1234
 \ hub0port0: e1000.0: index=0,type=nic,model=e1000,macaddr=52:54:00:12:34:22
(qemu) 

猜你喜欢

转载自haoningabc.iteye.com/blog/2307173