qemu 使用

来源:互联网 发布:32单片机蜂鸣器程序 编辑:程序博客网 时间:2024/06/05 17:24

1.手册

http://wiki.qemu.org/Manual


2.下载源代码

git clone git://git.qemu.org/qemu.git

./configure --enable-kvm --target-list=i386-softmmu --enable-debug

make

这样配置会编译出来qemu-system-i386

历史版本: http://qemu-project.org/index.php?title=Download&action=history


3.试用 

http://www.crashcourse.ca/wiki/index.php/QEMU

这个网页让我们了解什么是qemu,一起最基本的让我试用一下。看看究竟qemu是什么。


qemu分成两个部分:

user mode emulation  which allows you to run a simple cross-compiled executable, and

full system emulation which emulates a full system including the corresponding hard disk image.


使用 -M 选项查看支持哪种体系结构

$ qemu-system-arm -M ?
Supported machines are:
integratorcp ARM Integrator/CP (ARM926EJ-S) (default)
versatilepb ARM Versatile/PB (ARM926EJ-S)
versatileab ARM Versatile/AB (ARM926EJ-S)
realview   ARM RealView Emulation Baseboard (ARM926EJ-S)
akita      Akita PDA (PXA270)
spitz      Spitz PDA (PXA270)
borzoi     Borzoi PDA (PXA270)
terrier    Terrier PDA (PXA270)
cheetah    Palm Tungsten|E aka. Cheetah PDA (OMAP310)
lm3s811evb Stellaris LM3S811EVB
lm3s6965evb Stellaris LM3S6965EVB
connex     Gumstix Connex (PXA255)
verdex     Gumstix Verdex (PXA270)
mainstone  Mainstone II (PXA27x)


在这个页面http://wiki.qemu.org/Download上,列出了几个试用的版本。  比如

http://wiki.qemu.org/download/linux-user-test-0.3.tar.gz   这个是一个用户态程序的测试版本。 

    包括了各个平台上的应用程序和所需要的库文件。 值得一看。

http://wiki.qemu.org/download/arm-test-0.2.tar.gz    这是一个系统级的 arm结构的测试版本。 

   包括了一个arm的kernel 和  initrd。 值得学习。

http://wiki.qemu.org/download/linux-0.2.img.bz2    这是一个linux 2.6.20的磁盘映像。

   直接下载下来,解压缩  qemu linux-0.2.img 就可以运行了。 我很好奇,这个跟体系结构没有关系么?

   file 了一下,这个是个x86的boot sector. 应该是跟体系结构相关的。 有待证实。

   mount linux-0.2.img /tmp -o loop 进去看了一下,所有的可执行文件也是intel的。 那就应该是x86的了。

   如何访问虚拟盘 请看 下面的 qemu image

更多一些已经打包好的debian的image可以在这里找到 http://people.debian.org/~aurel32/qemu/


4.qemu image 使用

http://en.wikibooks.org/wiki/QEMU/Images

在host上查看guest的image

对于raw格式的磁盘映像,可以用 loopback 设备挂载到主机上。

挂载没有分驱的映像

    mount -o loop linux-0.2.img /tmp

挂在有分驱的映像

    mount -o loop,offset=32256 ubuntu.img /tmp


对于非raw格式的映像,是不能直接mount的。

对于qemu disk image,可以用到 nbd 模块来mount。  关于nbd可以参考 http://en.wikipedia.org/wiki/Network_block_device

modprobe nbd max_part=16
qemu-nbd -c /dev/nbd0 image.qcow2
partprobe /dev/nbd0
mount /dev/nbd0p1 /mnt/image

好了,这样可以在/mnt/image 目录中查看文件了。


查看映像文件的信息,格式,大小,用qemu-img info命令

$ qemu-img info test.vmdk
(VMDK) image open: flags=0x2 filename=test.vmdk
image: test.vmdk
file format: vmdk
virtual size: 20M (20971520 bytes)
disk size: 17M



创建guest image

