HOWTO KVM

From Gentoo Linux Wiki

Jump to: navigation, search
This article is part of the HOWTO series.
Installation Kernel & Hardware Networks Portage Software System X Server Gaming Non-x86 Emulators Misc

Contents

[edit] Introduction

KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 or x86_64 (AKA amd64) 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 which together provide for all that is needed to get virtualized CPU support.

KVM also requires a modified QEMU to provide for the remaining virtualized hardware (a virtual machine) including network, disk, and video adapters as well as block devices like hard drives, cdrom or floppies.

Be aware that not all Intel or AMD CPUs have virtualization extensions and some might need to have also a BIOS setting enabled in order to be able to use it. Refer to the KVM FAQ for more information.

[edit] Installation

Gentoo bug report 157987 is still waiting for a maintainer which is why there is not yet a kvm package to use from the official portage tree.

There are several different user proposed ebuilds available in the forums and bugzilla to choose from. The following instructions are based on the use of one of the many 3rd party overlay available.

[edit] Adding the overlay

[edit] layman

The following instructions assume that you already have layman installed in your system, if that is not the case proceed to install and configure layman as explained in the Gentoo overlays user guide

Code: Using layman to install the overlay
 # layman -f -o http://tapir.sajinet.com.pe/gentoo/layman.xml -a sajinet

To sync your overlay and pull new updates when they are available:

Code: Using layman to refresh the overlay
 # layman -s sajinet

[edit] paludis

The following instructions assume that you already have paludis installed in your system, if that is not the case proceed to install and configure paludis as explained in the Paludis Getting Started

The default configuration assume that the files with configuration for particular repositories (overlays) are in /etc/paludis/repositories

Please create the configuration file for sajinet overlay as below

Code: sajinet.conf

location = ${ROOT}/usr/overlays/sajinet
sync = rsync://tapir.sajinet.com.pe/portage
#profiles = ${ROOT}/usr/portage/profiles/default-linux/x86/2007.0/desktop
#eclassdirs = /usr/portage/eclass
#distdir = /usr/portage/distfiles
master_repository = gentoo
format = ebuild
names_cache = ${location}/.cache/names
write_cache = /var/cache/paludis/metadata

DON'T create overlay directory in other overlays directories. This is because when syncing any of overlay it will remove all directories that not belong to it. For example if your portage is in /usr/portage/ don't create any other overlay in this directory.

To install, sync your overlay and pull new updates when they are available:

Code: Using paludis to install/update the overlay
 # paludis -s sajinet

[edit] Configuring your USE flags

The ebuild has several configurable options that can be used to adjust it to your environment better. For more information about how to set the USE flags refer to HOWTO Use Portage Correctly, but to configure the recommended defaults in a non invasive way execute as root :

Code: Adding the recommended USE flags to portage for this package
 # echo "app-emulation/kvm kvm ncurses sdl" >> /etc/portage/package.use

[edit] alsa

Adds support for media-libs/alsa-lib (Advanced Linux Sound Architecture).

This used to be enabled by default for "<app-emulation/kvm-47" but was turned off because the same functionality is available by using alsa through SDL instead.

[edit] bios

Add support to recompile the bios and vgabios

KVM uses a patched version of the BOCHS bios which includes the patches added to QEMU as well as KVM specific patches (as needed for Windows Vista guests for example). The compiled version of it is provided and used by default as a BLOB (because building it requires several dependencies that are not commonly installed by default in common distributions), this flag allows you to rebuild your BIOS using the provided sources and replace the BIOS BLOB with your own version.

KVM uses a patched version of the vgabios which includes support for Wide Screen modes and that is provided in binary form for convenience but will be rebuild if this flag is enabled.

[edit] gcc4

Add support for gcc 4.

The KVM userspace application is a modified version of QEMU which talks to the kernel module CPU emulator through /dev/kvm.

QEMU uses a dynamic generator (dyngen) that disassembles code compiled by gcc to be used to emulate the instructions of the virtual CPU. This relies in undocumented features of gcc which are no longer valid for >=sys-devel/gcc-4.

When running with KVM's kernel module loaded, most of the CPU instructions are instead being emulated through hardware and therefore it is most likely possible (but not recommended) to run a userspace application compiled with gcc 4 without segmentation faults or other crashes as far as the kvm module is loaded and kvm emulation isn't disabled (-no-kvm).

When this flag is disabled, =sys-devel/gcc-3.4.6-r2 will be installed and used to compile the userspace application (qemu).

[edit] gnutls

Adds support for net-libs/gnutls.

Since >=app-emulation/kvm-45, the VNC server option available from the userspace application with the -vnc option is able to use TLS to encrypt the session established with the vnc client, this flag enables support for that functionality. If disabled vnc sessions are still possible but only using unencrypted channels.

[edit] kvm

Enables the kernel module.

If enabled (recommended), will prepare and install the kernel module

