Sunday 13 April 2014

KVM virtualization quick setup on Linux



Introduction


KVM (Kernel Virtual Machine) is a great implementation for virtualization in Linux on x86 architectures. It has both Intel and AMD virtualization extensions with :
  • Kernel module : kvm.ko - core virtualization infrastructure
  • Processor module : kvm-intel.ko or kvm-amd.ko
  • Files can be located e.g.
$ ls -lh /lib/modules/3.5.0-17-generic/kernel/arch/x86/kvm/
total 936K
-rw-r--r-- 1 root root  95K Oct  9  2012 kvm-amd.ko
-rw-r--r-- 1 root root 195K Oct  9  2012 kvm-intel.ko
-rw-r--r-- 1 root root 642K Oct  9  2012 kvm.ko


 Kernel Version Check

  • The KVM kernel component has been in Linux since 2.6.20
  • Check your version :
$ uname -r

Hardware Virtualization Support

  • A lot of hardware does not enable virtualization by default
  • You can review the BIOS and check for any virtualization enable options
  • Note : For Dell laptops (and potentially others) I have found you need to go into the BIOS and disable Trusted Execution
  • From the OS we can review flags set on /proc/cpu
 $ egrep "flags.*:.*(svm|vmx)" /proc/cpuinfo
  • If values are returned virtulaization is supported
  • Flags of interest
    • vmx — Intel VT-x, basic virtualization
    • svm — AMD SVM, basic virtualization 
  • Review of dmesg (kernel  message buffer) shows if kvm loaded e.g.
 $ dmesg | egrep kvm
[   25.601412] kvm: Nested Virtualization enabled

 

Package Requirements

 

qemu-kvm

  • qemu-kmv (Quick EMUlator) : allows userspace programs to access virtulization components of supported CPU's
  • As mention previously the kernel modules for KVM are included in mainline Linux
  • qemu features
    • Processor emulator (here x86)
    • Emulated devices e.g. keyboard, mouse, NIC, graphics card, hard disk etc
    • Generic devices connect emulated devices to the related host devices
    • Machine descriptions of virtual machines
    • Debugger
    • User interface
    • Hardware acceleration 
  • Emulated and generic devices comprise its device models for I/O virtualization

libvirt-bin

  • API that acts as a wrapper for qemu-kvm 
  • Toolkit to interact and manage  a quemu-kvm hypervisor
  • Features
    • Mange VM's, virtual networks and storage
    • Remote management with TLS and x509 certificates
  • Its possible to use qemu-kvm without libvirtapi
  • VM's created by qemu-kvm are not visible with the libvirt tools

bridge-utils

  • Contains brctl program to create and manage bridge devices
  • Required for configuring hosted VM ntwork

virt-goodies

  • A collection of helpful virtualization related tools
  • Tool to convert from VMWare images to kvm/libvirt e.g. vmware2libvirt
  • A libvirt munin plugin

Installation

  • The following installation is for Linux Mint but other Linux distributions will be similar
  • Install :
$ sudo apt-get install qemu-kvm libvirt-bin bridge-utils virt-goodies
    • This will also install 
    kvm-ipxe qemu-common qemu-utils

    Bridge Setup

    • One option is to remove the Linux Mint 'network-manager' but in my case I did not want to remove this component
    • Backup interfaces files
     $ sudo cp /etc/network/interfaces ~/tmp/interfaces.bak
    • Update /etc/network/interfaces as follows
    auto lo
    iface lo inet loopback

    iface eth0 inet manual

    auto br0
    iface br0 inet dhcp
            bridge_ports eth0
            bridge_maxwait 0

    • Restart networking
    $ sudo service networking restart

    • Check networking with ifconfig (I have changed the MAC's) :
    $ ifconfig br0
    br0       Link encap:Ethernet  HWaddr 11:11:11:11:11:11 
              inet addr:192.168.0.197  Bcast:192.168.0.255                          Mask:255.255.255.0
              inet6 addr: fe80::21f:16ff:fe45:49eb/64 Scope:Link
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:334 errors:0 dropped:0 overruns:0 frame:0
              TX packets:364 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:0
              RX bytes:127873 (127.8 KB)  TX bytes:89251 (89.2 KB)

    $ ifconfig eth0
    eth0      Link encap:Ethernet  HWaddr 11:11:11:11:11:00 
              UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
              RX packets:60003 errors:0 dropped:0 overruns:0 frame:0
              TX packets:47724 errors:0 dropped:0 overruns:0                          carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:45267129 (45.2 MB) TX bytes:11804118 (11.8 MB)

    • Check bridge status
    $ sudo brctl show
    bridge name    bridge id        STP enabled    interfaces
    br0        8000.001f164549eb    no        eth0


    • Ping Google's DNS to test connectivity
    $ ping  8.8.8.8
    • Reboot machine to ensure connection is retained

    GUI Management

    • Install GUI
     $ sudo apt-get install -y virt-manager
    • Check libvirtd group exists
    $ cat /etc/group | grep -i libvirt
    libvirtd:x:128:

    •  Add your username to this group i.e. replace 'cwishaw'
    $ sudo usermod -a -G libvirtd cwishaw
    • Logout and back in again for group change to take effect 
    • Open 'Virtual Machine manager from the Menu and following should appear

    •  If it does not connect locally check libvirtd is running and if not restart
    $ ps -ef | grep -i libvirtd
    root      1622     1  0 14:35 ?        00:00:05 /usr/sbin/libvirtd -d

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

      Change Default VM Location

    • By default, libvirt stores images in the /var/lib/libvirt/images directory
    • As the owner is root we would like to select our own dir
    • Create directory
    $ mkdir ~/KVMImages
    $ virsh pool-edit default 
    • Update <path> e.g.
    <path>/home/cwishaw/KVMImages</path>
    •  Restart libvirt
    $ sudo /etc/init.d/libvirt-bin restart
    • Open 'Virtual Machine manager select 'Edit -> Connection Details->Storage -> default'
    • Location should be new folder e.g. /home/cwishaw/KVMImages

    Custom Storage ISO Location

    • Create directory
    $ mkdir ~/ISOs
    • Open 'Virtual Machine manager select 'Edit -> Connection Details->Storage -> +'
    • Define new Storage Pool then select 'Next'

    • Define 'Target Path' as new directory then select 'Finish'
    • Disconnect then reconnect to GUI and new Storage Pool will appear 

     Create a VM

    • Right Click on 'localhost (QEMU) and select 'New'
    • The following screenshots show examples of setting up a new VM






    Graphs

    • There are some nice graph options that can be enabled in 'Virtual Machine Manager under 'Edit -> Preferences -> Stats


    • Change view under 'View -> Graph'





        No comments:

        Post a Comment