Using kvm, or kqemu, to speed up qemu

Posted by Azerthoth on Fri 24 Aug 2007 at 09:33

Tags: , ,

Have you ever wanted to play with a new distro without having to burn and then reboot into a liveCD or do an install into a spare partition that you may or may not have? QEMU has been an option for awhile but it is slow. There are several options available to run up a virtual machine, i.e. a second operating system running inside and seperate from your already running operating system. Here we will be focusing on kqemu and kvm.

If your kernel is already 2.6.20 or higher you can run kvm, if not then you will have to use kqemu.

kvm is in sid/unstable so you will have to add that to your apt sources if you're using someting different.

Become root and edit your sources list:

nano /etc/apt/sources.list

add this:

deb http://ftp.debian.org/debian unstable

Now update by running :

apt-get update
aptitude install kvm

After kvm and associated packages are installed it would be best to edit your sources.list again and comment out the line where you added in unstable with a "#" character at the beginning of the line.

kqemu, which is technically a qemu accelerator instead of kernel level virtualization, like kvm, is available in Debian stable and therefor as easy to install as:

aptitude install kqemu

You should have noticed if it wasn't already installed that qemu was installed regardless of whether you went with kvm or kqemu. Thats because both packages use qemu. So no worries, we need the qemu tools anyway.

If you installed kqemu then we need to load that module when we boot. Become root again and then run:

 echo kqemu >> /etc/modules

For kvm you need to know something whether you have an Intel or an AMD processor. You'll need to load a processor-specific module to take advantage of the system.

If you have an Intel processor you will need to load kvm-intel, for AMD processors you'll need to load kvm-amd. The module can again be added to /etc/modules.

Now that we have the modules ready to load, we need to make them accessible to you as a user.

adduser $USER kvm
adduser $USER kqemu

(Only one of thse commands needs to be run; it depends which program you installed.)

Congratulations, right now you should have everything just about ready to go. The problem is, even if you modprobe the modules active right now they still wont be usable to you, they need to be loaded against the kernel when you boot, so bookmark this page and reboot your computer, I'll wait here while you do.

OK so now we need to create a virtual drive to install our test distro to. I'll explain the parts of it after. From here on out everything is applicable to QEMU, Kqemu, and KVM. It is done as a normal user, so no more need to be root.

qemu-img create debian.img -f qcow 5G

The first bit is self explanatory, create an image (virtual drive) named debian.img. The next bit "-f qcow" tells it to format it in an inflatable structure. The 5G means a maximum physical size of 5 gig real hard drive space. The nice thing about this format is, if your VM installed only takes up 2.5 gig, then the virtual drive only takes up 2.5 gig of space on your real hard drive.

I guess the next thing that you'll be wanting to do is actually spin this up and try it out. I should note that I have had problems on occasion with KVM locking up during the install process, if this should happen to you drop back and use qemu with the same command line arguments. If you are using kqemu instead of kvm use qemu in the command line instead of kvm, kqemu is a module called by qemu when it starts. I'll be using kvm for the command line, you use what works for you.

I'm not ready yet to tell you how to start. A few things first if you don't mind. I usually make a seperate directory for my VM's because there are usually one or two files other than just the .img file. Such as an overlay file or two, which I will cover shortly and a script that simplifies launching the VM after its made. There are tons of command line arguments that can be added to the basic ones I am using here to get you started, the script is a huge time saver.

There are two basic ways to start this off, either with an .iso image or a CD/DVD. Lets start with an .iso image, the debian net-inst image in this case. We will assume for the sake of argument that the .iso is in the same folder as the virtual drive that you created is, and that it is also our working directory.

kvm -cdrom net-inst.iso -hda debian.img -m 512 -boot d

First this calls kvm and tells it that the .iso image is actually a CD-drive, then the "-hda debian.img" is its hard drive. The "-m 512" tells it that its a computer with 512 meg of memory. Careful here because this is the amount of physical memory that its going to block out for itself. Rule of thumb is no more than 1/2 of your actual physical memory. The final bit is -boot d, it tells it to boot from the cdrom drive.

kvm -cdrom /dev/cdrom -hda debian.img -m 512 -boot d

