# Kernel Based Virtualization (KVM) Install Problem

## Voorhees51

I'm trying to get KVM to work on my CPU that supports it.

 I was following the KVM guide https://help.ubuntu.com/community/KVM

 I compiled the KVM options in the kernel, built in to it not as modules.

  Kernel Version: 2.6.21-gentoo-r3

I emerged qemu as trying to follow the guide.

I can't find the kvm  command to start the machine, where's it at?

I want to be able to use KVM not just the qemu emulation.

Any help would be much appreciated.

 Thanks

----------

## Bones

Why are you following Ubuntu instructions?  That page is telling you to use the Ubuntu KVM package, which would install the userspace component as "kvm".  That is not how the official KVM source installs it.

Don't use the KVM version that shipped with the 2.6.21 kernel unless you want to use an older KVM version for some reason.  There is an ebuild in bugzilla that will build the KVM module and userspace for you, using a more recent version of KVM.  Or just download the KVM source and configure/compile by hand.  Either way, the KVM userspace program is "qemu", not "kvm", unless you want to manually symlink it.

----------

## Voorhees51

I was trying to follow the Ubuntu guide, because I couldn't find docs on gentoo site.

 Ok, I'll check out the e-build or compiling it by hand.

 So in the kernel do I need to even enable the Virtualization choice and the option for my intel CPU if I'm using the

  ebuild or compiling it by hand?

 Also does the kernel and the kvm packages need to be build with the same version of gcc?

 Once I get it installed, how do I tell if it is using it as opposed to not using it for emulation?

----------

## monkeynuthead

Voorhees51,

If you want to run qemu with the kvm acceleration, you need to get the kvm version of qemu (got mine from downloads here), switch to gcc 3.4.6 temporarily using gcc-config, compile it and install it.  There are instructions on how to do this on the site.  My kernel is compiled with gcc 4.1.2.  When running you'll see qemu/kvm in the window title, instead of the normal qemu.

The FAQ contains all the info needed to get things set-up for non-root use etc, etc.  Also useful is qemu site/wiki.

I'm on amd64 (2.6.21-gentoo-r2) kvm modules supplied with the kernel (compiled as modules) - the kvm-17 download is working fine for me.  Not sure whether you'll gain anything from getting the kvm modules from elsewhere - I'm just happy enough that it's working as is - although feel free to enlighten me on what I'm missing out on  :Smile: 

----------

## sundialsvc4

If you are going to try that, you would be much better to use the virtual-machine capabilities that are now available in native Linux.

----------

## Bones

 *monkeynuthead wrote:*   

> Not sure whether you'll gain anything from getting the kvm modules from elsewhere - I'm just happy enough that it's working as is - although feel free to enlighten me on what I'm missing out on 

 

The recent KVM work has included some major performance enhancements.

Also, KVM works just fine when compiled with gcc 4.  You only need 3.4.6 if you intend to use qemu without the KVM module, (i.e., with the -no-kvm switch).

----------

## monkeynuthead

 *Quote:*   

> The recent KVM work has included some major performance enhancements.

 

Ok, ta.  What's the easiest way to get hold of the latest kvm modules?

----------

## Voorhees51

So is this what I need to do to get KVM and qemu working?:

1. compile my kernel: 2.6.21-gentoo-r3 sources

   -- Do I need to compile kvm and kvm-intel options as modules or not at all?

   -- Which version of gcc 3 or 4?

2. Download the kvm-17 sources from the KVM website http://kvm.qumranet.com/kvmwiki/Downloads

   -- Chose this because on the download page it said to if using the 2.6.21 in kernel modules

   -- If I do the 'use external modules' option from the download page,

      do I need to enable it in the kernel at all then?

3. From http://kvm.qumranet.com/kvmwiki/HOWTO site:  

   -- Compile it using the --with-patched-kernel option, or do I leave this part out?

   -- Which version of gcc 3 or 4?

   -- probe the module kvm-intel in my case

   -- Then continue following the howto

Do I need to even have the qemu packages from portage installed if I'm going this route?

Is this process correct or am I missing something?

Thanks for the Help

----------

## Bones

1.  If you want to use the latest KVM version (yes you do!), do not enable KVM in your kernel at all.  

2.  Download the latest official KVM, not KVM-17.  The tarball includes the kernel module and the modified qemu userspace.  You may compile it with gcc 4 if you intend to use qemu with KVM always enabled.  If you want to preserve qemu's full emulation ability, then you must compile it with gcc 3.  The configure script has "--qemu-cc" and "--disable-gcc-check" options for this, choose appropriately.  Full emulation with qemu is useful for older operating systems like Windows 98, and for some LiveCDs that use unsupported real mode code in their bootloaders.

