QEMU

QENU 一个传奇
QEMU是一套由Fabrice Bellard所编写的模拟处理器的自由软件。它与Bochs,PearPC近似,但其具有某些后两者所不具备的特性,Qemu是一个广泛使用的开源计算机仿真器和虚拟机。
当作为仿真器时,可以在一种架构(如PC机)下运行另一种架构(如ARM)下的操作系统和程序。通过动态转化,可以获得很高的运行效率。
如高速度及跨平台的特性。经由kqemu这个开源的加速器,QEMU能模拟至接近真实电脑的速度。QEMU有两种主要运作模式:
    User mode,亦即是用户模式。QEMU能启动那些为不同中央处理器编译的Linux程序。而Wine及Dosemu是其主要目标。
        这种模式下可以运行在Linux或Darwin/Mac OS X单用户模式下
    System mode,亦即是系统模式。QEMU能模拟整个电脑系统,包括中央处理器及其他周边设备。它使得为系统源代码进行测试及除错工作变得容易。
        其亦能用来在一部主机上虚拟数部不同虚拟电脑。QEMU可以启动子操作系统,包括 Linux,Solaris,
        Windows ,DOS和BSD,他支持仿真几种指令设置,包括X86,MIPS,32-bit ARMv7,ARMv8,PowerPC,等等。
    KVM Hosting :
        这里QEMU处理设置和迁移KVM镜像的工作,他也包含仿真硬件,但是执行时被KVM所代替。
    Xen Hosting:
        这里QEMU只是仿真硬件,,只是执行哪些Xen无法仿真的硬件。

使用kqemu可使QEMU能模拟至接近实机速度,但其在虚拟的操作系统是Microsoft Windows 98或以下的情况下是无用的。现时主要使用qemu-kvm替代kqemu进行guest系统加速。该组件稳定性与效率较kqemu有所提升。
QEMU架构:
    
QEMU的优点
    可以模拟IA-32(x86)个人电脑,AMD64个人电脑,MIPS R4000,ARM v6, v7(Cortex-A8, A9, A15),昇阳的SPARC sun3与PowerPC(PReP及Power Macintosh)架构
    支持其他架构,不论在主机或虚拟系统上(请参看QEMU主页以获取完整的列表)
    增加了模拟速度,某些程序甚至可以实时运行
    可以在其他平台上运行Linux的程序
    可以存储及还原运行状态(如运行中的程序)
    可以虚拟网卡
    可模拟多CPU
加速器
    KQEMU是Linux内核的一个模块,依然是Fabrice Bellard编写,能显著的加速X86或者X86-64的平台的仿真在同样的COU工业标准。
    只能运行在user mode模式直接在主机的CPU,通过使用计算核和外围仿真。它可以执行代码在许多主机系统不支持HAV(haedware-assisted virtualization).
x86架构可仿真的设备
    单个或多个CPU,使用-smp参数
    4块硬盘或2块硬盘1个光驱,使用-hda和-cdrom参数
    软驱,使用-fda参数
    指定容量的内存,使用-m参数
    3种显示卡,使用-vga参数
    多种声效卡,含AC97,使用-soundhw参数
    多种并口设备,使用-parallel参数
    多种串口设备,使用-serial参数
    多种USB设备,使用-usb和-usbdevice参数
    PC喇叭,使用-soundhw pcspk参数
    PS/2键盘鼠标(默认)和USB键盘鼠标
    蓝牙设备,使用-bt参数
    多种网络控制器,使用-net nic,model= 参数
    内建DHCP服务器,使用-net user参数
    内建DNS服务器,使用-net user参数
    内建SMB服务器,使用-net user,smb= 参数
    内建TFTP服务器,使用-net user,tftp= 参数
    
