Table of Contents

KVM manual

General

This document shows and describes the most important commands for a KVM environment.

Host setup

Before you can start with any virtual machine, you have to install the following packages first:

dnf -y install virt-install libvirt-daemon-kvm qemu-kvm libvirt-client qemu libvirt-daemon

It’s not a bad idea, to check, if your host system is virtualisation capable.

virt-host-validate
  QEMU: Checking for hardware virtualization                                 : PASS
  QEMU: Checking if device /dev/kvm exists                                   : PASS
  QEMU: Checking if device /dev/kvm is accessible                            : PASS
  QEMU: Checking if device /dev/vhost-net exists                             : PASS
  QEMU: Checking if device /dev/net/tun exists                               : PASS
  QEMU: Checking for cgroup 'memory' controller support                      : PASS
  QEMU: Checking for cgroup 'memory' controller mount-point                  : PASS
  QEMU: Checking for cgroup 'cpu' controller support                         : PASS
  QEMU: Checking for cgroup 'cpu' controller mount-point                     : PASS
  QEMU: Checking for cgroup 'cpuacct' controller support                     : PASS
  QEMU: Checking for cgroup 'cpuacct' controller mount-point                 : PASS
  QEMU: Checking for cgroup 'cpuset' controller support                      : PASS
  QEMU: Checking for cgroup 'cpuset' controller mount-point                  : PASS
  QEMU: Checking for cgroup 'devices' controller support                     : PASS
  QEMU: Checking for cgroup 'devices' controller mount-point                 : PASS
  QEMU: Checking for cgroup 'blkio' controller support                       : PASS
  QEMU: Checking for cgroup 'blkio' controller mount-point                   : PASS
  QEMU: Checking for device assignment IOMMU support                         : PASS
  QEMU: Checking if IOMMU is enabled by kernel                               : WARN (IOMMU appears to be disabled in kernel. Add intel_iommu=on to kernel cmdline arguments)
   LXC: Checking for Linux >= 2.6.26                                         : PASS
   LXC: Checking for namespace ipc                                           : PASS
   LXC: Checking for namespace mnt                                           : PASS
   LXC: Checking for namespace pid                                           : PASS
   LXC: Checking for namespace uts                                           : PASS
   LXC: Checking for namespace net                                           : PASS
   LXC: Checking for namespace user                                          : PASS
   LXC: Checking for cgroup 'memory' controller support                      : PASS
   LXC: Checking for cgroup 'memory' controller mount-point                  : PASS
   LXC: Checking for cgroup 'cpu' controller support                         : PASS
   LXC: Checking for cgroup 'cpu' controller mount-point                     : PASS
   LXC: Checking for cgroup 'cpuacct' controller support                     : PASS
   LXC: Checking for cgroup 'cpuacct' controller mount-point                 : PASS
   LXC: Checking for cgroup 'cpuset' controller support                      : PASS
   LXC: Checking for cgroup 'cpuset' controller mount-point                  : PASS
   LXC: Checking for cgroup 'devices' controller support                     : PASS
   LXC: Checking for cgroup 'devices' controller mount-point                 : PASS
   LXC: Checking for cgroup 'blkio' controller support                       : PASS
   LXC: Checking for cgroup 'blkio' controller mount-point                   : PASS
Note: The warning about IOMMU can be ignored.

Test installation tree

curl -O http://192.168.122.1/fedora/27/os/
curl -O http://192.168.122.1/centos/7.4/os/

Environment

virsh dumpxml <domain name> > <outputfile>.xml
virsh create <outputfile>.xml

Basic commands

Note: To remove autostart toggle, just run this command again!

Installation of VM’s

If you want to add a second network interface, you should add it after the kickstart installation. If there are two or more interfaces configured, the installer (dracut) does not find the network based installation files. Here two example domains, one is a Fedora server and one is a CentOS server.

Fedora

In this example the new virtual machine or domain (fedora27) will be installed using kickstart. Following features are given:

virt-install --connect qemu:///system --name fedora27 \
    --disk path=/dev/mapper/sysvg-fedora27 --graphics spice \
    --vcpus 2 --ram=2048 --network bridge=virbr0 --os-type=linux \
    --os-variant=fedora26 --accelerate --noautoconsole --hvm \
    --location=http://dangiga.danlocal.net/fedora/27/os/ \
    --extra-args ks=http://dangiga.danlocal.net/ks/def-fedora27.ks console=ttyS0
virsh attach-interface --domain fedora27 --type direct --source enp0s31f6 --model virtio --config
virsh --connect qemu:///system console fedora27
Note: In this example, we install a second network interface for direct connection to physical interface of the KVM host!
Info: If you want to configure a fix MAC address, use --mac=52:54:00:f3:30:0f switch in the --network context!

Centos

In this example the new virtual machine or domain (centos7) will be installed using kickstart. Following features are given:

virt-install --connect qemu:///system --name centos7 \
    --disk path=/dev/mapper/sysvg-centos7 --graphics spice --vcpus 2 \
    --ram=2048 --network bridge=virbr0 --os-type=linux \
    --os-variant=centos7.0 --accelerate --noautoconsole --hvm \
    --location=http://dangiga.danlocal.net/centos/7.4/os/ \
    --extra-args "ks=http://dangiga.danlocal.net/ks/def-centos7.ks console=ttyS0"
virsh --connect qemu:///system console centos7

Networking

Note: The --live switch only works, if the VM is started!

Edit networks

1Network IP and netmask
2Define a fix IP address for a given MAC address

Harddisks

It’s also possible to add a disk to a running VM. The disk can be a disk image or a LVM volume. It’s possible to resize the disk, but, you have to reboot the client or relaod the kernel disk table (partx, only CentOS >=7).

Note: The disk is a logical volume on the KVM host

Resize (LVM) disk