The only difference here is that you're pointing it to your real cd drive here. If /dev/cdrom doesn't work for you then you can "cat /etc/fstab" and look there to see what you cdrom drive really is.

Now I mentioned that you might have problems doing an install using kvm, if this happens you have to explicitly tell qemu to not use the kvm module in the command line like this:

qemu -no-kvm -cdrom /dev/cdrom -hda debian.img -m 512 -boot d

Lets move on with the thought that you have installed your new virtual operating system. You're going to want to get in and play with it. The command for this is simply:

kvm -hda debian.img -m 512

With that you will be up and running in your new OS running inside your existing. However there is another trick that is really handy that will let you do whatever you want without permanently breaking this new creation, overlay files. Which basically takes a snapshot of your virtual drive and then run it from the overlay instead of the virtual drive. Really handy if you want to have several versions accessible but only have to do the install process once. Say like having a version of stable a version of testing and a version of unstable all available from the same install. To do this its as simple as:

qemu-img create -b debian.img -f qcow stable.ovl

To boot into this you just change the command line a little bit and tell it to use the overlay file you just made.

kvm -hda stable.ovl -m 512

As you can see the easy way to run multiple versions off the same install would be to do a base install of stable and then make your overlay file for it. Next you would make an overlay file named something like testing.ovl and another for unstable.ovl all from the debian.img that we made to start.

Then simply fire each up in turn via the overlay files, edit your /etc/sources.list to what ever you want and update yourself into debian nirvana.

My thanks to Scott Ruecker over at lxer.com for asking the question that started the process for this how-to. As I said though there are tons of switches that will add functionality to your virtual machine. More than I can adequately explain as I haven't managed to figure them all out yet either. This how to was written with the whole intent to get someone armed and dangerous before kicking them out the door and isn't intended to be all inclusive.

For more information on this topic you can start at the qemu homepage.

Have fun.
~Az

 

 


Posted by Anonymous (213.9.xx.xx) on Fri 24 Aug 2007 at 10:54
Maybe you should add the information that the KVM feature needs to be enabled in the BIOS and that the CPU needs to support the kvm feature, so if you don'get any output when running...

egrep '^flags.*(vmx|svm)' /proc/cpuinfo

... the CPUs is not supporting it.

Kind regards, mistersixt.

[ Parent | Reply to this comment ]

Posted by Azerthoth (65.74.xx.xx) on Fri 24 Aug 2007 at 20:35
[ Send Message | View Weblogs ]
Thanks, I had not run into that issue.

[ Parent | Reply to this comment ]

Posted by Anonymous (122.57.xx.xx) on Tue 4 Mar 2008 at 12:24
You do not need KVM. QEMU is quite OK without KVM, although is much better with it.

For an article on how to run Vista in QEMU, try,...

"QEMU (The Kernel Virtual Machine) and Windows Vista." at:

http://linuxhelp.150m.com/win/qemu-vista.htm
http://linux.50webs.org/win/qemu-vista.htm

[ Parent | Reply to this comment ]

Posted by Anonymous (213.171.xx.xx) on Fri 24 Aug 2007 at 15:34
"kqemu [...] is available in Debian stable and therefor as easy to install as:
aptitude install kqemu"

Let's see:
mybox:~# aptitude search kqemu
mybox:~# aptitude search qemu
i qemu - fast processor emulator
p qemu-launcher - GTK+ front-end to QEMU computer emulator
p qemuctl - controlling GUI for qemu
mybox:~#proyectos3:~# cat /etc/debian_version
4.0
mybox:~#

So, sorry but no: kqemu is *not* in Debian.
Maybe the case is I don't use "contrib non-free"?

Let's try again with those repositories avaliable:

mybox:~# aptitude search kqemu
p kqemu-common - Common files for the QEMU Accelerator module
p kqemu-source - Source for the QEMU Accelerator module
mybox:~#

Again, there's no such package as 'kqemu', and even if it were, please remember that regarding contrib and non-free, "The packages in these areas are not part of the Debian system" (from Debian's Social Contract).

[ Parent | Reply to this comment ]

Posted by Anonymous (72.240.xx.xx) on Fri 24 Aug 2007 at 17:43
Indeed kqemu is part of non-free for Etch. In Lenny and Sid it is in main. It was unfortunate that kqemu was released under a free license after the freeze was in place.

