Show pageOld revisionsBacklinksAdd to bookExport to MarkdownBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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: <code bash> dnf -y install virt-install libvirt-daemon-kvm qemu-kvm libvirt-client qemu libvirt-daemon </code> It’s not a bad idea, to check, if your host system is virtualisation capable. <code bash> 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 </code> | **Note:** | The warning about ''%%IOMMU%%'' can be ignored. | ==== Test installation tree ==== <code bash> curl -O http://192.168.122.1/fedora/27/os/ curl -O http://192.168.122.1/centos/7.4/os/ </code> ===== Environment ===== <code bash> virsh dumpxml <domain name> > <outputfile>.xml </code> <code bash> virsh create <outputfile>.xml </code> ===== Basic commands ===== * Completely delete a VM:<code bash> virsh --connect qemu:///system destroy fedora27 virsh --connect qemu:///system undefine fedora27 </code>| **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):<code bash> virsh edit <domain name> </code> * Unordered List ItemList all (also not started VM's):<code bash> virsh --connect qemu:///system list --all Id Name State ---------------------------------------------------- 4 cc running 9 centos7 running - fedora27 shut off </code> * Start a console session for a VM:<code bash> virsh --connect qemu:///system console centos7 </code> * Setup autostart (when booting the KVM server) for a VM:<code bash> virsh --connect qemu:///system autostart centos7 </code> | **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/ ) <code bash> 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 </code> | **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/ ) <code bash> 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 </code> ===== Networking ===== * List all networks:<code bash> virsh net-list --all </code> * Attach a network interface to a virtual machine (domain):<code bash> virsh attach-interface --domain <vm name> --type bridge --source virbr0 --model virtio --config --live </code><code bash> virsh attach-interface --domain <vm name> --type direct --source enp0s31f6 --model virtio --config --live </code> | **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:<code bash> 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 </code> ==== Edit networks ==== * Unordered List ItemThe ''%%virsh%%'' command also provides a editor for a network configuration. In this example the default network is edited.<code bash> virsh net-edit default </code> * Show the network configuration of a network (here ''default'' net):<code bash> virsh net-dumpxml default </code>Output:<code bash> <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.xxx.1' netmask='255.255.255.0'> (1) <dhcp> <range start='192.168.122.40' end='192.168.xxx.254'/> <host mac='52:54:00:f3:30:0f' ip='192.168.xxx.xx'/> (2) <host mac='52:54:00:f3:30:1f' ip='192.168.xxx.xx'/> <host mac='52:54:00:f3:30:2f' ip='192.168.xxx.xx'/> </dhcp> </ip> </network> </code> |**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:<code bash> virsh net-destroy default && virsh net-start default </code> * Delete a vm (host) in a network (here ''default'' net):<code bash> virsh net-update default delete ip-dhcp-host "<host mac='52:54:00:f3:30:2f' name='hostname' ip='192.168.xxx.xxx'/>" --live --config </code> * Add a network configuration (host) live:<code bash> virsh net-update default add ip-dhcp-host "<host mac='52:54:00:43:56:84' name='hostname' ip='192.168.xxx.xxx'/>" --live --config </code> ===== 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:<code bash> virsh attach-disk ds --source /dev/sysvg/ds_test --target vdb --persistent </code> \\ 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:<code bash> lvresize -G <size>G /dev/sysvg/<lv name> </code> * Configure the size of that domain and disk using ''virsh'':<code bash> virsh blockresize <domain> /dev/sysvg/<lv name> --size <size>G </code> \\ Now, you can resize the disk on the domain (VM). kvm.txt Last modified: 2021/02/14 09:36by dani