kqemu模块的加载、配置qemu使用tap/tun模式

1、kqemu模块的加载

在/etc/modules上增加kqemu,让系统启动时自动加载kqemu模块。

然后udev会自动生成一个叫做kqemu的设备:
xuwei@XIAO:~$ ls -l /dev | grep kqemu
crw-rw-rw- 1 root root 10, 63 2006-12-19 09:28 kqemu

2、配置qemu使用tap/tun模式

tap/tun模式需要内核支持tun模块,请先确认。

2.6.18 及以上版本内核,由于修改了对tun模块的执行权限,需要增加一个脚本。详细请参阅 bin/moin.cgi/FrequentlyAskedQuestions#head-989268518a820a9f33477d3b99754ab5113aa2a3。在此也把该faq贴过来。

I'm using a kernel >=2.6.18 and am having problems with tun/tap and qemu
Starting with 2.6.18 a user process must be root to manage tap interfaces. One way to make qemu behave as before is to wrap the process in a that manages the interface:

qemu-tap

#!/bin/sh# to manage tap interface allocation# for linux kernels >= 2.6.18# set up a tap interface for qemu# USERID - uid qemu is being run under.USERID=`whoami`iface=`sudo tunctl -b -u $USERID`# generate a random mac address for the qemu nic# shell borrowed from user pheldens @ qemu forumranmac=$(echo -n DE:AD:BE:EF ; for i in `seq 1 2` ; \do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done)# specify which NIC to use - see qemu.org for othersmodel=ne2k_pci# model=ne2k_isa# start qemu with our parametersqemu $@ -net nic,vlan=0,macaddr=$ranmac,model=$model \ -net tap,vlan=0,ifname=$iface# qemu has stopped - no longer using tap interfacesudo tunctl -d $iface &> /dev/null
You would then start qemu using this wrapper: ./qemu-tap -hda deb-sid.img -localtime

You need tunctl, a program from the usermode linux project, to make this work. On debian the package is called uml-utilities.

Please feel free to modify the .

然后,设置/etc/qemu-ifup
xuwei@XIAO:~$ cat /etc/qemu-ifup
#!/bin/sh
sudo -p "Password for $0:" /sbin/ifconfig $1 172.20.0.1
并且,把该脚本设置成可执行权限:
xuwei@XIAO:~$ ls -l /etc | grep qemu
-rwxr-xr-x 1 root root 66 2006-12-17 11:17 qemu-ifup

这就搞定了。直接qemu-tap .img就可开启虚拟机,并且使用tap/tun模式。开启虚拟机以后,ifconfig可以看到生成了一个tap0接口。host和guest正是通过该tap0接口通信的。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/90fd1ca2fe251454b50b28b497618ade.html