Difference between revisions of "HOWTO: Using KVM"

From Sabayon Wiki
Jump to: navigation, search
m (forget to mention sudo)
(remove sudo)
 
Line 17: Line 17:
 
Install qemu, libvirt and virt-manager:
 
Install qemu, libvirt and virt-manager:
  
  # sudo equo install qemu libvirt virt-manager
+
  # equo install qemu libvirt virt-manager
  
 
== Permissions ==
 
== Permissions ==
Line 24: Line 24:
 
By default the permissions on the device /dev/kvm are not enough to use KVM as a non-privileged user. You can correct this to change the group owner of this device to the group kvm.
 
By default the permissions on the device /dev/kvm are not enough to use KVM as a non-privileged user. You can correct this to change the group owner of this device to the group kvm.
  
  # sudo chown root:kvm /dev/kvm
+
  # chown root:kvm /dev/kvm
 
   
 
   
  # sudo chmod 660 /dev/kvm
+
  # chmod 660 /dev/kvm
  
 
And make a user a member of this group:
 
And make a user a member of this group:
  
  # sudo gpasswd -a <user> kvm
+
  # gpasswd -a <user> kvm
  
 
Also qemu must be a member of the kvm group:
 
Also qemu must be a member of the kvm group:
  
  # sudo gpasswd -a qemu kvm
+
  # gpasswd -a qemu kvm
  
 
This solution is not persistent across reboots, for that you have to create an udev rule: /etc/udev/rules.d/65-kvm.rules
 
This solution is not persistent across reboots, for that you have to create an udev rule: /etc/udev/rules.d/65-kvm.rules
Line 53: Line 53:
 
Create a group: libvirt and add user to this group:
 
Create a group: libvirt and add user to this group:
  
  # sudo groupadd libvirt
+
  # groupadd libvirt
  # sudo gpasswd -a <user> libvirt
+
  # gpasswd -a <user> libvirt
  
 
== Getting started ==
 
== Getting started ==
Line 60: Line 60:
 
Start the libvirtd daemon and make it started at boot.
 
Start the libvirtd daemon and make it started at boot.
  
  # sudo systemctl enable libvirtd  
+
  # systemctl enable libvirtd  
  
  # sudo systemctl start libvirtd
+
  # systemctl start libvirtd
  
 
It's not the purpose of this article to explain KVM and networking, but to getting started and provide networking to the virtual machine via NAT:  
 
It's not the purpose of this article to explain KVM and networking, but to getting started and provide networking to the virtual machine via NAT:  
Line 76: Line 76:
 
It's also not the purpose of this artice to explain KVM and networking, but it's a good idea to have an shared library with ISO files. The best way to do this:
 
It's also not the purpose of this artice to explain KVM and networking, but it's a good idea to have an shared library with ISO files. The best way to do this:
  
  # sudo mkdir /iso
+
  # mkdir /iso
  # sudo chmod -R g+rwx /iso
+
  # chmod -R g+rwx /iso
  # sudo chmod -R o+rx /iso
+
  # chmod -R o+rx /iso
  # sudo chgrp -R qemu /iso
+
  # chgrp -R qemu /iso
  
 
And add it to the storage pool in virt-manager.
 
And add it to the storage pool in virt-manager.
  
 
[[Category:Virtual Machines|Using KVM and Virt-Manager]]
 
[[Category:Virtual Machines|Using KVM and Virt-Manager]]

Latest revision as of 11:50, 16 November 2013

i18n: en

Introduction

KVM (for Kernel-based Virtual Machine) is a full virtualization solution for Linux on x86 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.

Qemu is the virtual machine monitor (VMM), to provide virtual machine hardware.

Libvirt is a layer providing a hypervisor agnostic management layer and api between the hypervisor and a management tool like virsh and virt-manager.

Virt-manager is a graphical tool to configure and manage storage, network and virtual machines.

Installation

These instructions assume that you have enough rights to install software. In this howto I am using the sudo mechanisme (Sabayon default: the user is a member of the wheel group).

Install qemu, libvirt and virt-manager:

# equo install qemu libvirt virt-manager

Permissions

The kvm module exports a device called /dev/kvm. By default the permissions on the device /dev/kvm are not enough to use KVM as a non-privileged user. You can correct this to change the group owner of this device to the group kvm.

# chown root:kvm /dev/kvm

# chmod 660 /dev/kvm

And make a user a member of this group:

# gpasswd -a <user> kvm

Also qemu must be a member of the kvm group:

# gpasswd -a qemu kvm

This solution is not persistent across reboots, for that you have to create an udev rule: /etc/udev/rules.d/65-kvm.rules

KERNEL=="kvm", NAME="%k", GROUP="kvm", MODE="0660"

If you are using virt-manager as a normal user, you're still not able to access libvirt. You have to create a polkit rule to make this possible. Create a file /etc/polkit-1/rules.d/51-libvirt.rules

polkit.addRule(function(action, subject) {
   if (action.id == "org.libvirt.unix.manage"
           && subject.local
           && subject.active
           && subject.isInGroup("libvirt")) {
       return polkit.Result.YES;
   }
});

Create a group: libvirt and add user to this group:

# groupadd libvirt
# gpasswd -a <user> libvirt

Getting started

Start the libvirtd daemon and make it started at boot.

# systemctl enable libvirtd 
# systemctl start libvirtd

It's not the purpose of this article to explain KVM and networking, but to getting started and provide networking to the virtual machine via NAT:

1) Start virt-manager for the first time. If there popups a question about installing the KVM packages: ignore it. A connection to the localhost will be made.

2) Go to the menu-item "Edit" and select "Connection Details"

3) Select the tab "Virtual Networks", click on the plus-sign.

4) Choose a name for the virtual network, accept the suggested IPv4 range or fill in another private range (http://www.ietf.org/rfc/rfc1918.txt) and enable DHCP. Connect to the physical network (the network of your physical host) by forwarding the virtual network traffic to the destination "Any physical device", mode: NAT.

It's also not the purpose of this artice to explain KVM and networking, but it's a good idea to have an shared library with ISO files. The best way to do this:

# mkdir /iso
# chmod -R g+rwx /iso
# chmod -R o+rx /iso
# chgrp -R qemu /iso

And add it to the storage pool in virt-manager.