安装
根据需要,可以从官方源选择安装 qemu.KVM (Kernel Virtual Machine 内核虚拟机)是linux内建的一个功能模块,可让虚拟机中的用户空间程序利用主机上不同处理器提供的硬件虚拟化功能。
现已支持英特尔和AMD的处理器(x86 和 x86_64), PPC 440, PPC 970, 和S/390处理器。QEMU 当运行与主机架构相同的目标架构时可以使用 KVM。
例如,当在一个x86兼容处理器上运行 qemu-system-x86 时,可以利用 KVM 加速——可以提供你的主机和客户机更好的性能。
创建硬盘镜像
要运行qemu你需要硬盘镜像。这是一个保存虚拟硬盘内容的特殊文件。 使用命令:
    qemu-img create -f qcow2 win.qcow 4G
来创建名为"win.qcow"的镜像文件。“4G”参数指定磁盘大小——此处为 4 GB 。M 后缀则代表 MB。(比如“256M”)。不用担心这个磁盘空间太小,qcow2 格式将压缩镜像中的空闲部分而使镜像文件不会因此增大。     
不是所有的处理器都支持 KVM。你需要一个基于 x86 的机器,运行一个较新的 ( >= 2.6.22 ) Linux 内核,使用带有VT-x (virtualization technology 虚拟化技术)的 Intel 处理器或者带有 SVM (Secure Virtual Machine 安全虚拟机) 扩展 (也叫做 AMD-V) 的 AMD 处理器。
准备安装介质
由于 QEMU 将会直接访问安装介质,因此需事先完成挂载。把安装介质转储为镜像文件是个很好的做法,既能提升性能又避免访问物理设备(也就是说可以完全以普通用户身份运行QEMU)。例如,假定光驱的设备节点名为“/dev/cdrom”,转储光盘文件的命令就是:
    dd if=/dev/cdrom of=win98icd.iso 软盘也一样:dd if=/dev/fd of=win95d1.img
    touch floppy.img
安装操作系统
    现在你将首次启动这个家伙。 牢记一件事:当你在QEMU窗口内点击鼠标后,鼠标光标就被其捕获。按Ctrl + Alt键可以解除捕获。
    从软盘引导的命令:
qemu -cdrom [[cdrom''image]] -fda [[floppy''image]] -boot a [[hd_image]]
or if you are on a x86_64 system (will avoid many problems afterwards):
qemu-system-x86_64 -cdrom [[cdrom''image]] -fda [[floppy''image]] -boot a [[hd_image]]
从光驱或光盘镜像文件引导的命令:
qemu -cdrom [[cdrom''image]] -boot d [[hd''image]]
在x86_64的宿主系统中则是:
qemu-system-x86_64 -cdrom [[cdrom''image]] -boot d [[hd''image]]
现在对虚拟硬盘分区、格式化和安装操作系统。
    del %windir%\security\*.log
运行系统
To run the system simply type:
    qemu [hd_image]
A good idea is to use overlay images. This way you can create hard disk image once and tell QEMU to store changes in external file. You get rid of all the instability, because it is so easy to revert to previous system state :)
To create an overlay image, type:
    qemu-img create -b [[base''image]] -f qcow2 [[overlay''image]]
Substitute the hard disk image for base_image (in our case win.qcow). After that you can run qemu with:
    qemu [overlay_image]
or if you are on a x86_64 system:
    qemu-system-x86_64 [overlay_image]
宿主机和虚拟机数据交互
If you have servers on your host OS they will be accessible with the ip-address 10.0.2.2 without any further configuration. So you could just FTP or SSH, etc to 10.0.2.2 from windows to share data, or if you would like to use Samba:
Samba QEMU supports Samba which allows you to mount host directories during the emulation. It seems that there is an incompatibility with Samba 3.x. and some versions of QEMU. But at least with a current snapshot of QEMU it should be working.
First, you need to have a working Samba installation. Then add the following section to your smb.conf:
[qemu]
   comment = Temporary file space
   path = /tmp
   read only = no
   public = yes
Now start QEMU with:
    qemu [hd_image] -smb qemu
Then you should be able to access your host's Samba server with the IP address 10.0.2.2. If you are running Win9x as a guest OS, you may need to add
10.0.2.2 smbserver
to C:\Windows\lmhosts (Win9x has Lmhosts.sam as a SAMple, rename it!).
挂载虚拟硬盘镜像
Fortunately there is a way to mount the hard disk image with a loopback device. Login as root, make a temporary directory and mount the image with the command:
    mount -o loop,offset=32256 [[hd''image]] [[tmp''dir]]
