Difference between revisions of "KVM"

(Kernel Setup)
(info about autoloading)
 
(10 intermediate revisions by 4 users not shown)
Line 8: Line 8:
 
To enable KVM, the following kernel config parameters should be enabled (this is based on a 3.x kernel):
 
To enable KVM, the following kernel config parameters should be enabled (this is based on a 3.x kernel):
  
Under <tt>Processor type and features</tt>, enable <tt>Linux guest support</tt>, and enable the following options:
+
Under <code>Processor type and features</code>, enable <code>Linux guest support</code>, and enable the following options:
  
 
{{kernelop|title=Processor type and features,Linux guest support|desc=
 
{{kernelop|title=Processor type and features,Linux guest support|desc=
Line 22: Line 22:
  
  
Under the <tt>Virtualization</tt> category from the main kernel config menu, enable <tt>Kernel-based Virtual Machine (KVM) support</tt>, and enable at least one type of KVM, either for Intel or AMD processors. It is also recommended to enable <tt>Host kernel acceleration for virtio net</tt>.
+
Under the <code>Virtualization</code> category from the main kernel config menu, enable <code>Kernel-based Virtual Machine (KVM) support</code>, and enable at least one type of KVM, either for Intel or AMD processors. It is also recommended to enable <code>Host kernel acceleration for virtio net</code>.
  
 
{{kernelop|title=Virtualization|desc=
 
{{kernelop|title=Virtualization|desc=
Line 38: Line 38:
 
== User-space tools ==
 
== User-space tools ==
  
KVM is essentially a kernel-accelerated version of QEMU. To enable KVM support in the user-space tools, add the following lines to <tt>/etc/portage/make.conf</tt>:
+
If you are using QEMU on your desktop, add the following USE flag to <code>/etc/portage/make.conf</code>:
  
 
<pre>
 
<pre>
QEMU_SOFTMMU_TARGETS="i386 x86_64"
+
USE="$USE usbredir"
QEMU_USER_TARGETS="i386 x86_64"
+
 
</pre>
 
</pre>
 +
This will enable good mouse support for QEMU on your desktop.
  
Once the <tt>make.conf</tt> variables above are set, emerge qemu:
+
Now, emerge qemu:
  
 
<console>
 
<console>
Line 59: Line 59:
 
</console>
 
</console>
  
== Starting your first KVM virtual machine ==
+
{{note|Alternatively you can edit /etc/conf.d/modules to load it automatically}}
  
To start your first KVM virtual machine, first download SysRescueCD and save it to systemrescuecd.iso. Then use the following commands, which will create a 10GB qcow disk image to use for the first disk, and then the next command will start your virtual machine, booting from the CD:
+
== Next Steps ==
  
<console>
+
{{important|Before using KVM, be sure that your user account is in the <code>kvm</code> group. You will need to use a command such as <code>vigr</code> or <code>gpasswd -a $USER kvm</code> as root to do this, and then log out and log back in for this to take effect.}}
# ##i##qemu-img create -f qcow2 vdisk.qcow2 10
+
# ##i##qemu-system-x86_64 vdisk.qcow2 -m 1024 -cdrom systemrescuecd.iso  -vnc 127.0.0.1:1 -cpu host -net nic -net user
+
VNC server running on `127.0.0.1:5900'
+
</console>
+
 
+
Now you should be able to use a VNC client to connect to 127.0.0.1:5901 (VNC session 1) and access your virtual machine.
+
 
+
== Networking Options ==
+
 
+
Above, networking will be enabled but will be on its own private LAN, and ping will not work. If you have a local bridge that you use for networking, the following steps will allow you use your existing bridge to provide higher-performance and full-featured network access to your virtual machine.
+
 
+
First, create <tt>/etc/qemu-ifup</tt> and add the following to it. Replace <tt>brlan</tt> with the name of your bridge:
+
 
+
<syntaxhighlight lang="bash">
+
#!/bin/bash
+
ifconfig $1 0.0.0.0 promisc up
+
brctl addif brlan $1
+
sleep 2
+
</syntaxhighlight>
+
 
+
Make it executable:
+
  
 +
{{important|Atm the group <code>kvm</code> has no access to /dev/kvm. Change this with
 
<console>
 
<console>
# ##i##chmod +x /etc/qemu-ifup
+
# ##i##chown root:kvm /dev/kvm
 +
# ##i##chmod g+rw /dev/kvm
 
</console>
 
</console>
 +
and kvm will start like expected [https://bugs.funtoo.org/browse/FL-1626 (see bug FL-1626)] }}
  
Start the virtual machine as follows:
+
For information on how to use KVM with various operating systems, see:
 
+
<console>
+
# ##i##qemu-system-x86_64 vdisk.qcow2 -m 1024 -cdrom systemrescuecd-x86-2.8.0.iso -cpu host -vnc 127.0.0.1:1 -net nic -net tap,id=foo
+
</console>
+
 
+
== Tweaking KVM ==
+
 
+
=== VNC Output ===
+
 
+
If you wanted to have VNC listen on a different IP address or port, you can use the format <tt>-vnc IP:vncnum</tt> which will cause VNC to listen on the IP specified, and the TCP port 5900+vncnum.
+
 
+
=== CPU Settings ===
+
  
By default, the KVM guest will have one CPU with one core. To change this, use <tt>-cpu host</tt> (to export all of the host's CPU features) and <tt>-smp cores=X,threads=Y</tt>, where X is the number of cores, and Y is the number of threads on each core. You can emulate more CPUs and cores than you actually have.
+
* [[Windows 7 Virtualization with KVM]]
 +
* [[Linux on KVM]]
  
 
[[Category:Virtualization]]
 
[[Category:Virtualization]]
[[Category:OpenStack]]
+
[[Category:KVM]]
[[Category:Featured]]
+

Latest revision as of 10:06, November 12, 2014

Introduction

KVM is a hardware-accelerated full-machine hypervisor and virtualization solution included as part of kernel 2.6.20 and later. It allows you to create and start hardware-accelerated virtual machines under Linux using the QEMU tools.

Kernel Setup

To enable KVM, the following kernel config parameters should be enabled (this is based on a 3.x kernel):

Under Processor type and features, enable Linux guest support, and enable the following options:

Under Processor type and features-->Linux guest support:

--- Linux guest support
[*]   Enable paravirtualization code
[ ]     paravirt-ops debugging (NEW)
[*]     Paravirtualization layer for spinlocks
[ ]     Xen guest support (NEW)
[*]   KVM Guest support (including kvmclock) (NEW)
[ ]     Enable debug information for KVM Guests in debugfs (NEW)
[ ]   Paravirtual steal time accounting (NEW)


Under the Virtualization category from the main kernel config menu, enable Kernel-based Virtual Machine (KVM) support, and enable at least one type of KVM, either for Intel or AMD processors. It is also recommended to enable Host kernel acceleration for virtio net.

Under Virtualization:

--- Virtualization
<M>   Kernel-based Virtual Machine (KVM) support
<M>     KVM for Intel processors support
<M>     KVM for AMD processors support
[*]     KVM legacy PCI device assignment support
<M>   Host kernel accelerator for virtio net


You can use modules or build these parts directly into the kernel. Build your new kernel and modules, and reboot.

User-space tools

If you are using QEMU on your desktop, add the following USE flag to /etc/portage/make.conf:

USE="$USE usbredir"

This will enable good mouse support for QEMU on your desktop.

Now, emerge qemu:

#  emerge qemu

Initial Setup

Prior to using KVM, modprobe the appropriate accelerated driver for Intel or AMD:

# modprobe kvm_intel

Note

Alternatively you can edit /etc/conf.d/modules to load it automatically

Next Steps

Important

Before using KVM, be sure that your user account is in the kvm group. You will need to use a command such as vigr or gpasswd -a $USER kvm as root to do this, and then log out and log back in for this to take effect.

Important

Atm the group kvm has no access to /dev/kvm. Change this with

# chown root:kvm /dev/kvm
# chmod g+rw /dev/kvm

and kvm will start like expected (see bug FL-1626)

For information on how to use KVM with various operating systems, see: