====== 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 > .xml virsh create .xml ===== Basic commands ===== * Completely delete a VM: 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)! | * Unordered List ItemEdit/configure the settings of a virtual machine (domain): virsh edit * Unordered List ItemList all (also not started VM's): virsh --connect qemu:///system list --all Id Name State ---------------------------------------------------- 4 cc running 9 centos7 running - fedora27 shut off * Start a console session for a VM: virsh --connect qemu:///system console centos7 * Setup autostart (when booting the KVM server) for a VM: virsh --connect qemu:///system autostart centos7 | **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: * 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! | ==== Centos ==== 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 ===== Networking ===== * List all networks: virsh net-list --all * Attach a network interface to a virtual machine (domain): virsh attach-interface --domain --type bridge --source virbr0 --model virtio --config --live virsh attach-interface --domain --type direct --source enp0s31f6 --model virtio --config --live | **Note:** | The ''%%--live%%'' switch only works, if the VM is started! | * Unordered List ItemIf 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 ==== Edit networks ==== * Unordered List ItemThe ''%%virsh%%'' command also provides a editor for a network configuration. In this example the default network is edited. virsh net-edit default * Show the network configuration of a network (here ''default'' net): virsh net-dumpxml default Output: default fffdb071-f34b-4672-b8c5-48ee04d7ab4b (1) (2) |**1**|Network IP and netmask | |**2**|Define a fix IP address for a given MAC address| * Unordered List ItemAfter changing anything, you must activate the changed configuration, you have to reload the network: virsh net-destroy default && virsh net-start default * Delete a vm (host) in a network (here ''default'' net): virsh net-update default delete ip-dhcp-host "" --live --config * Add a network configuration (host) live: virsh net-update default add ip-dhcp-host "" --live --config ===== 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). * Add (persistent) a disk to a VM: 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. | **Note:** | The disk is a logical volume on the KVM host | ==== Resize (LVM) disk ==== * First increase size of the LV on KVM host: lvresize -G G /dev/sysvg/ * Configure the size of that domain and disk using ''virsh'': virsh blockresize /dev/sysvg/ --size G \\ Now, you can resize the disk on the domain (VM).