kvm

This is an old revision of the document!


KVM manual

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

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.
curl -O http://192.168.122.1/fedora/27/os/
curl -O http://192.168.122.1/centos/7.4/os/
virsh dumpxml <domain name> > <outputfile>.xml
virsh create <outputfile>.xml
virsh --connect qemu:///system destroy fedora27
virsh --connect qemu:///system undefine fedora27
Note: This does not delete the data on the disk (Logical Volume or image)!
virsh edit <domain name>
virsh --connect qemu:///system list --all
 Id    Name                           State
----------------------------------------------------
 4     cc                             running
 9     centos7                        running
 -     fedora27                       shut off
virsh --connect qemu:///system console centos7

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.

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

  • The disk is a logical volume (/dev/mapper/sysvg-fedora27)
  • The VM has two CPU’s and 2GB of RAM
  • The VM has two network interfaces, one for internal guest/host comunication (the default network), and one for external connections via macvtap protocol
  • The installation tree lifes on a webserver ( http://dangiga.danlocal.net/fedora/27/os/ )
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!

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

  • The disk is a logical volume (/dev/mapper/sysvg-centos7)
  • The VM has two CPU’s and 2GB of RAM
  • The VM has one network interfaces for internal guest/host comunication (the default network) only
  • The installation tree lifes on a webserver ( http://dangiga.danlocal.net/centos/7.4/os/ )
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
virsh net-list --all
virsh attach-interface --domain <vm name> --type bridge --source virbr0 --model virtio --config --live
virsh attach-interface --domain <vm name> --type direct --source enp0s31f6 --model virtio --config --live
The --live switch only works, if the VM is started!

If you want to figure out the IP addresss of one or more VM’s, you can use the following command executed on the KVM host.

vm=fedora27
for mac in `virsh domiflist $vm |grep -o -E "([0-9a-f]{2}:){5}([0-9a-f]{2})"` ; do
    arp -e |grep $mac  |grep -o -P "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
done

The virsh command also provides a editor for a network configuration. In this example the default network is edited.

virsh net-edit default
virsh net-dumpxml default
<network>
  <name>default</name>
  <uuid>fffdb071-f34b-4672-b8c5-48ee04d7ab4b</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:65:80:59'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'> (1)
    <dhcp>
      <range start='192.168.122.40' end='192.168.122.254'/>
      <host mac='52:54:00:f3:30:0f' ip='192.168.122.10'/> (2)
      <host mac='52:54:00:f3:30:1f' ip='192.168.122.11'/>
      <host mac='52:54:00:f3:30:2f' ip='192.168.122.12'/>
      <host mac='52:54:00:f3:30:3f' ip='192.168.122.13'/>
      <host mac='52:54:00:f3:30:4f' ip='192.168.122.14'/>
      <host mac='52:54:00:f3:30:5f' ip='192.168.122.15'/>
      <host mac='52:54:00:f3:30:6f' ip='192.168.122.16'/>
      <host mac='52:54:00:43:56:0e' ip='192.168.122.17'/>
      <host mac='52:54:00:43:56:1e' ip='192.168.122.18'/>
      <host mac='52:54:00:43:56:2e' ip='192.168.122.19'/>
      <host mac='52:54:00:43:56:3e' ip='192.168.122.20'/>
      <host mac='52:54:00:43:56:4e' ip='192.168.122.21'/>
      <host mac='52:54:00:43:56:5e' ip='192.168.122.22'/>
      <host mac='52:54:00:43:56:6e' ip='192.168.122.23'/>
      <host mac='52:54:00:43:56:7e' ip='192.168.122.24'/>
    </dhcp>
  </ip>
</network>
1Network IP and netmask
2Define a fix IP address for a given MAC address

After changing anything, you must activate the changed configuration, you have to reload the network:

virsh net-destroy default && virsh net-start default
virsh net-update default delete ip-dhcp-host "<host mac='52:54:00:f3:30:2f' name='ds' ip='192.168.122.12'/>" --live --config

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).

virsh attach-disk ds --source /dev/sysvg/ds_test --target vdb --persistent

This adds a new disk to the ds VM as /dev/vdb persistently.

The disk is a logical volume on the KVM host
  • kvm.1549701583.txt.gz
  • Last modified: 2019/02/09 09:39
  • by admin