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
virsh --connect qemu:///system autostart centos7
Note: | To remove autostart toggle, just run this command again! |
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:
/dev/mapper/sysvg-fedora27
)macvtap
protocolvirt-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:
/dev/mapper/sysvg-centos7
)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
Note: | The --live switch only works, if the VM is started! |
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
virsh
command also provides a editor for a network configuration. In this example the default network is edited.virsh net-edit default
default
net):virsh net-dumpxml default
Output:
<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>
1 | Network IP and netmask |
2 | Define a fix IP address for a given MAC address |
virsh net-destroy default && virsh net-start default
default
net):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
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
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.
Note: | The disk is a logical volume on the KVM host |
lvresize -G <size>G /dev/sysvg/<lv name>
virsh
:virsh blockresize <domain> /dev/sysvg/<lv name> --size <size>G
Now, you can resize the disk on the domain (VM).