Now you can copy data in both directions. When you are done, umount with:
    umount [hd_image]
The drawback of this solution is that you cannot use it with qcow images (including overlay images). So you need to create you images without \"-f qcow\" option.
小贴士: Create a second, raw hard drive image. This way you will be able to transfer data easily and use qcow overlay images for the primary drive.
使用基于内核的虚拟机(KVM)
KVM is a full virtualization solution for Linux on x86 hardware containing virtualization extensions (Intel VT or AMD-V). It consists of a loadable kernel module, kvm.ko, that provides the core virtualization infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko. Using KVM, one can run multiple virtual machines running unmodified Linux or Windows images. Each virtual machine has private virtualized hardware: a network card, disk, graphics adapter, etc.
This technology requires an x86 machine running a recent ( >= 2.6.22) Linux kernel on an Intel processor with VT-x (virtualization technology) extensions, or an AMD processor with SVM (Secure Virtual Machine) extensions. It is included in the mainline Linux kernel since 2.6.20 and is enabled by default in the Arch Linux kernel.
Please refer to the KVM page itself, for more information on using QEMU with KVM on Arch Linux.
To take advantage of KVM, you simply need a compatible processor (the following command must return something on the screen):
    grep -E "(vmx|svm)" --color=always /proc/cpuinfo
And load the appropriate module from your /etc/rc.conf.
    For Intel® processors add kvm-intel to your MODULES array in /etc/rc.conf
    for AMD® processors add kvm-amd to your MODULES array in /etc/rc.conf
Also, you will need to add yourself to the group kvm.
    gpasswd -a <Your_User_Account> kvm
