wiki:LibVirt
Last modified 7 years ago Last modified on 07/25/11 17:03:38

LibVirt

LibVirt is a virtualization API used extensively in Red Hat Enterprise Linux and other Linux variants including Ubuntu LTS. In particular LibVirt is used to manage the KVM hypervisor built into the Linux kernel since 2.6.20 (and on Red Hat Enterprise Linux 5, both KVM and Xen).

In some ways KVM/LibVirt offer an alternative to VmwareEsxi though at least as of 2011 it's a little rougher around the edges, for example you must manually create your own network bridge interface. LibVirt does includes features such as live migration and load balancing that are not in the free VmwareEsxi, and of course it is better integrated into the Linux ecosystem.

Installing on Red Hat Enterprise Linux 6

After installing you will either need to reboot or start libvirtd to get the virbr0 NAT bridge interface. If you want a plain bridged interface br0 follow the section "Bridged networking with libvirt".

To install only the command line tools (virsh and virt-install etc):

yum install kvm libvirt libvirt-python python-virtinst libvirt-client virt-viewer

Everything including GUI (virt-manager):

yum groupinstall virtualization virtualization-client virtualization-platform virtualization-tools

To allow access from non-root users use PolicyKit

cat <<EOF > /etc/polkit-1/localauthority/50-local.d/50-org.example-libvirt-remote-access.pkla
[Remote libvirt SSH access]
Identity=unix-group:myvirtgroup
Action=org.libvirt.unix.manage
ResultAny=yes
ResultInactive=yes
ResultActive=yes
EOF
adduser myuser1
adduser myuser2
groupadd myvirtgroup
usermod -G myvirtgroup myuser1
usermod -G myvirtgroup myuser2
# needed for virt-install 
setfacl -m u:qemu:x /home/myuser1
setfacl -m u:qemu:x /home/myuser2
# optional
mkdir /data/virt
chmod 775 /data/virt/
chgrp myvirtgroup /data/virt

Installing guest OS

If you happen to be connecting from MacOS X11, there are a couple things you should know:

  • Bug 635346: keymapings of virtual machine graphical console borked will require you to specify the VNC "keymap=en-us" line or else you'll have trouble typing with e - backspace, r - tab, and 8 - enter. If you create a VM using the virt-manager GUI, before clicking "Finish" check the box "Customize configuration before install" and under "VNC Display" choose keymap "en-us".
  • Default MacOS X11.app uses the Option keys as Mode_Switch not Alt; you need to install XQuartz and in Preferences check the "option keys send Alt_L and Alt_R", or put this in ~/.Xmodmap :
    clear Mod1
    keycode 66 = Alt_L
    keycode 69 = Alt_R
    add Mod1 = Alt_L
    add Mod1 = Alt_R
    

You can fully script VM creation (note: the first time I ran this as a normal user it failed; after using the virt-manager GUI once added an ACL to my home dir (see setfacl -m u:qemu:x above).

From the Virtualized Guest Overview:

virt-install \
   --name=guest$$-fedora15-64 \
   --connect qemu:///system \
   --disk path=/data/virt/guest$$,size=8,bus=virtio \
   --vcpus=2 --ram=1024 \
   --graphics vnc,keymap=en-us \
   --location=http://ftp.osuosl.org/pub/fedora/linux/releases/15/Fedora/x86_64/os/ \
   -x "ks=http://ks.iths.org/ks/fm.cfg" \
   --network bridge=br0 \
   --os-type=linux \
   --os-variant=virtio26

Alternatively, with no GUI (note console=ttyS0):

virt-install \
   --name=guest$$-fedora15-64 \
   --connect qemu:///system \
   --disk path=/data/virt/guest$$,size=8,bus=virtio \
   --vcpus=2 --ram=1024 \
   --graphics none \
   --location=http://ftp.osuosl.org/pub/fedora/linux/releases/15/Fedora/x86_64/os/ \
   -x "console=ttyS0,115200 ks=http://ks.iths.org/ks/fm.cfg" \
   --network bridge=br0 \
   --os-type=linux \
   --os-variant=virtio26 

Managing hosts

To manage KVM host details in the virt-manager GUI, right-click the host (such as qemu://localhost) and select Details. That provides a basic interface to manage virtual networking and storage pools. virt-manager can also connect to remote hosts via ssh, providing a GUI to manage any number of headless KVM hosts.

The command line utility virsh has many many options. You can use it to connect to external hypervisors too:

export VIRSH_DEFAULT_CONNECT_URI="qemu+ssh://myuser2@vmhost3.example.org/system"
virsh list

# Id Name                 State
#----------------------------------
# 46 myt                  running
# 28 guest84517-el5-64    running

export VIRSH_DEFAULT_CONNECT_URI="qemu:///system"
virsh list --all
# Id Name                 State
#----------------------------------
#  3 guest13371-f15-64    running
# 11 t15                  running
#  - guest23713-fedora15-64 shut off

And getting rid of one, running or not:

virsh destroy t15; virsh undefine t15
#Domain t15 destroyed
#
#Domain t15 has been undefined

virsh undefine guest13371-fedora15-64 
# Domain guest13371-fedora15-64 has been undefined

Migrating virtual machines

You can migrate VMs using virt-v2v, including VMDK disks. It should soon be possible to directly migrate physical hosts with virt-p2v as well.