Certainly you could simply install the package in Lenny or Sid if you are averse to using non-free software.

[ Parent | Reply to this comment ]

Posted by Anonymous (213.171.xx.xx) on Mon 27 Aug 2007 at 08:03
"Indeed kqemu is part of non-free"

Indeed kqemu is *not* part of non-free. I would say I already offered enough evidence of it in my previous post. kqemu-common and kqemu-source are.

[ Parent | Reply to this comment ]

Posted by Anonymous (137.222.xx.xx) on Tue 4 Sep 2007 at 11:18
There is no binary package called kqemu. Anywhere in debian. Rather than arguing about it, check using dpkg, apt-cache or on packages.debian.org.

You need to either install kqemu-common kqemu-modules-$(uname -r) or kqemu-source and then use m-a on for kqemu.

[ Parent | Reply to this comment ]

Posted by Anonymous (68.229.xx.xx) on Fri 14 Sep 2007 at 04:10
Yep. The package is kqemu-source

> apt-get install qemu qemu-utils qemu-launcher m-a
> m-a update
> m-a prepare
> m-a a-i kqemu
"windows on linux"

I may not be 100% accurate, but it's still pretty simple.

And Kqemu used to be non-free. But, I suppose in response to things like KVM, but I beleive the Win4Lin folks helped pay for the release of Kqemu into open source.

Kqemu, in my experiance, is more stable and pretty much as fast as KVM. Kvm will end up being much faster as hardware support for virtualization improves, but kqemu kicks-ass right now.

[ Parent | Reply to this comment ]

Posted by Azerthoth (65.74.xx.xx) on Fri 24 Aug 2007 at 20:33
[ Send Message | View Weblogs ]
Please note at the top of the article it states that you will find kqemu in unstable not stable. As for the other issue's you pointed out, I'm not positive I can respond without giving offense, so I wont.

[ Parent | Reply to this comment ]

Posted by Anonymous (213.171.xx.xx) on Mon 27 Aug 2007 at 08:06
"Please note at the top of the article it states that you will find kqemu in unstable not stable."

Please note that it is kvm, not kqemu, the one referred to be in unstable.

[ Parent | Reply to this comment ]

