在BSD下使用qemu开源的虚拟机
大家在windows 下都用什么虚拟机?可能大多数都是用VM ware 吧,那在BSD下呢?在我还用4.10的时候我也是用VM ware,但是VM ware是要授权文件的,不过这个文件是可以下载到的。但是现在,在BSD下,有了更好的选择,那就是qemu,一个开源的虚拟机,小巧而快速,好了不多说,我现在 是在BSD下打算虚拟出一个win来,并且让 win能上网
1 首先,用ports安装
CODE:
[Copy to clipboard]
#cd /usr/ports/emulators/qemu
#路径不怎么记得了,不好意思
CODE:
[Copy to clipboard]
#make WITH_KQEMU=yes install clean
#这里有个选项,with_kqemu,当初我就是没装个,导致出错,用了这个选项后 会在BSD启动时加入 kqemu.ko
然后就是几分钟的时间,很快的就可以装好qemu了(呵呵,比VM 时间短)
2 接下来就是使用qemu了,
首先建议您重启BSD,当然,不重启也可以,用下面的步骤就可以了:
CODE:
[Copy to clipboard]
#rehash
#kldload kqemu.ko
3 要用虚拟机,当然先要为虚拟机准备一块磁盘,这里推荐使用img格式的文件做为虚拟磁盘,当然,qemu兼容vm的文件,有兴趣你也可以试试。个人感觉似乎img的磁盘文件会更快点。往下看
CODE:
[Copy to clipboard]
#mkdir /tmp/qemu #找个地方安装你的虚拟机,要注意下自己的空间哦,不要像我,直接放在了 /下,造成空间不足。强烈建议你先 df -h下
CODE:
[Copy to clipboard]
#cd /tmp/qemu
#qemu-img create windows.img 2G #这个就是创建虚拟机磁盘,后面的2G意思是创建一个2G的磁盘
4 接下就是要安装虚拟机了,我有两手准备,一是光驱,二是ISO文件。下面就开始安装
CODE:
[Copy to clipboard]
#qemu -hda windows.img -cdrom /dev/acd0 -boot d -m 512 -enable-audio -localtime
解释下上面的语法:
-hda 是指定硬盘,在本文里我们使用的是刚才创建的/tmp/qemu/windows.img
-cdrom 指定使用光驱,这个参数后面必须要跟个文件名,在上面的例子里我用的是我的物理光驱/dev/acd0,如果你要使用ISO文件,那就写上文件的路径,比如我第二安装时用的就是/mtn/e/xp.iso
-m 是指定虚拟机使用的内存大小,后面的数字即是你要指定的内存数,我的是512M
-boot 当然就是指定虚拟机启动用的设备,-boot后可以选择的参数是 a ,c ,d .其中a 是指从软盘启动,c 就是用硬盘启动,d 当然就是光驱启动了。
-enable-audio 这个意思我就不说了。
-localtime 就是说要使用你的实际主板bios上的时间。
好了,按以上的步骤做了以后,就可以顺利的进入虚拟机了,并开始安装了。
*注意:如果你在安装QEMU没有选择 WITH_KQEMU,那么你将不能使用ISO文件来安装,这一点要注意。要想自己是否安装了kqemu.ko就自己看下:
CODE:
[Copy to clipboard]
#kldstat
下面是引用Linux下的Qemu的使用,由天Qemu是跨平台的,所以应该通用,当然,我只是为可能出现的问题提供一个解决参考:
CODE:
[Copy to clipboard]
安装过程中,要求换盘:
在qemu中按ctrl+alt+2切换到qemu monitor模式 输入?或help可以查看可用命令及使用说明。
(在其他版本的qemu中,运行qemu加载OS后,这个shell就会自动变成qemu monitor模式)
change device filename -- change a removable media
看来它就是用来换盘的了 : change cdrom /rhel4/EL_disc2.iso
5 虚拟机装好以后就可以用了,这里要给大家说几个常用的快捷键:
CODE:
[Copy to clipboard]
ctrl+alt+F 全屏切换
ctrl+alt 鼠标在虚拟机和主机间切换
6 虚拟XP上网的问题:
其实用上面的方法安装完后,你的XP已经能上网了,当然你要用DHCP方式。你也可以在启动时加上 -user-net 这个参数。我觉得qemu可能是默认就带了这个参数了,因为我没用任何参数就可以上网。
不过要说的是,采用这种默认的参数你只能 ping 你的BSD主机,不能ping 外网的,而且你得到的IP是10.0.2.X,Gateway 也一定是10.0.2.2,所以你不要奇怪。这种上网方式主机跟虚拟机的通讯不方便,至少我的samba就不行,也没有解决掉。
qemu也提供了第二种上网方案:tun方式,这个方式其实就是在你的BSD下虚拟一张网卡tun0,这样你的BSD就是双网卡上网,这种方式最大好处就是虚拟机跟主机还有外网通讯比较方便。不过首先你的内核要支持tun设备才行。我的是5.4 release-p6
针对5.0以前的应该先这样做:
CODE:
[Copy to clipboard]
#cd /dev
# sh MAKEDEV tun0
如果你跟我一样,是5。X那就先:
CODE:
[Copy to clipboard]
#mknod tun0 c 20 0
#ls /dev
接下来:
#ifconfig tun0 XXX.XXX.XXX.XXX #设定tun0的IP,一般用192.168开头的,也有人喜欢10开头的,随便啦,我的是192.168.0.1
然后进入虚拟机XP里,设定你的IP为:192.168.0.XXX,网关为 192.168.0.1(*也就是tun0)dns 就是BSD主机的dns。
这样设定之后,虚拟机还不能上网,但已经能用samba 了。要想用虚拟机上网,这就要用到nat 了,设置方法完全跟NAT是一样的,我就不多说了,下面的东西就只有请你参考FreeBSD 的NAT来完成了。
Article submitted by Michael Williamson. We are running out of articles! Please help DPOTD and submit good articles about software you like!
QEMU lets you emulate a machine —in other words, you can run a virtual computer on top of your real computer. This makes it perfect for trying and testing the latest release of a distribution, running older operating systems, or just testing.
So, let’s say you wanted to run a LiveCD, which you have stored on your hard drive. Easy enough! We just type:
qemu -cdrom path/to/livecd.iso -boot d
The -cdrom option tells QEMU the path to the CD you want to use, while -boot dictates what device we’re booting from —in this case, we want to boot from the CD, which is always device d.
You might get a message about kqemu —you can safely ignore this for now.
Alternatively, you may want to boot from a real CD in your computer —so, you simply use the path to your CD drive in /dev. For instance, if your CD drive is /dev/hdc, then we would use:
qemu -cdrom /dev/hdc -boot d
Of course, we don’t just want to use CDs all the time —we might want to actually install something! So, we’ve got to make a hard drive image first. This is achieved by using qemu-img, like so:
qemu-img create virtualharddrive.qcow 5G -f qcow
This creates an image called virtualharddrive.qcow, which is 5 gigabytes big. If you wanted it to be 5 megabytes, you could type 5M instead, for ten gigabytes, type 10G, and so on.
Finally, the -f option tells qemu-img what format you want to use. While there are a few to choose from, qcow is the norm - it works well enough, and only takes up the space on the hard drive that it needs. If the virtual hard drive had a capacity of 5 gigabytes, but only contained 2 gigabytes of data, then it would only take up about 2 gigabytes on your real hard disk.
So, now we want to get installing. Simply type:
qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -boot d
Then, you can simply follow the instructions just as with an ordinary installation.
Now, not all distributions come on one CD —in some cases, you’ll need to swap CDs. To achieve this, we need to do two things. First of all, we make the QEMU monitor appear on the command line by adding the option -monitor stdio, so we end up with something like:
qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -boot d -monitor stdio
When you run this command, QEMU should let you enter commands. To change CDs, simply type in the commands:
eject cdrom
change cdrom path/to/newcd.iso
Voila! The virtual machine should now have changed CDs so you can continue the installation. Naturally, you can change CDs at any time, not just during installation.
Once the installation has finished, you’ll want to boot from the hard drive. Since QEMU does this by default, simply remove the -boot d part of the command:
qemu virtualharddrive.qcow -cdrom path/to/installcd.iso -monitor stdio
If you have no intention of using the CD after the installation, then you can cut that out as well:
qemu virtualharddrive.qcow -monitor stdio
This should let you play around with your newly installed system at your heart’s content without endangering your own PC. If you want to fiddle with something, but don’t want the changes written to the image, then add the option -snapshot. If, after using this option, you decide that you actually want to save the changes made to the hard drive, then simply type commit into the QEMU monitor, and the changes will be written.
While these commands work, unless you’re using lightweight distributions, you might find things going a little slowly. This is due to QEMU, by default, only taking up 128MB of RAM. You can increase the amount available by using the -m option, followed by the amount of RAM in megabytes. For instance, if I wanted to allocate 256MB to QEMU for running a LiveCD, then I would type:
qemu -cdrom path/to/livecd.iso -boot d -m 256
That should speed things up nicely! But don’t give QEMU too much memory —you want some left for your other applications. Unfortunately, things are probably still fairly slow— to speed things up even more, you’ll probably want to use kqemu, otherwise known as the QEMU accelerator.
Installing kqemu from the repositories is reasonably straightforward. First of, grab the kqemu-source package —if you apt-get is your package manager of choice, then the command used is:
apt-get install kqemu-source
If you don’t have module assistant already, you’ll need that installed as well:
apt-get install module-assistant
Then, type in the following commands (as root):
m-a prepare
m-a auto-install kqemu
That should be it! Now, every time you want to use the kqemu module, you first need to become root, and then type:
modprobe kqemu major=0
Then, as an ordinary user, QEMU will automatically use kqemu, which should help speed things up. If QEMU complains that it still cannot use kqemu, then you might not have the necessary permissions —try typing the following as root:
chmod 666 /dev/kqemu
Hopefully, kqemu should now be usable by QEMU.
There is one final option: -kernel-kqemu. This, in theory, speeds up the emulation even further. Unfortunately, it isn’t as simple as that. Firstly, the version of QEMU in the Debian repositories cannot use -kernel-kqemu. Secondly, even if it could, not all guest operating systems would work with this enabled —for instance, a recent distribution of GNU/Linux would probably work faster with this option (if it worked!), but Windows 98 just crashes.
QEMU is available from Debian Sarge and Ubuntu Warty.
虚拟机qemu的使用
Redflag Workstation 下已经带了qemu
#qemu-img create windows.img 2G #这个就是创建虚拟机磁盘,2G大小
#qemu -hda windows.img -cdrom /dev/acd0 -boot d -m 512 -enable-audio -localtime
-hda 是指定硬盘
-cdrom 指定使用光驱,这个参数后面必须要跟个文件名,可以是物理光驱/dev/hda,可以使用ISO文件,为文件的路径
-m 是指定虚拟机使用的内存大小,如512M
-boot 指定虚拟机启动用的设备,-boot后可以选择的参数是 a ,c ,d .其中a 是指从软盘启动,c 就是用硬盘启动,d 是光驱启动。
-enable-audio
-localtime 使用实际主板bios上的时间。
应该可以顺利的进入虚拟机了,并开始安装了。
Redflag Workstation 下已经带了qemu
#qemu-img create windows.img 2G #这个就是创建虚拟机磁盘,2G大小
#qemu -hda windows.img -cdrom /dev/acd0 -boot d -m 512 -enable-audio -localtime
-hda 是指定硬盘
-cdrom 指定使用光驱,这个参数后面必须要跟个文件名,可以是物理光驱/dev/hda,可以使用ISO文件,为文件的路径
-m 是指定虚拟机使用的内存大小,如512M
-boot 指定虚拟机启动用的设备,-boot后可以选择的参数是 a ,c ,d .其中a 是指从软盘启动,c 就是用硬盘启动,d 当然就是光驱启动了。
-enable-audio
-localtime 使用实际主板bios上的时间。
应该可以顺利的进入虚拟机了,并开始安装了。
安装过程中,要求换盘: 在qemu中按ctrl+alt+2切换到qemu monitor模式 输入?或help可以查看可用命令及使用说明。 (在其他版本的qemu中,运行qemu加载OS后,这个shell就会自动变成qemu monitor模式)
change device filename — change a removable media
如change cdrom /tmp/xp1.iso
常用的快捷键:
ctrl+alt+F 全屏切换
ctrl+alt 鼠标在虚拟机和主机间切换
虚拟机上网设置两种方式:
1. user mode network :
这种方式实现虚拟机上网很简单,类似vmware里的nat,qemu启动时加入-user-net参数,虚拟机里使用dhcp方式,即可与互联网通信,但是这种方式虚拟机与主机的通信不方便。
2. tap/tun network :
这种方式要比user mode复杂一些,但是设置好后 虚拟机互联网 虚拟机主机 通信都很容易
这种方式设置上类似vmware的host-only,qemu使用tun/tap设备在主机上增加一块虚拟网络设备(tun0),然后就可以象真实网卡一样配置它.
前提:
首先内核中支持tap/tuns设备:
Device Drivers —>
Networking support —>
[M] Universal TUN/TAP device driver support
在Redflag Workstation中已经有了支持
如果没有编译内核模块:make modules
将它cp到当前内核的模块目录的相应位置:
cp /usr/src/linux/drivers/net/tun.ko /lib/modules/`uname -r`/kernel/drivers/net
重新建立模块依赖关系:
depmod
再加载就OK了
如果成功了应该存在设备文件 /dev/net/tun
若使用这种方式需要以下几步:
a.需要一个tun/tap的初始化脚本 :
/etc/qemu-ifup :
#!/bin/sh
/sbin/ifconfig $1 172.20.0.1
再给予qemu-ifup x执行权限放在/etc下就可以了,这是有qemu自动执行的
这个脚本只能root用户执行,如果需要普通用户使用qemu,就需要改成sudo /sbin/ifconfig …..再设置sudo相关权限。 启动qemu后,它会在主机上增加一块虚拟网络设备(tun0)
b.设置虚拟机网络参数
和vmware host-only一样:
ip与tun0相同网段,gateway为tun0 ip 其余参数与tun0相同,dns与主机相同:
tun0 : ip :172.20.0.1 broadcast:172.20.255.255 netmask :255.255.0.0
qemu : ip :172.20.0.100 broadcast:172.20.255.255 netmask :255.255.0.0 gateway:172.20.0.1
与host-only一样,这样只实现了虚拟机主机间通信,还需要设置router,nat才可以连上互联网
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth0 -s 172.20.0.0/24 -j MASQUERADE
图形界面的使用:
首先安装qemu
我使用Debian, 一句apt-get install qemu命令即可安装, 或者你使用其它的方法.
安装qemu的图形界面前端qemu-laucher
因为我也不是完全的命令行爱好者, 所以我安装qemu-laucher来启动运行qemu. 见截图:
设置qemu, 安装WinXP
从桌面菜单启动qemu-launcher后, 1. 取消Snapshot Mode, 因为这个选项表示只有你按Ctrl+a s后才将虚拟机的运行结果写入虚拟机的磁盘文件.
2.选中Use CD-ROM, 因为我们需要光驱安装操作系统.
3. 在Hard Disk 0 一栏里填入你的虚拟磁盘在Linux下的文件, 只需要填入, qemu自会创建.
4. 默认内存为128MB, 你可以设置为192MB. 太大了qemu会无法运行虚拟机.
5. CPU, 声卡, 显卡等设置