网络
User-mode networking
By default, without any -netdev arguments, QEMU will use user-mode networking with a built-in DHCP server. Your virtual machines will be assigned an IP address when they run their DHCP client, and they will be able to access the physical host's network through IP masquerading done by QEMU. This only works with the TCP and UDP protocols, so ICMP, including ping, will not work.
This default configuration allows your virtual machines to easily access the Internet, provided that the host is connected to it, but the virtual machines will not be directly visible on the external network, nor will virtual machines be able to talk to each other if you start up more than one concurrently.
QEMU's user-mode networking can offer more capabilities such as built-in TFTP or SMB servers, or attaching guests to virtual LANs so that they can talk to each other. See the QEMU documentation on the -net user flag for more details.
However, user-mode networking has limitations in both utility and performance. More advanced network configurations require the use of tap devices or other methods.
虚拟拓扑网络
Basic idea
Tap devices are a Linux kernel feature that allows you to create virtual network interfaces that appear as real network interfaces. Packets sent to a "tap" interface are delivered to a userspace program, such as QEMU, that has bound itself to the interface.
QEMU can use tap networking for a virtual machine so that packets sent to the tap interface will be sent to the virtual machine and appear as coming from a network interface (usually an Ethernet interface) in the virtual machine. Conversely, everything that the virtual machine sends through its network interface will appear on the tap interface.
Tap devices are supported by the Linux bridge drivers, so it is possible to bridge together tap devices with each other and possibly with other host interfaces such as eth0. This is desirable if you want your virtual machines to be able to talk to each other, or if you want other machines on your LAN to be able to talk to the virtual machines.
Bridge virtual machines to external network
Tango-dialog-warning.png
Tango-dialog-warning.png
This article or section is out of date.
Reason: tunctl has been replaced by ip tuntap (Discuss in Talk:QEMU (简体中文)#)
The following describes how to bridge a virtual machine to a host interface such as eth0, which is probably the most common configuration. This configuration makes it appear that the virtual machine is located directly on the external network, on the same Ethernet segment as the physical host machine.
Warning: Beware that since your virtual machines will appear directly on the external network, this may expose them to attack. Depending on what resources your virtual machines have access to, you may need to take all the precautions you normally would take in securing a computer to secure your virtual machines.
We will replace the normal Ethernet adapter with a bridge adapter and bind the normal Ethernet adapter to it. See http://en.gentoo-wiki.com/wiki/KVM#Networking_2 .
    Make sure that the following package is installed:
        bridge-utils (provides brctl, to manipulate bridges)
    Enable IPv4 forwarding:
sysctl net.ipv4.ip_forward=1
To make the change permanent, add net.ipv4.ip_forward = 1 to /etc/sysctl.d/40-ip-forward.conf.
    Load the tun module and configure it to be loaded on boot. See Kernel modules for details.
    Now create the bridge. See Bridge with netcfg for details.
Remember to name your bridge as br0, or change the scripts below to your bridge's name.
    Create the script that QEMU uses to bring up the tap adapter with root:kvm 750 permissions:
/etc/qemu-ifup
#!/bin/sh
    echo "Executing /etc/qemu-ifup"
    echo "Bringing up $1 for bridged mode..."
    sudo /sbin/ip link set $1 up promisc on
    echo "Adding $1 to br0..."
    sudo /usr/sbin/brctl addif br0 $1
    sleep 2
    Create the script that QEMU uses to bring down the tap adapter in /etc/qemu-ifdown with root:kvm 750 permissions:
/etc/qemu-ifdown
#!/bin/sh
    echo "Executing /etc/qemu-ifdown"
    sudo /sbin/ip link set $1 down
    sudo /usr/sbin/brctl delif br0 $1
    sudo /sbin/ip link delete dev $1
    Use visudo to add the following to your sudoers file:
Cmnd_Alias      QEMU=/sbin/ip,/sbin/modprobe,/usr/sbin/brctl,/usr/bin/tunctl
%kvm     ALL=NOPASSWD: QEMU
    Make sure the user(s) wishing to use this new functionality are in the kvm group. Exit and log in again if necessary.
    You launch QEMU using the following run-qemu script:
run-qemu
#!/bin/bash
USERID=`whoami`
IFACE=$(sudo tunctl -b -u $USERID)
# This line creates a random mac address. The downside is the dhcp server will assign a different ip each time
printf -v macaddr "52:54:%02x:%02x:%02x:%02x" $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff )) $(( $RANDOM & 0xff)) $(( $RANDOM & 0xff ))
# Instead, uncomment and edit this line to set an static mac address. The benefit is that the dhcp server will assign the same ip.
# macaddr='52:54:be:36:42:a9'
qemu -net nic,macaddr=$macaddr -net tap,ifname="$IFACE" $*
sudo tunctl -d $IFACE &> /dev/null
Then to launch a VM, do something like this
$ run-qemu -hda myvm.img -m 512 -vga std
    If you cannot get a DHCP address in the host, it might be because iptables are up by default in the bridge. In that case (from http://www.linux-kvm.org/page/Networking ):
# cd /proc/sys/net/bridge
# ls
bridge-nf-call-arptables  bridge-nf-call-iptables
bridge-nf-call-ip6tables  bridge-nf-filter-vlan-tagged
# for f in bridge-nf-*; do echo 0 > $f; done

And if you still cannot get networking to work, see: Linux_Containers#Bridge_device_setup.
桥接网络
NOTE:这部分由User:athurg根据实际体验过程翻译,和原文的流程有比较大的出入。如果有任何问题,请联系User:athurg。
简介
在QEMU中,桥接网络就是将虚拟机注册到主机的一个单独网络接口上,然后将这个接口和主机对外的网络接口一起桥接起来。如果你需要让虚拟机和外部网络完全互联互通时,就需要这么干了!不过需要提醒的是,这样的话客户机就完全暴露在网络中了。
准备
首先,请确认下面的软件包已经安装了:
 bridge-utils (for brctl, to manipulate bridges)
 uml_utilities (for tunctl, to manipulate taps)
 sudo (for manipulating bridges and tunnels as root)
接下来加载桥接模块:
 # modprobe bridge
配置
首先,修改你的网络配置。增加一个桥接网络接口br0,用它来取代原来主机上的本地链接。打开/etc/rc.conf,将网络配置部分修改如下:
 eth0="eth0 up"
 br0="dhcp"      #如果你原来的本地链接是DHCP方式获取配置的话
 #br0="br0 192.168.0.2 netmask 255.255.255.0 up"      #如果你原来的本地链接是静态设置的话
 INTERFACES=(eth0 br0)
然后,将原来的网络接口eth0绑定到桥接网络接口br0上,使得主机上的网络应用能够通过桥接网络接口,传递到外网中。编辑/etc/conf.d/bridges:
 bridge_br0="eth0"
 BRIDGE_INTERFACES=(br0)
加载tap模块
 # modprobe tun
创建一个/etc/qemu-ifup脚本,用于QEMU用来创建和加载虚拟机对外访问的网络接口。
 #!/bin/sh
    echo "Executing /etc/qemu-ifup"
    echo "Bringing up $1 for bridged mode..."
    sudo /sbin/ifconfig $1 0.0.0.0 promisc up
    echo "Adding $1 to br0..."
    sudo /usr/sbin/brctl addif br0 $1
    sleep 2
这个脚本的属主应该设置为root:kvm,权限为750,以使得kvm组的用户(可以启动虚拟机的用户)也能执行。这里也提醒大家一次,所有需要通过qemu使用KVM虚拟机的用户,都应当加入到kvm组中。
由于这个脚本里需要调用ifconfig等命令来给虚拟机动态增减网络接口,为了方便我们在脚本里通过sudo来执行。如果不想脚本执行过程中向用户索取密码,请编辑sudo或者visudo的配置文件,加入下面这一行:
    Cmnd_Alias      QEMU=/sbin/ifconfig,/sbin/modprobe,/usr/sbin/brctl,/usr/bin/tunctl
    %kvm     ALL=NOPASSWD: QEMU
最后,将bridge和tun加入到文件/etc/rc.conf的MODULES变量中,以便开机就能加载。
启动一切配置妥当,我们就可以开始体验了。 启动带桥接模式网络接口的QEMU大致有三个步骤
    通过tunctl申请一个可用的虚拟网络接口——tap设备。
    配置这个设备并桥接到桥接网络接口,然后启动qemu
    退出qemu时,别网络删掉这个虚拟网络接口。
听上去很复杂吧?要是每次启动虚拟机都这么复杂不是很烦?我们可以创建一个脚本来实现:
    USERID=`whoami`
    IFACE=`sudo tunctl -b -u $USERID`
    qemu -net nic -net tap,ifname="$IFACE" $*
    sudo tunctl -d $IFACE &> /dev/null
需要注意一点,由于我们前面创建了/etc/qemu-ifup这个脚本,因此我们只需要把申请到的虚拟网络接口名通过-net tap,ifname="网络接口名"告诉qemu,他自己会用这个接口名为参数,调用/etc/qemu-ifup来完成桥接和配置这个网络接口的过程。
图形
QEMU 可以使用一下几个图形输出:std, cirrus, vmware, qxl, xenfs 和 vnc。
使用 vnc 选项,你可以单独运行客户机,并且通过 VNC 连接。其他选项是使用std, vmware, cirrus:
std
使用 -vga std 你可以得到最高 2560 x 1600 像素的分辨率。
vmware
尽管有一点怪怪的,但是这种方法确实比 std 和 cirrus 效果好。在客户机中,安装 VMware 驱动:
    pacman -S xf86-video-vmware xf86-input-vmmouse
Windows 客户机
如果你使用微软 Windows 客户机,你也许想使用远程桌面协议(RDP)连接到客户虚拟机。使用:(如果你使用 VLAN 或与客户机不在同一个网络之中)
    qemu -nographic -net user,hostfwd=tcp::5555-:3389
然后用 rdesktop 或者 freerdp 连接到客户机,例如:
    xfreerdp -g 2048x1152 localhost:5555 -z -x lan
Qemu的前端
Qemu有几个图形前端,不习惯命令行的可以使用:
    community/qemu-launcher
    community/qemulator
    community/qtemu
键盘不工作/方向键不工作
如果你发现一些键不工作或“按下”错误的键(尤其是方向键),你也许需要在选项中指定你的键盘布局。键盘布局可以在 /usr/share/qemu/keymaps 找到。
    qemu -k [keymap] [disk_image]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值