the kernel module is included with linux since version 2.6.20, but the first 2 versions of it up to kernel 2.6.21 didn't have an stable ABI and are therefore tied to specific versions of the kvm package as shown in :

 http://kvm.qumranet.com/kvmwiki/Downloads

the kernel module included with kernel 2.6.22 (kvm api version 12) or newer will be able to talk to recent versions of the userspace application but not be able to use all the provided functionality.

[edit] ncurses

for >=app-emulation/kvm-63 support was added to create a curses console as an option to using SDL and an X server.

If enabled (recommended), will allow you to create an interactive text console in the same terminal were kvm is running if kvm was called wit the -curses flag.

[edit] qemu

Use QEMU's qemu-img.

If enabled, will avoid installing kvm's version of qemu-img (to prevent a conflict) and will pull qemu-softmmu and use its qemu-img instead.

This will allow you to use qemu + kqemu in parallel with kvm and have both packages installed side by side.

[edit] sdl

Adds support for Simple Direct Layer (media library)

If enabled (recommended), will use SDL to emulate a console, so you can interact with your virtual machine through and X session.

If you are only interested on using kvm to run "headless" virtual machines then be sure to start them with either an emulated serial console going into a file (or /dev/null) or a vnc server console and disable this flag.

[edit] Installing

Code: Using emerge to install the package and its dependencies
 # emerge -Dvu kvm


[THIS WILL TAKE A WHILE]

[edit] Getting Started

[edit] Loading the kernel modules

If you have kernel support compiled into your kernel then you are ready to go, but if you where using the external modules which came with this package then you'll need to load them first.

There are 2 modules to load and 3 to choose from. You will need the generic one that enables KVM support (kvm.ko) and a CPU specific one (kvm-intel.ko or kvm-amd.ko) that depends on the generic one and that should match the type of CPU that you have.

You can use modprobe to resolve dependencies and load the module for you

Code: Using modprobe to load the module for a Core2 system
 # modprobe kvm-intel

Or if you want to load the modules by hand (remember to do it in the right order to cover for the dependencies):

Code: Using insmod to load the modules for a Core2 system
 # insmod /lib/modules/`uname -r`/misc/kvm.ko
 # insmod /lib/modules/`uname -r`/misc/kvm-intel.ko

[edit] Adding access to /dev/kvm to your user

The /dev/kvm device is owned by root but with read/write priviledges for the kvm group, so in order to be able to access it you have to add your user to it (using a user named "carenas" below as an example)

Code: Adding the "carenas" user to the kvm group
 # gpasswd -a carenas kvm

[edit] Create a virtual disk image

Code: Using qemu-img to create a RAW 10G disk image
 # qemu-img create gentoo-i386.img 10G

[edit] Installing the guest OS to your image

[edit] Gentoo example

Code: Installing gentoo into the disk image using an livecd ISO
 # kvm -hda gentoo-i386.img -cdrom livecd-i686-installer-2007.0.iso -boot d

[edit] Windows example

This is how i started my Windows installation using the kvm python script which is not distributed with the last versions of the package. I still have to fix my tap/tunnel support so i use --no-tap for now. (which means no networking!)

Code: Installing windows into the disk image using an real Windows CD-ROM
 # kvm --no-tap windows.img --install --cdrom /dev/cdrom

Image:windows_kvm_install.png

Because of ACPI problems you need to press F5 while starting the installer for the first time. Here you should choose "Standard PC", as showing in the screenshot.

Read http://kvm.qumranet.com/kvmwiki/Windows_ACPI_Workaround for more info.

Image:windows_kvm_acpi.png

[edit] Booting your system

Code: Booting your virtual machine
 # kvm gentoo-i386.img

[edit] Troubleshooting

[edit] My x86 Gentoo Guest dies with a kernel panic

KVM doesn't emulate the MMR registers needed for performance metrics and therefore it will generate a General Protection Fault if they were used. A workaround was added to the Linux kernel 2.6.22.5 and therefore upgrading the kernel in your guest will fix this problem. If you can't upgrade your kernel try using the "nolapic" or "nmi_watchdog=0" boot parameters instead.

[edit] But I can't boot my guest anymore

If you are using USE="qemu" then you can use qemu + kqemu instead to try to fix any problems with the guest that affected kvm, if that doesn't work try using kvm -no-kvm or qemu -no-kqemu as a last resort.

[edit] emerge failed on my system after following wiki

if you are running a stable version of gentoo, you need to tell portage to allow for unstable packages of kvm, which in a 64 bit Gentoo will be similar to :

Code: Adding the ~amd64 keyword for kvm
 # echo "app-emulation/kvm ~amd64" >> /etc/portage/package.keywords

[edit] can't find any packages to install from the overlay

Do not forget this step when installed layman

Code: Adding layman to the system configuration
 # echo "source /usr/portage/local/layman/make.conf" >> /etc/make.conf

[edit] External References

Personal tools