参考网址:
1 http://wiki.debian.org.tw/index.php/QEMU
2 http://wiki.debian.org/QEMU
1,qemu的安装这里就不赘述,下面就简单介绍安装kqemu。
#aptitude install module-assistant
# m-a a-i kqemu
# echo kqemu >> /etc/modules //这句是把keqmu模块加入开机自动加载
# modprobe kqemu
2,kqemu安装完后,安装tap,tap是虚拟网卡接口,连接虚拟机和host机的桥梁,安装 bridge-utils和uml-utilities.(bridge-utils是将tap/tun和实体网络连接在一起的,如果tap/tun没有和实体网络连接在一起,guest os 就只能放我host os,参考网址1)
#aptitude install bridge-utils uml-utilities
可以把一下四个命令放在你的bash.bashrc里面,不然每次重启机器,都要执行这几个命令。
#modprobe tun
#mknod /dev/kqemu c 250 0
#chmod 766 /dev/kqemu
#tunctl -u root -t tap0
保证/dev/net/tun的权限是666。
3,和bridge连接的网络ip必须是0.0.0.0,并且是promisc模式。
于是可以设置/etc/network/interfaces如下:
# The loopback network interface
auto lo
iface lo inet loopback
#auto eth0
#iface eth0 inet static
#address 192.168.1.244
#gateway 192.168.1.1
#netmask 255.255.255.0
iface br0 inet manual
up brctl addbr br0
up brctl setfd br0 0
up brctl sethello br0 0
up brctl stp br0 off
up brctl addif br0 eth0
up ifconfig eth0 0.0.0.0 promisc up
up dhclient br0
down ifconfig br0 down
down brctl delif br0 eth0
down brctl delbr br0
down dhclient eth0
上面是手动设置网桥,下面是设置静态ip给网桥,下面是我的/etc/network/interface内容:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
#auto eth0
#iface eth0 inet static
# address 192.168.2.244
# netmask 255.255.255.0
# gateway 192.168.2.1
auto br0
iface br0 inet static
address 192.168.2.244
netmask 255.255.255.0
gateway 192.168.2.1
bridge_ports eth0
这样,要用qemu的时候,就可以通过ifup br0开启bridge ,ifdown br0关闭bridge.当然tap0也需要弄成promisc模式和ip=0。可以通过设置/etc/qemu-ifup来实现。
#!/bin/sh
/sbin/brctl delif br0 $1
ifconfig $1 down
fi
/sbin/ifconfig $1 0.0.0.0 promisc up
/sbin/brctl addif br0 $1
/usr/bin/tunctl -u root -t $1
exit 0
3,现在,tap和bridge架构已经成功搭建,可以用qemu启动guest os了。举我的例子来说。
#qemu -s -hda i386.img -kernel linux-2.6.33.2/arch/x86/boot/bzImage -append "root=/dev/hda1 rw console=ttyS0" -net nic,vlan=0 -net tap,vlan=0,ifname=tap0,script=/etc/qemu-ifup -nographic
NOTE:i386.img是我已经装好的系统,bzImage是我想要调试的内核,加 -s是qemu中的可以用kgdb调试内核的选项。
如果还是无法上网,可以试下
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -j MASQUERADE
不过按照步骤来的话,几乎肯定是可以能上网的。