Difference between revisions of "KVM"

From Funtoo Linux
Jump to: navigation, search
m (Starting your first KVM virtual machine)
Line 43: Line 43:
 
<console>
 
<console>
 
# ##i##qemu-img create -f qcow2 vdisk.qcow2 10
 
# ##i##qemu-img create -f qcow2 vdisk.qcow2 10
# ##i##qemu-kvm vdisk.qcow2 -m 1024 -cdrom systemrescuecd.iso  -vnc 127.0.0.1:1 -cpu host -net nic -net user
+
# ##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'
 
VNC server running on `127.0.0.1:5900'
 
</console>
 
</console>
Line 51: Line 51:
 
== Networking Options ==
 
== 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. Please see read [[Funtoo_Linux_Networking#Bridge_Configuration]] on how to configure a bridge
+
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 your bridge and add a tap interface as a slave, make sure your user or group matches what you put in the template, Group is recomended over user for KVM.
+
First, create <tt>/etc/qemu-ifup</tt> and add the following to it. Replace <tt>brlan</tt> with the name of your bridge:
  
You should have a bridge configuration something like this.
+
<syntaxhighlight lang="bash">
<console>
+
#!/bin/bash
# brctl show
+
ifconfig $1 0.0.0.0 promisc up
bridge name    bridge id              STP enabled    interfaces
+
brctl addif brlan $1
br0            8000.00248c52ddc9      yes            eth1
+
sleep 2
                                                        tap0
+
</syntaxhighlight>
</console>
+
  
 
+
Make it executable:
To have kvm use the tap0 interface start the virtual machine as follows:
+
  
 
<console>
 
<console>
# ##i##qemu-kvm vdisk.qcow2 -m 1024 -cdrom systemrescuecd-x86-2.8.0.iso -cpu host -vnc 127.0.0.1:1 \
+
# ##i##chmod +x /etc/qemu-ifup
-net nic -net tap,ifname=tap0,script=no,downscript=no
+
 
</console>
 
</console>
  
 
+
Start the virtual machine as follows:
 
+
To enable the higher performance Virto network driver use the following method
+
  
 
<console>
 
<console>
# ##i##qemu-kvm vdisk.qcow2 -m 1024 -cdrom systemrescuecd-x86-2.8.0.iso -cpu host -vnc 127.0.0.1:1 \
+
# ##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
-net nic,model=virtio,macaddr=10:22:33:44:51:66 -net tap,ifname=tap0,script=no,downscript=no
+
 
</console>
 
</console>
;Note
 
 
:the '''macaddr''' portion, this is the mac address that the VM's nic will have.  It must be unique across all mac addresses, otherwise you will end up with dropped packets and dmesg spam, this is set in the tap template
 
 
;Note
 
 
:This also requires virtio drivers to be installed in the guest and host
 
  
 
== Tweaking KVM ==
 
== Tweaking KVM ==
 
=== Virtio Disks ===
 
 
If you want to enable the higher performance virtio block driver, you will need to use the following switch
 
<console>
 
-drive file=/vm.img,if=virtio
 
</console>
 
;Note
 
:This requires kernel modules for the guest and host
 
:Windows guest support is spotty, drivers can be acquired at the linux-kvm site [http://www.linux-kvm.org/page/WindowsGuestDrivers/Download_Drivers]
 
  
 
=== VNC Output ===
 
=== VNC Output ===

Revision as of 19:06, 22 October 2012

Contents

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 Paravirtualized Guest Support. Under the Paravirtualized Guest Support menu, enable any options related to KVM, such as KVM paravirtualized clock and in particular KVM Guest Support.

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.

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

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 /etc/make.conf:

QEMU_SOFTMMU_TARGETS="i386 x86_64"
QEMU_USER_TARGETS="i386 x86_64"

Once the make.conf variables above are set, emerge qemu-kvm:

#  emerge qemu-kvm

Initial Setup

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

# modprobe kvm_intel

Starting your first KVM virtual machine

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:

# qemu-img create -f qcow2 vdisk.qcow2 10
# 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'

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 /etc/qemu-ifup and add the following to it. Replace brlan with the name of your bridge:

#!/bin/bash
ifconfig $1 0.0.0.0 promisc up
brctl addif brlan $1
sleep 2

Make it executable:

# chmod +x /etc/qemu-ifup

Start the virtual machine as follows:

# 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

Tweaking KVM

VNC Output

If you wanted to have VNC listen on a different IP address or port, you can use the format -vnc IP:vncnum 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 -cpu host (to export all of the host's CPU features) and -smp cores=X,threads=Y, 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.

Personal tools
Namespaces

Variants
Actions
Categories
Toolbox
Stuff