Use the left/right arrow keys to navigate, 's' to enable/disable scrolling.

GNU/Linux Virtualization: kvm, libvirt, virt-manager

Virtualization, what’s?

A server (host) hosts many Virtual Machine (VM) guest

Virtualization, why?

Less:

  • electricity
  • server
  • air-conditioning
  • volume
  • failures

Virtualization

Hardware or software

Software virtualization

In the late 1990s, x86 virtualization was achieved by complex software techniques first introduced in VMware’s products, which overcame the platform’s hard-to-virtualize peculiarities, yet managed to attain reasonable performance.


VM doesn’t comunicate directly with hardware but through virtualization sofware that acts at low level.

Paravirtualization

  • virtualization technique that presents a software interface to virtual machines, similar but not identical to that of the underlying hardware
  • reduce the portion of the guest’s execution time spent performing operations which are substantially more difficult to run in a virtual environment compared to a non-virtualized environment
  • reduce the overall performance degradation of machine-execution inside the virtual-guest

Hardware virtualization

In the mid 2000s, both Intel and AMD added hardware support to their processors that first made the virtualization software simpler to write (with the 1st generation hardware assist), and later provided substantial speed improvements (2nd generation hardware assist).

Emulation/Virtualization on Linux

  • QEMU
  • QEMU + kqemu
  • XEN
  • VirtualBox
  • kvm + libvirt/qemu-kvm

libvirt is

A collection of software that provides a convenient way to manage virtual machines and other virtualization functionality, such as storage and network interface management. These software pieces include an API library, a daemon (libvirtd) and a command line utility (virsh).

libvirt is

  • a toolkit to interact with the virtualization capabilities of recent versions of Linux (and other OSes)
  • free software available under the GPL License
  • a long term stable C API
  • a set of bindings for common languages

Common languages binding

  • Python
  • Perl
  • Ruby
  • Java
  • C#
  • …besides pure C

libvirt supports

  • Xen hypervisor on Linux and Solaris hosts
  • QEMU emulator
  • KVM Linux hypervisor
  • VirtualBox hypervisor
  • VMware hypervisors

libvirt provides

  • remote management using TLS encryption and x509 certificates
  • remote management authenticating with Kerberos and SASL
  • management of virtual machines, virtual networks and storage
  • portable client API for Linux, Solaris and Windows
  • local access control using PolicyKit

libvirt features

VM Management

Various domain lifecycle operations such as start, stop, pause, save, restore, and migrate.


Hotplug operations for many device types including disk and network interfaces, memory, and cpus.

Remote machine support

All libvirt functionality is accessible on any machine running the libvirt daemon, including remote machines.


A variety of network transports are supported for connecting remotely, with the simplest being SSH, which requires no extra explicit configuration.

Remote machine support


virsh --connect qemu+ssh://root@example.com/system

The previous command will provide access to all virsh commands on the remote host for qemu/kvm

Storage management

Any host running the libvirt daemon can be used to manage various types of storage: create file images of various formats, mount NFS shares, enumerate existing LVM volume groups, create new LVM volume groups and logical volumes, partition raw disk devices, mount iSCSI share.


Since libvirt works remotely as well, all these options are available for remote hosts as well.

Network interface management

Any host running the libvirt daemon can be used to manage physical and logical network interfaces.


In future releases netcf will provide the functionalities to enumerate existing interfaces, as well as configure (and create) interfaces, bridges, vlans, and bond devices.

Virtual NAT and Route based networking

Any host running the libvirt daemon can manage and create virtual networks.

Libvirt virtual networks use firewall rules to act as a router, providing VMs transparent access to the host machines network.

libvirt HOWTO

Check if your CPU does or does not support hardware virtualization


kvm-ok

install


$ sudo apt-get install kvm libvirt-bin
$ sudo adduser $USER libvirtd

ubuntu-vm-builder


$ sudo apt-get install ubuntu-vm-builder
$ vmbuilder kvm ubuntu --suite lucid --flavour virtual --arch i386 \
    -o --libvirt qemu:///system --mirror http://localhost:9999/ubuntu \
    --addpkg openssh-server --addpkg acpid

virt-clone application can be used to copy one virtual machine to another