3.  The --with-patched-kernel option doesn't apply since you won't be using the module shipped with 2.6.21.  After the build, modprobe kvm-intel and continue.

 *Quote:*   

> Do I need to even have the qemu packages from portage installed if I'm going this route?

 

No.  KVM uses a customized qemu.  Normal qemu doesn't support KVM (yet).

----------

## Bones

I slapped together this HOWTO for KVM.  It's sort of rough around the edges.  Is it worth posting to the HOWTO section?  Maybe the bugzilla ebuild would obsolete this too quickly?

HOWTO Compile KVM from Scratch

KVM is a full virtualization solution for Linux.  This guide shows how to build and install the latest official KVM from the tarball downloaded from the KVM SourceForge download page.  Go get the source right now.  There is also an ebuild in bugzilla that hasn't made its way into Portage or an official overlay yet.  This guide describes manually configuring and compiling.

Don't confuse KVM with kqemu.  The KVM module replaces kqemu.  However, KVM requires a processor with virtualization extensions (AMD svm or Intel vmx), while kqemu does not.

After downloading, untar the KVM source and start configuring...

Configuration and Compiling

KVM relies on a modified qemu as its userspace program to provide emulated devices and a nice interface.  The KVM tarball has the customized qemu in it; don't try to use normal qemu with KVM.  Qemu, as set up by the KVM build scripting, requires SDL and ALSA.  If you don't have libsdl and ALSA installed yet, do that now, or follow the headless box instructions below.

You must now decide how you will use KVM/qemu.  If you want to use KVM on a headless box (one without X and sound), apply the patch below and then decide whether to compile with gcc 3 or 4.

KVM on a headless box

No problem.  KVM/qemu will happily build and run on a headless box if we patch the configure script:

```

diff -u kvm-25/configure kvm-25a/configure

--- kvm-25/configure    2007-05-23 08:10:43.000000000 -0400

+++ kvm-25a/configure   2007-06-29 15:01:48.000000000 -0400

@@ -85,7 +85,8 @@

     --disable-kqemu --extra-cflags="-I $PWD/../user" \

     --extra-ldflags="-L $PWD/../user" \

     --enable-kvm --kernel-path="$libkvm_kerneldir" \

-    --enable-alsa \

+    --disable-sdl \

+    --disable-gfx-check \

     ${disable_gcc_check:+"--disable-gcc-check"} \

     --prefix="$prefix"

 )

```

Save the above patch as headless.patch somewhere, and apply it like this:

```
kvmbox kvm-28 # patch -p1 < /path/to/headless.patch
```

GCC 3 or 4?

KVM has a limitation on what it can virtualize: it doesn't handle real mode instructions very well.  This is a problem with some older operating systems like Windows 95 and DOS.  Some modern LiveCDs and OS installers also use real mode code in their bootloaders, and therefore they won't boot with KVM.  It is still possible to run these in a virtual machine using qemu.  The KVM modified qemu fully retains its emulation ability, including being able to run real mode code.  The only penalty for this is performance.

The issue with using qemu emulation is that it relies on the compiling behavior of gcc 3.4.  If qemu is compiled with gcc 4.x, it will crash when attempting to emulate the x86 processor.  However, this does not affect qemu when it uses the KVM module for virtualization.

You must make a decision now: either compile KVM with gcc 4 and lose the ability to run without the KVM module, or compile with gcc 3 and be able to virtualize with KVM or without it.

Configuring KVM to compile with GCC 3.4.6

First, install gcc 3.4.6 if you don't already have it:

```
kvmbox kvm-28 # emerge =sys-devel/gcc-3.4.6-r2
```

This will place gcc-3.4.6 in its own slot, so it won't interfere with gcc 4.x.

Now decide where to put KVM.  The "--prefix" option determines where the qemu userspace tools end up (the KVM module itself goes in the usual place in /lib). I like it in /usr/local/kvm, but you may place it elsewhere.  Run the configure script and compile:

```
kvmbox kvm-28 # ./configure --prefix=/usr/local/kvm --qemu-cc=/usr/bin/gcc-3.4.6

kvmbox kvm-28 # make && make install

```

Configuring KVM to compile with GCC 4.x

This requires passing the "--disable-gcc-check" option to the configure script.  Again, feel free to put KVM whereever you like:

```
kvmbox kvm-28 # ./configure --prefix=/usr/local/kvm --disable-gcc-check

kvmbox kvm-28 # make && make install

```

Testing

Try to modprobe your shiny new KVM module:

```
kvmbox kvm-28 # modprobe kvm-intel

   - or -

kvmbox kvm-28 # modprobe kvm-amd

```

----------