qemu-img create [-f fmt] [-o options] filename [size]

fmt 可以有, raw, qcow2, host_device


5.qemu network

http://en.wikibooks.org/wiki/QEMU/Networking

Qemu提供了三种连接的网络方式。

User Mode Networking

kvm -drive file=ubuntu.img -boot dc -m 512  -monitor stdio -netdev user,id=usernet -device rtl8139,netdev=usernet

分配地址的情况

user mode默认的情况,或者用 -net user来指定。

The guest OS will see an E1000 NIC with a virtual DHCP server on 10.0.2.2 and will be allocated an address starting from 10.0.2.15. A virtual DNS server will be accessible on 10.0.2.3, and a virtual SAMBA file server (if present) will be accessible on 10.0.2.4 allowing you to access files on the host via SAMBA file shares.

但是这个samba的ip地址,我没有弄明白。


有一点比较重要的是,guest中怎么看得到host。

默认的情况下,host的地址是10.0.2.2, 这个地址也是可以改的。 可以用 -net user,host=addr 来改。


端口映射

将guest中的某个端口映射到host

-redir tcp:5555::8080

上面这个选项可以将guest中的8080端口映射到主机的5555


Tap interface(guest on a bridge)

sudo tunctl -t tap0

sudo brctl addif virbr0 tap0

sudo ifconfig tap0 0.0.0.0 up

sudo kvm -drive file=ubuntu.img -boot dc -m 512  -monitor stdio -netdev tap,id=tapnet,ifname=tap0,script=no -device rtl8139,netdev=tapnet,mac=96:83:DA:A0:06:34


然后进到guest,配置eth0为virtbr0同一网段地址。 添加默认路由。

ifconfig eth0 192.168.122.25

route add default gw 192.168.122.1

这样就可以和主机通信了。


记得在host上打开ip_forward...

echo 1 > /proc/sys/net/ipv4/ip_forward


但是要和外界通信,还要添加iptable规则。 我添加了如下两条。

iptables -t nat -A POSTROUTING  -o wlan0 -j MASQUERADE

iptables -A FORWARD -i wlan0 -j ACCEPT


最后需要在guest中添加dns server。

这个就直接拷贝host上的 /etc/resolv.conf文件就好了。


另外, 如果在bridge上有两台机器, 那么要指定mac地址,且不一样。 

否则在ARP解析的时候,会出现问题。

其实这个应该要设置成不一样的,否则的话在别的情况下也会发生问题。


增加一条host的iptable,可以导出guest的服务。

iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.122.25

   --dport  80     这个是对外的服务端口

   192.168.122.25  这个是虚拟机的地址


或者

iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 80 -j DNAT --to-destination 192.168.122.25

多加个-i eth0表示只处理从eth0进入的包。


再加一条iptable rule 可以让host在自己机器上访问 http://9.123.254.131

iptables -t nat -A OUTPUT -d 9.123.245.131 -p tcp --dport 80 -j DNAT --to-destination 192.168.122.25

需要进一步学习


Tap interface(guest on a tap, nat directly)

sudo tunctl -t tap0

sudo ifconfig tap0 172.16.0.1 up

sudo kvm -drive file=ubuntu.img -boot dc -m 512 -monitor stdio -netdev tap,id=tapnet,ifname=tap0,script=no -device rtl8139,netdev=tapnet,mac=96:83:DA:A0:06:34


进入guest

ifconfig eth0 172.16.0.2 up

route add default gw 172.16.0.1


最后在host上添加nat

iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE


Tap Interface(bridged)

这种方式比较特殊,整个guest完全暴露在网络里了。

在ubuntu中的配置方式如下:

# cat /etc/network/interfaces

auto wlan0

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet dhcp
        bridge_ports eth0
        bridge_stp   off
        bridge_fd    0
        bridge_maxwait 0


在guest中的ubuntu如下配置

auto lo

iface lo inet loopback


auto eth0

iface eth0 inet dhcp



6.配置samba以使host和guest之间用samba通信