Posted by Utumno (203.125.xx.xx) on Fri 24 Aug 2007 at 16:19
[ Send Message | View Utumno's Scratchpad | View Weblogs ]

I strongly recommend

echo "deb http://www.virtualbox.org/debian etch non-free" >> /etc/apt/sources.list
apt-get update
apt-get install virtualbox

VirtualBox ( www.virtualbox.org ) is based on Qemu,but much nicer. Its kernel module ( vboxsrv ) runs without any problems for me, it's fast, it's stable.
I upgraded my kernel twice already since I installed VirtualBox and the module detects that and automatically recompiles itself. Both times no problems. The graphical VM manager is really nice.

With Qemu, I oftern had lockups when I run it with --kernel-kqemu, and without it, it was visibly slower than VirtualBox.

[ Parent | Reply to this comment ]

Posted by Anonymous (81.224.xx.xx) on Mon 27 Aug 2007 at 00:15
Or even better.
Put it in a file for instance named kvm-virtualbox.org.list in directory /etc/apt/sources.list.d/

Like this:

# From http://www.virtualbox.org/
# See http://www.debian-administration.org/comment/onarticle/545/3
deb http://www.virtualbox.org/debian etch non-free
# eof

You can then easy unselect this by changeing suffix to something else than .list (.list-unselected)

[ Parent | Reply to this comment ]

Posted by Anonymous (68.74.xx.xx) on Fri 24 Aug 2007 at 18:33
Correct me if I'm wrong, but can't you use modprobe to load the driver and not need a reboot?

[ Parent | Reply to this comment ]

Posted by Anonymous (68.74.xx.xx) on Fri 24 Aug 2007 at 18:35
Sorry, not enough coffee this morning :(

[ Parent | Reply to this comment ]

Posted by Anonymous (82.226.xx.xx) on Fri 28 Sep 2007 at 02:02
Hi,

Tested on a XEON X5355 64BIts 2.66GHz quadri core. It's a VMX suporting CPU.
On this configuration KVM is the only choice with VMWARE ESX.

After getting the last source of KVM, patching the Kernel as recommended, i'm using the customized QEMU (qemu-system-x86_64) to make it work with the network. Tested on a Debian AMD64 Etch for the host. The working virtual machines Debian AMD64 testing work just fine and with no problem. Didn't get work Kubuntu. OpenBSD get a bad disk geometry but launch perfectly.
KVM is a really good & very light alternative, no need to have a special Kernel for the Virtual Machine, that's just amazing.

Regards,
Sacha.

[ Parent | Reply to this comment ]

Posted by Anonymous (203.126.xx.xx) on Wed 12 Dec 2007 at 18:46
KVM can cause issues with suspend/resume. The quirk checker script from the pm-utils website says "KVM doesn't suspend correctly in kernels earlier than 2.6.23-4".

Suspend/resume worked fine for me but KVM failed after resume. KVM crashed immediately after starting and entered waiting state. It can't be killed and the bad thing is it doesn't release the memory assigned to the virtual OS. The only way out is to reboot.

(Or)
do this after resume.
#rmmod kvm-amd (kvm-intel for intel users)
#modprobe kvm-amd

Then proceed to use KVM. This should help.



KVM rocks. The performance is quite good and it somehow gives you a good feeling.

[ Parent | Reply to this comment ]

Posted by Anonymous (87.194.xx.xx) on Mon 6 Oct 2008 at 17:30
"kvm is in sid/unstable so you will have to add that to your apt sources if you're using someting different.
Become root and edit your sources list:
nano /etc/apt/sources.list

add this:
deb http://ftp.debian.org/debian unstable

Now update by running :
apt-get update
aptitude install kvm

After kvm and associated packages are installed it would be best to edit your sources.list again and comment out the line where you added in unstable with a "#" character at the beginning of the line."

Somebody should remove this. Just shoving in the sid source line and installing a package is a very impressive way to totally bork your installation.

Why?

Well, if you're running stable, you probably want stability. Look at the dependencies of the KVM package from sid:

"...
libc6 (>= 2.7-1)
GNU C Library: Shared libraries
..."

By installing the KVM package directly from sid onto your lovely stable system, you have now upgraded a rather vital package that basically your entire system relies on. What if the sid version of libc6 has some show stopping bugs in it? You're stuffed.

INSTEAD of just dragging packages in from unstable without a thought as to what you're bringing into your system, compile KVM (and its dependencies not in stable) from source. That way the package you end up with doesn't have the version specific dependencies that force you to upgrade your libraries to unstable versions. Anybody that tells you otherwise is a moron, or trying to sell you something. Or both.

[ Parent | Reply to this comment ]

Posted by Anonymous (87.194.xx.xx) on Wed 23 Sep 2009 at 17:02
Just a note about kqemu, you get much better performance if you add the "-kernel-kqemu" argument when starting QEMU. This tells QEMU to execute kernel and user space code natively, without it user space code is ran natively and kernel code goes to the JIT compiler. Although it can cause a few problems... Locks up the Windows XP installer for instance, but once installed it runs fine.

[ Parent | Reply to this comment ]

Posted by Anonymous (76.179.xx.xx) on Wed 20 Jan 2010 at 16:40
Installing ubuntu studio with qemu with an iso image and when i get to partitioning disk Part 1 - Ext 4 it stops at 33 % ? Any suggestions?

[ Parent | Reply to this comment ]

Posted by velocity (115.113.xx.xx) on Wed 19 May 2010 at 10:12
[ Send Message ]
There was no fun as you mention.
I added to sources.list
deb http://ftp.debian.org/debian unstable

and did apt-get update and got following
kqemu is not available as a package this article needs to be updated.

E: Malformed line 35 in source list /etc/apt/sources.list (dist parse)
E: Malformed line 35 in source list /etc/apt/sources.list (dist parse)
E: The list of sources could not be read.
E: Malformed line 35 in source list /etc/apt/sources.list (dist parse)
E: The list of sources could not be read.
E: Malformed line 35 in source list /etc/apt/sources.list (dist parse)
E: Couldn't read list of package sources

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

Which init system are you using in Debian?






( 1061 votes ~ 6 comments )

 

 

Related Links