Getting Started with KVM on Debian Jessie

KVM is a full virtualisation solution for Linux on x86 (64-bit included) hardware containing virtualisation extensions, Intel VT or AMD-V. It consists of a loadable kernel module that provides the core virtualisation infrastructure and a processor specific module, kvm-intel.ko or kvm-amd.ko. 

Our server has an Intel CPU, which supports hardware virtualisation:

# grep -c vmx /proc/cpuinfo

We are going to use Debian Jessie.


# apt-get install qemu-kvm libvirt-bin virtinst virt-viewer ebtables dnsmasq

KVM Kernel Modules

Now, if we were to use the default 3.16 kernel which is shipped with Debian Jessie, we should have the following kernel configuration already in place:

# egrep 'KVM_INTEL|KVM_AMD' /boot/config-3.16.0-4-amd64

Therefore if the KVM kernel module is not loaded, we must load it as follows:

# modprobe kvm_intel #(for Intel processors)
# modprobe kvm_amd #(for AMD processors)

In our particular case, we have kvm compiled into the kernel and therefore no module is loaded:

# egrep 'KVM_INTEL|KVM_AMD' /boot/config-4.2.6-dev
# CONFIG_KVM_AMD is not set

After installing the packages, we may need to enable the libvirtd service and ensure it’s started:

# systemctl enable libvirtd
# systemctl start libvirtd

We can now check our node information (4 CPUs, 12GB of RAM):

# virsh nodeinfo
CPU model:           x86_64
CPU(s):              4
CPU frequency:       2719 MHz
CPU socket(s):       1
Core(s) per socket:  2
Thread(s) per core:  2
NUMA cell(s):        1
Memory size:         12006088 KiB

Create a New KVM Host

Create a new disk image /kvm_data/centos1.img of size 4GB and format qcow2:

# qemu-img create -f qcow2 /kvm_data/centos1.img 4G
Formatting '/kvm_data/centos1.img', fmt=qcow2 size=4294967296 encryption=off cluster_size=65536 lazy_refcounts=off

Provision a new CentOS 7 virtual machine allocating 1 CPU and 1GB of RAM:

# virt-install --virt-type kvm --name=centos1 --vcpu=1 --ram=1024 \
  --disk path=/kvm_data/centos1.img,size=4,format=qcow2,sparse=true \
  --os-variant=rhel7 --cpuset=auto \
  --graphics spice  \
  --cdrom /tmp/CentOS-7-x86_64-DVD-1511.iso \
  --network user

If we get a “ERROR Guest name ‘centos1’ is already in use.” error, we may need to undefine the guest first:

# virsh undefine centos1

By default, QEMU invokes the -nic and -user options to add a single network adapter to the guest and provide NATed external Internet access. The host and guest will not see each other. In order to provide full network accessibility for the VM clients, you may set up a public bridge. This is beyond the scope of this article. Please check this post for details.

Get information about the disk image filename:

# qemu-img info /kvm_data/centos1.img
image: /kvm_data/centos1.img
file format: qcow2
virtual size: 4.0G (4294967296 bytes)
disk size: 1.1G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

Get domain information:

# virsh dominfo centos1
Id:             4
Name:           centos1
UUID:           e6da3d3b-3526-48e1-9a0c-c16b2ac002ac
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       9.7s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: none
Security DOI:   0

List domains:

# virsh list --all
 Id    Name             State
 4     centos1          running

KVM Domain Management from the CLI

Start a (previously defined) inactive domain:

# virsh start centos1
Domain centos1 started

Connect to the guest console:

# virsh console centos1

Save a domain state to a file:

# virsh save centos1 /tmp/centos1.state

Domain centos1 saved to /tmp/centos1.state

Restore a domain from a saved state in a file:

# virsh restore /tmp/centos1.state
Domain restored from /tmp/centos1.state

Gracefully shutdown a domain:

# virsh shutdown centos1

If we need to make configuration changes for a domain, we can edit XML configuration:

# virsh edit centos1

Display the graphical console for a virtual machine:

# virt-viewer centos1

Autostart a domain:

# virsh autostart centos1

Disable autostart a domain:

# virsh autostart centos1 --disable

Virsh has over 180 commands, check virsh help for more information.

KVM Domain Management from the GUI

A graphical UI to manage VMs is available:

# apt-get install virt-manager

Open the Virtual Machine Manager:

# virt-manager


Leave a Reply

Your email address will not be published. Required fields are marked *