首先在host上创建samba服务。 在linux上,可以修改 /etc/samba/smb.conf,增加

[qemu]
   comment = Windows file space
   path = /mnt/windows
   read only = no
   public = yes

path 指定了需要共享的路径。


进入guest , mount这个samba目录

mount -t cifs -o username=qemu,ip=10.0.2.2 //10.0.2.2/qemu shared

10.0.2.2 是默认的guest中看到的host的地址。


但是这个页面http://en.wikibooks.org/wiki/QEMU/Networking#SMB_server 所说的-smb 选项,我不知道怎么用。


7.Qemu 的 monitor

qemu 提供一个monitor 来做控制

可以用Ctrl+Alt+2 来显示,或者在启动的时候 加上 -monitor stdio 可以直接在terminal上来操作。


或者用 -monitor telnet:localhost:5432,server,nowait 来定向到一个telnet口。


在nographic模式下 用

C-a c 在monitor之间切换


在host中输出guest中的console

qemu命令行的配置 。

/home/richard/git/qemu/i386-softmmu/qemu-system-i386  -drive file=ubuntu.img -boot dc -monitor stdio -kernel /home/richard/git/linux-2.6/build/arch/x86/boot/bzImage -append "root=/dev/sda1 rw console=ttyS0,115200n8" -enable-kvm -serial telnet:localhost:4321,server,nowait


出了qemu的配置, 要输出,还要在guest中配置。

要在ubuntu上配置,可以参考 http://blog.csdn.net/richardysteven/article/details/5524639


8.Tips

申请磁盘kvm-img create ubuntu.img 10G

安装kvm -drive file=ubuntu.img -cdrom ubuntu-11.04-desktop-i386.iso -boot d -m 512

运行kvm -drive file=ubuntu.img -boot dc -m 512


上面是个简单的例子,现在写几个稍微复杂点的。

1. 安装ubuntu

../git/qemu/i386-softmmu/qemu-system-i386 -hda ubuntu.img -cdrom /media/XP/ubuntu.iso -boot dc -m 1024 -vnc :1

-hda指定了disk image

-vnc 指定了 vnc的端口


2. 启动指定的kernel 并且调试

./qemu-system-i386 -kernel ../../linux-2.6/arch/x86/boot/bzImage -initrd /boot/initrd.img-3.1.0-rc1+ -append "root=/dev/sda ro" -boot c -m 512 -hda ../disk.img -vnc :2 -S -s

-kernel 指定kernel的elf文件

-initrd 指定了initrd, 这个使用mkinitramfs作出来的

-append 添加boot的命令,这个是说明用的是/dev/sda作为根,有时候需要指定为/dev/sda1


3. 使用kickstart文件进行自动安装

制作iso文件

mkisofs -o ks.iso ks.cfg

这个iso只包含了一个ks.cfg文件。


用mount -o loop ks.iso /tmp 可以查看制作是不是成功。


kvm  -drive file=rhel.img -drive file=RHEL6.1-20110510.1-Server-x86_64-DVD1.iso,media=cdrom,index=1 -drive file=ks.iso,media=cdrom,index=2  -kernel vmlinuz -initrd initrd.img -append 

在append后,加上ks的标识。

具体格式可以看http://fedoraproject.org/wiki/Anaconda/Kickstart#Boot_Diskette

其中有一个option指定了用哪个网络接口去读取ks文件, ksdevice。


下面是两个例子

 -append console=hvc0 ks=cdrom:/rhel6_ks.cfg

上面的命令中 kickstart的脚本文件在 rhel6_ks.iso这个镜像中的根目录下,叫rhel6_ks.cfg。 用 -append kernel命令来指定。


 -append console=hvc0 ks=http://ford11.ltc.austin.ibm.com/POWER_KVM_test/rhel6/rhel6_ks.cfg RHEL6_install.img

这个命令也是用kickstart脚本文件来实现自动安装的。 不过这个kickstart文件在一个webserver上。


1 0