Virtualization using KVM

Overview

This application provides a GUI tool for managing virtual machines via the libvirt library. It allows for the creation of virtual disks using the the following formats: raw, qcow, qcow2, cow, qed, vmdk, and vpc. You can manage the "virtual hardware" including dedicated RAM and CPUs. You can have control of local and remote machines either in graphical or text mode. State saving/restoration, pausing and rebooting through the GUI. Monitoring CPU load, memory usage and disk and network I/O. Adding various types of hardware to the virtual machine (including USB devices and disks).

The back end of virt-manager uses libvirt for managing Xen, QEMU & KVM virtual machines. The GUI is primarily tested with Xen and QEMU, but is intended to be portable to any virtualization backend that libvirt supports.

Installation and Basic Setup

Starting Note
It is very recommended to go over the settings prior to start creating/managing virtual machines. The default storage area is not within the /home tree and can lead to disk space shortage depending on your configuration.

1. Checking for virtualization support on your hardware.

Run the following command on your terminal.

grep -E 'svm|vmx' /proc/cpuinfo

If you don't get any output check if your BIOS configuration during boot time allows you to enable virtualization as some device come with this feature. If your BIOS has no such option, your computer's CPU may not support virtualization.

2. Installation of required packages.

sudo apt-get install libvirt-bin qemu-kvm bridge-utils virt-viewer virt-manager

3. libvirt daemon

After the required packages are installed start your libvirt daemon.
Although it would be running after installation, run the following command.

sudo /etc/init.d/libvirt-bin restart

Note: status/stop/start/restart are the options.

Add the current user you are logged in with to the "libvirtd" group.

sudo adduser $USER libvirtd

4. Bridging network interface.

Open the file "/etc/network/interfaces" in a editor and make changes according to the example file below.

Example for static ip network

##############################################################################
auto lo
iface lo inet loopback

auto eth0
iface   eth0    inet    manual

auto br0
iface   br0     inet    static
        address         192.168.1.10
        netmask         255.255.255.0
        gateway         192.168.1.1
        bridge_ports    eth0
#        bridge_stp      off  # uncomment this parameters, 
#        bridge_fd       0    # if you face any issues
#        bridge_maxwait  0    # these 3 parameters are related
                              # to the spanning tree protocol  
                              # to prevent looping of data packets
                              # in bridge mode

###############################################################################

Example for dhcp connections

###############################################################################
auto lo
iface lo inet loopback

auto eth0
iface   eth0    inet    manual

auto br0
iface   br0     inet    dhcp
        bridge_ports    eth0
#        bridge_stp      off
#        bridge_fd       0
#        bridge_maxwait  0

###############################################################################

For more details on bridging check man page of bridge utils.

man bridge-utils-interfaces

Reload your new network configurations by running the below command on your terminal.

 sudo ifdown br0

Followed by:

 sudo ifup br0

Note: The usage of "sudo /etc/init.d/networking restart" is no longer supported as suggested by the respective scripts, at least since Trisquel 7.0 LTS Belenos.

4.1 Checking network connections.

1. Run ifconfig to check if your network interfaces are up.

br0       Link encap:Ethernet  HWaddr 00:78:12:81:05:a6  
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::225:22ff:fe41:5e6/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3382 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3420 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:3187503 (3.1 MB)  TX bytes:527993 (527.9 KB)

eth0      Link encap:Ethernet  HWaddr 00:78:12:81:05:a6  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9631 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9736 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:10185985 (10.1 MB)  TX bytes:1163285 (1.1 MB)
          Interrupt:42 Base address:0x6000 

2. Ping your gateway's IP address.

ping 192.168.1.1

3. Ping to any IP address or IP of your DNS server.

ping 106.186.17.181

#That's an OpenNIC DNS server

4. Ping "trisquel.info".

If there were no issues then you are ready to start installing your virtual machine.

5. Virtual Machine Manager.
Start the virtual machine manager.

virt-manager

You do not need to run the above command as root or as "super user".

The screenshots below will guide you through the creation of a very simple virtual machine.






Access Control

To be able to access the libvirt socket and manage the virtual machines the user needs to belong to the "libvirt" group or have superuser privileges. Alternatively, you can use the session url links that look like "qemu:///session" for viewing.

Connecting a simple usb storage device

Note: The guest you want to share the device with should be powered off at the time of configuration, if it is a local virtual machine.
Go to the hardware details info for your guest and click on "Add Hardware". Choose the "USB host device" tab and then select the appropriately named device. Finish off with "Finish". It should be detected/mountable upon booting your guest.

Enabling bidirectional clipboard (host<->guest)

Starting Note
In order to support bidirectional clipboard, the guest machine has to install a package named spice-vdagent and not all distributions may provide. Therefore, it is best if you consult the repositories or consider building it from source(if possible).
Instead of VNC, the SPICE protocol will be used instead along with QLX graphics. No virtual machines should be running until it is time to install packages on the guest. For reference, the default video mode is Cirrus.

Necessary packages on the host

Install the following package to enable the viewing of SPICE guests.

sudo apt-get install python-spice-client-gtk 

Close and reopen virt-manager to load the module, but it may be necessary to reboot as well, if you get the very suggestive message when trying to view the guest.

Configuring the virtual machine's hardware