$ sudo virt-clone --original ubuntu --name test1 --auto-clone

VM Management

Command line: virsh

  • virsh -c qemu:///system list - list running virtual machines
  • virsh -c qemu:///system start web_devel - start a virtual machine
  • virsh -c qemu:///system autostart web_devel - start a virtual machine at boot
  • virsh -c qemu:///system reboot web_devel - reboot a virtual machine
  • virsh -c qemu:///system save web_devel web_devel-022708.state - save the state of virtual machines to a file for later restore
  • virsh -c qemu:///system restore web_devel-022708.state - restore a saved virtual machine
  • virsh -c qemu:///system shutdown web_devel - shutdown a virtual machine
  • virsh -c qemu:///system attach-disk web_devel /dev/cdrom /media/cdrom - mount a CDROM in a virtual machine

GUI: virt-manager

  • desktop user interface for managing virtual machines
  • presents a summary view of running domains, their live performance & resource utilization statistics
  • detailed view graphs performance & utilization over time
  • wizards enable the creation of new domains, and configuration & adjustment of a domain’s resource allocation & virtual hardware
  • embedded VNC client viewer presents a full graphical console to the guest domain
  • connect to remote libvirtd instances

virt-manager


$ sudo apt-get install virt-manager
# connect to a local VM
$ virt-manager -c qemu:///system
# connect to a remote VM
$ virt-manager -c qemu+ssh://root@HOSTNAME/system

virt-viewer

allows you to connect to a virtual machine’s console

virt-viewer


$ sudo apt-get install virt-viewer
$ virt-viewer -c qemu:///system web_devel
$ virt-viewer -c qemu+ssh://root@HOSTNAME/system web_devel

libvirt Low Level View

libvirt system configuration

/etc/libvirt/

/etc/libvirt

  • libvirtd.conf
  • qemu.conf
  • qemu/
  • storage/

/etc/libvirt/qemu

  • vmmachine1.xml
  • vmmachine2.xml
  • networks/

/etc/libvirt/qemu/networks

  • default.xml
  • network2.xml
  • autostart/default.xml -> ../default.xml

/etc/libvirt/storage

  • default.xml
  • data.xml
  • autostart/default.xml -> ../default.xml

libvirt domain XML config

libvirt XML format documentation

libvirt Domain XML format

libvirt Full XML format

WHEN?!?!?

Configuration Tweaks

Debugging

Custom configurations (not supported by libvirt UIs)

Use Case: Linux Kernel direct boot

Debugging libvirt/KVM problems

Common problems:

libvirt virtual machine disappear after config customizations (validation error on domain xml format)

virtual machine is too slow

…other libvirt silent errors (e.g. unknown xml tag will be just ignored)

Debugging Technique 1

Run libvirtd from command line


# /etc/init.d/libvirt-bin stop
...
# libvirtd
...
... libvirt log and errors
...

Debugging Technique 2

Check libvirt qemu log files


# tail /var/log/libvirt/qemu/myfirst-testvm.log
LC_ALL=C PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin HOME=/root USER=root LOGNAME=root /usr/bin/kvm -S -M pc-0.11 -m 512 -smp 1 -name myfirst-testvm -uuid 7b625c5b-91f3-4575-8782-406eaef80d9a  -monitor unix:/var/run/libvirt/qemu/myfirst-testvm.monitor,server,nowait -boot c -kernel /boot/vmlinuz-2.6.31-22-generic -initrd /boot/initrd-2.6.31-22-generic -append root=/dev/sda ro -drive file=/mnt/works/TOOLS/RIPLinux-9.3-non-X.iso,if=ide,media=cdrom,index=2 -drive file=/var/lib/libvirt/images/disk0.qcow2,if=scsi,index=0,boot=on -net nic,macaddr=54:52:00:07:2d:32,vlan=0,model=virtio,name=virtio.0 -net tap,fd=16,vlan=0,name=tap.0 -serial none -parallel none -usb -vnc 127.0.0.1:0 -k it -vga cirrus 
qemu: could not load initial ram disk '/boot/initrd-2.6.31-22-generic'

Copyright 2010 - Alca Soc. Coop.


http://learn.alcacoop.it - learn@alcacoop.it



released under CreativeCommons 2.5 by-nc-sa