Quick Start Guide: Virtualization with KVM

Introduction

KVM is an open source software providing full virtualization solution for Linux running on x86 (including 64-bit) processors that support virutalization Intel VT and AMD-V). KVM requires that the Host's machine processor has virutalization support, to find our whether your host's machine processor support virtualization, you may either run:

 $ LC_ALL=C lscpu | grep Virtualization
or
 $ grep -E --color=auto 'vmx|svm|0xc0f' /proc/cpuinfo
If you get no output, then your CPU does not support virtualization. KVM is made of loadable kernel modules kvm.ko that provides virtualization infrastructure for specific modules (kvm-intel.ko or kvm-amd.ko). The KVM kernel modules function as a hypervisor that enable us to run multiple virtual machines on the very same hardware. Each of these virtual machines will have its own virtualized hardware: graphic adapter, disk, network cards, etc. KVM support:
  1. Hot plug vCPUs1
  2. Dynamic memory management2
  3. Live Migration 3

Installation

For minimal installation on Debian Stretch, run:
# apt-get install qemu-kvm libvirt-clients libvirt-daemon-system
If you happen to be on Debian Jessie, you can add backport to your source lists (/etc/apt/sources.list) or run the following command:
# apt-get install qemu-kvm libvirt-bin
It is also recommended to install libvirt-daemon and virt-manager. The background program libvirt-daemon automatically get loaded during system boot and load all KVM components (kvm-amd or kvm-intel) to the kernel.

Installing Virtual Machine

Install virtinst by running:
 # apt install virtinst
To manage and install Virtual machines using GUI we run
 # virt-manager
as root.

Other options is to use the command line interface, from the command line we may install machines as normal user. For example we can define Kali Linux machine and name if foo by using the following command:

virt-install \
	--virt-type kvm \
	--name foo \
	--memory 2048 \
	--cdrom ~/Downloads/kali/kali-linux-2016.2-amd64.iso \
	--disk /home/user/kvm/kali2/kali2.qcow2,size=15 \
	--os-variant linux
When --memory 2048 means that the memory on this virtual machine will be set to 2048 KB and --cdrom indicates the source of installation and --disk is for the installation directory. In addition size=15 says that the virtual machine will install on 15 GB disk.

Initiating the Virtual Machine (VM)

Let us focus on managing and initializing VM via the command line interface or CLI. To list all virtual machines we run
 # virsh list --all
To boot (or run) VM foo we run:
 # virsh start foo
To view the machine we install virtinst
# apt install virtinst
and then run:
 # virt-viewer foo
We may also skip virtinst installation and communicate with "foo" machine by running
# virsh console foo
on the host machine. However, this command may halt on Debian or Ubuntu host machine. This issue may be fixed by applying the following steps:
  1. On the guest machine: run:
    # systemctl enable serial-getty@ttyS0.service
    # systemctl start serial-getty@ttyS0.service
  2. On the guest machine: Open /etc/default/grub file, find the lines
    GRUB_CMDLINE_LINUX_DEFAULT="quiet"
    #GRUB_TERMINAL=console
    and replace them with
    GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0"
    GRUB_TERMINAL="serial console"
  3. On the guest machine: run
    # update grub

    and then shutdown your guest machine.

  4. IN the host machine: run
    $ virsh start foo --console
    To get the virsh console on a running guest machine ("foo"), run
    $ virsh console foo

Links:

  1. Some basics about hot-add/hot-plug - All about virtualization
  2. Explain How KVM Does Support Dynamic Memory Management?
  3. Live Migrating QEMU-KVM Virtual Machines - Red Hat Developer
  4. Red Hat Enterprise Linux-7-Virtualization Deployment and Administration Guide (pdf doc)
  5. FAQ - KVM
  6. What is KVM?
  7. KVM Virtual vs Physical
  8. Dynamic Memory Management - Proxmox VE
  9. Virtualization getting started guide An introduction to virtualization concepts (pdf doc Red Hat Enterprise Linux 7)
  10. Kernel Based Virtual Machine (Bachelor thesis by Cong Li)
 
 
Disclaimer Privacy