Open the virtual hardware details window for whatever guest you are trying to configure. In the video tab, change the model to "QXL". In the Display tab, change the type to "SPICE". Click apply. Read the warning and you will have to accept to install SPICE channels on the guest.

Guest OS configuration

Power on your guest and verify that you can open up the viewer. Otherwise, reboot if you have not done so yet.

Install the spice-vdagent and check the output to see if the service has started (if you are running SPICE and QXL graphics the service should start automatically on Trisquel). For Trisquel install with the following command.

sudo apt-get install spice-vdagent

Reboot your guest.

Upon reboot check if the service is running.

sudo service spice-vdagent status

If it is running, you should now be able to copy and paste bidirectionally.

Enabling multidirectional clipboard (host<->guest)

To be tested.
Note: You should have to install spice-agent in all guests.

File sharing

SFTP, FTP and CIFS are all very accessible ways of sharing files when networks are involved. Otherwise, check the [http://www.linux-kvm.org/page/9p_virtio Project's Official Documentation] for clarification on adding a pass-through file system.

Troubleshooting

Core2Duo CPUs

Some Intel Core2Duo CPU models do not support virtualization.
Run the following command.

grep name /proc/cpuinfo

In this case it is recommended to verify on manufacturer's website if your cpu supports virtualization.
It would be something like "VT" for Intel.

Slow graphics performance

This can greatly depend on your configuration, hardware and hardware load, but using the SPICE protocol and QXL graphics over VNC can moderately improve graphical performance. See "Enabling bidirectional clipboard" in this page on how to make this change.
See "Missing characters when using the SPICE protocol with QXL" in this page.
It is also a good idea to disable compositors unless it is desired. See "Enabling bidirectional clipboard" in this page(GNOME Flashback).

Missing characters when using the SPICE protocol with QXL (at least [?])
Frequently, some of the letters on all windows, panels and Desktop will go missing without a specific pattern, which coincides with a spike in CPU usage. It seems not depend on resolution as it will happen on 1920x1080 and 860x600.
This modification also seems to moderately improve graphical performance.

This has been verified to happen at least on Trisquel 7.0 (LXDE and GNOME) and Ubuntu 14.04 hosts using SPICE and QXL (spice-vdagent was installed on the guest in all cases).

Edit the grub configuration file with your favorite text editor:

sudo nano /etc/default/grub

Change the value of the "GRUB_CMDLINE_LINUX_DEFAULT=" directive to include the "nomodeset" parameter, such as:

GRUB_CMDLINE_LINUX_DEFAULT="splash quiet nomodeset"

Finally, update grub:

sudo update-grub

After rebooting, the issue seems to no longer affect the guest.

References:
Red Hat Bugzilla (Bug Report)
Ubuntu Forumns (Discussion)
Ubuntu Bugs - Launchpad (Bug Report)

There is also a bug report on Ubuntu 15.05 as of 29/01/2015.

Resolution, scaling and graphical performance

The following results come from the following environment: Trisquel 7.0 host (GNOME Flashback), Trisquel 7.0 guest (Mini - LXDE), local guest, bridged networking on eth0, 2500 MB of RAM and 2 CPUS on the guest, Samsung NP530-U4B. I did not compare Network I/O, CPU load nor RAM usage.
Cirrus seems to be limited to 1280x768 and is only compatible with VNC.
VGA seems to be limited to 1280x768 , but it seems smoother than Cirrus.
QLX is both compatible with VNC and SPICE. It seems smoother than the previous two. It also seems more responsive in VNC than SPICE, but there is more tearing in the former. The white mouse does not really look good over GNOME Flashback's grey windows. Could not change mouse cursor. The resolution goes at least up to 1920x1080 on both protocols. The mouse seemed to disappear on occasion, but it still interacted with the Desktop.
I did not test Xen or VMVGA due to technical issues.

TODO

Test and optimize graphical performance.

VFIO (HIGH PRIOTRITY)
This module/tool/concept allows to achieve as much as 99% of benchmark results on virtual machines than on the host. It is required to have two graphics cards. The concept resides on passing-through one of the cards to the guest so it can have direct control over the hardware. There are a other requirements including kernel and boot parameters, but Trisquel 7.0's default kernel seems to have everything to setup this environment.
Here are some resources to get you started:
Kernel.org Documentation (very complete)
RHEL VFIO PDF
Debian Wiki (seems a bit incomplete)
Arch Forums (160 pages, very dense)
Gaming Example with Nvidia
Blog Post on seting up VFIO
Wikipedia IOMMU Briefing"
To check if your CPU supports the correct type of virtualization you can check the manufacturer's website or wikipedia.
"My device has an Intel(R) Core(TM) i5-2467M, which has the proper requirements for virtualization, but not for VFIO. One thing to be is aware that VFIO needs VT-d and not VT-x(mine) to be able to forward PCI devices. Do not be surprised if you read in dmesg that IOMMU was enabled after adding the intel_iommu=yes line to the grub parameters." - Trisquelian

Revisions

11/03/2013 - 17:42
Sachin
01/28/2014 - 10:07
aloniv
03/21/2014 - 08:32
lembas
10/17/2014 - 05:31
muhammed
12/01/2014 - 17:25
ssdclickofdeath
01/30/2015 - 13:35
Trisquelian
12/26/2015 - 20:33
pogiako12345